1 // Copyright (c) 2020 Google Inc.
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 // Tests for OpExtension validator rules.
16
17 #include <string>
18 #include <vector>
19
20 #include "gmock/gmock.h"
21 #include "source/enum_string_mapping.h"
22 #include "source/extensions.h"
23 #include "source/spirv_target_env.h"
24 #include "test/test_fixture.h"
25 #include "test/unit_spirv.h"
26 #include "test/val/val_fixtures.h"
27
28 namespace spvtools {
29 namespace val {
30 namespace {
31
32 using ::testing::HasSubstr;
33 using ::testing::Values;
34 using ::testing::ValuesIn;
35
36 using ValidateSpvKHRLinkOnceODR = spvtest::ValidateBase<bool>;
37
TEST_F(ValidateSpvKHRLinkOnceODR,Valid)38 TEST_F(ValidateSpvKHRLinkOnceODR, Valid) {
39 const std::string str = R"(
40 OpCapability Kernel
41 OpCapability Addresses
42 OpCapability Linkage
43 OpExtension "SPV_KHR_linkonce_odr"
44 OpMemoryModel Physical32 OpenCL
45 OpDecorate %var LinkageAttributes "foobar" LinkOnceODR
46
47 %uint = OpTypeInt 32 0
48 %ptr = OpTypePointer CrossWorkgroup %uint
49 %var = OpVariable %ptr CrossWorkgroup
50
51 )";
52 CompileSuccessfully(str.c_str());
53 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
54 }
55
TEST_F(ValidateSpvKHRLinkOnceODR,RequiresExtension)56 TEST_F(ValidateSpvKHRLinkOnceODR, RequiresExtension) {
57 const std::string str = R"(
58 OpCapability Kernel
59 OpCapability Addresses
60 OpCapability Linkage
61 OpMemoryModel Physical32 OpenCL
62 OpDecorate %var LinkageAttributes "foobar" LinkOnceODR
63
64 %uint = OpTypeInt 32 0
65 %ptr = OpTypePointer CrossWorkgroup %uint
66 %var = OpVariable %ptr CrossWorkgroup
67 )";
68 CompileSuccessfully(str.c_str());
69 EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
70 EXPECT_THAT(
71 getDiagnosticString(),
72 HasSubstr("4th operand of Decorate: operand LinkOnceODR(2) requires one "
73 "of these extensions: SPV_KHR_linkonce_odr \n"
74 " OpDecorate %1 LinkageAttributes \"foobar\" LinkOnceODR\n"));
75 }
76
TEST_F(ValidateSpvKHRLinkOnceODR,RequiresLinkageCapability)77 TEST_F(ValidateSpvKHRLinkOnceODR, RequiresLinkageCapability) {
78 const std::string str = R"(
79 OpCapability Kernel
80 OpCapability Addresses
81 OpExtension "SPV_KHR_linkonce_odr"
82 OpMemoryModel Physical32 OpenCL
83 OpDecorate %var LinkageAttributes "foobar" LinkOnceODR
84
85 %uint = OpTypeInt 32 0
86 %ptr = OpTypePointer CrossWorkgroup %uint
87 %var = OpVariable %ptr CrossWorkgroup
88 )";
89 CompileSuccessfully(str.c_str());
90 EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
91 EXPECT_THAT(
92 getDiagnosticString(),
93 HasSubstr(
94 "Operand 2 of Decorate requires one of these capabilities: Linkage \n"
95 " OpDecorate %1 LinkageAttributes \"foobar\" LinkOnceODR"));
96 }
97
98 } // namespace
99 } // namespace val
100 } // namespace spvtools
101