interp_fixup_pass.cpp 4.1 KB

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