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