From 3c3d2275fe67846ffaf9289616820dad8b972abf Mon Sep 17 00:00:00 2001 From: David Neto Date: Thu, 16 Jul 2020 21:13:58 +0000 Subject: [PATCH] [spirv-reader] Support Location on module vars Bug: tint:3 Change-Id: If7fc85251cbffab477b0b4f86c204de6c6a853f6 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/25103 Reviewed-by: dan sinclair --- src/reader/spirv/parser_impl.cc | 9 ++++ .../spirv/parser_impl_module_var_test.cc | 51 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index ebddea922e..69fe861240 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -942,6 +942,15 @@ std::unique_ptr ParserImpl::MakeVariable(uint32_t id, ast_decorations.emplace_back( std::make_unique(ast_builtin)); } + if (deco[0] == SpvDecorationLocation) { + if (deco.size() != 2) { + Fail() << "malformed Location decoration on ID " << id + << ": requires one literal operand"; + return nullptr; + } + ast_decorations.emplace_back( + std::make_unique(deco[1])); + } } if (!ast_decorations.empty()) { auto decorated_var = diff --git a/src/reader/spirv/parser_impl_module_var_test.cc b/src/reader/spirv/parser_impl_module_var_test.cc index 11e0f4a7ff..da57fd8a70 100644 --- a/src/reader/spirv/parser_impl_module_var_test.cc +++ b/src/reader/spirv/parser_impl_module_var_test.cc @@ -1117,6 +1117,57 @@ TEST_F(SpvParserTest, ModuleScopeVar_StructUndefInitializer) { << module_str; } +TEST_F(SpvParserTest, ModuleScopeVar_LocationDecoration_Valid) { + auto* p = parser(test::Assemble(R"( + OpName %myvar "myvar" + OpDecorate %myvar Location 3 +)" + CommonTypes() + R"( + %ptr = OpTypePointer Input %uint + %myvar = OpVariable %ptr Input + )")); + ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error(); + EXPECT_TRUE(p->error().empty()); + const auto module_str = p->module().to_str(); + EXPECT_THAT(module_str, HasSubstr(R"( + DecoratedVariable{ + Decorations{ + LocationDecoration{3} + } + myvar + in + __u32 + })")) + << module_str; +} + +TEST_F(SpvParserTest, + ModuleScopeVar_LocationDecoration_MissingOperandWontAssemble) { + const auto assembly = R"( + OpName %myvar "myvar" + OpDecorate %myvar Location +)" + CommonTypes() + R"( + %ptr = OpTypePointer Input %uint + %myvar = OpVariable %ptr Input + )"; + EXPECT_THAT(test::AssembleFailure(assembly), + Eq("4:4: Expected operand, found next instruction instead.")); +} + +TEST_F(SpvParserTest, + ModuleScopeVar_LocationDecoration_TwoOperandsWontAssemble) { + const auto assembly = R"( + OpName %myvar "myvar" + OpDecorate %myvar Location 3 4 +)" + CommonTypes() + R"( + %ptr = OpTypePointer Input %uint + %myvar = OpVariable %ptr Input + )"; + EXPECT_THAT( + test::AssembleFailure(assembly), + Eq("2:34: Expected or at the beginning of an " + "instruction, found '4'.")); +} + } // namespace } // namespace spirv } // namespace reader