| 1 |
// Copyright (c) 2019 The Khronos Group 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 |
// Validation tests for OpenCL env specific checks |
| 16 |
|
| 17 |
#include <string> |
| 18 |
|
| 19 |
#include "gmock/gmock.h" |
| 20 |
#include "test/val/val_fixtures.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace val { |
| 24 |
namespace { |
| 25 |
|
| 26 |
using testing::HasSubstr; |
| 27 |
|
| 28 |
using ValidateOpenCL = spvtest::ValidateBase<bool>; |
| 29 |
|
| 30 |
TEST_F(ValidateOpenCL, NonPhysicalAddressingModelBad) { |
| 31 |
std::string spirv = R"( |
| 32 |
OpCapability Kernel |
| 33 |
OpMemoryModel Logical OpenCL |
| 34 |
)"; |
| 35 |
|
| 36 |
CompileSuccessfully(spirv); |
| 37 |
|
| 38 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 39 |
EXPECT_THAT(getDiagnosticString(), |
| 40 |
HasSubstr("Addressing model must be Physical32 or Physical64 " |
| 41 |
"in the OpenCL environment.\n OpMemoryModel Logical " |
| 42 |
"OpenCL\n")); |
| 43 |
} |
| 44 |
|
| 45 |
TEST_F(ValidateOpenCL, NonOpenCLMemoryModelBad) { |
| 46 |
std::string spirv = R"( |
| 47 |
OpCapability Kernel |
| 48 |
OpCapability Addresses |
| 49 |
OpCapability VulkanMemoryModelKHR |
| 50 |
OpExtension "SPV_KHR_vulkan_memory_model" |
| 51 |
OpMemoryModel Physical32 VulkanKHR |
| 52 |
)"; |
| 53 |
|
| 54 |
CompileSuccessfully(spirv); |
| 55 |
|
| 56 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 57 |
EXPECT_THAT( |
| 58 |
getDiagnosticString(), |
| 59 |
HasSubstr("Memory model must be OpenCL in the OpenCL environment.")); |
| 60 |
} |
| 61 |
|
| 62 |
TEST_F(ValidateOpenCL, NonVoidSampledTypeImageBad) { |
| 63 |
std::string spirv = R"( |
| 64 |
OpCapability Addresses |
| 65 |
OpCapability Kernel |
| 66 |
OpMemoryModel Physical32 OpenCL |
| 67 |
%1 = OpTypeInt 32 0 |
| 68 |
%2 = OpTypeImage %1 2D 0 0 0 0 Unknown ReadOnly |
| 69 |
)"; |
| 70 |
|
| 71 |
CompileSuccessfully(spirv); |
| 72 |
|
| 73 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 74 |
EXPECT_THAT( |
| 75 |
getDiagnosticString(), |
| 76 |
HasSubstr("Sampled Type must be OpTypeVoid in the OpenCL environment." |
| 77 |
"\n %2 = OpTypeImage %uint 2D 0 0 0 0 Unknown ReadOnly\n")); |
| 78 |
} |
| 79 |
|
| 80 |
TEST_F(ValidateOpenCL, NonZeroMSImageBad) { |
| 81 |
std::string spirv = R"( |
| 82 |
OpCapability Addresses |
| 83 |
OpCapability Kernel |
| 84 |
OpMemoryModel Physical32 OpenCL |
| 85 |
%1 = OpTypeVoid |
| 86 |
%2 = OpTypeImage %1 2D 0 0 1 0 Unknown ReadOnly |
| 87 |
)"; |
| 88 |
|
| 89 |
CompileSuccessfully(spirv); |
| 90 |
|
| 91 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 92 |
EXPECT_THAT( |
| 93 |
getDiagnosticString(), |
| 94 |
HasSubstr("MS must be 0 in the OpenCL environement." |
| 95 |
"\n %2 = OpTypeImage %void 2D 0 0 1 0 Unknown ReadOnly\n")); |
| 96 |
} |
| 97 |
|
| 98 |
TEST_F(ValidateOpenCL, Non1D2DArrayedImageBad) { |
| 99 |
std::string spirv = R"( |
| 100 |
OpCapability Addresses |
| 101 |
OpCapability Kernel |
| 102 |
OpMemoryModel Physical32 OpenCL |
| 103 |
%1 = OpTypeVoid |
| 104 |
%2 = OpTypeImage %1 3D 0 1 0 0 Unknown ReadOnly |
| 105 |
)"; |
| 106 |
|
| 107 |
CompileSuccessfully(spirv); |
| 108 |
|
| 109 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 110 |
EXPECT_THAT( |
| 111 |
getDiagnosticString(), |
| 112 |
HasSubstr("In the OpenCL environment, Arrayed may only be set to 1 " |
| 113 |
"when Dim is either 1D or 2D." |
| 114 |
"\n %2 = OpTypeImage %void 3D 0 1 0 0 Unknown ReadOnly\n")); |
| 115 |
} |
| 116 |
|
| 117 |
TEST_F(ValidateOpenCL, NonZeroSampledImageBad) { |
| 118 |
std::string spirv = R"( |
| 119 |
OpCapability Addresses |
| 120 |
OpCapability Kernel |
| 121 |
OpMemoryModel Physical32 OpenCL |
| 122 |
%1 = OpTypeVoid |
| 123 |
%2 = OpTypeImage %1 3D 0 0 0 1 Unknown ReadOnly |
| 124 |
)"; |
| 125 |
|
| 126 |
CompileSuccessfully(spirv); |
| 127 |
|
| 128 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 129 |
EXPECT_THAT( |
| 130 |
getDiagnosticString(), |
| 131 |
HasSubstr("Sampled must be 0 in the OpenCL environment." |
| 132 |
"\n %2 = OpTypeImage %void 3D 0 0 0 1 Unknown ReadOnly\n")); |
| 133 |
} |
| 134 |
|
| 135 |
TEST_F(ValidateOpenCL, NoAccessQualifierImageBad) { |
| 136 |
std::string spirv = R"( |
| 137 |
OpCapability Addresses |
| 138 |
OpCapability Kernel |
| 139 |
OpMemoryModel Physical32 OpenCL |
| 140 |
%1 = OpTypeVoid |
| 141 |
%2 = OpTypeImage %1 3D 0 0 0 0 Unknown |
| 142 |
)"; |
| 143 |
|
| 144 |
CompileSuccessfully(spirv); |
| 145 |
|
| 146 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 147 |
EXPECT_THAT(getDiagnosticString(), |
| 148 |
HasSubstr("In the OpenCL environment, the optional " |
| 149 |
"Access Qualifier must be present." |
| 150 |
"\n %2 = OpTypeImage %void 3D 0 0 0 0 Unknown\n")); |
| 151 |
} |
| 152 |
|
| 153 |
TEST_F(ValidateOpenCL, ImageWriteWithOptionalImageOperandsBad) { |
| 154 |
std::string spirv = R"( |
| 155 |
OpCapability Addresses |
| 156 |
OpCapability Kernel |
| 157 |
OpCapability ImageBasic |
| 158 |
OpMemoryModel Physical64 OpenCL |
| 159 |
OpEntryPoint Kernel %5 "test" |
| 160 |
%uint = OpTypeInt 32 0 |
| 161 |
%uint_7 = OpConstant %uint 7 |
| 162 |
%uint_3 = OpConstant %uint 3 |
| 163 |
%uint_1 = OpConstant %uint 1 |
| 164 |
%uint_2 = OpConstant %uint 2 |
| 165 |
%uint_4 = OpConstant %uint 4 |
| 166 |
%void = OpTypeVoid |
| 167 |
%3 = OpTypeImage %void 2D 0 0 0 0 Unknown WriteOnly |
| 168 |
%4 = OpTypeFunction %void %3 |
| 169 |
%v2uint = OpTypeVector %uint 2 |
| 170 |
%v4uint = OpTypeVector %uint 4 |
| 171 |
%12 = OpConstantComposite %v2uint %uint_7 %uint_3 |
| 172 |
%17 = OpConstantComposite %v4uint %uint_1 %uint_2 %uint_3 %uint_4 |
| 173 |
%5 = OpFunction %void None %4 |
| 174 |
%img = OpFunctionParameter %3 |
| 175 |
%entry = OpLabel |
| 176 |
OpImageWrite %img %12 %17 ConstOffset %12 |
| 177 |
OpReturn |
| 178 |
OpFunctionEnd |
| 179 |
)"; |
| 180 |
|
| 181 |
CompileSuccessfully(spirv); |
| 182 |
|
| 183 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 184 |
EXPECT_THAT(getDiagnosticString(), |
| 185 |
HasSubstr("Optional Image Operands are not allowed in the " |
| 186 |
"OpenCL environment." |
| 187 |
"\n OpImageWrite %15 %13 %14 ConstOffset %13\n")); |
| 188 |
} |
| 189 |
|
| 190 |
TEST_F(ValidateOpenCL, ImageReadWithConstOffsetBad) { |
| 191 |
std::string spirv = R"( |
| 192 |
OpCapability Addresses |
| 193 |
OpCapability Kernel |
| 194 |
OpCapability ImageBasic |
| 195 |
OpMemoryModel Physical64 OpenCL |
| 196 |
OpEntryPoint Kernel %5 "image_kernel" |
| 197 |
OpName %img "img" |
| 198 |
OpName %coord "coord" |
| 199 |
OpName %call "call" |
| 200 |
%uint = OpTypeInt 32 0 |
| 201 |
%uint_7 = OpConstant %uint 7 |
| 202 |
%uint_3 = OpConstant %uint 3 |
| 203 |
%void = OpTypeVoid |
| 204 |
%3 = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly |
| 205 |
%4 = OpTypeFunction %void %3 |
| 206 |
%v4uint = OpTypeVector %uint 4 |
| 207 |
%v2uint = OpTypeVector %uint 2 |
| 208 |
%coord = OpConstantComposite %v2uint %uint_7 %uint_3 |
| 209 |
%5 = OpFunction %void None %4 |
| 210 |
%img = OpFunctionParameter %3 |
| 211 |
%entry = OpLabel |
| 212 |
%call = OpImageRead %v4uint %img %coord ConstOffset %coord |
| 213 |
OpReturn |
| 214 |
OpFunctionEnd |
| 215 |
)"; |
| 216 |
|
| 217 |
CompileSuccessfully(spirv); |
| 218 |
|
| 219 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 220 |
EXPECT_THAT( |
| 221 |
getDiagnosticString(), |
| 222 |
HasSubstr( |
| 223 |
"ConstOffset image operand not allowed in the OpenCL environment." |
| 224 |
"\n %call = OpImageRead %v4uint %img %coord ConstOffset %coord\n")); |
| 225 |
} |
| 226 |
|
| 227 |
TEST_F(ValidateOpenCL, ImageSampleExplicitLodWithConstOffsetBad) { |
| 228 |
std::string spirv = R"( |
| 229 |
OpCapability Addresses |
| 230 |
OpCapability Kernel |
| 231 |
OpCapability ImageBasic |
| 232 |
OpCapability LiteralSampler |
| 233 |
OpMemoryModel Physical64 OpenCL |
| 234 |
OpEntryPoint Kernel %5 "image_kernel" |
| 235 |
OpName %img "img" |
| 236 |
OpName %coord "coord" |
| 237 |
OpName %call "call" |
| 238 |
%uint = OpTypeInt 32 0 |
| 239 |
%uint_7 = OpConstant %uint 7 |
| 240 |
%uint_3 = OpConstant %uint 3 |
| 241 |
%void = OpTypeVoid |
| 242 |
%3 = OpTypeImage %void 2D 0 0 0 0 Unknown ReadOnly |
| 243 |
%4 = OpTypeFunction %void %3 |
| 244 |
%8 = OpTypeSampler |
| 245 |
%10 = OpTypeSampledImage %3 |
| 246 |
%v4uint = OpTypeVector %uint 4 |
| 247 |
%v2uint = OpTypeVector %uint 2 |
| 248 |
%float = OpTypeFloat 32 |
| 249 |
%9 = OpConstantSampler %8 None 0 Nearest |
| 250 |
%coord = OpConstantComposite %v2uint %uint_7 %uint_3 |
| 251 |
%float_0 = OpConstant %float 0 |
| 252 |
%5 = OpFunction %void None %4 |
| 253 |
%6 = OpFunctionParameter %3 |
| 254 |
%entry = OpLabel |
| 255 |
%img = OpSampledImage %10 %6 %9 |
| 256 |
%call = OpImageSampleExplicitLod %v4uint %img %coord |
| 257 |
Lod|ConstOffset %float_0 %coord |
| 258 |
OpReturn |
| 259 |
OpFunctionEnd |
| 260 |
)"; |
| 261 |
|
| 262 |
CompileSuccessfully(spirv); |
| 263 |
|
| 264 |
EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2)); |
| 265 |
EXPECT_THAT( |
| 266 |
getDiagnosticString(), |
| 267 |
HasSubstr( |
| 268 |
"ConstOffset image operand not allowed in the OpenCL environment." |
| 269 |
"\n %call = OpImageSampleExplicitLod %v4uint %img " |
| 270 |
"%coord Lod|ConstOffset %float_0 %coord\n")); |
| 271 |
} |
| 272 |
|
| 273 |
} // namespace |
| 274 |
} // namespace val |
| 275 |
} // namespace spvtools |