blob: 4ac92c047b941694bb1564a0da49b88ab8bb3df9 [file] [log] [blame]
dan sinclair5cd08fd2020-03-30 20:01:38 +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"
dan sinclair1c9b4862020-04-07 19:27:41 +000020#include "src/ast/binary_expression.h"
dan sinclair3c025922020-09-24 14:38:44 +000021#include "src/ast/bool_literal.h"
dan sinclair5cd08fd2020-03-30 20:01:38 +000022#include "src/ast/float_literal.h"
dan sinclair361e4572020-04-24 00:41:12 +000023#include "src/ast/identifier_expression.h"
dan sinclair3c025922020-09-24 14:38:44 +000024#include "src/ast/member_accessor_expression.h"
dan sinclaira322f5d2020-03-30 22:46:06 +000025#include "src/ast/scalar_constructor_expression.h"
dan sinclair3c025922020-09-24 14:38:44 +000026#include "src/ast/sint_literal.h"
27#include "src/ast/struct.h"
28#include "src/ast/struct_decoration.h"
29#include "src/ast/struct_member.h"
30#include "src/ast/type/array_type.h"
31#include "src/ast/type/bool_type.h"
dan sinclair5cd08fd2020-03-30 20:01:38 +000032#include "src/ast/type/f32_type.h"
dan sinclair3c025922020-09-24 14:38:44 +000033#include "src/ast/type/i32_type.h"
34#include "src/ast/type/matrix_type.h"
35#include "src/ast/type/struct_type.h"
36#include "src/ast/type/u32_type.h"
dan sinclair5cd08fd2020-03-30 20:01:38 +000037#include "src/ast/type/vector_type.h"
dan sinclaira322f5d2020-03-30 22:46:06 +000038#include "src/ast/type_constructor_expression.h"
dan sinclair3c025922020-09-24 14:38:44 +000039#include "src/ast/uint_literal.h"
dan sinclair361e4572020-04-24 00:41:12 +000040#include "src/context.h"
41#include "src/type_determiner.h"
dan sinclair5cd08fd2020-03-30 20:01:38 +000042#include "src/writer/spirv/builder.h"
43#include "src/writer/spirv/spv_dump.h"
dan sinclair196e0972020-11-13 18:13:24 +000044#include "src/writer/spirv/test_helper.h"
dan sinclair5cd08fd2020-03-30 20:01:38 +000045
46namespace tint {
47namespace writer {
48namespace spirv {
49namespace {
50
Ben Clayton0ae939b2020-11-17 17:53:38 +000051using SpvBuilderConstructorTest = TestHelper;
dan sinclair5cd08fd2020-03-30 20:01:38 +000052
Ben Clayton0ae939b2020-11-17 17:53:38 +000053TEST_F(SpvBuilderConstructorTest, Const) {
54 auto* c = Expr(42.2f);
dan sinclair5cd08fd2020-03-30 20:01:38 +000055
Ben Clayton0ae939b2020-11-17 17:53:38 +000056 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, c, true), 2u);
dan sinclair5cd08fd2020-03-30 20:01:38 +000057 ASSERT_FALSE(b.has_error()) << b.error();
58
59 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32
60%2 = OpConstant %1 42.2000008
61)");
62}
63
Ben Clayton0ae939b2020-11-17 17:53:38 +000064TEST_F(SpvBuilderConstructorTest, Type) {
65 auto* t = vec3<f32>(1.0f, 1.0f, 3.0f);
dan sinclair5cd08fd2020-03-30 20:01:38 +000066
Ben Clayton0ae939b2020-11-17 17:53:38 +000067 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair5cd08fd2020-03-30 20:01:38 +000068
Ben Clayton0ae939b2020-11-17 17:53:38 +000069 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, t, true), 5u);
dan sinclair5cd08fd2020-03-30 20:01:38 +000070 ASSERT_FALSE(b.has_error()) << b.error();
71
72 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
73%1 = OpTypeVector %2 3
74%3 = OpConstant %2 1
75%4 = OpConstant %2 3
dan sinclair73e1ef82020-03-31 21:15:51 +000076%5 = OpConstantComposite %1 %3 %3 %4
dan sinclair5cd08fd2020-03-30 20:01:38 +000077)");
78}
79
Ben Clayton0ae939b2020-11-17 17:53:38 +000080TEST_F(SpvBuilderConstructorTest, Type_WithCasts) {
81 auto* t = vec2<f32>(Construct<f32>(1), Construct<f32>(1));
dan sinclair435916e2020-10-14 15:14:11 +000082
Ben Clayton0ae939b2020-11-17 17:53:38 +000083 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +000084
dan sinclair435916e2020-10-14 15:14:11 +000085 b.push_function(Function{});
86
Ben Clayton0ae939b2020-11-17 17:53:38 +000087 EXPECT_EQ(b.GenerateExpression(t), 7u);
dan sinclair435916e2020-10-14 15:14:11 +000088 ASSERT_FALSE(b.has_error()) << b.error();
89
90 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
91%1 = OpTypeVector %2 2
92%4 = OpTypeInt 32 1
93%5 = OpConstant %4 1
94)");
95 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
96 R"(%3 = OpConvertSToF %2 %5
97%6 = OpConvertSToF %2 %5
98%7 = OpCompositeConstruct %1 %3 %6
99)");
100}
101
Ben Clayton0ae939b2020-11-17 17:53:38 +0000102TEST_F(SpvBuilderConstructorTest, Type_WithAlias) {
dan sinclair3c025922020-09-24 14:38:44 +0000103 // type Int = i32
Ben Clayton0ae939b2020-11-17 17:53:38 +0000104 // cast<Int>(2.3f)
dan sinclair3c025922020-09-24 14:38:44 +0000105
Ben Clayton0ae939b2020-11-17 17:53:38 +0000106 ast::type::AliasType alias("Int", ty.i32);
dan sinclair3c025922020-09-24 14:38:44 +0000107
Ben Clayton0ae939b2020-11-17 17:53:38 +0000108 ast::TypeConstructorExpression cast(&alias, ExprList(2.3f));
dan sinclair5b853ee2020-06-22 20:44:27 +0000109
dan sinclair3c025922020-09-24 14:38:44 +0000110 ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error();
dan sinclair5b853ee2020-06-22 20:44:27 +0000111
dan sinclair5b853ee2020-06-22 20:44:27 +0000112 b.push_function(Function{});
dan sinclair3c025922020-09-24 14:38:44 +0000113 EXPECT_EQ(b.GenerateExpression(&cast), 1u);
dan sinclair5b853ee2020-06-22 20:44:27 +0000114
dan sinclair3c025922020-09-24 14:38:44 +0000115 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
116%3 = OpTypeFloat 32
117%4 = OpConstant %3 2.29999995
118)");
119 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
120 R"(%1 = OpConvertFToS %2 %4
dan sinclair5b853ee2020-06-22 20:44:27 +0000121)");
122}
123
Ben Clayton0ae939b2020-11-17 17:53:38 +0000124TEST_F(SpvBuilderConstructorTest, Type_IdentifierExpression_Param) {
125 auto* var = Var("ident", ast::StorageClass::kFunction, ty.f32);
dan sinclair361e4572020-04-24 00:41:12 +0000126
Ben Clayton0ae939b2020-11-17 17:53:38 +0000127 auto* t = vec2<f32>(1.0f, "ident");
dan sinclair361e4572020-04-24 00:41:12 +0000128
Ben Clayton0ae939b2020-11-17 17:53:38 +0000129 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair361e4572020-04-24 00:41:12 +0000130
dan sinclair361e4572020-04-24 00:41:12 +0000131 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +0000132 ASSERT_TRUE(b.GenerateFunctionVariable(var)) << b.error();
dan sinclair361e4572020-04-24 00:41:12 +0000133
Ben Clayton0ae939b2020-11-17 17:53:38 +0000134 EXPECT_EQ(b.GenerateExpression(t), 8u);
dan sinclair361e4572020-04-24 00:41:12 +0000135 ASSERT_FALSE(b.has_error()) << b.error();
136
137 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
138%2 = OpTypePointer Function %3
dan sinclair2287f332020-05-05 14:21:19 +0000139%4 = OpConstantNull %3
140%5 = OpTypeVector %3 2
141%6 = OpConstant %3 1
dan sinclair361e4572020-04-24 00:41:12 +0000142)");
143 EXPECT_EQ(DumpInstructions(b.functions()[0].variables()),
dan sinclair2287f332020-05-05 14:21:19 +0000144 R"(%1 = OpVariable %2 Function %4
dan sinclair361e4572020-04-24 00:41:12 +0000145)");
146
147 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
dan sinclair2287f332020-05-05 14:21:19 +0000148 R"(%7 = OpLoad %3 %1
149%8 = OpCompositeConstruct %5 %6 %7
dan sinclair361e4572020-04-24 00:41:12 +0000150)");
151}
152
Ben Clayton0ae939b2020-11-17 17:53:38 +0000153TEST_F(SpvBuilderConstructorTest, Vector_Bitcast_Params) {
154 auto* t = vec2<u32>(1, 1);
dan sinclaira388d562020-10-07 14:06:28 +0000155
Ben Clayton0ae939b2020-11-17 17:53:38 +0000156 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclaira388d562020-10-07 14:06:28 +0000157
dan sinclaira388d562020-10-07 14:06:28 +0000158 b.push_function(Function{});
159
Ben Clayton0ae939b2020-11-17 17:53:38 +0000160 EXPECT_EQ(b.GenerateExpression(t), 7u);
dan sinclaira388d562020-10-07 14:06:28 +0000161 ASSERT_FALSE(b.has_error()) << b.error();
162
163 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
164%1 = OpTypeVector %2 2
165%3 = OpTypeInt 32 1
166%4 = OpConstant %3 1
167)");
168
169 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
170 R"(%5 = OpBitcast %2 %4
171%6 = OpBitcast %2 %4
172%7 = OpCompositeConstruct %1 %5 %6
173)");
174}
175
Ben Clayton0ae939b2020-11-17 17:53:38 +0000176TEST_F(SpvBuilderConstructorTest, Type_NonConst_Value_Fails) {
177 auto* rel = create<ast::BinaryExpression>(ast::BinaryOp::kAdd, Expr(3.0f),
178 Expr(3.0f));
dan sinclair5cd08fd2020-03-30 20:01:38 +0000179
Ben Clayton0ae939b2020-11-17 17:53:38 +0000180 auto* t = vec2<f32>(1.0f, rel);
dan sinclair5cd08fd2020-03-30 20:01:38 +0000181
Ben Clayton0ae939b2020-11-17 17:53:38 +0000182 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair5cd08fd2020-03-30 20:01:38 +0000183
Ben Clayton0ae939b2020-11-17 17:53:38 +0000184 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, t, true), 0u);
dan sinclair5cd08fd2020-03-30 20:01:38 +0000185 EXPECT_TRUE(b.has_error());
dan sinclaira322f5d2020-03-30 22:46:06 +0000186 EXPECT_EQ(b.error(), R"(constructor must be a constant expression)");
dan sinclair5cd08fd2020-03-30 20:01:38 +0000187}
188
Ben Clayton0ae939b2020-11-17 17:53:38 +0000189TEST_F(SpvBuilderConstructorTest, Type_Bool_With_Bool) {
190 ast::TypeConstructorExpression cast(ty.bool_, ExprList(true));
dan sinclair3c025922020-09-24 14:38:44 +0000191
Ben Clayton0ae939b2020-11-17 17:53:38 +0000192 ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000193
dan sinclair3c025922020-09-24 14:38:44 +0000194 b.push_function(Function{});
195
David Neto6cd6f742020-11-23 16:34:35 +0000196 EXPECT_EQ(b.GenerateExpression(&cast), 3u);
dan sinclair3c025922020-09-24 14:38:44 +0000197 ASSERT_FALSE(b.has_error()) << b.error();
198
199 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeBool
200%3 = OpConstantTrue %2
201)");
David Neto6cd6f742020-11-23 16:34:35 +0000202 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
dan sinclair3c025922020-09-24 14:38:44 +0000203}
204
Ben Clayton0ae939b2020-11-17 17:53:38 +0000205TEST_F(SpvBuilderConstructorTest, Type_I32_With_I32) {
206 ast::TypeConstructorExpression cast(ty.i32, ExprList(2));
dan sinclair3c025922020-09-24 14:38:44 +0000207
dan sinclair3c025922020-09-24 14:38:44 +0000208 ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error();
209
dan sinclair3c025922020-09-24 14:38:44 +0000210 b.push_function(Function{});
David Neto6cd6f742020-11-23 16:34:35 +0000211 EXPECT_EQ(b.GenerateExpression(&cast), 3u);
dan sinclair3c025922020-09-24 14:38:44 +0000212
213 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
214%3 = OpConstant %2 2
215)");
David Neto6cd6f742020-11-23 16:34:35 +0000216 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
dan sinclair3c025922020-09-24 14:38:44 +0000217}
218
Ben Clayton0ae939b2020-11-17 17:53:38 +0000219TEST_F(SpvBuilderConstructorTest, Type_U32_With_U32) {
220 ast::TypeConstructorExpression cast(ty.u32, ExprList(2u));
dan sinclair3c025922020-09-24 14:38:44 +0000221
dan sinclair3c025922020-09-24 14:38:44 +0000222 ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error();
223
dan sinclair3c025922020-09-24 14:38:44 +0000224 b.push_function(Function{});
David Neto6cd6f742020-11-23 16:34:35 +0000225 EXPECT_EQ(b.GenerateExpression(&cast), 3u);
dan sinclair3c025922020-09-24 14:38:44 +0000226
227 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
228%3 = OpConstant %2 2
229)");
David Neto6cd6f742020-11-23 16:34:35 +0000230 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
dan sinclair3c025922020-09-24 14:38:44 +0000231}
232
Ben Clayton0ae939b2020-11-17 17:53:38 +0000233TEST_F(SpvBuilderConstructorTest, Type_F32_With_F32) {
234 ast::TypeConstructorExpression cast(ty.f32, ExprList(2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000235
dan sinclair3c025922020-09-24 14:38:44 +0000236 ASSERT_TRUE(td.DetermineResultType(&cast)) << td.error();
237
dan sinclair3c025922020-09-24 14:38:44 +0000238 b.push_function(Function{});
David Neto6cd6f742020-11-23 16:34:35 +0000239 EXPECT_EQ(b.GenerateExpression(&cast), 3u);
dan sinclair3c025922020-09-24 14:38:44 +0000240
241 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
242%3 = OpConstant %2 2
243)");
David Neto6cd6f742020-11-23 16:34:35 +0000244 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
dan sinclair3c025922020-09-24 14:38:44 +0000245}
246
Ben Clayton0ae939b2020-11-17 17:53:38 +0000247TEST_F(SpvBuilderConstructorTest, Type_Vec2_With_F32_F32) {
248 auto* cast = vec2<f32>(2.0f, 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000249
Ben Clayton0ae939b2020-11-17 17:53:38 +0000250 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000251
dan sinclair3c025922020-09-24 14:38:44 +0000252 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000253 EXPECT_EQ(b.GenerateExpression(cast), 4u);
dan sinclair3c025922020-09-24 14:38:44 +0000254
255 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
256%1 = OpTypeVector %2 2
257%3 = OpConstant %2 2
258%4 = OpConstantComposite %1 %3 %3
259)");
260}
261
David Neto6cd6f742020-11-23 16:34:35 +0000262TEST_F(SpvBuilderConstructorTest, Type_Vec2_With_Vec2) {
263 auto* value = vec2<f32>(2.0f, 2.0f);
264 auto* cast = vec2<f32>(value);
265
266 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
267
268 b.push_function(Function{});
269 EXPECT_EQ(b.GenerateExpression(cast), 5u);
270
271 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
272%2 = OpTypeVector %3 2
273%4 = OpConstant %3 2
274%5 = OpConstantComposite %2 %4 %4
275)");
276 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
277}
278
Ben Clayton0ae939b2020-11-17 17:53:38 +0000279TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_F32_F32_F32) {
280 auto* cast = vec3<f32>(2.0f, 2.0f, 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000281
Ben Clayton0ae939b2020-11-17 17:53:38 +0000282 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000283
dan sinclair3c025922020-09-24 14:38:44 +0000284 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000285 EXPECT_EQ(b.GenerateExpression(cast), 4u);
dan sinclair3c025922020-09-24 14:38:44 +0000286
287 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
288%1 = OpTypeVector %2 3
289%3 = OpConstant %2 2
290%4 = OpConstantComposite %1 %3 %3 %3
291)");
292}
293
Ben Clayton0ae939b2020-11-17 17:53:38 +0000294TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_F32_Vec2) {
295 auto* cast = vec3<f32>(2.0f, vec2<f32>(2.0f, 2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000296
Ben Clayton0ae939b2020-11-17 17:53:38 +0000297 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000298
dan sinclair3c025922020-09-24 14:38:44 +0000299 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000300 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000301
302 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
303%1 = OpTypeVector %2 3
304%3 = OpConstant %2 2
305%4 = OpTypeVector %2 2
306%5 = OpConstantComposite %4 %3 %3
307)");
308 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
309 R"(%6 = OpCompositeExtract %2 %5 0
310%7 = OpCompositeExtract %2 %5 1
311%8 = OpCompositeConstruct %1 %3 %6 %7
312)");
313}
314
Ben Clayton0ae939b2020-11-17 17:53:38 +0000315TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_Vec2_F32) {
316 auto* cast = vec3<f32>(vec2<f32>(2.0f, 2.0f), 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000317
Ben Clayton0ae939b2020-11-17 17:53:38 +0000318 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000319
dan sinclair3c025922020-09-24 14:38:44 +0000320 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000321 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000322
323 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
324%1 = OpTypeVector %2 3
325%3 = OpTypeVector %2 2
326%4 = OpConstant %2 2
327%5 = OpConstantComposite %3 %4 %4
328)");
329 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
330 R"(%6 = OpCompositeExtract %2 %5 0
331%7 = OpCompositeExtract %2 %5 1
332%8 = OpCompositeConstruct %1 %6 %7 %4
333)");
334}
335
David Neto6cd6f742020-11-23 16:34:35 +0000336TEST_F(SpvBuilderConstructorTest, Type_Vec3_With_Vec3) {
337 auto* value = vec3<f32>(2.0f, 2.0f, 2.0f);
338 auto* cast = vec3<f32>(value);
339
340 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
341
342 b.push_function(Function{});
343 EXPECT_EQ(b.GenerateExpression(cast), 5u);
344
345 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
346%2 = OpTypeVector %3 3
347%4 = OpConstant %3 2
348%5 = OpConstantComposite %2 %4 %4 %4
349)");
350 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
351}
352
Ben Clayton0ae939b2020-11-17 17:53:38 +0000353TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_F32_F32_F32) {
354 auto* cast = vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000355
Ben Clayton0ae939b2020-11-17 17:53:38 +0000356 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000357
dan sinclair3c025922020-09-24 14:38:44 +0000358 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000359 EXPECT_EQ(b.GenerateExpression(cast), 4u);
dan sinclair3c025922020-09-24 14:38:44 +0000360
361 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
362%1 = OpTypeVector %2 4
363%3 = OpConstant %2 2
364%4 = OpConstantComposite %1 %3 %3 %3 %3
365)");
366}
367
Ben Clayton0ae939b2020-11-17 17:53:38 +0000368TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_F32_Vec2) {
369 auto* cast = vec4<f32>(2.0f, 2.0f, vec2<f32>(2.0f, 2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000370
Ben Clayton0ae939b2020-11-17 17:53:38 +0000371 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000372
dan sinclair3c025922020-09-24 14:38:44 +0000373 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000374 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000375
376 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
377%1 = OpTypeVector %2 4
378%3 = OpConstant %2 2
379%4 = OpTypeVector %2 2
380%5 = OpConstantComposite %4 %3 %3
381)");
382 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
383 R"(%6 = OpCompositeExtract %2 %5 0
384%7 = OpCompositeExtract %2 %5 1
385%8 = OpCompositeConstruct %1 %3 %3 %6 %7
386)");
387}
388
Ben Clayton0ae939b2020-11-17 17:53:38 +0000389TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_Vec2_F32) {
390 auto* cast = vec4<f32>(2.0f, vec2<f32>(2.0f, 2.0f), 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000391
Ben Clayton0ae939b2020-11-17 17:53:38 +0000392 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000393
dan sinclair3c025922020-09-24 14:38:44 +0000394 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000395 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000396
397 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
398%1 = OpTypeVector %2 4
399%3 = OpConstant %2 2
400%4 = OpTypeVector %2 2
401%5 = OpConstantComposite %4 %3 %3
402)");
403 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
404 R"(%6 = OpCompositeExtract %2 %5 0
405%7 = OpCompositeExtract %2 %5 1
406%8 = OpCompositeConstruct %1 %3 %6 %7 %3
407)");
408}
409
Ben Clayton0ae939b2020-11-17 17:53:38 +0000410TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec2_F32_F32) {
411 auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), 2.0f, 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000412
Ben Clayton0ae939b2020-11-17 17:53:38 +0000413 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000414
dan sinclair3c025922020-09-24 14:38:44 +0000415 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000416 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair3c025922020-09-24 14:38:44 +0000417
418 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
419%1 = OpTypeVector %2 4
420%3 = OpTypeVector %2 2
421%4 = OpConstant %2 2
422%5 = OpConstantComposite %3 %4 %4
423)");
424 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
425 R"(%6 = OpCompositeExtract %2 %5 0
426%7 = OpCompositeExtract %2 %5 1
427%8 = OpCompositeConstruct %1 %6 %7 %4 %4
428)");
429}
430
Ben Clayton0ae939b2020-11-17 17:53:38 +0000431TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec2_Vec2) {
432 auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000433
Ben Clayton0ae939b2020-11-17 17:53:38 +0000434 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000435
dan sinclair3c025922020-09-24 14:38:44 +0000436 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000437 EXPECT_EQ(b.GenerateExpression(cast), 10u);
dan sinclair3c025922020-09-24 14:38:44 +0000438
439 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
440%1 = OpTypeVector %2 4
441%3 = OpTypeVector %2 2
442%4 = OpConstant %2 2
443%5 = OpConstantComposite %3 %4 %4
444)");
445 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
446 R"(%6 = OpCompositeExtract %2 %5 0
447%7 = OpCompositeExtract %2 %5 1
448%8 = OpCompositeExtract %2 %5 0
449%9 = OpCompositeExtract %2 %5 1
450%10 = OpCompositeConstruct %1 %6 %7 %8 %9
451)");
452}
453
Ben Clayton0ae939b2020-11-17 17:53:38 +0000454TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_F32_Vec3) {
455 auto* cast = vec4<f32>(2.0f, vec3<f32>(2.0f, 2.0f, 2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000456
Ben Clayton0ae939b2020-11-17 17:53:38 +0000457 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000458
dan sinclair3c025922020-09-24 14:38:44 +0000459 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000460 EXPECT_EQ(b.GenerateExpression(cast), 9u);
dan sinclair3c025922020-09-24 14:38:44 +0000461
462 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
463%1 = OpTypeVector %2 4
464%3 = OpConstant %2 2
465%4 = OpTypeVector %2 3
466%5 = OpConstantComposite %4 %3 %3 %3
467)");
468 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
469 R"(%6 = OpCompositeExtract %2 %5 0
470%7 = OpCompositeExtract %2 %5 1
471%8 = OpCompositeExtract %2 %5 2
472%9 = OpCompositeConstruct %1 %3 %6 %7 %8
473)");
474}
475
Ben Clayton0ae939b2020-11-17 17:53:38 +0000476TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec3_F32) {
477 auto* cast = vec4<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000478
Ben Clayton0ae939b2020-11-17 17:53:38 +0000479 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000480
dan sinclair3c025922020-09-24 14:38:44 +0000481 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000482 EXPECT_EQ(b.GenerateExpression(cast), 9u);
dan sinclair3c025922020-09-24 14:38:44 +0000483
484 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
485%1 = OpTypeVector %2 4
486%3 = OpTypeVector %2 3
487%4 = OpConstant %2 2
488%5 = OpConstantComposite %3 %4 %4 %4
489)");
490 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
491 R"(%6 = OpCompositeExtract %2 %5 0
492%7 = OpCompositeExtract %2 %5 1
493%8 = OpCompositeExtract %2 %5 2
494%9 = OpCompositeConstruct %1 %6 %7 %8 %4
495)");
496}
497
David Neto6cd6f742020-11-23 16:34:35 +0000498TEST_F(SpvBuilderConstructorTest, Type_Vec4_With_Vec4) {
499 auto* value = vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f);
500 auto* cast = vec4<f32>(value);
501
502 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
503
504 b.push_function(Function{});
505 EXPECT_EQ(b.GenerateExpression(cast), 5u);
506
507 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
508%2 = OpTypeVector %3 4
509%4 = OpConstant %3 2
510%5 = OpConstantComposite %2 %4 %4 %4 %4
511)");
512 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()), R"()");
513}
514
515TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec2_With_Vec2) {
516 auto* cast = vec2<f32>(vec2<f32>(2.0f, 2.0f));
517
518 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
519
520 b.push_function(Function{});
521 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 5u);
522
523 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
524%2 = OpTypeVector %3 2
525%4 = OpConstant %3 2
526%5 = OpConstantComposite %2 %4 %4
527)");
528}
529
530TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec3_With_Vec3) {
531 auto* cast = vec3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f));
532
533 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
534
535 b.push_function(Function{});
536 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 5u);
537
538 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
539%2 = OpTypeVector %3 3
540%4 = OpConstant %3 2
541%5 = OpConstantComposite %2 %4 %4 %4
542)");
543}
544
545TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec4) {
546 auto* cast = vec4<f32>(vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f));
547
548 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
549
550 b.push_function(Function{});
551 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 5u);
552
553 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
554%2 = OpTypeVector %3 4
555%4 = OpConstant %3 2
556%5 = OpConstantComposite %2 %4 %4 %4 %4
557)");
558}
559
Ben Clayton0ae939b2020-11-17 17:53:38 +0000560TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec3_With_F32_Vec2) {
561 auto* cast = vec3<f32>(2.0f, vec2<f32>(2.0f, 2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000562
Ben Clayton0ae939b2020-11-17 17:53:38 +0000563 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000564
dan sinclair3c025922020-09-24 14:38:44 +0000565 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000566 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000567
568 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
569%1 = OpTypeVector %2 3
570%3 = OpConstant %2 2
571%4 = OpTypeVector %2 2
572%5 = OpConstantComposite %4 %3 %3
573%7 = OpTypeInt 32 0
574%8 = OpConstant %7 0
575%6 = OpSpecConstantOp %2 CompositeExtract %5 8
576%10 = OpConstant %7 1
577%9 = OpSpecConstantOp %2 CompositeExtract %5 10
578%11 = OpSpecConstantComposite %1 %3 %6 %9
579)");
580}
581
Ben Clayton0ae939b2020-11-17 17:53:38 +0000582TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec3_With_Vec2_F32) {
583 auto* cast = vec3<f32>(vec2<f32>(2.0f, 2.0f), 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000584
Ben Clayton0ae939b2020-11-17 17:53:38 +0000585 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000586
dan sinclair3c025922020-09-24 14:38:44 +0000587 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000588 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000589
590 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
591%1 = OpTypeVector %2 3
592%3 = OpTypeVector %2 2
593%4 = OpConstant %2 2
594%5 = OpConstantComposite %3 %4 %4
595%7 = OpTypeInt 32 0
596%8 = OpConstant %7 0
597%6 = OpSpecConstantOp %2 CompositeExtract %5 8
598%10 = OpConstant %7 1
599%9 = OpSpecConstantOp %2 CompositeExtract %5 10
600%11 = OpSpecConstantComposite %1 %6 %9 %4
601)");
602}
603
Ben Clayton0ae939b2020-11-17 17:53:38 +0000604TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_F32_F32_Vec2) {
605 auto* cast = vec4<f32>(2.0f, 2.0f, vec2<f32>(2.0f, 2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000606
Ben Clayton0ae939b2020-11-17 17:53:38 +0000607 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000608
dan sinclair3c025922020-09-24 14:38:44 +0000609 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000610 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000611
612 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
613%1 = OpTypeVector %2 4
614%3 = OpConstant %2 2
615%4 = OpTypeVector %2 2
616%5 = OpConstantComposite %4 %3 %3
617%7 = OpTypeInt 32 0
618%8 = OpConstant %7 0
619%6 = OpSpecConstantOp %2 CompositeExtract %5 8
620%10 = OpConstant %7 1
621%9 = OpSpecConstantOp %2 CompositeExtract %5 10
622%11 = OpSpecConstantComposite %1 %3 %3 %6 %9
623)");
624}
625
Ben Clayton0ae939b2020-11-17 17:53:38 +0000626TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_F32_Vec2_F32) {
627 auto* cast = vec4<f32>(2.0f, vec2<f32>(2.0f, 2.0f), 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000628
Ben Clayton0ae939b2020-11-17 17:53:38 +0000629 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000630
dan sinclair3c025922020-09-24 14:38:44 +0000631 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000632 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000633
634 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
635%1 = OpTypeVector %2 4
636%3 = OpConstant %2 2
637%4 = OpTypeVector %2 2
638%5 = OpConstantComposite %4 %3 %3
639%7 = OpTypeInt 32 0
640%8 = OpConstant %7 0
641%6 = OpSpecConstantOp %2 CompositeExtract %5 8
642%10 = OpConstant %7 1
643%9 = OpSpecConstantOp %2 CompositeExtract %5 10
644%11 = OpSpecConstantComposite %1 %3 %6 %9 %3
645)");
646}
647
Ben Clayton0ae939b2020-11-17 17:53:38 +0000648TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec2_F32_F32) {
649 auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), 2.0f, 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000650
Ben Clayton0ae939b2020-11-17 17:53:38 +0000651 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000652
dan sinclair3c025922020-09-24 14:38:44 +0000653 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000654 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 11u);
dan sinclair3c025922020-09-24 14:38:44 +0000655
656 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
657%1 = OpTypeVector %2 4
658%3 = OpTypeVector %2 2
659%4 = OpConstant %2 2
660%5 = OpConstantComposite %3 %4 %4
661%7 = OpTypeInt 32 0
662%8 = OpConstant %7 0
663%6 = OpSpecConstantOp %2 CompositeExtract %5 8
664%10 = OpConstant %7 1
665%9 = OpSpecConstantOp %2 CompositeExtract %5 10
666%11 = OpSpecConstantComposite %1 %6 %9 %4 %4
667)");
668}
669
Ben Clayton0ae939b2020-11-17 17:53:38 +0000670TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec2_Vec2) {
671 auto* cast = vec4<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000672
Ben Clayton0ae939b2020-11-17 17:53:38 +0000673 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000674
dan sinclair3c025922020-09-24 14:38:44 +0000675 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000676 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 13u);
dan sinclair3c025922020-09-24 14:38:44 +0000677
678 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
679%1 = OpTypeVector %2 4
680%3 = OpTypeVector %2 2
681%4 = OpConstant %2 2
682%5 = OpConstantComposite %3 %4 %4
683%7 = OpTypeInt 32 0
684%8 = OpConstant %7 0
685%6 = OpSpecConstantOp %2 CompositeExtract %5 8
686%10 = OpConstant %7 1
687%9 = OpSpecConstantOp %2 CompositeExtract %5 10
688%11 = OpSpecConstantOp %2 CompositeExtract %5 8
689%12 = OpSpecConstantOp %2 CompositeExtract %5 10
690%13 = OpSpecConstantComposite %1 %6 %9 %11 %12
691)");
692}
693
Ben Clayton0ae939b2020-11-17 17:53:38 +0000694TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_F32_Vec3) {
695 auto* cast = vec4<f32>(2.0f, vec3<f32>(2.0f, 2.0f, 2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000696
Ben Clayton0ae939b2020-11-17 17:53:38 +0000697 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000698
dan sinclair3c025922020-09-24 14:38:44 +0000699 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000700 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 13u);
dan sinclair3c025922020-09-24 14:38:44 +0000701
702 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
703%1 = OpTypeVector %2 4
704%3 = OpConstant %2 2
705%4 = OpTypeVector %2 3
706%5 = OpConstantComposite %4 %3 %3 %3
707%7 = OpTypeInt 32 0
708%8 = OpConstant %7 0
709%6 = OpSpecConstantOp %2 CompositeExtract %5 8
710%10 = OpConstant %7 1
711%9 = OpSpecConstantOp %2 CompositeExtract %5 10
712%12 = OpConstant %7 2
713%11 = OpSpecConstantOp %2 CompositeExtract %5 12
714%13 = OpSpecConstantComposite %1 %3 %6 %9 %11
715)");
716}
717
Ben Clayton0ae939b2020-11-17 17:53:38 +0000718TEST_F(SpvBuilderConstructorTest, Type_ModuleScope_Vec4_With_Vec3_F32) {
719 auto* cast = vec4<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), 2.0f);
dan sinclair3c025922020-09-24 14:38:44 +0000720
Ben Clayton0ae939b2020-11-17 17:53:38 +0000721 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000722
dan sinclair3c025922020-09-24 14:38:44 +0000723 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000724 EXPECT_EQ(b.GenerateConstructorExpression(nullptr, cast, true), 13u);
dan sinclair3c025922020-09-24 14:38:44 +0000725
726 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
727%1 = OpTypeVector %2 4
728%3 = OpTypeVector %2 3
729%4 = OpConstant %2 2
730%5 = OpConstantComposite %3 %4 %4 %4
731%7 = OpTypeInt 32 0
732%8 = OpConstant %7 0
733%6 = OpSpecConstantOp %2 CompositeExtract %5 8
734%10 = OpConstant %7 1
735%9 = OpSpecConstantOp %2 CompositeExtract %5 10
736%12 = OpConstant %7 2
737%11 = OpSpecConstantOp %2 CompositeExtract %5 12
738%13 = OpSpecConstantComposite %1 %6 %9 %11 %4
739)");
740}
741
Ben Clayton0ae939b2020-11-17 17:53:38 +0000742TEST_F(SpvBuilderConstructorTest, Type_Mat2x2_With_Vec2_Vec2) {
743 auto* cast = mat2x2<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f));
dan sinclaird6e063d2020-09-24 16:38:35 +0000744
Ben Clayton0ae939b2020-11-17 17:53:38 +0000745 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclaird6e063d2020-09-24 16:38:35 +0000746
dan sinclaird6e063d2020-09-24 16:38:35 +0000747 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000748 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000749
750 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
751%2 = OpTypeVector %3 2
752%1 = OpTypeMatrix %2 2
753%4 = OpConstant %3 2
754%5 = OpConstantComposite %2 %4 %4
755%6 = OpConstantComposite %1 %5 %5
756)");
dan sinclair3c025922020-09-24 14:38:44 +0000757}
758
Ben Clayton0ae939b2020-11-17 17:53:38 +0000759TEST_F(SpvBuilderConstructorTest, Type_Mat3x2_With_Vec2_Vec2_Vec2) {
760 auto* cast = mat3x2<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f),
761 vec2<f32>(2.0f, 2.0f));
dan sinclaird6e063d2020-09-24 16:38:35 +0000762
Ben Clayton0ae939b2020-11-17 17:53:38 +0000763 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclaird6e063d2020-09-24 16:38:35 +0000764
dan sinclaird6e063d2020-09-24 16:38:35 +0000765 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000766 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000767
768 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
769%2 = OpTypeVector %3 2
770%1 = OpTypeMatrix %2 3
771%4 = OpConstant %3 2
772%5 = OpConstantComposite %2 %4 %4
773%6 = OpConstantComposite %1 %5 %5 %5
774)");
dan sinclair3c025922020-09-24 14:38:44 +0000775}
776
Ben Clayton0ae939b2020-11-17 17:53:38 +0000777TEST_F(SpvBuilderConstructorTest, Type_Mat4x2_With_Vec2_Vec2_Vec2_Vec2) {
778 auto* cast = mat4x2<f32>(vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f),
779 vec2<f32>(2.0f, 2.0f), vec2<f32>(2.0f, 2.0f));
dan sinclaird6e063d2020-09-24 16:38:35 +0000780
Ben Clayton0ae939b2020-11-17 17:53:38 +0000781 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclaird6e063d2020-09-24 16:38:35 +0000782
dan sinclaird6e063d2020-09-24 16:38:35 +0000783 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000784 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000785
786 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
787%2 = OpTypeVector %3 2
788%1 = OpTypeMatrix %2 4
789%4 = OpConstant %3 2
790%5 = OpConstantComposite %2 %4 %4
791%6 = OpConstantComposite %1 %5 %5 %5 %5
792)");
dan sinclair3c025922020-09-24 14:38:44 +0000793}
794
Ben Clayton0ae939b2020-11-17 17:53:38 +0000795TEST_F(SpvBuilderConstructorTest, Type_Mat2x3_With_Vec3_Vec3) {
796 auto* cast =
797 mat2x3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f));
dan sinclaird6e063d2020-09-24 16:38:35 +0000798
Ben Clayton0ae939b2020-11-17 17:53:38 +0000799 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclaird6e063d2020-09-24 16:38:35 +0000800
dan sinclaird6e063d2020-09-24 16:38:35 +0000801 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000802 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000803
804 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
805%2 = OpTypeVector %3 3
806%1 = OpTypeMatrix %2 2
807%4 = OpConstant %3 2
808%5 = OpConstantComposite %2 %4 %4 %4
809%6 = OpConstantComposite %1 %5 %5
810)");
dan sinclair3c025922020-09-24 14:38:44 +0000811}
812
Ben Clayton0ae939b2020-11-17 17:53:38 +0000813TEST_F(SpvBuilderConstructorTest, Type_Mat3x3_With_Vec3_Vec3_Vec3) {
814 auto* cast =
815 mat3x3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f),
816 vec3<f32>(2.0f, 2.0f, 2.0f));
dan sinclaird6e063d2020-09-24 16:38:35 +0000817
Ben Clayton0ae939b2020-11-17 17:53:38 +0000818 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclaird6e063d2020-09-24 16:38:35 +0000819
dan sinclaird6e063d2020-09-24 16:38:35 +0000820 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000821 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000822
823 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
824%2 = OpTypeVector %3 3
825%1 = OpTypeMatrix %2 3
826%4 = OpConstant %3 2
827%5 = OpConstantComposite %2 %4 %4 %4
828%6 = OpConstantComposite %1 %5 %5 %5
829)");
dan sinclair3c025922020-09-24 14:38:44 +0000830}
831
Ben Clayton0ae939b2020-11-17 17:53:38 +0000832TEST_F(SpvBuilderConstructorTest, Type_Mat4x3_With_Vec3_Vec3_Vec3_Vec3) {
833 auto* cast =
834 mat4x3<f32>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f),
835 vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f));
dan sinclaird6e063d2020-09-24 16:38:35 +0000836
Ben Clayton0ae939b2020-11-17 17:53:38 +0000837 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclaird6e063d2020-09-24 16:38:35 +0000838
dan sinclaird6e063d2020-09-24 16:38:35 +0000839 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000840 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000841
842 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
843%2 = OpTypeVector %3 3
844%1 = OpTypeMatrix %2 4
845%4 = OpConstant %3 2
846%5 = OpConstantComposite %2 %4 %4 %4
847%6 = OpConstantComposite %1 %5 %5 %5 %5
848)");
dan sinclair3c025922020-09-24 14:38:44 +0000849}
850
Ben Clayton0ae939b2020-11-17 17:53:38 +0000851TEST_F(SpvBuilderConstructorTest, Type_Mat2x4_With_Vec4_Vec4) {
852 auto* cast = mat2x4<f32>(vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f),
853 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f));
dan sinclaird6e063d2020-09-24 16:38:35 +0000854
Ben Clayton0ae939b2020-11-17 17:53:38 +0000855 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclaird6e063d2020-09-24 16:38:35 +0000856
dan sinclaird6e063d2020-09-24 16:38:35 +0000857 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000858 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000859
860 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
861%2 = OpTypeVector %3 4
862%1 = OpTypeMatrix %2 2
863%4 = OpConstant %3 2
864%5 = OpConstantComposite %2 %4 %4 %4 %4
865%6 = OpConstantComposite %1 %5 %5
866)");
dan sinclair3c025922020-09-24 14:38:44 +0000867}
868
Ben Clayton0ae939b2020-11-17 17:53:38 +0000869TEST_F(SpvBuilderConstructorTest, Type_Mat3x4_With_Vec4_Vec4_Vec4) {
870 auto* cast = mat3x4<f32>(vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f),
871 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f),
872 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f));
dan sinclaird6e063d2020-09-24 16:38:35 +0000873
Ben Clayton0ae939b2020-11-17 17:53:38 +0000874 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclaird6e063d2020-09-24 16:38:35 +0000875
dan sinclaird6e063d2020-09-24 16:38:35 +0000876 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000877 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclaird6e063d2020-09-24 16:38:35 +0000878
879 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
880%2 = OpTypeVector %3 4
881%1 = OpTypeMatrix %2 3
882%4 = OpConstant %3 2
883%5 = OpConstantComposite %2 %4 %4 %4 %4
884%6 = OpConstantComposite %1 %5 %5 %5
885)");
dan sinclair3c025922020-09-24 14:38:44 +0000886}
887
Ben Clayton0ae939b2020-11-17 17:53:38 +0000888TEST_F(SpvBuilderConstructorTest, Type_Mat4x4_With_Vec4_Vec4_Vec4_Vec4) {
889 auto* cast = mat4x4<f32>(
890 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f),
891 vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f), vec4<f32>(2.0f, 2.0f, 2.0f, 2.0f));
dan sinclair3c025922020-09-24 14:38:44 +0000892
Ben Clayton0ae939b2020-11-17 17:53:38 +0000893 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000894
dan sinclaird6e063d2020-09-24 16:38:35 +0000895 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000896 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclair3c025922020-09-24 14:38:44 +0000897
dan sinclaird6e063d2020-09-24 16:38:35 +0000898 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
899%2 = OpTypeVector %3 4
900%1 = OpTypeMatrix %2 4
901%4 = OpConstant %3 2
902%5 = OpConstantComposite %2 %4 %4 %4 %4
903%6 = OpConstantComposite %1 %5 %5 %5 %5
904)");
dan sinclair3c025922020-09-24 14:38:44 +0000905}
906
Ben Clayton0ae939b2020-11-17 17:53:38 +0000907TEST_F(SpvBuilderConstructorTest, Type_Array_5_F32) {
908 auto* cast = array<f32, 5>(2.0f, 2.0f, 2.0f, 2.0f, 2.0f);
dan sinclair8a220a62020-09-24 16:48:35 +0000909
Ben Clayton0ae939b2020-11-17 17:53:38 +0000910 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair8a220a62020-09-24 16:48:35 +0000911
dan sinclair8a220a62020-09-24 16:48:35 +0000912 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000913 EXPECT_EQ(b.GenerateExpression(cast), 6u);
dan sinclair8a220a62020-09-24 16:48:35 +0000914
915 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
916%3 = OpTypeInt 32 0
917%4 = OpConstant %3 5
918%1 = OpTypeArray %2 %4
919%5 = OpConstant %2 2
920%6 = OpConstantComposite %1 %5 %5 %5 %5 %5
921)");
dan sinclair3c025922020-09-24 14:38:44 +0000922}
923
Ben Clayton0ae939b2020-11-17 17:53:38 +0000924TEST_F(SpvBuilderConstructorTest, Type_Array_2_Vec3) {
925 auto* cast =
926 array<f32, 2>(vec3<f32>(2.0f, 2.0f, 2.0f), vec3<f32>(2.0f, 2.0f, 2.0f));
dan sinclair8a220a62020-09-24 16:48:35 +0000927
Ben Clayton0ae939b2020-11-17 17:53:38 +0000928 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair8a220a62020-09-24 16:48:35 +0000929
dan sinclair8a220a62020-09-24 16:48:35 +0000930 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +0000931 EXPECT_EQ(b.GenerateExpression(cast), 8u);
dan sinclair8a220a62020-09-24 16:48:35 +0000932
Ben Clayton0ae939b2020-11-17 17:53:38 +0000933 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
934%3 = OpTypeInt 32 0
935%4 = OpConstant %3 2
936%1 = OpTypeArray %2 %4
937%5 = OpTypeVector %2 3
938%6 = OpConstant %2 2
939%7 = OpConstantComposite %5 %6 %6 %6
dan sinclair8a220a62020-09-24 16:48:35 +0000940%8 = OpConstantComposite %1 %7 %7
941)");
dan sinclair3c025922020-09-24 14:38:44 +0000942}
943
Ben Clayton0ae939b2020-11-17 17:53:38 +0000944TEST_F(SpvBuilderConstructorTest, Type_Struct) {
dan sinclair571bce62020-09-24 18:18:05 +0000945 ast::StructMemberDecorationList decos;
Ben Clayton0ae939b2020-11-17 17:53:38 +0000946 auto* s = create<ast::Struct>(ast::StructMemberList{
947 create<ast::StructMember>("a", ty.f32, decos),
948 create<ast::StructMember>("b", ty.vec3<f32>(), decos),
949 });
Ben Clayton4bfe4612020-11-16 16:41:47 +0000950 ast::type::StructType s_type("my_struct", s);
dan sinclair571bce62020-09-24 18:18:05 +0000951
Ben Clayton0ae939b2020-11-17 17:53:38 +0000952 auto* t = Construct(&s_type, 2.0f, vec3<f32>(2.0f, 2.0f, 2.0f));
dan sinclair571bce62020-09-24 18:18:05 +0000953
Ben Clayton0ae939b2020-11-17 17:53:38 +0000954 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair571bce62020-09-24 18:18:05 +0000955
dan sinclair571bce62020-09-24 18:18:05 +0000956 b.push_function(Function{});
957
Ben Clayton0ae939b2020-11-17 17:53:38 +0000958 EXPECT_EQ(b.GenerateExpression(t), 6u);
dan sinclair571bce62020-09-24 18:18:05 +0000959 ASSERT_FALSE(b.has_error()) << b.error();
960
961 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
962%3 = OpTypeVector %2 3
963%1 = OpTypeStruct %2 %3
964%4 = OpConstant %2 2
965%5 = OpConstantComposite %3 %4 %4 %4
966%6 = OpConstantComposite %1 %4 %5
967)");
dan sinclair3c025922020-09-24 14:38:44 +0000968}
969
Ben Clayton0ae939b2020-11-17 17:53:38 +0000970TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_F32) {
971 auto* t = Construct(ty.f32);
dan sinclair3c025922020-09-24 14:38:44 +0000972
Ben Clayton0ae939b2020-11-17 17:53:38 +0000973 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000974
dan sinclair3c025922020-09-24 14:38:44 +0000975 b.push_function(Function{});
976
Ben Clayton0ae939b2020-11-17 17:53:38 +0000977 EXPECT_EQ(b.GenerateExpression(t), 2u);
dan sinclair3c025922020-09-24 14:38:44 +0000978 ASSERT_FALSE(b.has_error()) << b.error();
979
980 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeFloat 32
981%2 = OpConstantNull %1
982)");
983}
984
Ben Clayton0ae939b2020-11-17 17:53:38 +0000985TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_I32) {
986 auto* t = Construct(ty.i32);
dan sinclair3c025922020-09-24 14:38:44 +0000987
Ben Clayton0ae939b2020-11-17 17:53:38 +0000988 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +0000989
dan sinclair3c025922020-09-24 14:38:44 +0000990 b.push_function(Function{});
991
Ben Clayton0ae939b2020-11-17 17:53:38 +0000992 EXPECT_EQ(b.GenerateExpression(t), 2u);
dan sinclair3c025922020-09-24 14:38:44 +0000993 ASSERT_FALSE(b.has_error()) << b.error();
994
995 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 1
996%2 = OpConstantNull %1
997)");
998}
999
Ben Clayton0ae939b2020-11-17 17:53:38 +00001000TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_U32) {
1001 auto* t = Construct(ty.u32);
dan sinclair3c025922020-09-24 14:38:44 +00001002
Ben Clayton0ae939b2020-11-17 17:53:38 +00001003 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001004
dan sinclair3c025922020-09-24 14:38:44 +00001005 b.push_function(Function{});
1006
Ben Clayton0ae939b2020-11-17 17:53:38 +00001007 EXPECT_EQ(b.GenerateExpression(t), 2u);
dan sinclair3c025922020-09-24 14:38:44 +00001008 ASSERT_FALSE(b.has_error()) << b.error();
1009
1010 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeInt 32 0
1011%2 = OpConstantNull %1
1012)");
1013}
1014
Ben Clayton0ae939b2020-11-17 17:53:38 +00001015TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Bool) {
1016 auto* t = Construct(ty.bool_);
dan sinclair3c025922020-09-24 14:38:44 +00001017
Ben Clayton0ae939b2020-11-17 17:53:38 +00001018 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001019
dan sinclair3c025922020-09-24 14:38:44 +00001020 b.push_function(Function{});
1021
Ben Clayton0ae939b2020-11-17 17:53:38 +00001022 EXPECT_EQ(b.GenerateExpression(t), 2u);
dan sinclair3c025922020-09-24 14:38:44 +00001023 ASSERT_FALSE(b.has_error()) << b.error();
1024
1025 EXPECT_EQ(DumpInstructions(b.types()), R"(%1 = OpTypeBool
1026%2 = OpConstantNull %1
1027)");
1028}
1029
Ben Clayton0ae939b2020-11-17 17:53:38 +00001030TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Vector) {
1031 auto* t = vec2<i32>();
dan sinclair3c025922020-09-24 14:38:44 +00001032
Ben Clayton0ae939b2020-11-17 17:53:38 +00001033 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001034
dan sinclair3c025922020-09-24 14:38:44 +00001035 b.push_function(Function{});
1036
Ben Clayton0ae939b2020-11-17 17:53:38 +00001037 EXPECT_EQ(b.GenerateExpression(t), 3u);
dan sinclair3c025922020-09-24 14:38:44 +00001038 ASSERT_FALSE(b.has_error()) << b.error();
1039
1040 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
1041%1 = OpTypeVector %2 2
1042%3 = OpConstantNull %1
1043)");
1044}
1045
Ben Clayton0ae939b2020-11-17 17:53:38 +00001046TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Matrix) {
1047 auto* t = mat4x2<f32>();
dan sinclair3c025922020-09-24 14:38:44 +00001048
Ben Clayton0ae939b2020-11-17 17:53:38 +00001049 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001050
dan sinclair3c025922020-09-24 14:38:44 +00001051 b.push_function(Function{});
1052
Ben Clayton0ae939b2020-11-17 17:53:38 +00001053 EXPECT_EQ(b.GenerateExpression(t), 4u);
dan sinclair3c025922020-09-24 14:38:44 +00001054 ASSERT_FALSE(b.has_error()) << b.error();
1055
1056 EXPECT_EQ(DumpInstructions(b.types()), R"(%3 = OpTypeFloat 32
1057%2 = OpTypeVector %3 2
1058%1 = OpTypeMatrix %2 4
1059%4 = OpConstantNull %1
1060)");
1061}
1062
Ben Clayton0ae939b2020-11-17 17:53:38 +00001063TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Array) {
1064 auto* t = array<i32, 2>();
dan sinclair3c025922020-09-24 14:38:44 +00001065
Ben Clayton0ae939b2020-11-17 17:53:38 +00001066 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001067
dan sinclair3c025922020-09-24 14:38:44 +00001068 b.push_function(Function{});
1069
Ben Clayton0ae939b2020-11-17 17:53:38 +00001070 EXPECT_EQ(b.GenerateExpression(t), 5u);
dan sinclair3c025922020-09-24 14:38:44 +00001071 ASSERT_FALSE(b.has_error()) << b.error();
1072
1073 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
1074%3 = OpTypeInt 32 0
1075%4 = OpConstant %3 2
1076%1 = OpTypeArray %2 %4
1077%5 = OpConstantNull %1
1078)");
1079}
1080
Ben Clayton0ae939b2020-11-17 17:53:38 +00001081TEST_F(SpvBuilderConstructorTest, Type_ZeroInit_Struct) {
dan sinclair3c025922020-09-24 14:38:44 +00001082 ast::StructMemberDecorationList decos;
Ben Clayton0ae939b2020-11-17 17:53:38 +00001083 auto* s = create<ast::Struct>(ast::StructMemberList{
1084 create<ast::StructMember>("a", ty.f32, decos),
1085 });
Ben Clayton4bfe4612020-11-16 16:41:47 +00001086 ast::type::StructType s_type("my_struct", s);
dan sinclair3c025922020-09-24 14:38:44 +00001087
Ben Clayton0ae939b2020-11-17 17:53:38 +00001088 auto* t = Construct(&s_type);
dan sinclair3c025922020-09-24 14:38:44 +00001089
Ben Clayton0ae939b2020-11-17 17:53:38 +00001090 EXPECT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001091
dan sinclair3c025922020-09-24 14:38:44 +00001092 b.push_function(Function{});
1093
Ben Clayton0ae939b2020-11-17 17:53:38 +00001094 EXPECT_EQ(b.GenerateExpression(t), 3u);
dan sinclair3c025922020-09-24 14:38:44 +00001095 ASSERT_FALSE(b.has_error()) << b.error();
1096
1097 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
1098%1 = OpTypeStruct %2
1099%3 = OpConstantNull %1
1100)");
1101}
1102
Ben Clayton0ae939b2020-11-17 17:53:38 +00001103TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_I32) {
1104 auto* cast = Construct(ty.i32, 2u);
dan sinclair3c025922020-09-24 14:38:44 +00001105
Ben Clayton0ae939b2020-11-17 17:53:38 +00001106 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001107
dan sinclair3c025922020-09-24 14:38:44 +00001108 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001109 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001110
1111 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
1112%3 = OpTypeInt 32 0
1113%4 = OpConstant %3 2
1114)");
1115 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1116 R"(%1 = OpBitcast %2 %4
1117)");
1118}
1119
Ben Clayton0ae939b2020-11-17 17:53:38 +00001120TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_U32) {
1121 auto* cast = Construct(ty.u32, 2);
dan sinclair3c025922020-09-24 14:38:44 +00001122
Ben Clayton0ae939b2020-11-17 17:53:38 +00001123 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001124
dan sinclair3c025922020-09-24 14:38:44 +00001125 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001126 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001127
1128 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
1129%3 = OpTypeInt 32 1
1130%4 = OpConstant %3 2
1131)");
1132 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1133 R"(%1 = OpBitcast %2 %4
1134)");
1135}
1136
Ben Clayton0ae939b2020-11-17 17:53:38 +00001137TEST_F(SpvBuilderConstructorTest, Type_Convert_F32_To_I32) {
1138 auto* cast = Construct(ty.i32, 2.4f);
dan sinclair3c025922020-09-24 14:38:44 +00001139
Ben Clayton0ae939b2020-11-17 17:53:38 +00001140 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001141
dan sinclair3c025922020-09-24 14:38:44 +00001142 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001143 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001144
1145 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
1146%3 = OpTypeFloat 32
1147%4 = OpConstant %3 2.4000001
1148)");
1149 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1150 R"(%1 = OpConvertFToS %2 %4
1151)");
1152}
1153
Ben Clayton0ae939b2020-11-17 17:53:38 +00001154TEST_F(SpvBuilderConstructorTest, Type_Convert_F32_To_U32) {
1155 auto* cast = Construct(ty.u32, 2.4f);
dan sinclair3c025922020-09-24 14:38:44 +00001156
Ben Clayton0ae939b2020-11-17 17:53:38 +00001157 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001158
dan sinclair3c025922020-09-24 14:38:44 +00001159 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001160 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001161
1162 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 0
1163%3 = OpTypeFloat 32
1164%4 = OpConstant %3 2.4000001
1165)");
1166 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1167 R"(%1 = OpConvertFToU %2 %4
1168)");
1169}
1170
Ben Clayton0ae939b2020-11-17 17:53:38 +00001171TEST_F(SpvBuilderConstructorTest, Type_Convert_I32_To_F32) {
1172 auto* cast = Construct(ty.f32, 2);
dan sinclair3c025922020-09-24 14:38:44 +00001173
Ben Clayton0ae939b2020-11-17 17:53:38 +00001174 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001175
dan sinclair3c025922020-09-24 14:38:44 +00001176 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001177 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001178
1179 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
1180%3 = OpTypeInt 32 1
1181%4 = OpConstant %3 2
1182)");
1183 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1184 R"(%1 = OpConvertSToF %2 %4
1185)");
1186}
1187
Ben Clayton0ae939b2020-11-17 17:53:38 +00001188TEST_F(SpvBuilderConstructorTest, Type_Convert_U32_To_F32) {
1189 auto* cast = Construct(ty.f32, 2u);
dan sinclair3c025922020-09-24 14:38:44 +00001190
Ben Clayton0ae939b2020-11-17 17:53:38 +00001191 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001192
dan sinclair3c025922020-09-24 14:38:44 +00001193 b.push_function(Function{});
Ben Clayton0ae939b2020-11-17 17:53:38 +00001194 EXPECT_EQ(b.GenerateExpression(cast), 1u);
dan sinclair3c025922020-09-24 14:38:44 +00001195
1196 EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
1197%3 = OpTypeInt 32 0
1198%4 = OpConstant %3 2
1199)");
1200 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1201 R"(%1 = OpConvertUToF %2 %4
1202)");
1203}
1204
Ben Clayton0ae939b2020-11-17 17:53:38 +00001205TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_I32) {
1206 auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<u32>());
dan sinclair3c025922020-09-24 14:38:44 +00001207
Ben Clayton0ae939b2020-11-17 17:53:38 +00001208 auto* cast = Construct(ty.vec3<i32>(), "i");
dan sinclair3c025922020-09-24 14:38:44 +00001209
Ben Clayton0ae939b2020-11-17 17:53:38 +00001210 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001211
dan sinclair3c025922020-09-24 14:38:44 +00001212 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001213 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001214 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001215
1216 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 0
1217%3 = OpTypeVector %4 3
1218%2 = OpTypePointer Private %3
1219%5 = OpConstantNull %3
1220%1 = OpVariable %2 Private %5
1221%8 = OpTypeInt 32 1
1222%7 = OpTypeVector %8 3
1223)");
1224 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1225 R"(%9 = OpLoad %3 %1
1226%6 = OpBitcast %7 %9
1227)");
1228}
1229
Ben Clayton0ae939b2020-11-17 17:53:38 +00001230TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_F32_to_I32) {
1231 auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<f32>());
dan sinclair3c025922020-09-24 14:38:44 +00001232
Ben Clayton0ae939b2020-11-17 17:53:38 +00001233 auto* cast = Construct(ty.vec3<i32>(), "i");
dan sinclair3c025922020-09-24 14:38:44 +00001234
Ben Clayton0ae939b2020-11-17 17:53:38 +00001235 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001236
dan sinclair3c025922020-09-24 14:38:44 +00001237 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001238 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001239 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001240
1241 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32
1242%3 = OpTypeVector %4 3
1243%2 = OpTypePointer Private %3
1244%5 = OpConstantNull %3
1245%1 = OpVariable %2 Private %5
1246%8 = OpTypeInt 32 1
1247%7 = OpTypeVector %8 3
1248)");
1249 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1250 R"(%9 = OpLoad %3 %1
1251%6 = OpConvertFToS %7 %9
1252)");
1253}
1254
Ben Clayton0ae939b2020-11-17 17:53:38 +00001255TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_I32_to_U32) {
1256 auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<i32>());
dan sinclair3c025922020-09-24 14:38:44 +00001257
Ben Clayton0ae939b2020-11-17 17:53:38 +00001258 auto* cast = Construct(ty.vec3<u32>(), "i");
dan sinclair3c025922020-09-24 14:38:44 +00001259
Ben Clayton0ae939b2020-11-17 17:53:38 +00001260 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001261
dan sinclair3c025922020-09-24 14:38:44 +00001262 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001263 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001264 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001265
1266 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1
1267%3 = OpTypeVector %4 3
1268%2 = OpTypePointer Private %3
1269%5 = OpConstantNull %3
1270%1 = OpVariable %2 Private %5
1271%8 = OpTypeInt 32 0
1272%7 = OpTypeVector %8 3
1273)");
1274 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1275 R"(%9 = OpLoad %3 %1
1276%6 = OpBitcast %7 %9
1277)");
1278}
1279
Ben Clayton0ae939b2020-11-17 17:53:38 +00001280TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_F32_to_U32) {
1281 auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<f32>());
dan sinclair3c025922020-09-24 14:38:44 +00001282
Ben Clayton0ae939b2020-11-17 17:53:38 +00001283 auto* cast = Construct(ty.vec3<u32>(), "i");
dan sinclair3c025922020-09-24 14:38:44 +00001284
Ben Clayton0ae939b2020-11-17 17:53:38 +00001285 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001286
dan sinclair3c025922020-09-24 14:38:44 +00001287 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001288 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001289 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001290
1291 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32
1292%3 = OpTypeVector %4 3
1293%2 = OpTypePointer Private %3
1294%5 = OpConstantNull %3
1295%1 = OpVariable %2 Private %5
1296%8 = OpTypeInt 32 0
1297%7 = OpTypeVector %8 3
1298)");
1299 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1300 R"(%9 = OpLoad %3 %1
1301%6 = OpConvertFToU %7 %9
1302)");
1303}
1304
Ben Clayton0ae939b2020-11-17 17:53:38 +00001305TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_I32_to_F32) {
1306 auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<i32>());
dan sinclair3c025922020-09-24 14:38:44 +00001307
Ben Clayton0ae939b2020-11-17 17:53:38 +00001308 auto* cast = Construct(ty.vec3<f32>(), "i");
dan sinclair3c025922020-09-24 14:38:44 +00001309
Ben Clayton0ae939b2020-11-17 17:53:38 +00001310 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001311
dan sinclair3c025922020-09-24 14:38:44 +00001312 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001313 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001314 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001315
1316 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 1
1317%3 = OpTypeVector %4 3
1318%2 = OpTypePointer Private %3
1319%5 = OpConstantNull %3
1320%1 = OpVariable %2 Private %5
1321%8 = OpTypeFloat 32
1322%7 = OpTypeVector %8 3
1323)");
1324 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1325 R"(%9 = OpLoad %3 %1
1326%6 = OpConvertSToF %7 %9
1327)");
1328}
1329
Ben Clayton0ae939b2020-11-17 17:53:38 +00001330TEST_F(SpvBuilderConstructorTest, Type_Convert_Vectors_U32_to_F32) {
1331 auto* var = Var("i", ast::StorageClass::kPrivate, ty.vec3<u32>());
dan sinclair3c025922020-09-24 14:38:44 +00001332
Ben Clayton0ae939b2020-11-17 17:53:38 +00001333 auto* cast = Construct(ty.vec3<f32>(), "i");
dan sinclair3c025922020-09-24 14:38:44 +00001334
Ben Clayton0ae939b2020-11-17 17:53:38 +00001335 ASSERT_TRUE(td.DetermineResultType(cast)) << td.error();
dan sinclair3c025922020-09-24 14:38:44 +00001336
dan sinclair3c025922020-09-24 14:38:44 +00001337 b.push_function(Function{});
Ben Claytonb053acf2020-11-16 16:31:07 +00001338 ASSERT_TRUE(b.GenerateGlobalVariable(var)) << b.error();
Ben Clayton0ae939b2020-11-17 17:53:38 +00001339 EXPECT_EQ(b.GenerateExpression(cast), 6u) << b.error();
dan sinclair3c025922020-09-24 14:38:44 +00001340
1341 EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeInt 32 0
1342%3 = OpTypeVector %4 3
1343%2 = OpTypePointer Private %3
1344%5 = OpConstantNull %3
1345%1 = OpVariable %2 Private %5
1346%8 = OpTypeFloat 32
1347%7 = OpTypeVector %8 3
1348)");
1349 EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
1350 R"(%9 = OpLoad %3 %1
1351%6 = OpConvertUToF %7 %9
1352)");
1353}
1354
Ben Clayton0ae939b2020-11-17 17:53:38 +00001355TEST_F(SpvBuilderConstructorTest,
1356 IsConstructorConst_GlobalVectorWithAllConstConstructors) {
dan sinclair435916e2020-10-14 15:14:11 +00001357 // vec3<f32>(1.0, 2.0, 3.0) -> true
Ben Clayton0ae939b2020-11-17 17:53:38 +00001358 auto* t = vec3<f32>(1.f, 2.f, 3.f);
dan sinclair435916e2020-10-14 15:14:11 +00001359
Ben Clayton0ae939b2020-11-17 17:53:38 +00001360 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001361
Ben Clayton0ae939b2020-11-17 17:53:38 +00001362 EXPECT_TRUE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001363 EXPECT_FALSE(b.has_error());
1364}
1365
Ben Clayton0ae939b2020-11-17 17:53:38 +00001366TEST_F(SpvBuilderConstructorTest, IsConstructorConst_GlobalVector_WithIdent) {
dan sinclair435916e2020-10-14 15:14:11 +00001367 // vec3<f32>(a, b, c) -> false -- ERROR
Ben Clayton0ae939b2020-11-17 17:53:38 +00001368 auto* t = vec3<f32>("a", "b", "c");
dan sinclair435916e2020-10-14 15:14:11 +00001369
Ben Clayton0ae939b2020-11-17 17:53:38 +00001370 Var("a", ast::StorageClass::kPrivate, ty.f32);
1371 Var("b", ast::StorageClass::kPrivate, ty.f32);
1372 Var("c", ast::StorageClass::kPrivate, ty.f32);
dan sinclair435916e2020-10-14 15:14:11 +00001373
Ben Clayton0ae939b2020-11-17 17:53:38 +00001374 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclairff267ca2020-10-14 18:26:31 +00001375
Ben Clayton0ae939b2020-11-17 17:53:38 +00001376 EXPECT_FALSE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001377 EXPECT_TRUE(b.has_error());
1378 EXPECT_EQ(b.error(), "constructor must be a constant expression");
1379}
1380
Ben Clayton0ae939b2020-11-17 17:53:38 +00001381TEST_F(SpvBuilderConstructorTest,
1382 IsConstructorConst_GlobalArrayWithAllConstConstructors) {
dan sinclair435916e2020-10-14 15:14:11 +00001383 // array<vec3<f32>, 2>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 2.0, 3.0))
1384 // -> true
Ben Clayton0ae939b2020-11-17 17:53:38 +00001385 auto* t = Construct(ty.array(ty.vec2<f32>(), 2), vec3<f32>(1.f, 2.f, 3.f),
1386 vec3<f32>(1.f, 2.f, 3.f));
dan sinclair435916e2020-10-14 15:14:11 +00001387
Ben Clayton0ae939b2020-11-17 17:53:38 +00001388 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001389
Ben Clayton0ae939b2020-11-17 17:53:38 +00001390 EXPECT_TRUE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001391 EXPECT_FALSE(b.has_error());
1392}
1393
Ben Clayton0ae939b2020-11-17 17:53:38 +00001394TEST_F(SpvBuilderConstructorTest,
dan sinclair435916e2020-10-14 15:14:11 +00001395 IsConstructorConst_GlobalVectorWithMatchingTypeConstructors) {
1396 // vec3<f32>(f32(1.0), f32(2.0)) -> false
dan sinclair435916e2020-10-14 15:14:11 +00001397
Ben Clayton0ae939b2020-11-17 17:53:38 +00001398 auto* t = vec2<f32>(Construct<f32>(1.f), Construct<f32>(2.f));
dan sinclair435916e2020-10-14 15:14:11 +00001399
Ben Clayton0ae939b2020-11-17 17:53:38 +00001400 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001401
Ben Clayton0ae939b2020-11-17 17:53:38 +00001402 EXPECT_FALSE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001403 EXPECT_FALSE(b.has_error());
1404}
1405
Ben Clayton0ae939b2020-11-17 17:53:38 +00001406TEST_F(SpvBuilderConstructorTest,
1407 IsConstructorConst_GlobalWithTypeCastConstructor) {
dan sinclair435916e2020-10-14 15:14:11 +00001408 // vec3<f32>(f32(1), f32(2)) -> false
dan sinclair435916e2020-10-14 15:14:11 +00001409
Ben Clayton0ae939b2020-11-17 17:53:38 +00001410 auto* t = vec2<f32>(Construct<f32>(1), Construct<f32>(2));
dan sinclair435916e2020-10-14 15:14:11 +00001411
Ben Clayton0ae939b2020-11-17 17:53:38 +00001412 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001413
Ben Clayton0ae939b2020-11-17 17:53:38 +00001414 EXPECT_FALSE(b.is_constructor_const(t, true));
dan sinclair435916e2020-10-14 15:14:11 +00001415 EXPECT_FALSE(b.has_error());
1416}
1417
Ben Clayton0ae939b2020-11-17 17:53:38 +00001418TEST_F(SpvBuilderConstructorTest,
1419 IsConstructorConst_VectorWithAllConstConstructors) {
dan sinclair435916e2020-10-14 15:14:11 +00001420 // vec3<f32>(1.0, 2.0, 3.0) -> true
dan sinclair435916e2020-10-14 15:14:11 +00001421
Ben Clayton0ae939b2020-11-17 17:53:38 +00001422 auto* t = vec3<f32>(1.f, 2.f, 3.f);
dan sinclair435916e2020-10-14 15:14:11 +00001423
Ben Clayton0ae939b2020-11-17 17:53:38 +00001424 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001425
Ben Clayton0ae939b2020-11-17 17:53:38 +00001426 EXPECT_TRUE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001427 EXPECT_FALSE(b.has_error());
1428}
1429
Ben Clayton0ae939b2020-11-17 17:53:38 +00001430TEST_F(SpvBuilderConstructorTest, IsConstructorConst_Vector_WithIdent) {
dan sinclair435916e2020-10-14 15:14:11 +00001431 // vec3<f32>(a, b, c) -> false
dan sinclair435916e2020-10-14 15:14:11 +00001432
Ben Clayton0ae939b2020-11-17 17:53:38 +00001433 auto* t = vec3<f32>("a", "b", "c");
dan sinclair435916e2020-10-14 15:14:11 +00001434
Ben Clayton0ae939b2020-11-17 17:53:38 +00001435 Var("a", ast::StorageClass::kPrivate, ty.f32);
1436 Var("b", ast::StorageClass::kPrivate, ty.f32);
1437 Var("c", ast::StorageClass::kPrivate, ty.f32);
dan sinclairff267ca2020-10-14 18:26:31 +00001438
Ben Clayton0ae939b2020-11-17 17:53:38 +00001439 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001440
Ben Clayton0ae939b2020-11-17 17:53:38 +00001441 EXPECT_FALSE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001442 EXPECT_FALSE(b.has_error());
1443}
1444
Ben Clayton0ae939b2020-11-17 17:53:38 +00001445TEST_F(SpvBuilderConstructorTest,
1446 IsConstructorConst_ArrayWithAllConstConstructors) {
dan sinclair435916e2020-10-14 15:14:11 +00001447 // array<vec3<f32>, 2>(vec3<f32>(1.0, 2.0, 3.0), vec3<f32>(1.0, 2.0, 3.0))
1448 // -> true
dan sinclair435916e2020-10-14 15:14:11 +00001449
Ben Clayton0ae939b2020-11-17 17:53:38 +00001450 auto* first = vec3<f32>(1.f, 2.f, 3.f);
1451 auto* second = vec3<f32>(1.f, 2.f, 3.f);
dan sinclair435916e2020-10-14 15:14:11 +00001452
Ben Clayton0ae939b2020-11-17 17:53:38 +00001453 auto* t = Construct(ty.array(ty.vec3<f32>(), 2), first, second);
dan sinclair435916e2020-10-14 15:14:11 +00001454
Ben Clayton0ae939b2020-11-17 17:53:38 +00001455 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001456
Ben Clayton0ae939b2020-11-17 17:53:38 +00001457 EXPECT_TRUE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001458 EXPECT_FALSE(b.has_error());
1459}
1460
Ben Clayton0ae939b2020-11-17 17:53:38 +00001461TEST_F(SpvBuilderConstructorTest,
1462 IsConstructorConst_VectorWith_TypeCastConstConstructors) {
1463 // vec2<f32>(f32(1), f32(2)) -> false
dan sinclair435916e2020-10-14 15:14:11 +00001464
Ben Clayton0ae939b2020-11-17 17:53:38 +00001465 auto* t = vec2<f32>(1, 2);
dan sinclair435916e2020-10-14 15:14:11 +00001466
Ben Clayton0ae939b2020-11-17 17:53:38 +00001467 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001468
Ben Clayton0ae939b2020-11-17 17:53:38 +00001469 EXPECT_FALSE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001470 EXPECT_FALSE(b.has_error());
1471}
1472
Ben Clayton0ae939b2020-11-17 17:53:38 +00001473TEST_F(SpvBuilderConstructorTest, IsConstructorConst_WithTypeCastConstructor) {
dan sinclair435916e2020-10-14 15:14:11 +00001474 // vec3<f32>(f32(1), f32(2)) -> false
dan sinclair435916e2020-10-14 15:14:11 +00001475
Ben Clayton0ae939b2020-11-17 17:53:38 +00001476 auto* t = vec3<f32>(1, 2);
dan sinclair435916e2020-10-14 15:14:11 +00001477
Ben Clayton0ae939b2020-11-17 17:53:38 +00001478 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001479
Ben Clayton0ae939b2020-11-17 17:53:38 +00001480 EXPECT_FALSE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001481 EXPECT_FALSE(b.has_error());
1482}
1483
Ben Clayton0ae939b2020-11-17 17:53:38 +00001484TEST_F(SpvBuilderConstructorTest, IsConstructorConst_BitCastScalars) {
1485 auto* t = vec2<u32>(1, 1);
dan sinclair435916e2020-10-14 15:14:11 +00001486
Ben Clayton0ae939b2020-11-17 17:53:38 +00001487 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001488
Ben Clayton0ae939b2020-11-17 17:53:38 +00001489 EXPECT_FALSE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001490 EXPECT_FALSE(b.has_error());
1491}
1492
Ben Clayton0ae939b2020-11-17 17:53:38 +00001493TEST_F(SpvBuilderConstructorTest, IsConstructorConst_Struct) {
dan sinclair435916e2020-10-14 15:14:11 +00001494 ast::StructMemberDecorationList decos;
Ben Clayton0ae939b2020-11-17 17:53:38 +00001495 auto* s = create<ast::Struct>(ast::StructMemberList{
1496 create<ast::StructMember>("a", ty.f32, decos),
1497 create<ast::StructMember>("b", ty.vec3<f32>(), decos),
1498 });
Ben Clayton4bfe4612020-11-16 16:41:47 +00001499 ast::type::StructType s_type("my_struct", s);
dan sinclair435916e2020-10-14 15:14:11 +00001500
Ben Clayton0ae939b2020-11-17 17:53:38 +00001501 auto* t = Construct(&s_type, 2.f, vec3<f32>(2.f, 2.f, 2.f));
dan sinclair435916e2020-10-14 15:14:11 +00001502
Ben Clayton0ae939b2020-11-17 17:53:38 +00001503 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001504
Ben Clayton0ae939b2020-11-17 17:53:38 +00001505 EXPECT_TRUE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001506 EXPECT_FALSE(b.has_error());
1507}
1508
Ben Clayton0ae939b2020-11-17 17:53:38 +00001509TEST_F(SpvBuilderConstructorTest,
1510 IsConstructorConst_Struct_WithIdentSubExpression) {
dan sinclair435916e2020-10-14 15:14:11 +00001511 ast::StructMemberDecorationList decos;
Ben Clayton0ae939b2020-11-17 17:53:38 +00001512 auto* s = create<ast::Struct>(ast::StructMemberList{
1513 create<ast::StructMember>("a", ty.f32, decos),
1514 create<ast::StructMember>("b", ty.vec3<f32>(), decos),
1515 });
dan sinclair435916e2020-10-14 15:14:11 +00001516
Ben Clayton4bfe4612020-11-16 16:41:47 +00001517 ast::type::StructType s_type("my_struct", s);
dan sinclair435916e2020-10-14 15:14:11 +00001518
Ben Clayton0ae939b2020-11-17 17:53:38 +00001519 auto* t = Construct(&s_type, 2.f, "a", 2.f);
dan sinclair435916e2020-10-14 15:14:11 +00001520
Ben Clayton0ae939b2020-11-17 17:53:38 +00001521 Var("a", ast::StorageClass::kPrivate, ty.f32);
1522 Var("b", ast::StorageClass::kPrivate, ty.f32);
dan sinclair435916e2020-10-14 15:14:11 +00001523
Ben Clayton0ae939b2020-11-17 17:53:38 +00001524 ASSERT_TRUE(td.DetermineResultType(t)) << td.error();
dan sinclair435916e2020-10-14 15:14:11 +00001525
Ben Clayton0ae939b2020-11-17 17:53:38 +00001526 EXPECT_FALSE(b.is_constructor_const(t, false));
dan sinclair435916e2020-10-14 15:14:11 +00001527 EXPECT_FALSE(b.has_error());
1528}
1529
dan sinclair5cd08fd2020-03-30 20:01:38 +00001530} // namespace
1531} // namespace spirv
1532} // namespace writer
1533} // namespace tint