interp_fixup_pass.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. constexpr int kSpvVariableStorageClassInIdx = 0;
  27. // Folding rule function which attempts to replace |op(OpLoad(a),...)|
  28. // by |op(a,...)|, where |op| is one of the GLSLstd450 InterpolateAt*
  29. // instructions. Returns true if replaced, false otherwise.
  30. bool ReplaceInternalInterpolate(IRContext* ctx, Instruction* inst,
  31. const std::vector<const analysis::Constant*>&) {
  32. uint32_t glsl450_ext_inst_id =
  33. ctx->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  34. assert(glsl450_ext_inst_id != 0);
  35. uint32_t ext_opcode = inst->GetSingleWordInOperand(1);
  36. uint32_t op1_id = inst->GetSingleWordInOperand(2);
  37. Instruction* load_inst = ctx->get_def_use_mgr()->GetDef(op1_id);
  38. if (load_inst->opcode() != spv::Op::OpLoad) return false;
  39. Instruction* base_inst = load_inst->GetBaseAddress();
  40. USE_ASSERT(base_inst->opcode() == spv::Op::OpVariable &&
  41. spv::StorageClass(base_inst->GetSingleWordInOperand(
  42. kSpvVariableStorageClassInIdx)) == spv::StorageClass::Input &&
  43. "unexpected interpolant in InterpolateAt*");
  44. uint32_t ptr_id = load_inst->GetSingleWordInOperand(0);
  45. uint32_t op2_id = (ext_opcode != GLSLstd450InterpolateAtCentroid)
  46. ? inst->GetSingleWordInOperand(3)
  47. : 0;
  48. Instruction::OperandList new_operands;
  49. new_operands.push_back({SPV_OPERAND_TYPE_ID, {glsl450_ext_inst_id}});
  50. new_operands.push_back(
  51. {SPV_OPERAND_TYPE_EXTENSION_INSTRUCTION_NUMBER, {ext_opcode}});
  52. new_operands.push_back({SPV_OPERAND_TYPE_ID, {ptr_id}});
  53. if (op2_id != 0) new_operands.push_back({SPV_OPERAND_TYPE_ID, {op2_id}});
  54. inst->SetInOperands(std::move(new_operands));
  55. ctx->UpdateDefUse(inst);
  56. return true;
  57. }
  58. class InterpFoldingRules : public FoldingRules {
  59. public:
  60. explicit InterpFoldingRules(IRContext* ctx) : FoldingRules(ctx) {}
  61. protected:
  62. virtual void AddFoldingRules() override {
  63. uint32_t extension_id =
  64. context()->get_feature_mgr()->GetExtInstImportId_GLSLstd450();
  65. if (extension_id != 0) {
  66. ext_rules_[{extension_id, GLSLstd450InterpolateAtCentroid}].push_back(
  67. ReplaceInternalInterpolate);
  68. ext_rules_[{extension_id, GLSLstd450InterpolateAtSample}].push_back(
  69. ReplaceInternalInterpolate);
  70. ext_rules_[{extension_id, GLSLstd450InterpolateAtOffset}].push_back(
  71. ReplaceInternalInterpolate);
  72. }
  73. }
  74. };
  75. class InterpConstFoldingRules : public ConstantFoldingRules {
  76. public:
  77. InterpConstFoldingRules(IRContext* ctx) : ConstantFoldingRules(ctx) {}
  78. protected:
  79. virtual void AddFoldingRules() override {}
  80. };
  81. } // namespace
  82. Pass::Status InterpFixupPass::Process() {
  83. bool changed = false;
  84. // Traverse the body of the functions to replace instructions that require
  85. // the extensions.
  86. InstructionFolder folder(
  87. context(),
  88. std::unique_ptr<InterpFoldingRules>(new InterpFoldingRules(context())),
  89. MakeUnique<InterpConstFoldingRules>(context()));
  90. for (Function& func : *get_module()) {
  91. func.ForEachInst([&changed, &folder](Instruction* inst) {
  92. if (folder.FoldInstruction(inst)) {
  93. changed = true;
  94. }
  95. });
  96. }
  97. return changed ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  98. }
  99. } // namespace opt
  100. } // namespace spvtools