validate_bitwise.cpp 9.1 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 "validate.h"
  16. #include "diagnostic.h"
  17. #include "opcode.h"
  18. #include "val/instruction.h"
  19. #include "val/validation_state.h"
  20. namespace libspirv {
  21. namespace {
  22. // Returns operand word for given instruction and operand index.
  23. // The operand is expected to only have one word.
  24. inline uint32_t GetOperandWord(const spv_parsed_instruction_t* inst,
  25. size_t operand_index) {
  26. assert(operand_index < inst->num_operands);
  27. const spv_parsed_operand_t& operand = inst->operands[operand_index];
  28. assert(operand.num_words == 1);
  29. return inst->words[operand.offset];
  30. }
  31. // Returns the type id of instruction operand at |operand_index|.
  32. // The operand is expected to be an id.
  33. inline uint32_t GetOperandTypeId(ValidationState_t& _,
  34. const spv_parsed_instruction_t* inst,
  35. size_t operand_index) {
  36. return _.GetTypeId(GetOperandWord(inst, operand_index));
  37. }
  38. } // namespace
  39. // Validates correctness of bitwise instructions.
  40. spv_result_t BitwisePass(ValidationState_t& _,
  41. const spv_parsed_instruction_t* inst) {
  42. const SpvOp opcode = static_cast<SpvOp>(inst->opcode);
  43. const uint32_t result_type = inst->type_id;
  44. switch (opcode) {
  45. case SpvOpShiftRightLogical:
  46. case SpvOpShiftRightArithmetic:
  47. case SpvOpShiftLeftLogical: {
  48. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  49. return _.diag(SPV_ERROR_INVALID_DATA)
  50. << "Expected int scalar or vector type as Result Type: "
  51. << spvOpcodeString(opcode);
  52. const uint32_t result_dimension = _.GetDimension(result_type);
  53. const uint32_t base_type = GetOperandTypeId(_, inst, 2);
  54. const uint32_t shift_type = GetOperandTypeId(_, inst, 3);
  55. if (!base_type ||
  56. (!_.IsIntScalarType(base_type) && !_.IsIntVectorType(base_type)))
  57. return _.diag(SPV_ERROR_INVALID_DATA)
  58. << "Expected Base to be int scalar or vector: "
  59. << spvOpcodeString(opcode);
  60. if (_.GetDimension(base_type) != result_dimension)
  61. return _.diag(SPV_ERROR_INVALID_DATA)
  62. << "Expected Base to have the same dimension "
  63. << "as Result Type: " << spvOpcodeString(opcode);
  64. if (_.GetBitWidth(base_type) != _.GetBitWidth(result_type))
  65. return _.diag(SPV_ERROR_INVALID_DATA)
  66. << "Expected Base to have the same bit width "
  67. << "as Result Type: " << spvOpcodeString(opcode);
  68. if (!shift_type ||
  69. (!_.IsIntScalarType(shift_type) && !_.IsIntVectorType(shift_type)))
  70. return _.diag(SPV_ERROR_INVALID_DATA)
  71. << "Expected Shift to be int scalar or vector: "
  72. << spvOpcodeString(opcode);
  73. if (_.GetDimension(shift_type) != result_dimension)
  74. return _.diag(SPV_ERROR_INVALID_DATA)
  75. << "Expected Shift to have the same dimension "
  76. << "as Result Type: " << spvOpcodeString(opcode);
  77. break;
  78. }
  79. case SpvOpBitwiseOr:
  80. case SpvOpBitwiseXor:
  81. case SpvOpBitwiseAnd:
  82. case SpvOpNot: {
  83. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  84. return _.diag(SPV_ERROR_INVALID_DATA)
  85. << "Expected int scalar or vector type as Result Type: "
  86. << spvOpcodeString(opcode);
  87. const uint32_t result_dimension = _.GetDimension(result_type);
  88. const uint32_t result_bit_width = _.GetBitWidth(result_type);
  89. for (size_t operand_index = 2; operand_index < inst->num_operands;
  90. ++operand_index) {
  91. const uint32_t type_id = GetOperandTypeId(_, inst, operand_index);
  92. if (!type_id ||
  93. (!_.IsIntScalarType(type_id) && !_.IsIntVectorType(type_id)))
  94. return _.diag(SPV_ERROR_INVALID_DATA)
  95. << "Expected int scalar or vector as operand: "
  96. << spvOpcodeString(opcode) << " operand index "
  97. << operand_index;
  98. if (_.GetDimension(type_id) != result_dimension)
  99. return _.diag(SPV_ERROR_INVALID_DATA)
  100. << "Expected operands to have the same dimension "
  101. << "as Result Type: " << spvOpcodeString(opcode)
  102. << " operand index " << operand_index;
  103. if (_.GetBitWidth(type_id) != result_bit_width)
  104. return _.diag(SPV_ERROR_INVALID_DATA)
  105. << "Expected operands to have the same bit width "
  106. << "as Result Type: " << spvOpcodeString(opcode)
  107. << " operand index " << operand_index;
  108. }
  109. break;
  110. }
  111. case SpvOpBitFieldInsert: {
  112. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  113. return _.diag(SPV_ERROR_INVALID_DATA)
  114. << "Expected int scalar or vector type as Result Type: "
  115. << spvOpcodeString(opcode);
  116. const uint32_t base_type = GetOperandTypeId(_, inst, 2);
  117. const uint32_t insert_type = GetOperandTypeId(_, inst, 3);
  118. const uint32_t offset_type = GetOperandTypeId(_, inst, 4);
  119. const uint32_t count_type = GetOperandTypeId(_, inst, 5);
  120. if (base_type != result_type)
  121. return _.diag(SPV_ERROR_INVALID_DATA)
  122. << "Expected Base Type to be equal to Result Type: "
  123. << spvOpcodeString(opcode);
  124. if (insert_type != result_type)
  125. return _.diag(SPV_ERROR_INVALID_DATA)
  126. << "Expected Insert Type to be equal to Result Type: "
  127. << spvOpcodeString(opcode);
  128. if (!offset_type || !_.IsIntScalarType(offset_type))
  129. return _.diag(SPV_ERROR_INVALID_DATA)
  130. << "Expected Offset Type to be int scalar: "
  131. << spvOpcodeString(opcode);
  132. if (!count_type || !_.IsIntScalarType(count_type))
  133. return _.diag(SPV_ERROR_INVALID_DATA)
  134. << "Expected Count Type to be int scalar: "
  135. << spvOpcodeString(opcode);
  136. break;
  137. }
  138. case SpvOpBitFieldSExtract:
  139. case SpvOpBitFieldUExtract: {
  140. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  141. return _.diag(SPV_ERROR_INVALID_DATA)
  142. << "Expected int scalar or vector type as Result Type: "
  143. << spvOpcodeString(opcode);
  144. const uint32_t base_type = GetOperandTypeId(_, inst, 2);
  145. const uint32_t offset_type = GetOperandTypeId(_, inst, 3);
  146. const uint32_t count_type = GetOperandTypeId(_, inst, 4);
  147. if (base_type != result_type)
  148. return _.diag(SPV_ERROR_INVALID_DATA)
  149. << "Expected Base Type to be equal to Result Type: "
  150. << spvOpcodeString(opcode);
  151. if (!offset_type || !_.IsIntScalarType(offset_type))
  152. return _.diag(SPV_ERROR_INVALID_DATA)
  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)
  157. << "Expected Count Type to be int scalar: "
  158. << spvOpcodeString(opcode);
  159. break;
  160. }
  161. case SpvOpBitReverse: {
  162. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  163. return _.diag(SPV_ERROR_INVALID_DATA)
  164. << "Expected int scalar or vector type as Result Type: "
  165. << spvOpcodeString(opcode);
  166. const uint32_t base_type = GetOperandTypeId(_, inst, 2);
  167. if (base_type != result_type)
  168. return _.diag(SPV_ERROR_INVALID_DATA)
  169. << "Expected Base Type to be equal to Result Type: "
  170. << spvOpcodeString(opcode);
  171. break;
  172. }
  173. case SpvOpBitCount: {
  174. if (!_.IsIntScalarType(result_type) && !_.IsIntVectorType(result_type))
  175. return _.diag(SPV_ERROR_INVALID_DATA)
  176. << "Expected int scalar or vector type as Result Type: "
  177. << spvOpcodeString(opcode);
  178. const uint32_t base_type = GetOperandTypeId(_, inst, 2);
  179. if (!base_type ||
  180. (!_.IsIntScalarType(base_type) && !_.IsIntVectorType(base_type)))
  181. return _.diag(SPV_ERROR_INVALID_DATA)
  182. << "Expected Base Type to be int scalar or vector: "
  183. << spvOpcodeString(opcode);
  184. const uint32_t base_dimension = _.GetDimension(base_type);
  185. const uint32_t result_dimension = _.GetDimension(result_type);
  186. if (base_dimension != result_dimension)
  187. return _.diag(SPV_ERROR_INVALID_DATA)
  188. << "Expected Base dimension to be equal to Result Type "
  189. "dimension: "
  190. << spvOpcodeString(opcode);
  191. break;
  192. }
  193. default:
  194. break;
  195. }
  196. return SPV_SUCCESS;
  197. }
  198. } // namespace libspirv