validate_derivatives.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 derivative SPIR-V instructions.
  15. #include "source/val/validate.h"
  16. #include <string>
  17. #include "source/diagnostic.h"
  18. #include "source/opcode.h"
  19. #include "source/val/instruction.h"
  20. #include "source/val/validation_state.h"
  21. namespace spvtools {
  22. namespace val {
  23. // Validates correctness of derivative instructions.
  24. spv_result_t DerivativesPass(ValidationState_t& _, const Instruction* inst) {
  25. const SpvOp opcode = inst->opcode();
  26. const uint32_t result_type = inst->type_id();
  27. switch (opcode) {
  28. case SpvOpDPdx:
  29. case SpvOpDPdy:
  30. case SpvOpFwidth:
  31. case SpvOpDPdxFine:
  32. case SpvOpDPdyFine:
  33. case SpvOpFwidthFine:
  34. case SpvOpDPdxCoarse:
  35. case SpvOpDPdyCoarse:
  36. case SpvOpFwidthCoarse: {
  37. if (!_.IsFloatScalarOrVectorType(result_type)) {
  38. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  39. << "Expected Result Type to be float scalar or vector type: "
  40. << spvOpcodeString(opcode);
  41. }
  42. if (!_.ContainsSizedIntOrFloatType(result_type, SpvOpTypeFloat, 32)) {
  43. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  44. << "Result type component width must be 32 bits";
  45. }
  46. const uint32_t p_type = _.GetOperandTypeId(inst, 2);
  47. if (p_type != result_type) {
  48. return _.diag(SPV_ERROR_INVALID_DATA, inst)
  49. << "Expected P type and Result Type to be the same: "
  50. << spvOpcodeString(opcode);
  51. }
  52. _.function(inst->function()->id())
  53. ->RegisterExecutionModelLimitation([opcode](SpvExecutionModel model,
  54. std::string* message) {
  55. if (model != SpvExecutionModelFragment &&
  56. model != SpvExecutionModelGLCompute) {
  57. if (message) {
  58. *message =
  59. std::string(
  60. "Derivative instructions require Fragment or GLCompute "
  61. "execution model: ") +
  62. spvOpcodeString(opcode);
  63. }
  64. return false;
  65. }
  66. return true;
  67. });
  68. _.function(inst->function()->id())
  69. ->RegisterLimitation([opcode](const ValidationState_t& state,
  70. const Function* entry_point,
  71. std::string* message) {
  72. const auto* models = state.GetExecutionModels(entry_point->id());
  73. const auto* modes = state.GetExecutionModes(entry_point->id());
  74. if (models->find(SpvExecutionModelGLCompute) != models->end() &&
  75. modes->find(SpvExecutionModeDerivativeGroupLinearNV) ==
  76. modes->end() &&
  77. modes->find(SpvExecutionModeDerivativeGroupQuadsNV) ==
  78. modes->end()) {
  79. if (message) {
  80. *message = std::string(
  81. "Derivative instructions require "
  82. "DerivativeGroupQuadsNV "
  83. "or DerivativeGroupLinearNV execution mode for "
  84. "GLCompute execution model: ") +
  85. spvOpcodeString(opcode);
  86. }
  87. return false;
  88. }
  89. return true;
  90. });
  91. break;
  92. }
  93. default:
  94. break;
  95. }
  96. return SPV_SUCCESS;
  97. }
  98. } // namespace val
  99. } // namespace spvtools