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