validate_bitwise.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/diagnostic.h"
  16. #include "source/opcode.h"
  17. #include "source/spirv_target_env.h"
  18. #include "source/val/instruction.h"
  19. #include "source/val/validate.h"
  20. #include "source/val/validation_state.h"
  21. namespace spvtools {
  22. namespace val {
  23. // Validates when base and result need to be the same type
  24. spv_result_t ValidateBaseType(ValidationState_t& _, const Instruction* inst,
  25. const uint32_t base_type) {
  26. const SpvOp opcode = inst->opcode();
  27. if (!_.IsIntScalarType(base_type) && !_.IsIntVectorType(base_type)) {
  28. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  29. << _.VkErrorID(4781)
  30. << "Expected int scalar or vector type for Base operand: "
  31. << spvOpcodeString(opcode);
  32. }
  33. // Vulkan has a restriction to 32 bit for base
  34. if (spvIsVulkanEnv(_.context()->target_env)) {
  35. if (_.GetBitWidth(base_type) != 32) {
  36. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  37. << _.VkErrorID(4781)
  38. << "Expected 32-bit int type for Base operand: "
  39. << spvOpcodeString(opcode);
  40. }
  41. }
  42. // OpBitCount just needs same number of components
  43. if (base_type != inst->type_id() && opcode != SpvOpBitCount) {
  44. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  45. << "Expected Base Type to be equal to Result Type: "
  46. << spvOpcodeString(opcode);
  47. }
  48. return SPV_SUCCESS;
  49. }
  50. // Validates correctness of bitwise instructions.
  51. spv_result_t BitwisePass(ValidationState_t& _, const Instruction* inst) {
  52. const SpvOp opcode = inst->opcode();
  53. const uint32_t result_type = inst->type_id();
  54. switch (opcode) {
  55. case SpvOpShiftRightLogical:
  56. case SpvOpShiftRightArithmetic:
  57. case SpvOpShiftLeftLogical: {
  58. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(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. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  68. << "Expected Base to be int scalar or vector: "
  69. << spvOpcodeString(opcode);
  70. if (_.GetDimension(base_type) != result_dimension)
  71. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  72. << "Expected Base to have the same dimension "
  73. << "as Result Type: " << spvOpcodeString(opcode);
  74. if (_.GetBitWidth(base_type) != _.GetBitWidth(result_type))
  75. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  76. << "Expected Base to have the same bit width "
  77. << "as Result Type: " << spvOpcodeString(opcode);
  78. if (!shift_type ||
  79. (!_.IsIntScalarType(shift_type) && !_.IsIntVectorType(shift_type)))
  80. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  81. << "Expected Shift to be int scalar or vector: "
  82. << spvOpcodeString(opcode);
  83. if (_.GetDimension(shift_type) != result_dimension)
  84. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  85. << "Expected Shift to have the same dimension "
  86. << "as Result Type: " << spvOpcodeString(opcode);
  87. break;
  88. }
  89. case SpvOpBitwiseOr:
  90. case SpvOpBitwiseXor:
  91. case SpvOpBitwiseAnd:
  92. case SpvOpNot: {
  93. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  94. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  95. << "Expected int scalar or vector type as Result Type: "
  96. << spvOpcodeString(opcode);
  97. const uint32_t result_dimension = _.GetDimension(result_type);
  98. const uint32_t result_bit_width = _.GetBitWidth(result_type);
  99. for (size_t operand_index = 2; operand_index < inst->operands().size();
  100. ++operand_index) {
  101. const uint32_t type_id = _.GetOperandTypeId(inst, operand_index);
  102. if (!type_id ||
  103. (!_.IsIntScalarType(type_id) && !_.IsIntVectorType(type_id)))
  104. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  105. << "Expected int scalar or vector as operand: "
  106. << spvOpcodeString(opcode) << " operand index "
  107. << operand_index;
  108. if (_.GetDimension(type_id) != result_dimension)
  109. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  110. << "Expected operands to have the same dimension "
  111. << "as Result Type: " << spvOpcodeString(opcode)
  112. << " operand index " << operand_index;
  113. if (_.GetBitWidth(type_id) != result_bit_width)
  114. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  115. << "Expected operands to have the same bit width "
  116. << "as Result Type: " << spvOpcodeString(opcode)
  117. << " operand index " << operand_index;
  118. }
  119. break;
  120. }
  121. case SpvOpBitFieldInsert: {
  122. const uint32_t base_type = _.GetOperandTypeId(inst, 2);
  123. const uint32_t insert_type = _.GetOperandTypeId(inst, 3);
  124. const uint32_t offset_type = _.GetOperandTypeId(inst, 4);
  125. const uint32_t count_type = _.GetOperandTypeId(inst, 5);
  126. if (spv_result_t error = ValidateBaseType(_, inst, base_type)) {
  127. return error;
  128. }
  129. if (insert_type != result_type)
  130. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  131. << "Expected Insert Type to be equal to Result Type: "
  132. << spvOpcodeString(opcode);
  133. if (!offset_type || !_.IsIntScalarType(offset_type))
  134. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  135. << "Expected Offset Type to be int scalar: "
  136. << spvOpcodeString(opcode);
  137. if (!count_type || !_.IsIntScalarType(count_type))
  138. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  139. << "Expected Count Type to be int scalar: "
  140. << spvOpcodeString(opcode);
  141. break;
  142. }
  143. case SpvOpBitFieldSExtract:
  144. case SpvOpBitFieldUExtract: {
  145. const uint32_t base_type = _.GetOperandTypeId(inst, 2);
  146. const uint32_t offset_type = _.GetOperandTypeId(inst, 3);
  147. const uint32_t count_type = _.GetOperandTypeId(inst, 4);
  148. if (spv_result_t error = ValidateBaseType(_, inst, base_type)) {
  149. return error;
  150. }
  151. if (!offset_type || !_.IsIntScalarType(offset_type))
  152. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  153. << "Expected Offset Type to be int scalar: "
  154. << spvOpcodeString(opcode);
  155. if (!count_type || !_.IsIntScalarType(count_type))
  156. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  157. << "Expected Count Type to be int scalar: "
  158. << spvOpcodeString(opcode);
  159. break;
  160. }
  161. case SpvOpBitReverse: {
  162. const uint32_t base_type = _.GetOperandTypeId(inst, 2);
  163. if (spv_result_t error = ValidateBaseType(_, inst, base_type)) {
  164. return error;
  165. }
  166. break;
  167. }
  168. case SpvOpBitCount: {
  169. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  170. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  171. << "Expected int scalar or vector type as Result Type: "
  172. << spvOpcodeString(opcode);
  173. const uint32_t base_type = _.GetOperandTypeId(inst, 2);
  174. const uint32_t base_dimension = _.GetDimension(base_type);
  175. const uint32_t result_dimension = _.GetDimension(result_type);
  176. if (spv_result_t error = ValidateBaseType(_, inst, base_type)) {
  177. return error;
  178. }
  179. if (base_dimension != result_dimension)
  180. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  181. << "Expected Base dimension to be equal to Result Type "
  182. "dimension: "
  183. << spvOpcodeString(opcode);
  184. break;
  185. }
  186. default:
  187. break;
  188. }
  189. return SPV_SUCCESS;
  190. }
  191. } // namespace val
  192. } // namespace spvtools