• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2019 The Khronos Group Inc.
2 // Copyright (c) 2019 Valve Corporation
3 // Copyright (c) 2019 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_INST_BUFFER_ADDRESS_PASS_H_
18 #define LIBSPIRV_OPT_INST_BUFFER_ADDRESS_PASS_H_
19 
20 #include "instrument_pass.h"
21 
22 namespace spvtools {
23 namespace opt {
24 
25 // This class/pass is designed to support the GPU-assisted validation layer of
26 // the Buffer Device Address (BDA) extension in
27 // https://github.com/KhronosGroup/Vulkan-ValidationLayers. The internal and
28 // external design of this class may change as the layer evolves.
29 class InstBuffAddrCheckPass : public InstrumentPass {
30  public:
31   // For test harness only
InstBuffAddrCheckPass()32   InstBuffAddrCheckPass() : InstrumentPass(0, 23, false, true) {}
33   // For all other interfaces
InstBuffAddrCheckPass(uint32_t shader_id)34   InstBuffAddrCheckPass(uint32_t shader_id)
35       : InstrumentPass(0, shader_id, false, true) {}
36 
37   ~InstBuffAddrCheckPass() override = default;
38 
39   // See optimizer.hpp for pass user documentation.
40   Status Process() override;
41 
name()42   const char* name() const override { return "inst-buff-addr-check-pass"; }
43 
44  private:
45   // Return byte length of type |type_id|. Must be int, float, vector, matrix,
46   // struct, array or physical pointer. Uses std430 alignment and sizes.
47   uint32_t GetTypeLength(uint32_t type_id);
48 
49   // Add |type_id| param to |input_func| and add id to |param_vec|.
50   void AddParam(uint32_t type_id, std::vector<uint32_t>* param_vec,
51                 std::unique_ptr<Function>* input_func);
52 
53   // Return id for search and test function. Generate it if not already gen'd.
54   uint32_t GetSearchAndTestFuncId();
55 
56   // Generate code into |builder| to do search of the BDA debug input buffer
57   // for the buffer used by |ref_inst| and test that all bytes of reference
58   // are within the buffer. Returns id of boolean value which is true if
59   // search and test is successful, false otherwise.
60   uint32_t GenSearchAndTest(Instruction* ref_inst, InstructionBuilder* builder,
61                             uint32_t* ref_uptr_id, uint32_t stage_idx);
62 
63   // This function does checking instrumentation on a single
64   // instruction which references through a physical storage buffer address.
65   // GenBuffAddrCheckCode generates code that checks that all bytes that
66   // are referenced fall within a buffer that was queried via
67   // the Vulkan API call vkGetBufferDeviceAddressEXT().
68   //
69   // The function is designed to be passed to
70   // InstrumentPass::InstProcessEntryPointCallTree(), which applies the
71   // function to each instruction in a module and replaces the instruction
72   // with instrumented code if warranted.
73   //
74   // If |ref_inst_itr| is a physical storage buffer reference, return in
75   // |new_blocks| the result of instrumenting it with validation code within
76   // its block at |ref_block_itr|.  The validation code first executes a check
77   // for the specific condition called for. If the check passes, it executes
78   // the remainder of the reference, otherwise writes a record to the debug
79   // output buffer stream including |function_idx, instruction_idx, stage_idx|
80   // and replaces the reference with the null value of the original type. The
81   // block at |ref_block_itr| can just be replaced with the blocks in
82   // |new_blocks|, which will contain at least two blocks. The last block will
83   // comprise all instructions following |ref_inst_itr|,
84   // preceded by a phi instruction if needed.
85   //
86   // This instrumentation function utilizes GenDebugStreamWrite() to write its
87   // error records. The validation-specific part of the error record will
88   // have the format:
89   //
90   //    Validation Error Code (=kInstErrorBuffAddr)
91   //    Buffer Address (lowest 32 bits)
92   //    Buffer Address (highest 32 bits)
93   //
94   void GenBuffAddrCheckCode(
95       BasicBlock::iterator ref_inst_itr,
96       UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx,
97       std::vector<std::unique_ptr<BasicBlock>>* new_blocks);
98 
99   // Return true if |ref_inst| is a physical buffer address reference, false
100   // otherwise.
101   bool IsPhysicalBuffAddrReference(Instruction* ref_inst);
102 
103   // Clone original reference |ref_inst| into |builder| and return id of result
104   uint32_t CloneOriginalReference(Instruction* ref_inst,
105                                   InstructionBuilder* builder);
106 
107   // Generate instrumentation code for boolean test result |check_id|,
108   // adding new blocks to |new_blocks|. Generate conditional branch to valid
109   // or invalid reference blocks. Generate valid reference block which does
110   // original reference |ref_inst|. Then generate invalid reference block which
111   // writes debug error output utilizing |ref_inst|, |error_id| and
112   // |stage_idx|. Generate merge block for valid and invalid reference blocks.
113   // Kill original reference.
114   void GenCheckCode(uint32_t check_id, Instruction* ref_inst,
115                     std::vector<std::unique_ptr<BasicBlock>>* new_blocks);
116 
117   // Initialize state for instrumenting physical buffer address checking
118   void InitInstBuffAddrCheck();
119 
120   // Apply GenBuffAddrCheckCode to every instruction in module.
121   Pass::Status ProcessImpl();
122 
123   // Id of search and test function, if already gen'd, else zero.
124   uint32_t search_test_func_id_;
125 };
126 
127 }  // namespace opt
128 }  // namespace spvtools
129 
130 #endif  // LIBSPIRV_OPT_INST_BUFFER_ADDRESS_PASS_H_
131