• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2018 The Khronos Group Inc.
2 // Copyright (c) 2018 Valve Corporation
3 // Copyright (c) 2018 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 #ifndef LIBSPIRV_OPT_INSTRUMENT_PASS_H_
18 #define LIBSPIRV_OPT_INSTRUMENT_PASS_H_
19 
20 #include <list>
21 #include <memory>
22 #include <vector>
23 
24 #include "source/opt/ir_builder.h"
25 #include "source/opt/pass.h"
26 #include "spirv-tools/instrument.hpp"
27 
28 // This is a base class to assist in the creation of passes which instrument
29 // shader modules. More specifically, passes which replace instructions with a
30 // larger and more capable set of instructions. Commonly, these new
31 // instructions will add testing of operands and execute different
32 // instructions depending on the outcome, including outputting of debug
33 // information into a buffer created especially for that purpose.
34 //
35 // This class contains helper functions to create an InstProcessFunction,
36 // which is the heart of any derived class implementing a specific
37 // instrumentation pass. It takes an instruction as an argument, decides
38 // if it should be instrumented, and generates code to replace it. This class
39 // also supplies function InstProcessEntryPointCallTree which applies the
40 // InstProcessFunction to every reachable instruction in a module and replaces
41 // the instruction with new instructions if generated.
42 //
43 // Chief among the helper functions are output code generation functions,
44 // used to generate code in the shader which writes data to output buffers
45 // associated with that validation. Currently one such function,
46 // GenDebugStreamWrite, exists. Other such functions may be added in the
47 // future. Each is accompanied by documentation describing the format of
48 // its output buffer.
49 //
50 // A validation pass may read or write multiple buffers. All such buffers
51 // are located in a single debug descriptor set whose index is passed at the
52 // creation of the instrumentation pass. The bindings of the buffers used by
53 // a validation pass are permanently assigned and fixed and documented by
54 // the kDebugOutput* static consts.
55 
56 namespace spvtools {
57 namespace opt {
58 
59 class InstrumentPass : public Pass {
60   using cbb_ptr = const BasicBlock*;
61 
62  public:
63   using InstProcessFunction =
64       std::function<void(BasicBlock::iterator, UptrVectorIterator<BasicBlock>,
65                          uint32_t, std::vector<std::unique_ptr<BasicBlock>>*)>;
66 
67   ~InstrumentPass() override = default;
68 
GetPreservedAnalyses()69   IRContext::Analysis GetPreservedAnalyses() override {
70     return IRContext::kAnalysisDefUse | IRContext::kAnalysisDecorations |
71            IRContext::kAnalysisCombinators | IRContext::kAnalysisNameMap |
72            IRContext::kAnalysisBuiltinVarId | IRContext::kAnalysisConstants;
73   }
74 
75  protected:
76   // Create instrumentation pass for |validation_id| which utilizes descriptor
77   // set |desc_set| for debug input and output buffers and writes |shader_id|
78   // into debug output records. |opt_direct_reads| indicates that the pass
79   // will see direct input buffer reads and should prepare to optimize them.
InstrumentPass(uint32_t desc_set,uint32_t shader_id,bool opt_direct_reads,bool use_stage_info)80   InstrumentPass(uint32_t desc_set, uint32_t shader_id, bool opt_direct_reads,
81                  bool use_stage_info)
82       : Pass(),
83         desc_set_(desc_set),
84         shader_id_(shader_id),
85         opt_direct_reads_(opt_direct_reads),
86         use_stage_info_(use_stage_info) {}
87 
88   // Initialize state for instrumentation of module.
89   void InitializeInstrument();
90 
91   // Call |pfn| on all instructions in all functions in the call tree of the
92   // entry points in |module|. If code is generated for an instruction, replace
93   // the instruction's block with the new blocks that are generated. Continue
94   // processing at the top of the last new block.
95   bool InstProcessEntryPointCallTree(InstProcessFunction& pfn);
96 
97   // Move all code in |ref_block_itr| preceding the instruction |ref_inst_itr|
98   // to be instrumented into block |new_blk_ptr|.
99   void MovePreludeCode(BasicBlock::iterator ref_inst_itr,
100                        UptrVectorIterator<BasicBlock> ref_block_itr,
101                        std::unique_ptr<BasicBlock>* new_blk_ptr);
102 
103   // Move all code in |ref_block_itr| succeeding the instruction |ref_inst_itr|
104   // to be instrumented into block |new_blk_ptr|.
105   void MovePostludeCode(UptrVectorIterator<BasicBlock> ref_block_itr,
106                         BasicBlock* new_blk_ptr);
107 
108   // Return true if all instructions in |ids| are constants or spec constants.
109   bool AllConstant(const std::vector<uint32_t>& ids);
110 
111   uint32_t GenReadFunctionCall(uint32_t return_id, uint32_t func_id,
112                                const std::vector<uint32_t>& args,
113                                InstructionBuilder* builder);
114 
115   // Generate code to convert integer |value_id| to 32bit, if needed. Return
116   // an id to the 32bit equivalent.
117   uint32_t Gen32BitCvtCode(uint32_t value_id, InstructionBuilder* builder);
118 
119   // Generate code to cast integer |value_id| to 32bit unsigned, if needed.
120   // Return an id to the Uint equivalent.
121   uint32_t GenUintCastCode(uint32_t value_id, InstructionBuilder* builder);
122 
123   std::unique_ptr<Function> StartFunction(
124       uint32_t func_id, const analysis::Type* return_type,
125       const std::vector<const analysis::Type*>& param_types);
126 
127   std::vector<uint32_t> AddParameters(
128       Function& func, const std::vector<const analysis::Type*>& param_types);
129 
130   std::unique_ptr<Instruction> EndFunction();
131 
132   // Return new label.
133   std::unique_ptr<Instruction> NewLabel(uint32_t label_id);
134 
135   // Set the name function parameter or local variable
136   std::unique_ptr<Instruction> NewName(uint32_t id,
137                                        const std::string& name_str);
138 
139   // Return id for 32-bit unsigned type
140   uint32_t GetUintId();
141 
142   // Return id for 64-bit unsigned type
143   uint32_t GetUint64Id();
144 
145   // Return id for 8-bit unsigned type
146   uint32_t GetUint8Id();
147 
148   // Return id for 32-bit unsigned type
149   uint32_t GetBoolId();
150 
151   // Return id for void type
152   uint32_t GetVoidId();
153 
154   // Get registered type structures
155   analysis::Integer* GetInteger(uint32_t width, bool is_signed);
156   analysis::Struct* GetStruct(const std::vector<const analysis::Type*>& fields);
157   analysis::RuntimeArray* GetRuntimeArray(const analysis::Type* element);
158   analysis::Array* GetArray(const analysis::Type* element, uint32_t size);
159   analysis::Function* GetFunction(
160       const analysis::Type* return_val,
161       const std::vector<const analysis::Type*>& args);
162 
163   // Return pointer to type for runtime array of uint
164   analysis::RuntimeArray* GetUintXRuntimeArrayType(
165       uint32_t width, analysis::RuntimeArray** rarr_ty);
166 
167   // Return pointer to type for runtime array of uint
168   analysis::RuntimeArray* GetUintRuntimeArrayType(uint32_t width);
169 
170   // Add storage buffer extension if needed
171   void AddStorageBufferExt();
172 
173   // Return id for 32-bit float type
174   uint32_t GetFloatId();
175 
176   // Return id for v4float type
177   uint32_t GetVec4FloatId();
178 
179   // Return id for uint vector type of |length|
180   uint32_t GetVecUintId(uint32_t length);
181 
182   // Return id for v4uint type
183   uint32_t GetVec4UintId();
184 
185   // Return id for v3uint type
186   uint32_t GetVec3UintId();
187 
188   // Split block |block_itr| into two new blocks where the second block
189   // contains |inst_itr| and place in |new_blocks|.
190   void SplitBlock(BasicBlock::iterator inst_itr,
191                   UptrVectorIterator<BasicBlock> block_itr,
192                   std::vector<std::unique_ptr<BasicBlock>>* new_blocks);
193 
194   // Apply instrumentation function |pfn| to every instruction in |func|.
195   // If code is generated for an instruction, replace the instruction's
196   // block with the new blocks that are generated. Continue processing at the
197   // top of the last new block.
198   virtual bool InstrumentFunction(Function* func, uint32_t stage_idx,
199                                   InstProcessFunction& pfn);
200 
201   // Call |pfn| on all functions in the call tree of the function
202   // ids in |roots|.
203   bool InstProcessCallTreeFromRoots(InstProcessFunction& pfn,
204                                     std::queue<uint32_t>* roots,
205                                     uint32_t stage_idx);
206 
207   // Generate instructions into |builder| which will load |var_id| and return
208   // its result id.
209   uint32_t GenVarLoad(uint32_t var_id, InstructionBuilder* builder);
210 
211   uint32_t GenStageInfo(uint32_t stage_idx, InstructionBuilder* builder);
212 
213   // Return true if instruction must be in the same block that its result
214   // is used.
215   bool IsSameBlockOp(const Instruction* inst) const;
216 
217   // Clone operands which must be in same block as consumer instructions.
218   // Look in same_blk_pre for instructions that need cloning. Look in
219   // same_blk_post for instructions already cloned. Add cloned instruction
220   // to same_blk_post.
221   void CloneSameBlockOps(
222       std::unique_ptr<Instruction>* inst,
223       std::unordered_map<uint32_t, uint32_t>* same_blk_post,
224       std::unordered_map<uint32_t, Instruction*>* same_blk_pre,
225       BasicBlock* block_ptr);
226 
227   // Update phis in succeeding blocks to point to new last block
228   void UpdateSucceedingPhis(
229       std::vector<std::unique_ptr<BasicBlock>>& new_blocks);
230 
231   // Debug descriptor set index
232   uint32_t desc_set_;
233 
234   // Shader module ID written into output record
235   uint32_t shader_id_;
236 
237   // Map from function id to function pointer.
238   std::unordered_map<uint32_t, Function*> id2function_;
239 
240   // Map from block's label id to block. TODO(dnovillo): This is superfluous wrt
241   // CFG. It has functionality not present in CFG. Consolidate.
242   std::unordered_map<uint32_t, BasicBlock*> id2block_;
243 
244   // Map from instruction's unique id to offset in original file.
245   std::unordered_map<uint32_t, uint32_t> uid2offset_;
246 
247   // id for debug output function
248   std::unordered_map<uint32_t, uint32_t> param2output_func_id_;
249 
250   // ids for debug input functions
251   std::unordered_map<uint32_t, uint32_t> param2input_func_id_;
252 
253   // id for 32-bit float type
254   uint32_t float_id_{0};
255 
256   // id for v4float type
257   uint32_t v4float_id_{0};
258 
259   // id for v4uint type
260   uint32_t v4uint_id_{0};
261 
262   // id for v3uint type
263   uint32_t v3uint_id_{0};
264 
265   // id for 32-bit unsigned type
266   uint32_t uint_id_{0};
267 
268   // id for 64-bit unsigned type
269   uint32_t uint64_id_{0};
270 
271   // id for 8-bit unsigned type
272   uint32_t uint8_id_{0};
273 
274   // id for bool type
275   uint32_t bool_id_{0};
276 
277   // id for void type
278   uint32_t void_id_{0};
279 
280   // boolean to remember storage buffer extension
281   bool storage_buffer_ext_defined_{false};
282 
283   // runtime array of uint type
284   analysis::RuntimeArray* uint64_rarr_ty_{nullptr};
285 
286   // runtime array of uint type
287   analysis::RuntimeArray* uint32_rarr_ty_{nullptr};
288 
289   // Pre-instrumentation same-block insts
290   std::unordered_map<uint32_t, Instruction*> same_block_pre_;
291 
292   // Post-instrumentation same-block op ids
293   std::unordered_map<uint32_t, uint32_t> same_block_post_;
294 
295   // Map function calls to result id. Clear for every function.
296   // This is for debug input reads with constant arguments that
297   // have been generated into the first block of the function.
298   // This mechanism is used to avoid multiple identical debug
299   // input buffer reads.
300   struct vector_hash_ {
operatorvector_hash_301     std::size_t operator()(const std::vector<uint32_t>& v) const {
302       std::size_t hash = v.size();
303       for (auto& u : v) {
304         hash ^= u + 0x9e3779b9 + (hash << 11) + (hash >> 21);
305       }
306       return hash;
307     }
308   };
309   std::unordered_map<std::vector<uint32_t>, uint32_t, vector_hash_> call2id_;
310 
311   // Function currently being instrumented
312   Function* curr_func_{nullptr};
313 
314   // Optimize direct debug input buffer reads. Specifically, move all such
315   // reads with constant args to first block and reuse them.
316   const bool opt_direct_reads_;
317 
318   // Set true if the instrumentation needs to know the current stage.
319   // Note that this does not work with multi-stage modules.
320   const bool use_stage_info_;
321 };
322 
323 }  // namespace opt
324 }  // namespace spvtools
325 
326 #endif  // LIBSPIRV_OPT_INSTRUMENT_PASS_H_
327