validate_bitwise.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright (c) 2017 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. // Validates correctness of bitwise instructions.
  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. // Validates when base and result need to be the same type
  23. spv_result_t ValidateBaseType(ValidationState_t& _, const Instruction* inst,
  24. const uint32_t base_type) {
  25. const spv::Op opcode = inst->opcode();
  26. if (!_.IsIntScalarType(base_type) && !_.IsIntVectorType(base_type)) {
  27. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  28. << "Expected int scalar or vector type for Base operand: "
  29. << spvOpcodeString(opcode);
  30. }
  31. // Vulkan has a restriction to 32 bit for base
  32. if (spvIsVulkanEnv(_.context()->target_env)) {
  33. if (_.GetBitWidth(base_type) != 32 &&
  34. !_.options()->allow_vulkan_32_bit_bitwise) {
  35. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  36. << _.VkErrorID(10824)
  37. << "Expected 32-bit int type for Base operand: "
  38. << spvOpcodeString(opcode)
  39. << _.MissingFeature("maintenance9 feature",
  40. "--allow-vulkan-32-bit-bitwise", false);
  41. }
  42. }
  43. // OpBitCount just needs same number of components
  44. if (base_type != inst->type_id() && opcode != spv::Op::OpBitCount) {
  45. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  46. << "Expected Base Type to be equal to Result Type: "
  47. << spvOpcodeString(opcode);
  48. }
  49. return SPV_SUCCESS;
  50. }
  51. // Validates correctness of bitwise instructions.
  52. spv_result_t BitwisePass(ValidationState_t& _, const Instruction* inst) {
  53. const spv::Op opcode = inst->opcode();
  54. const uint32_t result_type = inst->type_id();
  55. switch (opcode) {
  56. case spv::Op::OpShiftRightLogical:
  57. case spv::Op::OpShiftRightArithmetic:
  58. case spv::Op::OpShiftLeftLogical: {
  59. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type) &&
  60. !_.IsIntCooperativeVectorNVType(result_type))
  61. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  62. << "Expected int scalar or vector type as Result Type: "
  63. << spvOpcodeString(opcode);
  64. const uint32_t result_dimension = _.GetDimension(result_type);
  65. const uint32_t base_type = _.GetOperandTypeId(inst, 2);
  66. const uint32_t shift_type = _.GetOperandTypeId(inst, 3);
  67. if (!base_type ||
  68. (!_.IsIntScalarType(base_type) && !_.IsIntVectorType(base_type) &&
  69. !_.IsIntCooperativeVectorNVType(base_type)))
  70. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  71. << "Expected Base to be int scalar or vector: "
  72. << spvOpcodeString(opcode);
  73. if (_.GetDimension(base_type) != result_dimension)
  74. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  75. << "Expected Base to have the same dimension "
  76. << "as Result Type: " << spvOpcodeString(opcode);
  77. if (_.GetBitWidth(base_type) != _.GetBitWidth(result_type))
  78. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  79. << "Expected Base to have the same bit width "
  80. << "as Result Type: " << spvOpcodeString(opcode);
  81. if (!shift_type ||
  82. (!_.IsIntScalarType(shift_type) && !_.IsIntVectorType(shift_type) &&
  83. !_.IsIntCooperativeVectorNVType(shift_type)))
  84. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  85. << "Expected Shift to be int scalar or vector: "
  86. << spvOpcodeString(opcode);
  87. if (_.GetDimension(shift_type) != result_dimension)
  88. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  89. << "Expected Shift to have the same dimension "
  90. << "as Result Type: " << spvOpcodeString(opcode);
  91. break;
  92. }
  93. case spv::Op::OpBitwiseOr:
  94. case spv::Op::OpBitwiseXor:
  95. case spv::Op::OpBitwiseAnd:
  96. case spv::Op::OpNot: {
  97. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type) &&
  98. !_.IsIntCooperativeVectorNVType(result_type))
  99. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  100. << "Expected int scalar or vector type as Result Type: "
  101. << spvOpcodeString(opcode);
  102. const uint32_t result_dimension = _.GetDimension(result_type);
  103. const uint32_t result_bit_width = _.GetBitWidth(result_type);
  104. for (size_t operand_index = 2; operand_index < inst->operands().size();
  105. ++operand_index) {
  106. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  107. if (!type_id ||
  108. (!_.IsIntScalarType(type_id) && !_.IsIntVectorType(type_id) &&
  109. !_.IsIntCooperativeVectorNVType(type_id)))
  110. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  111. << "Expected int scalar or vector as operand: "
  112. << spvOpcodeString(opcode) << " operand index "
  113. << operand_index;
  114. if (_.GetDimension(type_id) != result_dimension)
  115. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  116. << "Expected operands to have the same dimension "
  117. << "as Result Type: " << spvOpcodeString(opcode)
  118. << " operand index " << operand_index;
  119. if (_.GetBitWidth(type_id) != result_bit_width)
  120. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  121. << "Expected operands to have the same bit width "
  122. << "as Result Type: " << spvOpcodeString(opcode)
  123. << " operand index " << operand_index;
  124. }
  125. break;
  126. }
  127. case spv::Op::OpBitFieldInsert: {
  128. const uint32_t base_type = _.GetOperandTypeId(inst, 2);
  129. const uint32_t insert_type = _.GetOperandTypeId(inst, 3);
  130. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  131. const uint32_t count_type = _.GetOperandTypeId(inst, 5);
  132. if (spv_result_t error = ValidateBaseType(_, inst, base_type)) {
  133. return error;
  134. }
  135. if (insert_type != result_type)
  136. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  137. << "Expected Insert Type to be equal to Result Type: "
  138. << spvOpcodeString(opcode);
  139. if (!offset_type || !_.IsIntScalarType(offset_type))
  140. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  141. << "Expected Offset Type to be int scalar: "
  142. << spvOpcodeString(opcode);
  143. if (!count_type || !_.IsIntScalarType(count_type))
  144. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  145. << "Expected Count Type to be int scalar: "
  146. << spvOpcodeString(opcode);
  147. break;
  148. }
  149. case spv::Op::OpBitFieldSExtract:
  150. case spv::Op::OpBitFieldUExtract: {
  151. const uint32_t base_type = _.GetOperandTypeId(inst, 2);
  152. const uint32_t offset_type = _.GetOperandTypeId(inst, 3);
  153. const uint32_t count_type = _.GetOperandTypeId(inst, 4);
  154. if (spv_result_t error = ValidateBaseType(_, inst, base_type)) {
  155. return error;
  156. }
  157. if (!offset_type || !_.IsIntScalarType(offset_type))
  158. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  159. << "Expected Offset Type to be int scalar: "
  160. << spvOpcodeString(opcode);
  161. if (!count_type || !_.IsIntScalarType(count_type))
  162. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  163. << "Expected Count Type to be int scalar: "
  164. << spvOpcodeString(opcode);
  165. break;
  166. }
  167. case spv::Op::OpBitReverse: {
  168. const uint32_t base_type = _.GetOperandTypeId(inst, 2);
  169. if (spv_result_t error = ValidateBaseType(_, inst, base_type)) {
  170. return error;
  171. }
  172. break;
  173. }
  174. case spv::Op::OpBitCount: {
  175. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  176. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  177. << "Expected int scalar or vector type as Result Type: "
  178. << spvOpcodeString(opcode);
  179. const uint32_t base_type = _.GetOperandTypeId(inst, 2);
  180. if (spv_result_t error = ValidateBaseType(_, inst, base_type)) {
  181. return error;
  182. }
  183. const uint32_t base_dimension = _.GetDimension(base_type);
  184. const uint32_t result_dimension = _.GetDimension(result_type);
  185. if (base_dimension != result_dimension)
  186. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  187. << "Expected Base dimension to be equal to Result Type "
  188. "dimension: "
  189. << spvOpcodeString(opcode);
  190. break;
  191. }
  192. default:
  193. break;
  194. }
  195. return SPV_SUCCESS;
  196. }
  197. } // namespace val
  198. } // namespace spvtools