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
TEST_F(BuilderTest,Return)25 TEST_F(BuilderTest, Return) {
26 auto* ret = Return();
27 WrapInFunction(ret);
28
29 spirv::Builder& b = Build();
30
31 b.push_function(Function{});
32 EXPECT_TRUE(b.GenerateReturnStatement(ret));
33 ASSERT_FALSE(b.has_error()) << b.error();
34
35 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"(OpReturn
36 )");
37 }
38
TEST_F(BuilderTest,Return_WithValue)39 TEST_F(BuilderTest, Return_WithValue) {
40 auto* val = vec3<f32>(1.f, 1.f, 3.f);
41
42 auto* ret = Return(val);
43 Func("test", {}, ty.vec3<f32>(), {ret}, {});
44
45 spirv::Builder& b = Build();
46
47 b.push_function(Function{});
48 EXPECT_TRUE(b.GenerateReturnStatement(ret));
49 ASSERT_FALSE(b.has_error()) << b.error();
50
51 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
52 %1 = OpTypeVector %2 3
53 %3 = OpConstant %2 1
54 %4 = OpConstant %2 3
55 %5 = OpConstantComposite %1 %3 %3 %4
56 )");
57 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
58 R"(OpReturnValue %5
59 )");
60 }
61
TEST_F(BuilderTest,Return_WithValue_GeneratesLoad)62 TEST_F(BuilderTest, Return_WithValue_GeneratesLoad) {
63 auto* var = Var("param", ty.f32());
64
65 auto* ret = Return(var);
66 Func("test", {}, ty.f32(), {Decl(var), ret}, {});
67
68 spirv::Builder& b = Build();
69
70 b.push_function(Function{});
71 EXPECT_TRUE(b.GenerateFunctionVariable(var)) << b.error();
72 EXPECT_TRUE(b.GenerateReturnStatement(ret)) << b.error();
73 ASSERT_FALSE(b.has_error()) << b.error();
74
75 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
76 %2 = OpTypePointer Function %3
77 %4 = OpConstantNull %3
78 )");
79 EXPECT_EQ(DumpInstructions(b.functions()[0].variables()),
80 R"(%1 = OpVariable %2 Function %4
81 )");
82 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
83 R"(%5 = OpLoad %3 %1
84 OpReturnValue %5
85 )");
86 }
87
88 } // namespace
89 } // namespace spirv
90 } // namespace writer
91 } // namespace tint
92