1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
// Copyright (c) 2018 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include "source/opcode.h"
#include "source/table2.h"
#include "source/val/instruction.h"
#include "source/val/validate.h"
#include "source/val/validation_state.h"
namespace spvtools {
namespace val {
namespace {
// Returns true if |a| and |b| are instructions defining pointers that point to
// types logically match and the decorations that apply to |b| are a subset
// of the decorations that apply to |a|.
bool DoPointeesLogicallyMatch(val::Instruction* a, val::Instruction* b,
ValidationState_t& _) {
if (a->opcode() != spv::Op::OpTypePointer ||
b->opcode() != spv::Op::OpTypePointer) {
return false;
}
const auto& dec_a = _.id_decorations(a->id());
const auto& dec_b = _.id_decorations(b->id());
for (const auto& dec : dec_b) {
if (std::find(dec_a.begin(), dec_a.end(), dec) == dec_a.end()) {
return false;
}
}
uint32_t a_type = a->GetOperandAs<uint32_t>(2);
uint32_t b_type = b->GetOperandAs<uint32_t>(2);
if (a_type == b_type) {
return true;
}
Instruction* a_type_inst = _.FindDef(a_type);
Instruction* b_type_inst = _.FindDef(b_type);
return _.LogicallyMatch(a_type_inst, b_type_inst, true);
}
spv_result_t ValidateFunction(ValidationState_t& _, const Instruction* inst) {
const auto function_type_id = inst->GetOperandAs<uint32_t>(3);
const auto function_type = _.FindDef(function_type_id);
if (!function_type || spv::Op::OpTypeFunction != function_type->opcode()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpFunction Function Type <id> " << _.getIdName(function_type_id)
<< " is not a function type.";
}
const auto return_id = function_type->GetOperandAs<uint32_t>(1);
if (return_id != inst->type_id()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpFunction Result Type <id> " << _.getIdName(inst->type_id())
<< " does not match the Function Type's return type <id> "
<< _.getIdName(return_id) << ".";
}
const std::vector<spv::Op> acceptable = {
spv::Op::OpGroupDecorate,
spv::Op::OpDecorate,
spv::Op::OpEnqueueKernel,
spv::Op::OpEntryPoint,
spv::Op::OpExecutionMode,
spv::Op::OpExecutionModeId,
spv::Op::OpFunctionCall,
spv::Op::OpGetKernelNDrangeSubGroupCount,
spv::Op::OpGetKernelNDrangeMaxSubGroupSize,
spv::Op::OpGetKernelWorkGroupSize,
spv::Op::OpGetKernelPreferredWorkGroupSizeMultiple,
spv::Op::OpGetKernelLocalSizeForSubgroupCount,
spv::Op::OpGetKernelMaxNumSubgroups,
spv::Op::OpName,
spv::Op::OpCooperativeMatrixPerElementOpNV,
spv::Op::OpCooperativeMatrixReduceNV,
spv::Op::OpCooperativeMatrixLoadTensorNV,
spv::Op::OpConditionalEntryPointINTEL,
};
for (auto& pair : inst->uses()) {
const auto* use = pair.first;
if (std::find(acceptable.begin(), acceptable.end(), use->opcode()) ==
acceptable.end() &&
!use->IsNonSemantic() && !use->IsDebugInfo()) {
return _.diag(SPV_ERROR_INVALID_ID, use)
<< "Invalid use of function result id " << _.getIdName(inst->id())
<< ".";
}
}
return SPV_SUCCESS;
}
spv_result_t ValidateFunctionParameter(ValidationState_t& _,
const Instruction* inst) {
// NOTE: Find OpFunction & ensure OpFunctionParameter is not out of place.
size_t param_index = 0;
size_t inst_num = inst->LineNum() - 1;
auto func_inst = &_.ordered_instructions()[inst_num];
while (--inst_num) {
func_inst = &_.ordered_instructions()[inst_num];
if (func_inst->opcode() == spv::Op::OpFunction) {
break;
} else if (func_inst->opcode() == spv::Op::OpFunctionParameter) {
++param_index;
}
}
if (func_inst->opcode() != spv::Op::OpFunction) {
return _.diag(SPV_ERROR_INVALID_LAYOUT, inst)
<< "Function parameter must be preceded by a function.";
}
const auto function_type_id = func_inst->GetOperandAs<uint32_t>(3);
const auto function_type = _.FindDef(function_type_id);
if (!function_type) {
return _.diag(SPV_ERROR_INVALID_ID, func_inst)
<< "Missing function type definition.";
}
if (param_index >= function_type->words().size() - 3) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Too many OpFunctionParameters for " << func_inst->id()
<< ": expected " << function_type->words().size() - 3
<< " based on the function's type";
}
const auto param_type =
_.FindDef(function_type->GetOperandAs<uint32_t>(param_index + 2));
if (!param_type || inst->type_id() != param_type->id()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpFunctionParameter Result Type <id> "
<< _.getIdName(inst->type_id())
<< " does not match the OpTypeFunction parameter "
"type of the same index.";
}
return SPV_SUCCESS;
}
spv_result_t ValidateFunctionCall(ValidationState_t& _,
const Instruction* inst) {
const auto function_id = inst->GetOperandAs<uint32_t>(2);
const auto function = _.FindDef(function_id);
if (!function || spv::Op::OpFunction != function->opcode()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpFunctionCall Function <id> " << _.getIdName(function_id)
<< " is not a function.";
}
auto return_type = _.FindDef(function->type_id());
if (!return_type || return_type->id() != inst->type_id()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpFunctionCall Result Type <id> " << _.getIdName(inst->type_id())
<< "s type does not match Function <id> "
<< _.getIdName(return_type->id()) << "s return type.";
}
if (!_.options()->relax_logical_pointer &&
(_.addressing_model() == spv::AddressingModel::Logical ||
_.addressing_model() == spv::AddressingModel::PhysicalStorageBuffer64)) {
if (return_type->opcode() == spv::Op::OpTypePointer ||
return_type->opcode() == spv::Op::OpTypeUntypedPointerKHR) {
const auto sc = return_type->GetOperandAs<spv::StorageClass>(1);
if (sc != spv::StorageClass::PhysicalStorageBuffer) {
if (!_.HasCapability(spv::Capability::VariablePointersStorageBuffer) &&
sc == spv::StorageClass::StorageBuffer) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "In Logical addressing, functions may only return a "
"storage buffer pointer if the "
"VariablePointersStorageBuffer capability is declared";
} else if (!_.HasCapability(spv::Capability::VariablePointers) &&
sc == spv::StorageClass::Workgroup) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "In Logical addressing, functions may only return a "
"workgroup pointer if the VariablePointers capability is "
"declared";
} else if (sc != spv::StorageClass::StorageBuffer &&
sc != spv::StorageClass::Workgroup) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "In Logical addressing, functions may not return a pointer "
"in this storage class";
}
}
}
}
const auto function_type_id = function->GetOperandAs<uint32_t>(3);
const auto function_type = _.FindDef(function_type_id);
if (!function_type || function_type->opcode() != spv::Op::OpTypeFunction) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Missing function type definition.";
}
const auto function_call_arg_count = inst->words().size() - 4;
const auto function_param_count = function_type->words().size() - 3;
if (function_param_count != function_call_arg_count) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpFunctionCall Function <id>'s parameter count does not match "
"the argument count.";
}
for (size_t argument_index = 3, param_index = 2;
argument_index < inst->operands().size();
argument_index++, param_index++) {
const auto argument_id = inst->GetOperandAs<uint32_t>(argument_index);
const auto argument = _.FindDef(argument_id);
if (!argument) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Missing argument " << argument_index - 3 << " definition.";
}
const auto argument_type = _.FindDef(argument->type_id());
if (!argument_type) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Missing argument " << argument_index - 3
<< " type definition.";
}
const auto parameter_type_id =
function_type->GetOperandAs<uint32_t>(param_index);
const auto parameter_type = _.FindDef(parameter_type_id);
if (!parameter_type || argument_type->id() != parameter_type->id()) {
if (!parameter_type || !_.options()->before_hlsl_legalization ||
!DoPointeesLogicallyMatch(argument_type, parameter_type, _)) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpFunctionCall Argument <id> " << _.getIdName(argument_id)
<< "s type does not match Function <id> "
<< _.getIdName(parameter_type_id) << "s parameter type.";
}
}
if (_.addressing_model() == spv::AddressingModel::Logical ||
_.addressing_model() == spv::AddressingModel::PhysicalStorageBuffer64) {
if ((parameter_type->opcode() == spv::Op::OpTypePointer ||
parameter_type->opcode() == spv::Op::OpTypeUntypedPointerKHR) &&
!_.options()->relax_logical_pointer) {
spv::StorageClass sc =
parameter_type->GetOperandAs<spv::StorageClass>(1u);
if (sc != spv::StorageClass::PhysicalStorageBuffer) {
// Validate which storage classes can be pointer operands.
switch (sc) {
case spv::StorageClass::UniformConstant:
case spv::StorageClass::Function:
case spv::StorageClass::Private:
case spv::StorageClass::Workgroup:
case spv::StorageClass::AtomicCounter:
// SPV_EXT_tile_image
case spv::StorageClass::TileImageEXT:
// SPV_KHR_ray_tracing
case spv::StorageClass::ShaderRecordBufferKHR:
// These are always allowed.
break;
case spv::StorageClass::StorageBuffer:
if (!_.features().variable_pointers) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "StorageBuffer pointer operand "
<< _.getIdName(argument_id)
<< " requires a variable pointers capability";
}
break;
default:
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Invalid storage class for pointer operand "
<< _.getIdName(argument_id);
}
// Validate memory object declaration requirements.
if (argument->opcode() != spv::Op::OpVariable &&
argument->opcode() != spv::Op::OpUntypedVariableKHR &&
argument->opcode() != spv::Op::OpFunctionParameter) {
const bool ssbo_vptr =
_.HasCapability(
spv::Capability::VariablePointersStorageBuffer) &&
sc == spv::StorageClass::StorageBuffer;
const bool wg_vptr =
_.HasCapability(spv::Capability::VariablePointers) &&
sc == spv::StorageClass::Workgroup;
const bool uc_ptr = sc == spv::StorageClass::UniformConstant;
if (!_.options()->before_hlsl_legalization && !ssbo_vptr &&
!wg_vptr && !uc_ptr) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "Pointer operand " << _.getIdName(argument_id)
<< " must be a memory object declaration";
}
}
}
}
}
}
return SPV_SUCCESS;
}
spv_result_t ValidateCooperativeMatrixPerElementOp(ValidationState_t& _,
const Instruction* inst) {
const auto function_id = inst->GetOperandAs<uint32_t>(3);
const auto function = _.FindDef(function_id);
if (!function || spv::Op::OpFunction != function->opcode()) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpCooperativeMatrixPerElementOpNV Function <id> "
<< _.getIdName(function_id) << " is not a function.";
}
const auto matrix_id = inst->GetOperandAs<uint32_t>(2);
const auto matrix = _.FindDef(matrix_id);
const auto matrix_type_id = matrix->type_id();
if (!_.IsCooperativeMatrixKHRType(matrix_type_id)) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpCooperativeMatrixPerElementOpNV Matrix <id> "
<< _.getIdName(matrix_id) << " is not a cooperative matrix.";
}
const auto result_type_id = inst->GetOperandAs<uint32_t>(0);
if (matrix_type_id != result_type_id) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpCooperativeMatrixPerElementOpNV Result Type <id> "
<< _.getIdName(result_type_id) << " must match matrix type <id> "
<< _.getIdName(matrix_type_id) << ".";
}
const auto matrix_comp_type_id =
_.FindDef(matrix_type_id)->GetOperandAs<uint32_t>(1);
const auto function_type_id = function->GetOperandAs<uint32_t>(3);
const auto function_type = _.FindDef(function_type_id);
auto return_type_id = function_type->GetOperandAs<uint32_t>(1);
if (return_type_id != matrix_comp_type_id) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpCooperativeMatrixPerElementOpNV function return type <id> "
<< _.getIdName(return_type_id)
<< " must match matrix component type <id> "
<< _.getIdName(matrix_comp_type_id) << ".";
}
if (function_type->operands().size() < 5) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpCooperativeMatrixPerElementOpNV function type <id> "
<< _.getIdName(function_type_id)
<< " must have a least three parameters.";
}
const auto param0_id = function_type->GetOperandAs<uint32_t>(2);
const auto param1_id = function_type->GetOperandAs<uint32_t>(3);
const auto param2_id = function_type->GetOperandAs<uint32_t>(4);
if (!_.IsIntScalarType(param0_id) || _.GetBitWidth(param0_id) != 32) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpCooperativeMatrixPerElementOpNV function type first parameter "
"type <id> "
<< _.getIdName(param0_id) << " must be a 32-bit integer.";
}
if (!_.IsIntScalarType(param1_id) || _.GetBitWidth(param1_id) != 32) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpCooperativeMatrixPerElementOpNV function type second "
"parameter type <id> "
<< _.getIdName(param1_id) << " must be a 32-bit integer.";
}
if (param2_id != matrix_comp_type_id) {
return _.diag(SPV_ERROR_INVALID_ID, inst)
<< "OpCooperativeMatrixPerElementOpNV function type third parameter "
"type <id> "
<< _.getIdName(param2_id) << " must match matrix component type.";
}
return SPV_SUCCESS;
}
} // namespace
spv_result_t FunctionPass(ValidationState_t& _, const Instruction* inst) {
switch (inst->opcode()) {
case spv::Op::OpFunction:
if (auto error = ValidateFunction(_, inst)) return error;
break;
case spv::Op::OpFunctionParameter:
if (auto error = ValidateFunctionParameter(_, inst)) return error;
break;
case spv::Op::OpFunctionCall:
if (auto error = ValidateFunctionCall(_, inst)) return error;
break;
case spv::Op::OpCooperativeMatrixPerElementOpNV:
if (auto error = ValidateCooperativeMatrixPerElementOp(_, inst))
return error;
break;
default:
break;
}
return SPV_SUCCESS;
}
} // namespace val
} // namespace spvtools
|