1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/writer/spirv/spv_dump.h"
16 #include "src/writer/spirv/test_helper.h"
17
18 namespace tint {
19 namespace writer {
20 namespace spirv {
21 namespace {
22
23 using BuilderTest = TestHelper;
24
25 // TODO(amaiorano): Disabled because Resolver now emits "redeclared identifier
26 // 'var'".
TEST_F(BuilderTest,DISABLED_Block)27 TEST_F(BuilderTest, DISABLED_Block) {
28 // Note, this test uses shadow variables which aren't allowed in WGSL but
29 // serves to prove the block code is pushing new scopes as needed.
30 auto* inner = Block(Decl(Var("var", ty.f32(), ast::StorageClass::kNone)),
31 Assign("var", 2.f));
32 auto* outer = Block(Decl(Var("var", ty.f32(), ast::StorageClass::kNone)),
33 Assign("var", 1.f), inner, Assign("var", 3.f));
34
35 WrapInFunction(outer);
36
37 spirv::Builder& b = Build();
38
39 b.push_function(Function{});
40 ASSERT_FALSE(b.has_error()) << b.error();
41
42 EXPECT_TRUE(b.GenerateStatement(outer)) << b.error();
43 EXPECT_FALSE(b.has_error());
44
45 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
46 %2 = OpTypePointer Function %3
47 %4 = OpConstantNull %3
48 %5 = OpConstant %3 1
49 %7 = OpConstant %3 2
50 %8 = OpConstant %3 3
51 )");
52
53 EXPECT_EQ(DumpInstructions(b.functions()[0].variables()),
54 R"(%1 = OpVariable %2 Function %4
55 %6 = OpVariable %2 Function %4
56 )");
57
58 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
59 R"(OpStore %1 %5
60 OpStore %6 %7
61 OpStore %1 %8
62 )");
63 }
64
65 } // namespace
66 } // namespace spirv
67 } // namespace writer
68 } // namespace tint
69