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