Implemement const-eval for some unpack routines.

This CL adds const-eval for unpack2x16snorm, unpack2x16unorm,
unpack4x8snorm and unpack4x8unorm.

Bug: tint:1581
Change-Id: Ieda938b797dfe3f8d8046101d12b99da69ad68f4
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/108864
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
This commit is contained in:
dan sinclair
2022-11-07 22:28:01 +00:00
committed by Dawn LUCI CQ
parent ec4b650adb
commit 1bdaded736
25 changed files with 263 additions and 153 deletions

View File

@@ -550,10 +550,10 @@ fn transpose<M: num, N: num, T: f32_f16>(mat<M, N, T>) -> mat<N, M, T>
fn trunc<T: f32_f16>(T) -> T
fn trunc<N: num, T: f32_f16>(vec<N, T>) -> vec<N, T>
fn unpack2x16float(u32) -> vec2<f32>
fn unpack2x16snorm(u32) -> vec2<f32>
fn unpack2x16unorm(u32) -> vec2<f32>
fn unpack4x8snorm(u32) -> vec4<f32>
fn unpack4x8unorm(u32) -> vec4<f32>
@const fn unpack2x16snorm(u32) -> vec2<f32>
@const fn unpack2x16unorm(u32) -> vec2<f32>
@const fn unpack4x8snorm(u32) -> vec4<f32>
@const fn unpack4x8unorm(u32) -> vec4<f32>
@stage("compute") fn workgroupBarrier()
fn textureDimensions<T: fiu32>(texture: texture_1d<T>) -> u32

View File

@@ -2168,6 +2168,68 @@ ConstEval::Result ConstEval::step(const sem::Type* ty,
return TransformElements(builder, ty, transform, args[0], args[1]);
}
ConstEval::Result ConstEval::unpack2x16snorm(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
auto* inner_ty = sem::Type::DeepestElementOf(ty);
auto e = args[0]->As<u32>().value;
utils::Vector<const sem::Constant*, 2> els;
els.Reserve(2);
for (size_t i = 0; i < 2; ++i) {
auto val = f32(
std::max(static_cast<float>(int16_t((e >> (16 * i)) & 0x0000'ffff)) / 32767.f, -1.f));
els.Push(CreateElement(builder, inner_ty, val));
}
return CreateComposite(builder, ty, std::move(els));
}
ConstEval::Result ConstEval::unpack2x16unorm(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
auto* inner_ty = sem::Type::DeepestElementOf(ty);
auto e = args[0]->As<u32>().value;
utils::Vector<const sem::Constant*, 2> els;
els.Reserve(2);
for (size_t i = 0; i < 2; ++i) {
auto val = f32(static_cast<float>(uint16_t((e >> (16 * i)) & 0x0000'ffff)) / 65535.f);
els.Push(CreateElement(builder, inner_ty, val));
}
return CreateComposite(builder, ty, std::move(els));
}
ConstEval::Result ConstEval::unpack4x8snorm(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
auto* inner_ty = sem::Type::DeepestElementOf(ty);
auto e = args[0]->As<u32>().value;
utils::Vector<const sem::Constant*, 4> els;
els.Reserve(4);
for (size_t i = 0; i < 4; ++i) {
auto val =
f32(std::max(static_cast<float>(int8_t((e >> (8 * i)) & 0x0000'00ff)) / 127.f, -1.f));
els.Push(CreateElement(builder, inner_ty, val));
}
return CreateComposite(builder, ty, std::move(els));
}
ConstEval::Result ConstEval::unpack4x8unorm(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {
auto* inner_ty = sem::Type::DeepestElementOf(ty);
auto e = args[0]->As<u32>().value;
utils::Vector<const sem::Constant*, 4> els;
els.Reserve(4);
for (size_t i = 0; i < 4; ++i) {
auto val = f32(static_cast<float>(uint8_t((e >> (8 * i)) & 0x0000'00ff)) / 255.f);
els.Push(CreateElement(builder, inner_ty, val));
}
return CreateComposite(builder, ty, std::move(els));
}
ConstEval::Result ConstEval::quantizeToF16(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source&) {

View File

@@ -638,6 +638,42 @@ class ConstEval {
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// unpack2x16snorm 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 unpack2x16snorm(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// unpack2x16unorm 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 unpack2x16unorm(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// unpack4x8snorm 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 unpack4x8snorm(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// unpack4x8unorm 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 unpack4x8unorm(const sem::Type* ty,
utils::VectorRef<const sem::Constant*> args,
const Source& source);
/// quantizeToF16 builtin
/// @param ty the expression type
/// @param args the input arguments

View File

@@ -9,7 +9,7 @@
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// See the License for the empecific language governing permissions and
// limitations under the License.
#include "src/tint/resolver/const_eval_test.h"
@@ -1335,6 +1335,74 @@ INSTANTIATE_TEST_SUITE_P( //
StepCases<f32>(),
StepCases<f16>()))));
std::vector<Case> Unpack4x8snormCases() {
return {
C({Val(u32(0x0000'0000))}, Vec(f32(0), f32(0), f32(0), f32(0))),
C({Val(u32(0x8100'0000))}, Vec(f32(0), f32(0), f32(0), f32(-1))),
C({Val(u32(0x7f00'0000))}, Vec(f32(0), f32(0), f32(0), f32(1))),
C({Val(u32(0x0081'0000))}, Vec(f32(0), f32(0), f32(-1), f32(0))),
C({Val(u32(0x0000'7f00))}, Vec(f32(0), f32(1), f32(0), f32(0))),
C({Val(u32(0x0000'0081))}, Vec(f32(-1), f32(0), f32(0), f32(0))),
C({Val(u32(0x817f'817f))}, Vec(f32(1), f32(-1), f32(1), f32(-1))),
C({Val(u32(0x816d'937f))},
Vec(f32(1), f32(-0.8582677165354), f32(0.8582677165354), f32(-1))),
};
}
INSTANTIATE_TEST_SUITE_P( //
Unpack4x8snorm,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kUnpack4X8Snorm),
testing::ValuesIn(Unpack4x8snormCases())));
std::vector<Case> Unpack4x8unormCases() {
return {
C({Val(u32(0x0000'0000))}, Vec(f32(0), f32(0), f32(0), f32(0))),
C({Val(u32(0xff00'0000))}, Vec(f32(0), f32(0), f32(0), f32(1))),
C({Val(u32(0x00ff'0000))}, Vec(f32(0), f32(0), f32(1), f32(0))),
C({Val(u32(0x0000'ff00))}, Vec(f32(0), f32(1), f32(0), f32(0))),
C({Val(u32(0x0000'00ff))}, Vec(f32(1), f32(0), f32(0), f32(0))),
C({Val(u32(0x00ff'00ff))}, Vec(f32(1), f32(0), f32(1), f32(0))),
C({Val(u32(0x0066'00ff))}, Vec(f32(1), f32(0), f32(0.4), f32(0))),
};
}
INSTANTIATE_TEST_SUITE_P( //
Unpack4x8unorm,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kUnpack4X8Unorm),
testing::ValuesIn(Unpack4x8unormCases())));
std::vector<Case> Unpack2x16snormCases() {
return {
C({Val(u32(0x0000'0000))}, Vec(f32(0), f32(0))),
C({Val(u32(0x8001'0000))}, Vec(f32(0), f32(-1))),
C({Val(u32(0x7fff'0000))}, Vec(f32(0), f32(1))),
C({Val(u32(0x0000'8001))}, Vec(f32(-1), f32(0))),
C({Val(u32(0x0000'7fff))}, Vec(f32(1), f32(0))),
C({Val(u32(0x8001'7fff))}, Vec(f32(1), f32(-1))),
C({Val(u32(0x8001'7fff))}, Vec(f32(1), f32(-1))),
C({Val(u32(0x4000'999a))}, Vec(f32(-0.80001220740379), f32(0.500015259254737))).FloatComp(),
};
}
INSTANTIATE_TEST_SUITE_P( //
Unpack2x16snorm,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kUnpack2X16Snorm),
testing::ValuesIn(Unpack2x16snormCases())));
std::vector<Case> Unpack2x16unormCases() {
return {
C({Val(u32(0xffff'0000))}, Vec(f32(0), f32(1))),
C({Val(u32(0x0000'ffff))}, Vec(f32(1), f32(0))),
C({Val(u32(0x0000'6666))}, Vec(f32(0.4), f32(0))),
C({Val(u32(0x0000'ffff))}, Vec(f32(1), f32(0))),
};
}
INSTANTIATE_TEST_SUITE_P( //
Unpack2x16unorm,
ResolverConstEvalBuiltinTest,
testing::Combine(testing::Values(sem::BuiltinType::kUnpack2X16Unorm),
testing::ValuesIn(Unpack2x16unormCases())));
std::vector<Case> QuantizeToF16Cases() {
(void)E({Vec(0_f, 0_f)}, ""); // Currently unused, but will be soon.
return {

View File

@@ -13838,7 +13838,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[952],
/* return matcher indices */ &kMatcherIndices[134],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::unpack4x8unorm,
},
{
/* [459] */
@@ -13850,7 +13850,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[962],
/* return matcher indices */ &kMatcherIndices[134],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::unpack4x8snorm,
},
{
/* [460] */
@@ -13862,7 +13862,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[963],
/* return matcher indices */ &kMatcherIndices[132],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::unpack2x16unorm,
},
{
/* [461] */
@@ -13874,7 +13874,7 @@ constexpr OverloadInfo kOverloads[] = {
/* parameters */ &kParameters[973],
/* return matcher indices */ &kMatcherIndices[132],
/* flags */ OverloadFlags(OverloadFlag::kIsBuiltin, OverloadFlag::kSupportsVertexPipeline, OverloadFlag::kSupportsFragmentPipeline, OverloadFlag::kSupportsComputePipeline),
/* const eval */ nullptr,
/* const eval */ &ConstEval::unpack2x16snorm,
},
{
/* [462] */