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