• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,UnaryOp_Negation_Integer)25 TEST_F(BuilderTest, UnaryOp_Negation_Integer) {
26   auto* expr = create<ast::UnaryOpExpression>(ast::UnaryOp::kNegation, Expr(1));
27   WrapInFunction(expr);
28 
29   spirv::Builder& b = Build();
30 
31   b.push_function(Function{});
32   EXPECT_EQ(b.GenerateUnaryOpExpression(expr), 1u) << b.error();
33   EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
34 %3 = OpConstant %2 1
35 )");
36   EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
37             R"(%1 = OpSNegate %2 %3
38 )");
39 }
40 
TEST_F(BuilderTest,UnaryOp_Negation_Float)41 TEST_F(BuilderTest, UnaryOp_Negation_Float) {
42   auto* expr =
43       create<ast::UnaryOpExpression>(ast::UnaryOp::kNegation, Expr(1.f));
44   WrapInFunction(expr);
45 
46   spirv::Builder& b = Build();
47 
48   b.push_function(Function{});
49   EXPECT_EQ(b.GenerateUnaryOpExpression(expr), 1u) << b.error();
50   EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeFloat 32
51 %3 = OpConstant %2 1
52 )");
53   EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
54             R"(%1 = OpFNegate %2 %3
55 )");
56 }
57 
TEST_F(BuilderTest,UnaryOp_Complement)58 TEST_F(BuilderTest, UnaryOp_Complement) {
59   auto* expr =
60       create<ast::UnaryOpExpression>(ast::UnaryOp::kComplement, Expr(1));
61   WrapInFunction(expr);
62 
63   spirv::Builder& b = Build();
64 
65   b.push_function(Function{});
66   EXPECT_EQ(b.GenerateUnaryOpExpression(expr), 1u) << b.error();
67   EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeInt 32 1
68 %3 = OpConstant %2 1
69 )");
70   EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
71             R"(%1 = OpNot %2 %3
72 )");
73 }
74 
TEST_F(BuilderTest,UnaryOp_Not)75 TEST_F(BuilderTest, UnaryOp_Not) {
76   auto* expr = create<ast::UnaryOpExpression>(ast::UnaryOp::kNot, Expr(false));
77   WrapInFunction(expr);
78 
79   spirv::Builder& b = Build();
80 
81   b.push_function(Function{});
82   EXPECT_EQ(b.GenerateUnaryOpExpression(expr), 1u) << b.error();
83   EXPECT_EQ(DumpInstructions(b.types()), R"(%2 = OpTypeBool
84 %3 = OpConstantFalse %2
85 )");
86   EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
87             R"(%1 = OpLogicalNot %2 %3
88 )");
89 }
90 
TEST_F(BuilderTest,UnaryOp_LoadRequired)91 TEST_F(BuilderTest, UnaryOp_LoadRequired) {
92   auto* var = Var("param", ty.vec3<f32>());
93 
94   auto* expr =
95       create<ast::UnaryOpExpression>(ast::UnaryOp::kNegation, Expr("param"));
96   WrapInFunction(var, expr);
97 
98   spirv::Builder& b = Build();
99 
100   b.push_function(Function{});
101   EXPECT_TRUE(b.GenerateFunctionVariable(var)) << b.error();
102   EXPECT_EQ(b.GenerateUnaryOpExpression(expr), 6u) << b.error();
103   ASSERT_FALSE(b.has_error()) << b.error();
104 
105   EXPECT_EQ(DumpInstructions(b.types()), R"(%4 = OpTypeFloat 32
106 %3 = OpTypeVector %4 3
107 %2 = OpTypePointer Function %3
108 %5 = OpConstantNull %3
109 )");
110   EXPECT_EQ(DumpInstructions(b.functions()[0].variables()),
111             R"(%1 = OpVariable %2 Function %5
112 )");
113   EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
114             R"(%7 = OpLoad %3 %1
115 %6 = OpFNegate %3 %7
116 )");
117 }
118 
119 }  // namespace
120 }  // namespace spirv
121 }  // namespace writer
122 }  // namespace tint
123