validate_bitwise.cpp 8.8 KB

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