tint: const eval of firstTrailingBit

Bug: tint:1581
Change-Id: I4a9cb113780439849aec10cd0fc8e2f6d2ab55cc
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/107881
Commit-Queue: Antonio Maiorano <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Antonio Maiorano
2022-10-31 22:57:14 +00:00
committed by Dawn LUCI CQ
parent 1abe52dc1c
commit a3175f28f6
46 changed files with 315 additions and 1568 deletions

View File

@@ -469,8 +469,8 @@ fn extractBits<N: num, T: iu32>(vec<N, T>, u32, u32) -> vec<N, T>
fn faceForward<N: num, T: f32_f16>(vec<N, T>, vec<N, T>, vec<N, T>) -> vec<N, T>
@const fn firstLeadingBit<T: iu32>(T) -> T
@const fn firstLeadingBit<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
fn firstTrailingBit<T: iu32>(T) -> T
fn firstTrailingBit<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
@const fn firstTrailingBit<T: iu32>(T) -> T
@const fn firstTrailingBit<N: num, T: iu32>(vec<N, T>) -> vec<N, T>
fn floor<T: f32_f16>(T) -> T
fn floor<N: num, T: f32_f16>(vec<N, T>) -> vec<N, T>
fn fma<T: f32_f16>(T, T, T) -> T

View File

@@ -209,6 +209,23 @@ auto CountLeadingBits(T e, T bit_value_to_count) -> std::make_unsigned_t<T> {
return count;
}
/// @returns the number of consecutive trailing bits set to `@p bit_value_to_count` in `@p e`
template <typename T>
auto CountTrailingBits(T e, T bit_value_to_count) -> std::make_unsigned_t<T> {
using UT = std::make_unsigned_t<T>;
constexpr UT kNumBits = sizeof(UT) * 8;
constexpr UT kRightMost = UT{1};
const UT b = static_cast<UT>(bit_value_to_count);
auto v = static_cast<UT>(e);
auto count = UT{0};
while ((count < kNumBits) && ((v & kRightMost) == b)) {
++count;
v >>= 1;
}
return count;
}
/// ImplConstant inherits from sem::Constant to add an private implementation method for conversion.
struct ImplConstant : public sem::Constant {
/// Convert attempts to convert the constant value to the given type. On error, Convert()
@@ -1695,17 +1712,7 @@ ConstEval::Result ConstEval::countTrailingZeros(const sem::Type* ty,
auto create = [&](auto e) {
using NumberT = decltype(e);
using T = UnwrapNumber<NumberT>;
using UT = std::make_unsigned_t<T>;
constexpr UT kNumBits = sizeof(UT) * 8;
constexpr UT kRightMost = UT{1};
auto v = static_cast<UT>(e);
auto count = UT{0};
while ((count < kNumBits) && ((v & kRightMost) == 0)) {
++count;
v >>= 1;
}
auto count = CountTrailingBits(T{e}, T{0});
return CreateElement(builder, c0->Type(), NumberT(count));
};
return Dispatch_iu32(create, c0);
@@ -1757,6 +1764,32 @@ ConstEval::Result ConstEval::firstLeadingBit(const sem::Type* ty,
return TransformElements(builder, ty, transform, args[0]);
}
ConstEval::Result ConstEval::firstTrailingBit(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
auto transform = [&](const sem::Constant* c0) {
auto create = [&](auto e) {
using NumberT = decltype(e);
using T = UnwrapNumber<NumberT>;
using UT = std::make_unsigned_t<T>;
NumberT result;
if (e == T{0}) {
// T(-1) if e is zero.
result = NumberT(static_cast<T>(-1));
} else {
// Otherwise the position of the least significant 1 bit in e.
UT pos = CountTrailingBits(T{e}, T{0});
result = NumberT(pos);
}
return CreateElement(builder, c0->Type(), result);
};
return Dispatch_iu32(create, c0);
};
return TransformElements(builder, ty, transform, args[0]);
}
ConstEval::Result ConstEval::saturate(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {

View File

@@ -476,6 +476,15 @@ class ConstEval {
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// firstTrailingBit builtin
/// @param ty the expression type
/// @param args the input arguments
/// @param source the source location of the conversion
/// @return the result value, or null if the value cannot be calculated
Result firstTrailingBit(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// saturate builtin
/// @param ty the expression type
/// @param args the input arguments

View File

@@ -680,6 +680,39 @@ INSTANTIATE_TEST_SUITE_P( //
testing::ValuesIn(Concat(FirstLeadingBitCases<i32>(), //
FirstLeadingBitCases<u32>()))));
template <typename T>
std::vector<Case> FirstTrailingBitCases() {
using B = BitValues<T>;
auto r = std::vector<Case>{
C({T(0)}, T(-1)),
C({B::Lsh(1, 31)}, T(31)), //
C({B::Lsh(1, 30)}, T(30)), //
C({B::Lsh(1, 29)}, T(29)), //
C({B::Lsh(1, 28)}, T(28)),
//...
C({B::Lsh(1, 3)}, T(3)), //
C({B::Lsh(1, 2)}, T(2)), //
C({B::Lsh(1, 1)}, T(1)), //
C({B::Lsh(1, 0)}, T(0)),
C({T(0b0000'0000'0100'1000'1000'1000'0000'0000)}, T(11)),
C({T(0b0000'0100'1000'1000'1000'0000'0000'0000)}, T(15)),
// Vector tests
C({Vec(B::Lsh(1, 31), B::Lsh(1, 30), B::Lsh(1, 29))}, Vec(T(31), T(30), T(29))),
C({Vec(B::Lsh(1, 2), B::Lsh(1, 1), B::Lsh(1, 0))}, Vec(T(2), T(1), T(0))),
};
return r;
}
INSTANTIATE_TEST_SUITE_P( //
FirstTrailingBit,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kFirstTrailingBit),
testing::ValuesIn(Concat(FirstTrailingBitCases<i32>(), //
FirstTrailingBitCases<u32>()))));
template <typename T>
std::vector<Case> SaturateCases() {
return {

View File

@@ -13516,7 +13516,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[908],
/* return matcher indices */ &kMatcherIndices[1],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::firstTrailingBit,
},
{
/* [433] */
@@ -13528,7 +13528,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[907],
/* return matcher indices */ &kMatcherIndices[30],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::firstTrailingBit,
},
{
/* [434] */