Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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 "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 sinclair | 258cbaf | 2022-04-07 19:01:25 +0000 | [diff] [blame] | 19 | namespace tint::reader::spirv { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | using ::testing::HasSubstr; |
| 23 | |
| 24 | TEST_F(SpvParserTest, Impl_Uint32VecEmpty) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 25 | std::vector<uint32_t> data; |
| 26 | auto p = parser(data); |
| 27 | EXPECT_FALSE(p->Parse()); |
| 28 | // TODO(dneto): What message? |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | TEST_F(SpvParserTest, Impl_InvalidModuleFails) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 32 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | TEST_F(SpvParserTest, Impl_GenericVulkanShader_SimpleMemoryModel) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 40 | auto spv = test::Assemble(R"( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 41 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 52 | auto p = parser(spv); |
| 53 | EXPECT_TRUE(p->Parse()); |
| 54 | EXPECT_TRUE(p->error().empty()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | TEST_F(SpvParserTest, Impl_GenericVulkanShader_GLSL450MemoryModel) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 58 | auto spv = test::Assemble(R"( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 59 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 70 | auto p = parser(spv); |
| 71 | EXPECT_TRUE(p->Parse()); |
| 72 | EXPECT_TRUE(p->error().empty()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | TEST_F(SpvParserTest, Impl_GenericVulkanShader_VulkanMemoryModel) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 76 | auto spv = test::Assemble(R"( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 77 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 90 | auto p = parser(spv); |
| 91 | EXPECT_TRUE(p->Parse()); |
| 92 | EXPECT_TRUE(p->error().empty()); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | TEST_F(SpvParserTest, Impl_OpenCLKernel_Fails) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 96 | auto spv = test::Assemble(R"( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 97 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 108 | auto p = parser(spv); |
| 109 | EXPECT_FALSE(p->Parse()); |
| 110 | EXPECT_THAT(p->error(), HasSubstr("Capability Kernel is not allowed")); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | TEST_F(SpvParserTest, Impl_Source_NoOpLine) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 114 | auto spv = test::Assemble(R"( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 115 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 128 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | TEST_F(SpvParserTest, Impl_Source_WithOpLine_WithOpNoLine) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 144 | auto spv = test::Assemble(R"( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 145 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 161 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | TEST_F(SpvParserTest, Impl_Source_InvalidId) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 178 | auto spv = test::Assemble(R"( |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 179 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 191 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | TEST_F(SpvParserTest, Impl_IsValidIdentifier) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 200 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 209 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 210 | // 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Antonio Maiorano | aff1656 | 2022-11-18 05:45:17 +0000 | [diff] [blame] | 220 | TEST_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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 250 | } // namespace |
dan sinclair | 258cbaf | 2022-04-07 19:01:25 +0000 | [diff] [blame] | 251 | } // namespace tint::reader::spirv |