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