interp_fixup_pass.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2021 The Khronos Group Inc.
  2. // Copyright (c) 2021 Valve Corporation
  3. // Copyright (c) 2021 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include "source/opt/interp_fixup_pass.h"
  17. #include <set>
  18. #include <string>
  19. #include "ir_builder.h"
  20. #include "source/opt/ir_context.h"
  21. #include "type_manager.h"
  22. namespace spvtools {
  23. namespace opt {
  24. namespace {
  25. // Input Operand Indices
  26. static const int kSpvVariableStorageClassInIdx = 0;
  27. // Avoid unused variable warning/error on Linux
  28. #ifndef NDEBUG
  29. #define USE_ASSERT(x) assert(x)
  30. #else
  31. #define USE_ASSERT(x) ((void)(x))
  32. #endif
  33. // Folding rule function which attempts to replace |op(OpLoad(a),...)|
  34. // by |op(a,...)|, where |op| is one of the GLSLstd450 InterpolateAt*
  35. // instructions. Returns true if replaced, false otherwise.
  36. bool ReplaceInternalInterpolate(IRContext* ctx, Instruction* inst,
  37. const std::vector<const analysis::Constant*>&) {
  38. uint32_t glsl450_ext_inst_id =
  39. ctx->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  40. assert(glsl450_ext_inst_id != 0);
  41. uint32_t ext_opcode = inst->GetSingleWordInOperand(1);
  42. uint32_t op1_id = inst->GetSingleWordInOperand(2);
  43. Instruction* load_inst = ctx->get_def_use_mgr()->GetDef(op1_id);
  44. if (load_inst->opcode() != SpvOpLoad) return false;
  45. Instruction* base_inst = load_inst->GetBaseAddress();
  46. USE_ASSERT(base_inst->opcode() == SpvOpVariable &&
  47. base_inst->GetSingleWordInOperand(kSpvVariableStorageClassInIdx) ==
  48. SpvStorageClassInput &&
  49. "unexpected interpolant in InterpolateAt*");
  50. uint32_t ptr_id = load_inst->GetSingleWordInOperand(0);
  51. uint32_t op2_id = (ext_opcode != GLSLstd450InterpolateAtCentroid)
  52. ? inst->GetSingleWordInOperand(3)
  53. : 0;
  54. Instruction::OperandList new_operands;
  55. new_operands.push_back({SPV_OPERAND_TYPE_ID, {glsl450_ext_inst_id}});
  56. new_operands.push_back(
  57. {SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {ext_opcode}});
  58. new_operands.push_back({SPV_OPERAND_TYPE_ID, {ptr_id}});
  59. if (op2_id != 0) new_operands.push_back({SPV_OPERAND_TYPE_ID, {op2_id}});
  60. inst->SetInOperands(std::move(new_operands));
  61. ctx->UpdateDefUse(inst);
  62. return true;
  63. }
  64. class InterpFoldingRules : public FoldingRules {
  65. public:
  66. explicit InterpFoldingRules(IRContext* ctx) : FoldingRules(ctx) {}
  67. protected:
  68. virtual void AddFoldingRules() override {
  69. uint32_t extension_id =
  70. context()->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  71. if (extension_id != 0) {
  72. ext_rules_[{extension_id, GLSLstd450InterpolateAtCentroid}].push_back(
  73. ReplaceInternalInterpolate);
  74. ext_rules_[{extension_id, GLSLstd450InterpolateAtSample}].push_back(
  75. ReplaceInternalInterpolate);
  76. ext_rules_[{extension_id, GLSLstd450InterpolateAtOffset}].push_back(
  77. ReplaceInternalInterpolate);
  78. }
  79. }
  80. };
  81. class InterpConstFoldingRules : public ConstantFoldingRules {
  82. public:
  83. InterpConstFoldingRules(IRContext* ctx) : ConstantFoldingRules(ctx) {}
  84. protected:
  85. virtual void AddFoldingRules() override {}
  86. };
  87. } // namespace
  88. Pass::Status InterpFixupPass::Process() {
  89. bool changed = false;
  90. // Traverse the body of the functions to replace instructions that require
  91. // the extensions.
  92. InstructionFolder folder(
  93. context(),
  94. std::unique_ptr<InterpFoldingRules>(new InterpFoldingRules(context())),
  95. MakeUnique<InterpConstFoldingRules>(context()));
  96. for (Function& func : *get_module()) {
  97. func.ForEachInst([&changed, &folder](Instruction* inst) {
  98. if (folder.FoldInstruction(inst)) {
  99. changed = true;
  100. }
  101. });
  102. }
  103. return changed ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  104. }
  105. } // namespace opt
  106. } // namespace spvtools