• 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(7, 23, kInstValidationIdBuffAddr) {}
33   // For all other interfaces
InstBuffAddrCheckPass(uint32_t desc_set,uint32_t shader_id)34   InstBuffAddrCheckPass(uint32_t desc_set, uint32_t shader_id)
35       : InstrumentPass(desc_set, shader_id, kInstValidationIdBuffAddr) {}
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 alignment of type |type_id|. Must be int, float, vector,
46   // matrix, struct, array or physical pointer. Uses std430 alignment.
47   uint32_t GetTypeAlignment(uint32_t type_id);
48 
49   // Return byte length of type |type_id|. Must be int, float, vector, matrix,
50   // struct, array or physical pointer. Uses std430 alignment and sizes.
51   uint32_t GetTypeLength(uint32_t type_id);
52 
53   // Add |type_id| param to |input_func| and add id to |param_vec|.
54   void AddParam(uint32_t type_id, std::vector<uint32_t>* param_vec,
55                 std::unique_ptr<Function>* input_func);
56 
57   // Return id for search and test function. Generate it if not already gen'd.
58   uint32_t GetSearchAndTestFuncId();
59 
60   // Generate code into |builder| to do search of the BDA debug input buffer
61   // for the buffer used by |ref_inst| and test that all bytes of reference
62   // are within the buffer. Returns id of boolean value which is true if
63   // search and test is successful, false otherwise.
64   uint32_t GenSearchAndTest(Instruction* ref_inst, InstructionBuilder* builder,
65                             uint32_t* ref_uptr_id);
66 
67   // This function does checking instrumentation on a single
68   // instruction which references through a physical storage buffer address.
69   // GenBuffAddrCheckCode generates code that checks that all bytes that
70   // are referenced fall within a buffer that was queried via
71   // the Vulkan API call vkGetBufferDeviceAddressEXT().
72   //
73   // The function is designed to be passed to
74   // InstrumentPass::InstProcessEntryPointCallTree(), which applies the
75   // function to each instruction in a module and replaces the instruction
76   // with instrumented code if warranted.
77   //
78   // If |ref_inst_itr| is a physical storage buffer reference, return in
79   // |new_blocks| the result of instrumenting it with validation code within
80   // its block at |ref_block_itr|.  The validation code first executes a check
81   // for the specific condition called for. If the check passes, it executes
82   // the remainder of the reference, otherwise writes a record to the debug
83   // output buffer stream including |function_idx, instruction_idx, stage_idx|
84   // and replaces the reference with the null value of the original type. The
85   // block at |ref_block_itr| can just be replaced with the blocks in
86   // |new_blocks|, which will contain at least two blocks. The last block will
87   // comprise all instructions following |ref_inst_itr|,
88   // preceded by a phi instruction if needed.
89   //
90   // This instrumentation function utilizes GenDebugStreamWrite() to write its
91   // error records. The validation-specific part of the error record will
92   // have the format:
93   //
94   //    Validation Error Code (=kInstErrorBuffAddr)
95   //    Buffer Address (lowest 32 bits)
96   //    Buffer Address (highest 32 bits)
97   //
98   void GenBuffAddrCheckCode(
99       BasicBlock::iterator ref_inst_itr,
100       UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx,
101       std::vector<std::unique_ptr<BasicBlock>>* new_blocks);
102 
103   // Return true if |ref_inst| is a physical buffer address reference, false
104   // otherwise.
105   bool IsPhysicalBuffAddrReference(Instruction* ref_inst);
106 
107   // Clone original reference |ref_inst| into |builder| and return id of result
108   uint32_t CloneOriginalReference(Instruction* ref_inst,
109                                   InstructionBuilder* builder);
110 
111   // Generate instrumentation code for boolean test result |check_id|,
112   // adding new blocks to |new_blocks|. Generate conditional branch to valid
113   // or invalid reference blocks. Generate valid reference block which does
114   // original reference |ref_inst|. Then generate invalid reference block which
115   // writes debug error output utilizing |ref_inst|, |error_id| and
116   // |stage_idx|. Generate merge block for valid and invalid reference blocks.
117   // Kill original reference.
118   void GenCheckCode(uint32_t check_id, uint32_t error_id, uint32_t length_id,
119                     uint32_t stage_idx, Instruction* ref_inst,
120                     std::vector<std::unique_ptr<BasicBlock>>* new_blocks);
121 
122   // Initialize state for instrumenting physical buffer address checking
123   void InitInstBuffAddrCheck();
124 
125   // Apply GenBuffAddrCheckCode to every instruction in module.
126   Pass::Status ProcessImpl();
127 
128   // Id of search and test function, if already gen'd, else zero.
129   uint32_t search_test_func_id_;
130 };
131 
132 }  // namespace opt
133 }  // namespace spvtools
134 
135 #endif  // LIBSPIRV_OPT_INST_BUFFER_ADDRESS_PASS_H_
136