From cc0c67bce8d8b782d3a83940a1e86d40a94d0d10 Mon Sep 17 00:00:00 2001 From: David Neto Date: Wed, 12 Oct 2022 20:42:26 +0000 Subject: [PATCH] spirv-reader: Support GLSL.std.450 FindSMsb Fixed: tint:1058 Change-Id: Ifdc2ee9c54d18e1efe6b908991e8eb1b8c6a6de4 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/104903 Kokoro: Kokoro Commit-Queue: David Neto Auto-Submit: David Neto Reviewed-by: Dan Sinclair --- src/tint/reader/spirv/function.cc | 4 +- .../spirv/function_glsl_std_450_test.cc | 53 ++++++++++++++++++- src/tint/reader/spirv/parser_impl.cc | 3 +- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/tint/reader/spirv/function.cc b/src/tint/reader/spirv/function.cc index efc31ca34d..79c55dec11 100644 --- a/src/tint/reader/spirv/function.cc +++ b/src/tint/reader/spirv/function.cc @@ -329,6 +329,8 @@ std::string GetGlslStd450FuncName(uint32_t ext_opcode) { return "faceForward"; case GLSLstd450FindILsb: return "firstTrailingBit"; + case GLSLstd450FindSMsb: + return "firstLeadingBit"; case GLSLstd450FindUMsb: return "firstLeadingBit"; case GLSLstd450Floor: @@ -431,8 +433,6 @@ std::string GetGlslStd450FuncName(uint32_t ext_opcode) { case GLSLstd450PackDouble2x32: case GLSLstd450UnpackDouble2x32: - case GLSLstd450FindSMsb: - case GLSLstd450InterpolateAtCentroid: case GLSLstd450InterpolateAtSample: case GLSLstd450InterpolateAtOffset: diff --git a/src/tint/reader/spirv/function_glsl_std_450_test.cc b/src/tint/reader/spirv/function_glsl_std_450_test.cc index a87723e7ee..584ec2ad56 100644 --- a/src/tint/reader/spirv/function_glsl_std_450_test.cc +++ b/src/tint/reader/spirv/function_glsl_std_450_test.cc @@ -583,7 +583,8 @@ TEST_P(SpvParserTest_GlslStd450_Inting_IntingIntingInting, Vector) { INSTANTIATE_TEST_SUITE_P(Samples, SpvParserTest_GlslStd450_Inting_Inting, ::testing::Values(GlslStd450Case{"SAbs", "abs"}, - GlslStd450Case{"FindILsb", "firstTrailingBit"})); + GlslStd450Case{"FindILsb", "firstTrailingBit"}, + GlslStd450Case{"FindSMsb", "firstLeadingBit"})); INSTANTIATE_TEST_SUITE_P(Samples, SpvParserTest_GlslStd450_Inting_IntingInting, @@ -978,6 +979,56 @@ let x_4 : vec2 = bitcast>(firstTrailingBit(v2u1));)")) << body; } +TEST_F(SpvParserTest, RectifyOperandsAndResult_FindSMsb) { + // Check signedness conversion of arguments and results. + // SPIR-V signed arg -> keep it + // signed result -> keep it + // unsigned result -> cast result to unsigned + // + // SPIR-V unsigned arg -> cast it to signed + // signed result -> keept it + // unsigned result -> cast result to unsigned + const auto assembly = Preamble() + R"( + ; signed arg + ; signed result + %1 = OpExtInst %int %glsl FindSMsb %i1 + %2 = OpExtInst %v2int %glsl FindSMsb %v2i1 + + ; signed arg + ; unsigned result + %3 = OpExtInst %uint %glsl FindSMsb %i1 + %4 = OpExtInst %v2uint %glsl FindSMsb %v2i1 + + ; unsigned arg + ; signed result + %5 = OpExtInst %int %glsl FindSMsb %u1 + %6 = OpExtInst %v2int %glsl FindSMsb %v2u1 + + ; unsigned arg + ; unsigned result + %7 = OpExtInst %uint %glsl FindSMsb %u1 + %8 = OpExtInst %v2uint %glsl FindSMsb %v2u1 + OpReturn + OpFunctionEnd + )"; + auto p = parser(test::Assemble(assembly)); + ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()); + auto fe = p->function_emitter(100); + EXPECT_TRUE(fe.EmitBody()) << p->error(); + auto ast_body = fe.ast_body(); + const auto body = test::ToString(p->program(), ast_body); + EXPECT_THAT(body, HasSubstr(R"( +let x_1 : i32 = firstLeadingBit(i1); +let x_2 : vec2 = firstLeadingBit(v2i1); +let x_3 : u32 = bitcast(firstLeadingBit(i1)); +let x_4 : vec2 = bitcast>(firstLeadingBit(v2i1)); +let x_5 : i32 = firstLeadingBit(bitcast(u1)); +let x_6 : vec2 = firstLeadingBit(bitcast>(v2u1)); +let x_7 : u32 = bitcast(firstLeadingBit(bitcast(u1))); +let x_8 : vec2 = bitcast>(firstLeadingBit(bitcast>(v2u1))); +)")) << body; +} + TEST_F(SpvParserTest, RectifyOperandsAndResult_FindUMsb) { // Check signedness conversion of arguments and results. // SPIR-V signed arg -> cast arg to unsigned diff --git a/src/tint/reader/spirv/parser_impl.cc b/src/tint/reader/spirv/parser_impl.cc index c1a905693f..c64586a44a 100644 --- a/src/tint/reader/spirv/parser_impl.cc +++ b/src/tint/reader/spirv/parser_impl.cc @@ -117,6 +117,7 @@ bool AssumesSignedOperands(GLSLstd450 extended_opcode) { case GLSLstd450SMin: case GLSLstd450SMax: case GLSLstd450SClamp: + case GLSLstd450FindSMsb: return true; default: break; @@ -223,8 +224,8 @@ bool AssumesResultSignednessMatchesFirstOperand(GLSLstd450 extended_opcode) { case GLSLstd450UMax: case GLSLstd450UClamp: case GLSLstd450FindILsb: + case GLSLstd450FindSMsb: case GLSLstd450FindUMsb: - // TODO(dneto): FindSMsb? return true; default: break;