blob: 908d36956a7769ab1c604847e3496c9bd2de47c5 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +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 "gmock/gmock.h"
16#include "src/tint/reader/spirv/parser_impl_test_helper.h"
17#include "src/tint/reader/spirv/spirv_tools_helpers_test.h"
18
dan sinclair258cbaf2022-04-07 19:01:25 +000019namespace tint::reader::spirv {
Ryan Harrisondbc13af2022-02-21 15:19:07 +000020namespace {
21
22using ::testing::HasSubstr;
23
24TEST_F(SpvParserTest, Impl_Uint32VecEmpty) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000025 std::vector<uint32_t> data;
26 auto p = parser(data);
27 EXPECT_FALSE(p->Parse());
28 // TODO(dneto): What message?
Ryan Harrisondbc13af2022-02-21 15:19:07 +000029}
30
31TEST_F(SpvParserTest, Impl_InvalidModuleFails) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000032 auto invalid_spv = test::Assemble("%ty = OpTypeInt 3 0");
33 auto p = parser(invalid_spv);
34 EXPECT_FALSE(p->Parse());
35 EXPECT_THAT(p->error(), HasSubstr("TypeInt cannot appear before the memory model instruction"));
36 EXPECT_THAT(p->error(), HasSubstr("OpTypeInt 3 0"));
Ryan Harrisondbc13af2022-02-21 15:19:07 +000037}
38
39TEST_F(SpvParserTest, Impl_GenericVulkanShader_SimpleMemoryModel) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000040 auto spv = test::Assemble(R"(
Ryan Harrisondbc13af2022-02-21 15:19:07 +000041 OpCapability Shader
42 OpMemoryModel Logical Simple
43 OpEntryPoint GLCompute %main "main"
44 OpExecutionMode %main LocalSize 1 1 1
45 %void = OpTypeVoid
46 %voidfn = OpTypeFunction %void
47 %main = OpFunction %void None %voidfn
48 %entry = OpLabel
49 OpReturn
50 OpFunctionEnd
51)");
dan sinclair41e4d9a2022-05-01 14:40:55 +000052 auto p = parser(spv);
53 EXPECT_TRUE(p->Parse());
54 EXPECT_TRUE(p->error().empty());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000055}
56
57TEST_F(SpvParserTest, Impl_GenericVulkanShader_GLSL450MemoryModel) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000058 auto spv = test::Assemble(R"(
Ryan Harrisondbc13af2022-02-21 15:19:07 +000059 OpCapability Shader
60 OpMemoryModel Logical GLSL450
61 OpEntryPoint GLCompute %main "main"
62 OpExecutionMode %main LocalSize 1 1 1
63 %void = OpTypeVoid
64 %voidfn = OpTypeFunction %void
65 %main = OpFunction %void None %voidfn
66 %entry = OpLabel
67 OpReturn
68 OpFunctionEnd
69)");
dan sinclair41e4d9a2022-05-01 14:40:55 +000070 auto p = parser(spv);
71 EXPECT_TRUE(p->Parse());
72 EXPECT_TRUE(p->error().empty());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000073}
74
75TEST_F(SpvParserTest, Impl_GenericVulkanShader_VulkanMemoryModel) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000076 auto spv = test::Assemble(R"(
Ryan Harrisondbc13af2022-02-21 15:19:07 +000077 OpCapability Shader
78 OpCapability VulkanMemoryModelKHR
79 OpExtension "SPV_KHR_vulkan_memory_model"
80 OpMemoryModel Logical VulkanKHR
81 OpEntryPoint GLCompute %main "main"
82 OpExecutionMode %main LocalSize 1 1 1
83 %void = OpTypeVoid
84 %voidfn = OpTypeFunction %void
85 %main = OpFunction %void None %voidfn
86 %entry = OpLabel
87 OpReturn
88 OpFunctionEnd
89)");
dan sinclair41e4d9a2022-05-01 14:40:55 +000090 auto p = parser(spv);
91 EXPECT_TRUE(p->Parse());
92 EXPECT_TRUE(p->error().empty());
Ryan Harrisondbc13af2022-02-21 15:19:07 +000093}
94
95TEST_F(SpvParserTest, Impl_OpenCLKernel_Fails) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000096 auto spv = test::Assemble(R"(
Ryan Harrisondbc13af2022-02-21 15:19:07 +000097 OpCapability Kernel
98 OpCapability Addresses
99 OpMemoryModel Physical32 OpenCL
100 OpEntryPoint Kernel %main "main"
101 %void = OpTypeVoid
102 %voidfn = OpTypeFunction %void
103 %main = OpFunction %void None %voidfn
104 %entry = OpLabel
105 OpReturn
106 OpFunctionEnd
107)");
dan sinclair41e4d9a2022-05-01 14:40:55 +0000108 auto p = parser(spv);
109 EXPECT_FALSE(p->Parse());
110 EXPECT_THAT(p->error(), HasSubstr("Capability Kernel is not allowed"));
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000111}
112
113TEST_F(SpvParserTest, Impl_Source_NoOpLine) {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000114 auto spv = test::Assemble(R"(
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000115 OpCapability Shader
116 OpMemoryModel Logical Simple
117 OpEntryPoint GLCompute %main "main"
118 OpExecutionMode %main LocalSize 1 1 1
119 %void = OpTypeVoid
120 %voidfn = OpTypeFunction %void
121 %5 = OpTypeInt 32 0
122 %60 = OpConstantNull %5
123 %main = OpFunction %void None %voidfn
124 %1 = OpLabel
125 OpReturn
126 OpFunctionEnd
127)");
dan sinclair41e4d9a2022-05-01 14:40:55 +0000128 auto p = parser(spv);
129 EXPECT_TRUE(p->Parse());
130 EXPECT_TRUE(p->error().empty());
131 // Use instruction counting.
132 auto s5 = p->GetSourceForResultIdForTest(5);
133 EXPECT_EQ(7u, s5.range.begin.line);
134 EXPECT_EQ(0u, s5.range.begin.column);
135 auto s60 = p->GetSourceForResultIdForTest(60);
136 EXPECT_EQ(8u, s60.range.begin.line);
137 EXPECT_EQ(0u, s60.range.begin.column);
138 auto s1 = p->GetSourceForResultIdForTest(1);
139 EXPECT_EQ(10u, s1.range.begin.line);
140 EXPECT_EQ(0u, s1.range.begin.column);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000141}
142
143TEST_F(SpvParserTest, Impl_Source_WithOpLine_WithOpNoLine) {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000144 auto spv = test::Assemble(R"(
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000145 OpCapability Shader
146 OpMemoryModel Logical Simple
147 OpEntryPoint GLCompute %main "main"
148 OpExecutionMode %main LocalSize 1 1 1
149 %15 = OpString "myfile"
150 %void = OpTypeVoid
151 %voidfn = OpTypeFunction %void
152 OpLine %15 42 53
153 %5 = OpTypeInt 32 0
154 %60 = OpConstantNull %5
155 OpNoLine
156 %main = OpFunction %void None %voidfn
157 %1 = OpLabel
158 OpReturn
159 OpFunctionEnd
160)");
dan sinclair41e4d9a2022-05-01 14:40:55 +0000161 auto p = parser(spv);
162 EXPECT_TRUE(p->Parse());
163 EXPECT_TRUE(p->error().empty());
164 // Use the information from the OpLine that is still in scope.
165 auto s5 = p->GetSourceForResultIdForTest(5);
166 EXPECT_EQ(42u, s5.range.begin.line);
167 EXPECT_EQ(53u, s5.range.begin.column);
168 auto s60 = p->GetSourceForResultIdForTest(60);
169 EXPECT_EQ(42u, s60.range.begin.line);
170 EXPECT_EQ(53u, s60.range.begin.column);
171 // After OpNoLine, revert back to instruction counting.
172 auto s1 = p->GetSourceForResultIdForTest(1);
173 EXPECT_EQ(14u, s1.range.begin.line);
174 EXPECT_EQ(0u, s1.range.begin.column);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000175}
176
177TEST_F(SpvParserTest, Impl_Source_InvalidId) {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000178 auto spv = test::Assemble(R"(
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000179 OpCapability Shader
180 OpMemoryModel Logical Simple
181 OpEntryPoint GLCompute %main "main"
182 OpExecutionMode %main LocalSize 1 1 1
183 %15 = OpString "myfile"
184 %void = OpTypeVoid
185 %voidfn = OpTypeFunction %void
186 %main = OpFunction %void None %voidfn
187 %1 = OpLabel
188 OpReturn
189 OpFunctionEnd
190)");
dan sinclair41e4d9a2022-05-01 14:40:55 +0000191 auto p = parser(spv);
192 EXPECT_TRUE(p->Parse());
193 EXPECT_TRUE(p->error().empty());
194 auto s99 = p->GetSourceForResultIdForTest(99);
195 EXPECT_EQ(0u, s99.range.begin.line);
196 EXPECT_EQ(0u, s99.range.begin.column);
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000197}
198
199TEST_F(SpvParserTest, Impl_IsValidIdentifier) {
dan sinclair41e4d9a2022-05-01 14:40:55 +0000200 EXPECT_FALSE(ParserImpl::IsValidIdentifier("")); // empty
201 EXPECT_FALSE(ParserImpl::IsValidIdentifier("_"));
202 EXPECT_FALSE(ParserImpl::IsValidIdentifier("__"));
203 EXPECT_TRUE(ParserImpl::IsValidIdentifier("_x"));
204 EXPECT_FALSE(ParserImpl::IsValidIdentifier("9")); // leading digit, but ok later
205 EXPECT_FALSE(ParserImpl::IsValidIdentifier(" ")); // leading space
206 EXPECT_FALSE(ParserImpl::IsValidIdentifier("a ")); // trailing space
207 EXPECT_FALSE(ParserImpl::IsValidIdentifier("a 1")); // space in the middle
208 EXPECT_FALSE(ParserImpl::IsValidIdentifier(".")); // weird character
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000209
dan sinclair41e4d9a2022-05-01 14:40:55 +0000210 // a simple identifier
211 EXPECT_TRUE(ParserImpl::IsValidIdentifier("A"));
212 // each upper case letter
213 EXPECT_TRUE(ParserImpl::IsValidIdentifier("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
214 // each lower case letter
215 EXPECT_TRUE(ParserImpl::IsValidIdentifier("abcdefghijklmnopqrstuvwxyz"));
216 EXPECT_TRUE(ParserImpl::IsValidIdentifier("a0123456789")); // each digit
217 EXPECT_TRUE(ParserImpl::IsValidIdentifier("x_")); // has underscore
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000218}
219
Antonio Maioranoaff16562022-11-18 05:45:17 +0000220TEST_F(SpvParserTest, Impl_FailOnNonFiniteLiteral) {
221 auto spv = test::Assemble(R"(
222 OpCapability Shader
223 OpMemoryModel Logical GLSL450
224 OpEntryPoint Fragment %main "main" %out_var_SV_TARGET
225 OpExecutionMode %main OriginUpperLeft
226 OpSource HLSL 600
227 OpName %out_var_SV_TARGET "out.var.SV_TARGET"
228 OpName %main "main"
229 OpDecorate %out_var_SV_TARGET Location 0
230 %float = OpTypeFloat 32
231 %float_0x1p_128 = OpConstant %float -0x1p+128
232 %v4float = OpTypeVector %float 4
233%_ptr_Output_v4float = OpTypePointer Output %v4float
234 %void = OpTypeVoid
235 %9 = OpTypeFunction %void
236 %out_var_SV_TARGET = OpVariable %_ptr_Output_v4float Output
237 %main = OpFunction %void None %9
238 %10 = OpLabel
239 %12 = OpCompositeConstruct %v4float %float_0x1p_128 %float_0x1p_128 %float_0x1p_128 %float_0x1p_128
240 OpStore %out_var_SV_TARGET %12
241 OpReturn
242 OpFunctionEnd
243
244)");
245 auto p = parser(spv);
246 EXPECT_FALSE(p->Parse());
247 EXPECT_THAT(p->error(), HasSubstr("value cannot be represented as 'f32': -inf"));
248}
249
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000250} // namespace
dan sinclair258cbaf2022-04-07 19:01:25 +0000251} // namespace tint::reader::spirv