validate_tensor_layout.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2024 NVIDIA Corporation
  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. // Validate instructions that manipulate tensor layout and view objects
  15. #include "source/opcode.h"
  16. #include "source/spirv_target_env.h"
  17. #include "source/val/instruction.h"
  18. #include "source/val/validate.h"
  19. #include "source/val/validation_state.h"
  20. namespace spvtools {
  21. namespace val {
  22. namespace {
  23. spv_result_t ValidateTensorLayoutResultTypeNV(ValidationState_t& _,
  24. const Instruction* inst) {
  25. const auto result_type_index = 0;
  26. const auto result_type_id = inst->GetOperandAs<uint32_t>(result_type_index);
  27. const auto result_type = _.FindDef(result_type_id);
  28. if (!result_type || spv::Op::OpTypeTensorLayoutNV != result_type->opcode()) {
  29. return _.diag(SPV_ERROR_INVALID_ID, inst)
  30. << spvOpcodeString(inst->opcode()) << " Result Type <id> "
  31. << _.getIdName(result_type_id) << " is not a tensor layout type.";
  32. }
  33. return SPV_SUCCESS;
  34. }
  35. spv_result_t ValidateTensorViewResultTypeNV(ValidationState_t& _,
  36. const Instruction* inst) {
  37. const auto result_type_index = 0;
  38. const auto result_type_id = inst->GetOperandAs<uint32_t>(result_type_index);
  39. const auto result_type = _.FindDef(result_type_id);
  40. if (!result_type || spv::Op::OpTypeTensorViewNV != result_type->opcode()) {
  41. return _.diag(SPV_ERROR_INVALID_ID, inst)
  42. << spvOpcodeString(inst->opcode()) << " Result Type <id> "
  43. << _.getIdName(result_type_id) << " is not a tensor view type.";
  44. }
  45. return SPV_SUCCESS;
  46. }
  47. spv_result_t ValidateCreateTensorLayoutNV(ValidationState_t& _,
  48. const Instruction* inst) {
  49. if (auto error = ValidateTensorLayoutResultTypeNV(_, inst)) return error;
  50. return SPV_SUCCESS;
  51. }
  52. spv_result_t ValidateCreateTensorViewNV(ValidationState_t& _,
  53. const Instruction* inst) {
  54. if (auto error = ValidateTensorViewResultTypeNV(_, inst)) return error;
  55. return SPV_SUCCESS;
  56. }
  57. enum ExpectedNumValues {
  58. DIM,
  59. DIMx2,
  60. ONE,
  61. FOUR,
  62. };
  63. spv_result_t ValidateTensorTypeWithDimValuesNV(ValidationState_t& _,
  64. const Instruction* inst,
  65. ExpectedNumValues expected,
  66. bool is_view) {
  67. std::string type_str;
  68. if (is_view) {
  69. if (auto error = ValidateTensorViewResultTypeNV(_, inst)) return error;
  70. type_str = "TensorView";
  71. } else {
  72. if (auto error = ValidateTensorLayoutResultTypeNV(_, inst)) return error;
  73. type_str = "TensorLayout";
  74. }
  75. const auto result_type_id = inst->GetOperandAs<uint32_t>(0);
  76. const auto tensor_id = inst->GetOperandAs<uint32_t>(2);
  77. const auto tensor = _.FindDef(tensor_id);
  78. if (!tensor || result_type_id != tensor->type_id()) {
  79. return _.diag(SPV_ERROR_INVALID_ID, inst)
  80. << spvOpcodeString(inst->opcode()) << " Result Type <id> "
  81. << _.getIdName(result_type_id) << " does not match " << type_str
  82. << " type.";
  83. }
  84. const auto num_values = inst->operands().size() - 3;
  85. const auto result_type = _.FindDef(result_type_id);
  86. const auto dim_index = 1;
  87. const auto dim_id = result_type->GetOperandAs<uint32_t>(dim_index);
  88. uint64_t dim_value;
  89. if (_.EvalConstantValUint64(dim_id, &dim_value)) {
  90. uint64_t expected_num_values = 0;
  91. switch (expected) {
  92. case DIM:
  93. expected_num_values = dim_value;
  94. break;
  95. case DIMx2:
  96. expected_num_values = dim_value * 2;
  97. break;
  98. case ONE:
  99. expected_num_values = 1;
  100. break;
  101. case FOUR:
  102. expected_num_values = 4;
  103. break;
  104. }
  105. if (num_values != expected_num_values) {
  106. return _.diag(SPV_ERROR_INVALID_ID, inst)
  107. << spvOpcodeString(inst->opcode())
  108. << " unexpected number of operands.";
  109. }
  110. }
  111. for (uint32_t i = 0; i < num_values; ++i) {
  112. const auto val_id = inst->GetOperandAs<uint32_t>(i + 3);
  113. const auto val = _.FindDef(val_id);
  114. if (!val || !_.IsIntScalarType(val->type_id()) ||
  115. _.GetBitWidth(val->type_id()) != 32) {
  116. return _.diag(SPV_ERROR_INVALID_ID, inst)
  117. << spvOpcodeString(inst->opcode()) << " operand <id> "
  118. << _.getIdName(val_id) << " is not a 32-bit integer.";
  119. }
  120. }
  121. return SPV_SUCCESS;
  122. }
  123. } // namespace
  124. spv_result_t TensorLayoutPass(ValidationState_t& _, const Instruction* inst) {
  125. switch (inst->opcode()) {
  126. case spv::Op::OpCreateTensorLayoutNV:
  127. if (auto error = ValidateCreateTensorLayoutNV(_, inst)) return error;
  128. break;
  129. case spv::Op::OpCreateTensorViewNV:
  130. if (auto error = ValidateCreateTensorViewNV(_, inst)) return error;
  131. break;
  132. case spv::Op::OpTensorLayoutSetBlockSizeNV:
  133. case spv::Op::OpTensorLayoutSetDimensionNV:
  134. case spv::Op::OpTensorLayoutSetStrideNV:
  135. if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, DIM, false))
  136. return error;
  137. break;
  138. case spv::Op::OpTensorLayoutSliceNV:
  139. if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, DIMx2, false))
  140. return error;
  141. break;
  142. case spv::Op::OpTensorLayoutSetClampValueNV:
  143. if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, ONE, false))
  144. return error;
  145. break;
  146. case spv::Op::OpTensorViewSetDimensionNV:
  147. case spv::Op::OpTensorViewSetStrideNV:
  148. if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, DIM, true))
  149. return error;
  150. break;
  151. case spv::Op::OpTensorViewSetClipNV:
  152. if (auto error = ValidateTensorTypeWithDimValuesNV(_, inst, FOUR, true))
  153. return error;
  154. break;
  155. default:
  156. break;
  157. }
  158. return SPV_SUCCESS;
  159. }
  160. } // namespace val
  161. } // namespace spvtools