blob: 16189d6aac1f1161393bfa3a9e0257868173de08 [file] [log] [blame]
dan sinclaira8274b22020-09-21 18:49:01 +00001// 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 <string>
16
17#include "gtest/gtest.h"
18#include "spirv/unified1/spirv.h"
19#include "spirv/unified1/spirv.hpp11"
20#include "src/ast/assignment_statement.h"
21#include "src/ast/function.h"
22#include "src/ast/identifier_expression.h"
23#include "src/ast/pipeline_stage.h"
24#include "src/ast/stage_decoration.h"
dan sinclaira8274b22020-09-21 18:49:01 +000025#include "src/ast/variable.h"
26#include "src/ast/workgroup_decoration.h"
dan sinclaira8274b22020-09-21 18:49:01 +000027#include "src/type_determiner.h"
28#include "src/writer/spirv/builder.h"
29#include "src/writer/spirv/spv_dump.h"
dan sinclair196e0972020-11-13 18:13:24 +000030#include "src/writer/spirv/test_helper.h"
dan sinclaira8274b22020-09-21 18:49:01 +000031
32namespace tint {
33namespace writer {
34namespace spirv {
35namespace {
36
dan sinclair196e0972020-11-13 18:13:24 +000037using BuilderTest = TestHelper;
dan sinclaira8274b22020-09-21 18:49:01 +000038
39TEST_F(BuilderTest, FunctionDecoration_Stage) {
Ben Claytone6e70412021-01-05 15:29:29 +000040 auto* func =
41 Func("main", {}, ty.void_, ast::StatementList{},
42 ast::FunctionDecorationList{
43 create<ast::StageDecoration>(ast::PipelineStage::kVertex),
44 });
dan sinclaira8274b22020-09-21 18:49:01 +000045
dan sinclairb5839932020-12-16 21:38:40 +000046 ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
dan sinclair84f82752020-11-10 21:49:56 +000047 EXPECT_EQ(DumpInstructions(b.entry_points()),
dan sinclair987376c2021-01-12 04:34:53 +000048 R"(OpEntryPoint Vertex %3 "main"
dan sinclaira8274b22020-09-21 18:49:01 +000049)");
50}
51
52struct FunctionStageData {
53 ast::PipelineStage stage;
54 SpvExecutionModel model;
55};
56inline std::ostream& operator<<(std::ostream& out, FunctionStageData data) {
57 out << data.stage;
58 return out;
59}
dan sinclair196e0972020-11-13 18:13:24 +000060using FunctionDecoration_StageTest = TestParamHelper<FunctionStageData>;
dan sinclaira8274b22020-09-21 18:49:01 +000061TEST_P(FunctionDecoration_StageTest, Emit) {
62 auto params = GetParam();
63
dan sinclairb5839932020-12-16 21:38:40 +000064 auto* func = Func("main", {}, ty.void_, ast::StatementList{},
65 ast::FunctionDecorationList{
Ben Claytone6e70412021-01-05 15:29:29 +000066 create<ast::StageDecoration>(params.stage),
dan sinclairb5839932020-12-16 21:38:40 +000067 });
dan sinclaira8274b22020-09-21 18:49:01 +000068
dan sinclairb5839932020-12-16 21:38:40 +000069 ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
dan sinclaira8274b22020-09-21 18:49:01 +000070
dan sinclair488d7a92020-10-06 21:20:28 +000071 auto preamble = b.entry_points();
dan sinclaird5fd7e02020-11-03 16:26:09 +000072 ASSERT_GE(preamble.size(), 1u);
dan sinclaira8274b22020-09-21 18:49:01 +000073 EXPECT_EQ(preamble[0].opcode(), spv::Op::OpEntryPoint);
74
75 ASSERT_GE(preamble[0].operands().size(), 3u);
76 EXPECT_EQ(preamble[0].operands()[0].to_i(), params.model);
77}
78INSTANTIATE_TEST_SUITE_P(
79 BuilderTest,
80 FunctionDecoration_StageTest,
81 testing::Values(FunctionStageData{ast::PipelineStage::kVertex,
82 SpvExecutionModelVertex},
83 FunctionStageData{ast::PipelineStage::kFragment,
84 SpvExecutionModelFragment},
85 FunctionStageData{ast::PipelineStage::kCompute,
86 SpvExecutionModelGLCompute}));
87
88TEST_F(BuilderTest, FunctionDecoration_Stage_WithUnusedInterfaceIds) {
Ben Claytone6e70412021-01-05 15:29:29 +000089 auto* func =
90 Func("main", {}, ty.void_, ast::StatementList{},
91 ast::FunctionDecorationList{
92 create<ast::StageDecoration>(ast::PipelineStage::kVertex),
93 });
Ben Clayton234b7de2020-12-07 20:45:14 +000094
dan sinclairb5839932020-12-16 21:38:40 +000095 auto* v_in = Var("my_in", ast::StorageClass::kInput, ty.f32);
96 auto* v_out = Var("my_out", ast::StorageClass::kOutput, ty.f32);
97 auto* v_wg = Var("my_wg", ast::StorageClass::kWorkgroup, ty.f32);
dan sinclaira8274b22020-09-21 18:49:01 +000098
Ben Claytonb053acf2020-11-16 16:31:07 +000099 EXPECT_TRUE(b.GenerateGlobalVariable(v_in)) << b.error();
100 EXPECT_TRUE(b.GenerateGlobalVariable(v_out)) << b.error();
101 EXPECT_TRUE(b.GenerateGlobalVariable(v_wg)) << b.error();
dan sinclaira8274b22020-09-21 18:49:01 +0000102
Ben Clayton2f4096b2020-11-18 20:58:20 +0000103 mod->AddGlobalVariable(v_in);
104 mod->AddGlobalVariable(v_out);
105 mod->AddGlobalVariable(v_wg);
dan sinclaira8274b22020-09-21 18:49:01 +0000106
dan sinclairb5839932020-12-16 21:38:40 +0000107 ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
dan sinclair987376c2021-01-12 04:34:53 +0000108 EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "my_in"
109OpName %4 "my_out"
110OpName %7 "my_wg"
111OpName %11 "main"
dan sinclaira8274b22020-09-21 18:49:01 +0000112)");
113 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
114%2 = OpTypePointer Input %3
115%1 = OpVariable %2 Input
116%5 = OpTypePointer Output %3
117%6 = OpConstantNull %3
118%4 = OpVariable %5 Output %6
119%8 = OpTypePointer Workgroup %3
120%7 = OpVariable %8 Workgroup
121%10 = OpTypeVoid
122%9 = OpTypeFunction %10
123)");
dan sinclair488d7a92020-10-06 21:20:28 +0000124 EXPECT_EQ(DumpInstructions(b.entry_points()),
dan sinclair987376c2021-01-12 04:34:53 +0000125 R"(OpEntryPoint Vertex %11 "main"
dan sinclaira8274b22020-09-21 18:49:01 +0000126)");
127}
128
129TEST_F(BuilderTest, FunctionDecoration_Stage_WithUsedInterfaceIds) {
Ben Claytone6e70412021-01-05 15:29:29 +0000130 auto* func =
131 Func("main", {}, ty.void_,
132 ast::StatementList{
133 create<ast::AssignmentStatement>(Expr("my_out"), Expr("my_in")),
134 create<ast::AssignmentStatement>(Expr("my_wg"), Expr("my_wg")),
135 // Add duplicate usages so we show they don't get
136 // output multiple times.
137 create<ast::AssignmentStatement>(Expr("my_out"), Expr("my_in"))},
138 ast::FunctionDecorationList{
139 create<ast::StageDecoration>(ast::PipelineStage::kVertex),
140 });
dan sinclaira8274b22020-09-21 18:49:01 +0000141
dan sinclairb5839932020-12-16 21:38:40 +0000142 auto* v_in = Var("my_in", ast::StorageClass::kInput, ty.f32);
143 auto* v_out = Var("my_out", ast::StorageClass::kOutput, ty.f32);
144 auto* v_wg = Var("my_wg", ast::StorageClass::kWorkgroup, ty.f32);
dan sinclaira8274b22020-09-21 18:49:01 +0000145
Ben Claytonb053acf2020-11-16 16:31:07 +0000146 td.RegisterVariableForTesting(v_in);
147 td.RegisterVariableForTesting(v_out);
148 td.RegisterVariableForTesting(v_wg);
dan sinclaira8274b22020-09-21 18:49:01 +0000149
dan sinclairb5839932020-12-16 21:38:40 +0000150 ASSERT_TRUE(td.DetermineFunction(func)) << td.error();
dan sinclaira8274b22020-09-21 18:49:01 +0000151
Ben Claytonb053acf2020-11-16 16:31:07 +0000152 EXPECT_TRUE(b.GenerateGlobalVariable(v_in)) << b.error();
153 EXPECT_TRUE(b.GenerateGlobalVariable(v_out)) << b.error();
154 EXPECT_TRUE(b.GenerateGlobalVariable(v_wg)) << b.error();
dan sinclaira8274b22020-09-21 18:49:01 +0000155
Ben Clayton2f4096b2020-11-18 20:58:20 +0000156 mod->AddGlobalVariable(v_in);
157 mod->AddGlobalVariable(v_out);
158 mod->AddGlobalVariable(v_wg);
dan sinclaira8274b22020-09-21 18:49:01 +0000159
dan sinclairb5839932020-12-16 21:38:40 +0000160 ASSERT_TRUE(b.GenerateFunction(func)) << b.error();
dan sinclair987376c2021-01-12 04:34:53 +0000161 EXPECT_EQ(DumpInstructions(b.debug()), R"(OpName %1 "my_in"
162OpName %4 "my_out"
163OpName %7 "my_wg"
164OpName %11 "main"
dan sinclaira8274b22020-09-21 18:49:01 +0000165)");
166 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
167%2 = OpTypePointer Input %3
168%1 = OpVariable %2 Input
169%5 = OpTypePointer Output %3
170%6 = OpConstantNull %3
171%4 = OpVariable %5 Output %6
172%8 = OpTypePointer Workgroup %3
173%7 = OpVariable %8 Workgroup
174%10 = OpTypeVoid
175%9 = OpTypeFunction %10
176)");
dan sinclair488d7a92020-10-06 21:20:28 +0000177 EXPECT_EQ(DumpInstructions(b.entry_points()),
dan sinclair987376c2021-01-12 04:34:53 +0000178 R"(OpEntryPoint Vertex %11 "main" %4 %1
dan sinclaira8274b22020-09-21 18:49:01 +0000179)");
180}
181
182TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_Fragment_OriginUpperLeft) {
Ben Claytone6e70412021-01-05 15:29:29 +0000183 auto* func =
184 Func("main", {}, ty.void_, ast::StatementList{},
185 ast::FunctionDecorationList{
186 create<ast::StageDecoration>(ast::PipelineStage::kFragment),
187 });
dan sinclaira8274b22020-09-21 18:49:01 +0000188
dan sinclairb5839932020-12-16 21:38:40 +0000189 ASSERT_TRUE(b.GenerateExecutionModes(func, 3)) << b.error();
dan sinclair488d7a92020-10-06 21:20:28 +0000190 EXPECT_EQ(DumpInstructions(b.execution_modes()),
dan sinclaira8274b22020-09-21 18:49:01 +0000191 R"(OpExecutionMode %3 OriginUpperLeft
192)");
193}
194
Ben Clayton8df62842020-12-15 12:36:08 +0000195TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_WorkgroupSize_Default) {
Ben Claytone6e70412021-01-05 15:29:29 +0000196 auto* func =
197 Func("main", {}, ty.void_, ast::StatementList{},
198 ast::FunctionDecorationList{
199 create<ast::StageDecoration>(ast::PipelineStage::kCompute),
200 });
dan sinclaira8274b22020-09-21 18:49:01 +0000201
dan sinclairb5839932020-12-16 21:38:40 +0000202 ASSERT_TRUE(b.GenerateExecutionModes(func, 3)) << b.error();
dan sinclair488d7a92020-10-06 21:20:28 +0000203 EXPECT_EQ(DumpInstructions(b.execution_modes()),
dan sinclaira8274b22020-09-21 18:49:01 +0000204 R"(OpExecutionMode %3 LocalSize 1 1 1
205)");
206}
207
Ben Clayton8df62842020-12-15 12:36:08 +0000208TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_WorkgroupSize) {
Ben Claytone6e70412021-01-05 15:29:29 +0000209 auto* func =
210 Func("main", {}, ty.void_, ast::StatementList{},
211 ast::FunctionDecorationList{
212 create<ast::WorkgroupDecoration>(2u, 4u, 6u),
213 create<ast::StageDecoration>(ast::PipelineStage::kCompute),
214 });
dan sinclaira8274b22020-09-21 18:49:01 +0000215
dan sinclairb5839932020-12-16 21:38:40 +0000216 ASSERT_TRUE(b.GenerateExecutionModes(func, 3)) << b.error();
dan sinclair488d7a92020-10-06 21:20:28 +0000217 EXPECT_EQ(DumpInstructions(b.execution_modes()),
dan sinclaira8274b22020-09-21 18:49:01 +0000218 R"(OpExecutionMode %3 LocalSize 2 4 6
219)");
220}
221
dan sinclair488d7a92020-10-06 21:20:28 +0000222TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_MultipleFragment) {
Ben Claytone6e70412021-01-05 15:29:29 +0000223 auto* func1 =
224 Func("main1", {}, ty.void_, ast::StatementList{},
225 ast::FunctionDecorationList{
226 create<ast::StageDecoration>(ast::PipelineStage::kFragment),
227 });
dan sinclair488d7a92020-10-06 21:20:28 +0000228
Ben Claytone6e70412021-01-05 15:29:29 +0000229 auto* func2 =
230 Func("main2", {}, ty.void_, ast::StatementList{},
231 ast::FunctionDecorationList{
232 create<ast::StageDecoration>(ast::PipelineStage::kFragment),
233 });
dan sinclair488d7a92020-10-06 21:20:28 +0000234
dan sinclairb5839932020-12-16 21:38:40 +0000235 ASSERT_TRUE(b.GenerateFunction(func1)) << b.error();
236 ASSERT_TRUE(b.GenerateFunction(func2)) << b.error();
dan sinclair488d7a92020-10-06 21:20:28 +0000237 EXPECT_EQ(DumpBuilder(b),
dan sinclair987376c2021-01-12 04:34:53 +0000238 R"(OpEntryPoint Fragment %3 "main1"
239OpEntryPoint Fragment %5 "main2"
dan sinclair488d7a92020-10-06 21:20:28 +0000240OpExecutionMode %3 OriginUpperLeft
241OpExecutionMode %5 OriginUpperLeft
dan sinclair987376c2021-01-12 04:34:53 +0000242OpName %3 "main1"
243OpName %5 "main2"
dan sinclair488d7a92020-10-06 21:20:28 +0000244%2 = OpTypeVoid
245%1 = OpTypeFunction %2
246%3 = OpFunction %2 None %1
247%4 = OpLabel
dan sinclaira7c93912020-11-18 18:25:30 +0000248OpReturn
dan sinclair488d7a92020-10-06 21:20:28 +0000249OpFunctionEnd
250%5 = OpFunction %2 None %1
251%6 = OpLabel
dan sinclaira7c93912020-11-18 18:25:30 +0000252OpReturn
dan sinclair488d7a92020-10-06 21:20:28 +0000253OpFunctionEnd
254)");
255}
256
Ben Clayton8df62842020-12-15 12:36:08 +0000257TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_FragDepth) {
258 auto* fragdepth =
dan sinclairb5839932020-12-16 21:38:40 +0000259 Var("fragdepth", ast::StorageClass::kOutput, ty.f32, nullptr,
Ben Clayton8df62842020-12-15 12:36:08 +0000260 ast::VariableDecorationList{
261 create<ast::BuiltinDecoration>(ast::Builtin::kFragDepth),
262 });
263 mod->AddGlobalVariable(fragdepth);
264
dan sinclair181d8ba2020-12-16 15:15:40 +0000265 auto* func =
dan sinclairb5839932020-12-16 21:38:40 +0000266 Func("main", ast::VariableList{}, ty.void_,
dan sinclair181d8ba2020-12-16 15:15:40 +0000267 ast::StatementList{
268 create<ast::AssignmentStatement>(Expr("fragdepth"), Expr(1.f)),
269 },
270 ast::FunctionDecorationList{});
Ben Clayton8df62842020-12-15 12:36:08 +0000271
272 func->add_referenced_module_variable(fragdepth);
273
274 ASSERT_TRUE(b.GenerateExecutionModes(func, 3)) << b.error();
275 EXPECT_EQ(DumpInstructions(b.execution_modes()),
276 R"(OpExecutionMode %3 DepthReplacing
277)");
278}
279
dan sinclaira8274b22020-09-21 18:49:01 +0000280} // namespace
281} // namespace spirv
282} // namespace writer
283} // namespace tint