| 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 |
#include "inst_bindless_check_pass.h" |
| 18 |
|
| 19 |
namespace { |
| 20 |
|
| 21 |
// Input Operand Indices |
| 22 |
static const int kSpvImageSampleImageIdInIdx = 0; |
| 23 |
static const int kSpvSampledImageImageIdInIdx = 0; |
| 24 |
static const int kSpvSampledImageSamplerIdInIdx = 1; |
| 25 |
static const int kSpvImageSampledImageIdInIdx = 0; |
| 26 |
static const int kSpvLoadPtrIdInIdx = 0; |
| 27 |
static const int kSpvAccessChainBaseIdInIdx = 0; |
| 28 |
static const int kSpvAccessChainIndex0IdInIdx = 1; |
| 29 |
static const int kSpvTypeArrayLengthIdInIdx = 1; |
| 30 |
static const int kSpvConstantValueInIdx = 0; |
| 31 |
static const int kSpvVariableStorageClassInIdx = 0; |
| 32 |
|
| 33 |
} // anonymous namespace |
| 34 |
|
| 35 |
// Avoid unused variable warning/error on Linux |
| 36 |
#ifndef NDEBUG |
| 37 |
#define USE_ASSERT(x) assert(x) |
| 38 |
#else |
| 39 |
#define USE_ASSERT(x) ((void)(x)) |
| 40 |
#endif |
| 41 |
|
| 42 |
namespace spvtools { |
| 43 |
namespace opt { |
| 44 |
|
| 45 |
uint32_t InstBindlessCheckPass::GenDebugReadLength( |
| 46 |
uint32_t var_id, InstructionBuilder* builder) { |
| 47 |
uint32_t desc_set_idx = |
| 48 |
var2desc_set_[var_id] + kDebugInputBindlessOffsetLengths; |
| 49 |
uint32_t desc_set_idx_id = builder->GetUintConstantId(desc_set_idx); |
| 50 |
uint32_t binding_idx_id = builder->GetUintConstantId(var2binding_[var_id]); |
| 51 |
return GenDebugDirectRead({desc_set_idx_id, binding_idx_id}, builder); |
| 52 |
} |
| 53 |
|
| 54 |
uint32_t InstBindlessCheckPass::GenDebugReadInit(uint32_t var_id, |
| 55 |
uint32_t desc_idx_id, |
| 56 |
InstructionBuilder* builder) { |
| 57 |
uint32_t binding_idx_id = builder->GetUintConstantId(var2binding_[var_id]); |
| 58 |
uint32_t u_desc_idx_id = GenUintCastCode(desc_idx_id, builder); |
| 59 |
// If desc index checking is not enabled, we know the offset of initialization |
| 60 |
// entries is 1, so we can avoid loading this value and just add 1 to the |
| 61 |
// descriptor set. |
| 62 |
if (!desc_idx_enabled_) { |
| 63 |
uint32_t desc_set_idx_id = |
| 64 |
builder->GetUintConstantId(var2desc_set_[var_id] + 1); |
| 65 |
return GenDebugDirectRead({desc_set_idx_id, binding_idx_id, u_desc_idx_id}, |
| 66 |
builder); |
| 67 |
} else { |
| 68 |
uint32_t desc_set_base_id = |
| 69 |
builder->GetUintConstantId(kDebugInputBindlessInitOffset); |
| 70 |
uint32_t desc_set_idx_id = |
| 71 |
builder->GetUintConstantId(var2desc_set_[var_id]); |
| 72 |
return GenDebugDirectRead( |
| 73 |
{desc_set_base_id, desc_set_idx_id, binding_idx_id, u_desc_idx_id}, |
| 74 |
builder); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
uint32_t InstBindlessCheckPass::CloneOriginalReference( |
| 79 |
ref_analysis* ref, InstructionBuilder* builder) { |
| 80 |
// If original is image based, start by cloning descriptor load |
| 81 |
uint32_t new_image_id = 0; |
| 82 |
if (ref->desc_load_id != 0) { |
| 83 |
Instruction* desc_load_inst = get_def_use_mgr()->GetDef(ref->desc_load_id); |
| 84 |
Instruction* new_load_inst = builder->AddLoad( |
| 85 |
desc_load_inst->type_id(), |
| 86 |
desc_load_inst->GetSingleWordInOperand(kSpvLoadPtrIdInIdx)); |
| 87 |
uid2offset_[new_load_inst->unique_id()] = |
| 88 |
uid2offset_[desc_load_inst->unique_id()]; |
| 89 |
uint32_t new_load_id = new_load_inst->result_id(); |
| 90 |
get_decoration_mgr()->CloneDecorations(desc_load_inst->result_id(), |
| 91 |
new_load_id); |
| 92 |
new_image_id = new_load_id; |
| 93 |
// Clone Image/SampledImage with new load, if needed |
| 94 |
if (ref->image_id != 0) { |
| 95 |
Instruction* image_inst = get_def_use_mgr()->GetDef(ref->image_id); |
| 96 |
if (image_inst->opcode() == SpvOp::SpvOpSampledImage) { |
| 97 |
Instruction* new_image_inst = builder->AddBinaryOp( |
| 98 |
image_inst->type_id(), SpvOpSampledImage, new_load_id, |
| 99 |
image_inst->GetSingleWordInOperand(kSpvSampledImageSamplerIdInIdx)); |
| 100 |
uid2offset_[new_image_inst->unique_id()] = |
| 101 |
uid2offset_[image_inst->unique_id()]; |
| 102 |
new_image_id = new_image_inst->result_id(); |
| 103 |
} else { |
| 104 |
assert(image_inst->opcode() == SpvOp::SpvOpImage && |
| 105 |
"expecting OpImage"); |
| 106 |
Instruction* new_image_inst = |
| 107 |
builder->AddUnaryOp(image_inst->type_id(), SpvOpImage, new_load_id); |
| 108 |
uid2offset_[new_image_inst->unique_id()] = |
| 109 |
uid2offset_[image_inst->unique_id()]; |
| 110 |
new_image_id = new_image_inst->result_id(); |
| 111 |
} |
| 112 |
get_decoration_mgr()->CloneDecorations(ref->image_id, new_image_id); |
| 113 |
} |
| 114 |
} |
| 115 |
// Clone original reference |
| 116 |
std::unique_ptr<Instruction> new_ref_inst(ref->ref_inst->Clone(context())); |
| 117 |
uint32_t ref_result_id = ref->ref_inst->result_id(); |
| 118 |
uint32_t new_ref_id = 0; |
| 119 |
if (ref_result_id != 0) { |
| 120 |
new_ref_id = TakeNextId(); |
| 121 |
new_ref_inst->SetResultId(new_ref_id); |
| 122 |
} |
| 123 |
// Update new ref with new image if created |
| 124 |
if (new_image_id != 0) |
| 125 |
new_ref_inst->SetInOperand(kSpvImageSampleImageIdInIdx, {new_image_id}); |
| 126 |
// Register new reference and add to new block |
| 127 |
Instruction* added_inst = builder->AddInstruction(std::move(new_ref_inst)); |
| 128 |
uid2offset_[added_inst->unique_id()] = |
| 129 |
uid2offset_[ref->ref_inst->unique_id()]; |
| 130 |
if (new_ref_id != 0) |
| 131 |
get_decoration_mgr()->CloneDecorations(ref_result_id, new_ref_id); |
| 132 |
return new_ref_id; |
| 133 |
} |
| 134 |
|
| 135 |
uint32_t InstBindlessCheckPass::GetImageId(Instruction* inst) { |
| 136 |
switch (inst->opcode()) { |
| 137 |
case SpvOp::SpvOpImageSampleImplicitLod: |
| 138 |
case SpvOp::SpvOpImageSampleExplicitLod: |
| 139 |
case SpvOp::SpvOpImageSampleDrefImplicitLod: |
| 140 |
case SpvOp::SpvOpImageSampleDrefExplicitLod: |
| 141 |
case SpvOp::SpvOpImageSampleProjImplicitLod: |
| 142 |
case SpvOp::SpvOpImageSampleProjExplicitLod: |
| 143 |
case SpvOp::SpvOpImageSampleProjDrefImplicitLod: |
| 144 |
case SpvOp::SpvOpImageSampleProjDrefExplicitLod: |
| 145 |
case SpvOp::SpvOpImageGather: |
| 146 |
case SpvOp::SpvOpImageDrefGather: |
| 147 |
case SpvOp::SpvOpImageQueryLod: |
| 148 |
case SpvOp::SpvOpImageSparseSampleImplicitLod: |
| 149 |
case SpvOp::SpvOpImageSparseSampleExplicitLod: |
| 150 |
case SpvOp::SpvOpImageSparseSampleDrefImplicitLod: |
| 151 |
case SpvOp::SpvOpImageSparseSampleDrefExplicitLod: |
| 152 |
case SpvOp::SpvOpImageSparseSampleProjImplicitLod: |
| 153 |
case SpvOp::SpvOpImageSparseSampleProjExplicitLod: |
| 154 |
case SpvOp::SpvOpImageSparseSampleProjDrefImplicitLod: |
| 155 |
case SpvOp::SpvOpImageSparseSampleProjDrefExplicitLod: |
| 156 |
case SpvOp::SpvOpImageSparseGather: |
| 157 |
case SpvOp::SpvOpImageSparseDrefGather: |
| 158 |
case SpvOp::SpvOpImageFetch: |
| 159 |
case SpvOp::SpvOpImageRead: |
| 160 |
case SpvOp::SpvOpImageQueryFormat: |
| 161 |
case SpvOp::SpvOpImageQueryOrder: |
| 162 |
case SpvOp::SpvOpImageQuerySizeLod: |
| 163 |
case SpvOp::SpvOpImageQuerySize: |
| 164 |
case SpvOp::SpvOpImageQueryLevels: |
| 165 |
case SpvOp::SpvOpImageQuerySamples: |
| 166 |
case SpvOp::SpvOpImageSparseFetch: |
| 167 |
case SpvOp::SpvOpImageSparseRead: |
| 168 |
case SpvOp::SpvOpImageWrite: |
| 169 |
return inst->GetSingleWordInOperand(kSpvImageSampleImageIdInIdx); |
| 170 |
default: |
| 171 |
break; |
| 172 |
} |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
Instruction* InstBindlessCheckPass::GetPointeeTypeInst(Instruction* ptr_inst) { |
| 177 |
uint32_t pte_ty_id = GetPointeeTypeId(ptr_inst); |
| 178 |
return get_def_use_mgr()->GetDef(pte_ty_id); |
| 179 |
} |
| 180 |
|
| 181 |
bool InstBindlessCheckPass::AnalyzeDescriptorReference(Instruction* ref_inst, |
| 182 |
ref_analysis* ref) { |
| 183 |
ref->ref_inst = ref_inst; |
| 184 |
if (ref_inst->opcode() == SpvOpLoad || ref_inst->opcode() == SpvOpStore) { |
| 185 |
ref->desc_load_id = 0; |
| 186 |
ref->ptr_id = ref_inst->GetSingleWordInOperand(kSpvLoadPtrIdInIdx); |
| 187 |
Instruction* ptr_inst = get_def_use_mgr()->GetDef(ref->ptr_id); |
| 188 |
if (ptr_inst->opcode() != SpvOp::SpvOpAccessChain) return false; |
| 189 |
ref->var_id = ptr_inst->GetSingleWordInOperand(kSpvAccessChainBaseIdInIdx); |
| 190 |
Instruction* var_inst = get_def_use_mgr()->GetDef(ref->var_id); |
| 191 |
if (var_inst->opcode() != SpvOp::SpvOpVariable) return false; |
| 192 |
uint32_t storage_class = |
| 193 |
var_inst->GetSingleWordInOperand(kSpvVariableStorageClassInIdx); |
| 194 |
switch (storage_class) { |
| 195 |
case SpvStorageClassUniform: |
| 196 |
case SpvStorageClassUniformConstant: |
| 197 |
case SpvStorageClassStorageBuffer: |
| 198 |
break; |
| 199 |
default: |
| 200 |
return false; |
| 201 |
break; |
| 202 |
} |
| 203 |
Instruction* desc_type_inst = GetPointeeTypeInst(var_inst); |
| 204 |
switch (desc_type_inst->opcode()) { |
| 205 |
case SpvOpTypeArray: |
| 206 |
case SpvOpTypeRuntimeArray: |
| 207 |
// A load through a descriptor array will have at least 3 operands. We |
| 208 |
// do not want to instrument loads of descriptors here which are part of |
| 209 |
// an image-based reference. |
| 210 |
if (ptr_inst->NumInOperands() < 3) return false; |
| 211 |
ref->desc_idx_id = |
| 212 |
ptr_inst->GetSingleWordInOperand(kSpvAccessChainIndex0IdInIdx); |
| 213 |
break; |
| 214 |
default: |
| 215 |
ref->desc_idx_id = 0; |
| 216 |
break; |
| 217 |
} |
| 218 |
return true; |
| 219 |
} |
| 220 |
// Reference is not load or store. If not an image-based reference, return. |
| 221 |
ref->image_id = GetImageId(ref_inst); |
| 222 |
if (ref->image_id == 0) return false; |
| 223 |
Instruction* image_inst = get_def_use_mgr()->GetDef(ref->image_id); |
| 224 |
Instruction* desc_load_inst = nullptr; |
| 225 |
if (image_inst->opcode() == SpvOp::SpvOpSampledImage) { |
| 226 |
ref->desc_load_id = |
| 227 |
image_inst->GetSingleWordInOperand(kSpvSampledImageImageIdInIdx); |
| 228 |
desc_load_inst = get_def_use_mgr()->GetDef(ref->desc_load_id); |
| 229 |
} else if (image_inst->opcode() == SpvOp::SpvOpImage) { |
| 230 |
ref->desc_load_id = |
| 231 |
image_inst->GetSingleWordInOperand(kSpvImageSampledImageIdInIdx); |
| 232 |
desc_load_inst = get_def_use_mgr()->GetDef(ref->desc_load_id); |
| 233 |
} else { |
| 234 |
ref->desc_load_id = ref->image_id; |
| 235 |
desc_load_inst = image_inst; |
| 236 |
ref->image_id = 0; |
| 237 |
} |
| 238 |
if (desc_load_inst->opcode() != SpvOp::SpvOpLoad) { |
| 239 |
// TODO(greg-lunarg): Handle additional possibilities? |
| 240 |
return false; |
| 241 |
} |
| 242 |
ref->ptr_id = desc_load_inst->GetSingleWordInOperand(kSpvLoadPtrIdInIdx); |
| 243 |
Instruction* ptr_inst = get_def_use_mgr()->GetDef(ref->ptr_id); |
| 244 |
if (ptr_inst->opcode() == SpvOp::SpvOpVariable) { |
| 245 |
ref->desc_idx_id = 0; |
| 246 |
ref->var_id = ref->ptr_id; |
| 247 |
} else if (ptr_inst->opcode() == SpvOp::SpvOpAccessChain) { |
| 248 |
if (ptr_inst->NumInOperands() != 2) { |
| 249 |
assert(false && "unexpected bindless index number"); |
| 250 |
return false; |
| 251 |
} |
| 252 |
ref->desc_idx_id = |
| 253 |
ptr_inst->GetSingleWordInOperand(kSpvAccessChainIndex0IdInIdx); |
| 254 |
ref->var_id = ptr_inst->GetSingleWordInOperand(kSpvAccessChainBaseIdInIdx); |
| 255 |
Instruction* var_inst = get_def_use_mgr()->GetDef(ref->var_id); |
| 256 |
if (var_inst->opcode() != SpvOpVariable) { |
| 257 |
assert(false && "unexpected bindless base"); |
| 258 |
return false; |
| 259 |
} |
| 260 |
} else { |
| 261 |
// TODO(greg-lunarg): Handle additional possibilities? |
| 262 |
return false; |
| 263 |
} |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
uint32_t InstBindlessCheckPass::FindStride(uint32_t ty_id, |
| 268 |
uint32_t stride_deco) { |
| 269 |
uint32_t stride = 0xdeadbeef; |
| 270 |
bool found = !get_decoration_mgr()->WhileEachDecoration( |
| 271 |
ty_id, stride_deco, [&stride](const Instruction& deco_inst) { |
| 272 |
stride = deco_inst.GetSingleWordInOperand(2u); |
| 273 |
return false; |
| 274 |
}); |
| 275 |
USE_ASSERT(found && "stride not found"); |
| 276 |
return stride; |
| 277 |
} |
| 278 |
|
| 279 |
uint32_t InstBindlessCheckPass::ByteSize(uint32_t ty_id) { |
| 280 |
analysis::TypeManager* type_mgr = context()->get_type_mgr(); |
| 281 |
const analysis::Type* sz_ty = type_mgr->GetType(ty_id); |
| 282 |
if (sz_ty->kind() == analysis::Type::kPointer) { |
| 283 |
// Assuming PhysicalStorageBuffer pointer |
| 284 |
return 8; |
| 285 |
} |
| 286 |
uint32_t size = 1; |
| 287 |
if (sz_ty->kind() == analysis::Type::kMatrix) { |
| 288 |
const analysis::Matrix* m_ty = sz_ty->AsMatrix(); |
| 289 |
size = m_ty->element_count() * size; |
| 290 |
uint32_t stride = FindStride(ty_id, SpvDecorationMatrixStride); |
| 291 |
if (stride != 0) return size * stride; |
| 292 |
sz_ty = m_ty->element_type(); |
| 293 |
} |
| 294 |
if (sz_ty->kind() == analysis::Type::kVector) { |
| 295 |
const analysis::Vector* v_ty = sz_ty->AsVector(); |
| 296 |
size = v_ty->element_count() * size; |
| 297 |
sz_ty = v_ty->element_type(); |
| 298 |
} |
| 299 |
switch (sz_ty->kind()) { |
| 300 |
case analysis::Type::kFloat: { |
| 301 |
const analysis::Float* f_ty = sz_ty->AsFloat(); |
| 302 |
size *= f_ty->width(); |
| 303 |
} break; |
| 304 |
case analysis::Type::kInteger: { |
| 305 |
const analysis::Integer* i_ty = sz_ty->AsInteger(); |
| 306 |
size *= i_ty->width(); |
| 307 |
} break; |
| 308 |
default: { assert(false && "unexpected type"); } break; |
| 309 |
} |
| 310 |
size /= 8; |
| 311 |
return size; |
| 312 |
} |
| 313 |
|
| 314 |
uint32_t InstBindlessCheckPass::GenLastByteIdx(ref_analysis* ref, |
| 315 |
InstructionBuilder* builder) { |
| 316 |
// Find outermost buffer type and its access chain index |
| 317 |
Instruction* var_inst = get_def_use_mgr()->GetDef(ref->var_id); |
| 318 |
Instruction* desc_ty_inst = GetPointeeTypeInst(var_inst); |
| 319 |
uint32_t buff_ty_id; |
| 320 |
uint32_t ac_in_idx = 1; |
| 321 |
switch (desc_ty_inst->opcode()) { |
| 322 |
case SpvOpTypeArray: |
| 323 |
case SpvOpTypeRuntimeArray: |
| 324 |
buff_ty_id = desc_ty_inst->GetSingleWordInOperand(0); |
| 325 |
++ac_in_idx; |
| 326 |
break; |
| 327 |
default: |
| 328 |
assert(desc_ty_inst->opcode() == SpvOpTypeStruct && |
| 329 |
"unexpected descriptor type"); |
| 330 |
buff_ty_id = desc_ty_inst->result_id(); |
| 331 |
break; |
| 332 |
} |
| 333 |
// Process remaining access chain indices |
| 334 |
Instruction* ac_inst = get_def_use_mgr()->GetDef(ref->ptr_id); |
| 335 |
uint32_t curr_ty_id = buff_ty_id; |
| 336 |
uint32_t sum_id = 0; |
| 337 |
while (ac_in_idx < ac_inst->NumInOperands()) { |
| 338 |
uint32_t curr_idx_id = ac_inst->GetSingleWordInOperand(ac_in_idx); |
| 339 |
Instruction* curr_idx_inst = get_def_use_mgr()->GetDef(curr_idx_id); |
| 340 |
Instruction* curr_ty_inst = get_def_use_mgr()->GetDef(curr_ty_id); |
| 341 |
uint32_t curr_offset_id = 0; |
| 342 |
switch (curr_ty_inst->opcode()) { |
| 343 |
case SpvOpTypeArray: |
| 344 |
case SpvOpTypeRuntimeArray: |
| 345 |
case SpvOpTypeMatrix: { |
| 346 |
// Get array/matrix stride and multiply by current index |
| 347 |
uint32_t stride_deco = (curr_ty_inst->opcode() == SpvOpTypeMatrix) |
| 348 |
? SpvDecorationMatrixStride |
| 349 |
: SpvDecorationArrayStride; |
| 350 |
uint32_t arr_stride = FindStride(curr_ty_id, stride_deco); |
| 351 |
uint32_t arr_stride_id = builder->GetUintConstantId(arr_stride); |
| 352 |
uint32_t curr_idx_32b_id = Gen32BitCvtCode(curr_idx_id, builder); |
| 353 |
Instruction* curr_offset_inst = builder->AddBinaryOp( |
| 354 |
GetUintId(), SpvOpIMul, arr_stride_id, curr_idx_32b_id); |
| 355 |
curr_offset_id = curr_offset_inst->result_id(); |
| 356 |
// Get element type for next step |
| 357 |
curr_ty_id = curr_ty_inst->GetSingleWordInOperand(0); |
| 358 |
} break; |
| 359 |
case SpvOpTypeVector: { |
| 360 |
// Stride is size of component type |
| 361 |
uint32_t comp_ty_id = curr_ty_inst->GetSingleWordInOperand(0u); |
| 362 |
uint32_t vec_stride = ByteSize(comp_ty_id); |
| 363 |
uint32_t vec_stride_id = builder->GetUintConstantId(vec_stride); |
| 364 |
uint32_t curr_idx_32b_id = Gen32BitCvtCode(curr_idx_id, builder); |
| 365 |
Instruction* curr_offset_inst = builder->AddBinaryOp( |
| 366 |
GetUintId(), SpvOpIMul, vec_stride_id, curr_idx_32b_id); |
| 367 |
curr_offset_id = curr_offset_inst->result_id(); |
| 368 |
// Get element type for next step |
| 369 |
curr_ty_id = comp_ty_id; |
| 370 |
} break; |
| 371 |
case SpvOpTypeStruct: { |
| 372 |
// Get buffer byte offset for the referenced member |
| 373 |
assert(curr_idx_inst->opcode() == SpvOpConstant && |
| 374 |
"unexpected struct index"); |
| 375 |
uint32_t member_idx = curr_idx_inst->GetSingleWordInOperand(0); |
| 376 |
uint32_t member_offset = 0xdeadbeef; |
| 377 |
bool found = !get_decoration_mgr()->WhileEachDecoration( |
| 378 |
curr_ty_id, SpvDecorationOffset, |
| 379 |
[&member_idx, &member_offset](const Instruction& deco_inst) { |
| 380 |
if (deco_inst.GetSingleWordInOperand(1u) != member_idx) |
| 381 |
return true; |
| 382 |
member_offset = deco_inst.GetSingleWordInOperand(3u); |
| 383 |
return false; |
| 384 |
}); |
| 385 |
USE_ASSERT(found && "member offset not found"); |
| 386 |
curr_offset_id = builder->GetUintConstantId(member_offset); |
| 387 |
// Get element type for next step |
| 388 |
curr_ty_id = curr_ty_inst->GetSingleWordInOperand(member_idx); |
| 389 |
} break; |
| 390 |
default: { assert(false && "unexpected non-composite type"); } break; |
| 391 |
} |
| 392 |
if (sum_id == 0) |
| 393 |
sum_id = curr_offset_id; |
| 394 |
else { |
| 395 |
Instruction* sum_inst = |
| 396 |
builder->AddBinaryOp(GetUintId(), SpvOpIAdd, sum_id, curr_offset_id); |
| 397 |
sum_id = sum_inst->result_id(); |
| 398 |
} |
| 399 |
++ac_in_idx; |
| 400 |
} |
| 401 |
// Add in offset of last byte of referenced object |
| 402 |
uint32_t bsize = ByteSize(curr_ty_id); |
| 403 |
uint32_t last = bsize - 1; |
| 404 |
uint32_t last_id = builder->GetUintConstantId(last); |
| 405 |
Instruction* sum_inst = |
| 406 |
builder->AddBinaryOp(GetUintId(), SpvOpIAdd, sum_id, last_id); |
| 407 |
return sum_inst->result_id(); |
| 408 |
} |
| 409 |
|
| 410 |
void InstBindlessCheckPass::GenCheckCode( |
| 411 |
uint32_t check_id, uint32_t error_id, uint32_t offset_id, |
| 412 |
uint32_t length_id, uint32_t stage_idx, ref_analysis* ref, |
| 413 |
std::vector<std::unique_ptr<BasicBlock>>* new_blocks) { |
| 414 |
BasicBlock* back_blk_ptr = &*new_blocks->back(); |
| 415 |
InstructionBuilder builder( |
| 416 |
context(), back_blk_ptr, |
| 417 |
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping); |
| 418 |
// Gen conditional branch on check_id. Valid branch generates original |
| 419 |
// reference. Invalid generates debug output and zero result (if needed). |
| 420 |
uint32_t merge_blk_id = TakeNextId(); |
| 421 |
uint32_t valid_blk_id = TakeNextId(); |
| 422 |
uint32_t invalid_blk_id = TakeNextId(); |
| 423 |
std::unique_ptr<Instruction> merge_label(NewLabel(merge_blk_id)); |
| 424 |
std::unique_ptr<Instruction> valid_label(NewLabel(valid_blk_id)); |
| 425 |
std::unique_ptr<Instruction> invalid_label(NewLabel(invalid_blk_id)); |
| 426 |
(void)builder.AddConditionalBranch(check_id, valid_blk_id, invalid_blk_id, |
| 427 |
merge_blk_id, SpvSelectionControlMaskNone); |
| 428 |
// Gen valid bounds branch |
| 429 |
std::unique_ptr<BasicBlock> new_blk_ptr( |
| 430 |
new BasicBlock(std::move(valid_label))); |
| 431 |
builder.SetInsertPoint(&*new_blk_ptr); |
| 432 |
uint32_t new_ref_id = CloneOriginalReference(ref, &builder); |
| 433 |
(void)builder.AddBranch(merge_blk_id); |
| 434 |
new_blocks->push_back(std::move(new_blk_ptr)); |
| 435 |
// Gen invalid block |
| 436 |
new_blk_ptr.reset(new BasicBlock(std::move(invalid_label))); |
| 437 |
builder.SetInsertPoint(&*new_blk_ptr); |
| 438 |
uint32_t u_index_id = GenUintCastCode(ref->desc_idx_id, &builder); |
| 439 |
if (offset_id != 0) { |
| 440 |
// Buffer OOB |
| 441 |
uint32_t u_offset_id = GenUintCastCode(offset_id, &builder); |
| 442 |
uint32_t u_length_id = GenUintCastCode(length_id, &builder); |
| 443 |
GenDebugStreamWrite(uid2offset_[ref->ref_inst->unique_id()], stage_idx, |
| 444 |
{error_id, u_index_id, u_offset_id, u_length_id}, |
| 445 |
&builder); |
| 446 |
} else if (buffer_bounds_enabled_) { |
| 447 |
// Uninitialized Descriptor - Return additional unused zero so all error |
| 448 |
// modes will use same debug stream write function |
| 449 |
uint32_t u_length_id = GenUintCastCode(length_id, &builder); |
| 450 |
GenDebugStreamWrite( |
| 451 |
uid2offset_[ref->ref_inst->unique_id()], stage_idx, |
| 452 |
{error_id, u_index_id, u_length_id, builder.GetUintConstantId(0)}, |
| 453 |
&builder); |
| 454 |
} else { |
| 455 |
// Uninitialized Descriptor - Normal error return |
| 456 |
uint32_t u_length_id = GenUintCastCode(length_id, &builder); |
| 457 |
GenDebugStreamWrite(uid2offset_[ref->ref_inst->unique_id()], stage_idx, |
| 458 |
{error_id, u_index_id, u_length_id}, &builder); |
| 459 |
} |
| 460 |
// Remember last invalid block id |
| 461 |
uint32_t last_invalid_blk_id = new_blk_ptr->GetLabelInst()->result_id(); |
| 462 |
// Gen zero for invalid reference |
| 463 |
uint32_t ref_type_id = ref->ref_inst->type_id(); |
| 464 |
(void)builder.AddBranch(merge_blk_id); |
| 465 |
new_blocks->push_back(std::move(new_blk_ptr)); |
| 466 |
// Gen merge block |
| 467 |
new_blk_ptr.reset(new BasicBlock(std::move(merge_label))); |
| 468 |
builder.SetInsertPoint(&*new_blk_ptr); |
| 469 |
// Gen phi of new reference and zero, if necessary, and replace the |
| 470 |
// result id of the original reference with that of the Phi. Kill original |
| 471 |
// reference. |
| 472 |
if (new_ref_id != 0) { |
| 473 |
Instruction* phi_inst = builder.AddPhi( |
| 474 |
ref_type_id, {new_ref_id, valid_blk_id, GetNullId(ref_type_id), |
| 475 |
last_invalid_blk_id}); |
| 476 |
context()->ReplaceAllUsesWith(ref->ref_inst->result_id(), |
| 477 |
phi_inst->result_id()); |
| 478 |
} |
| 479 |
new_blocks->push_back(std::move(new_blk_ptr)); |
| 480 |
context()->KillInst(ref->ref_inst); |
| 481 |
} |
| 482 |
|
| 483 |
void InstBindlessCheckPass::GenDescIdxCheckCode( |
| 484 |
BasicBlock::iterator ref_inst_itr, |
| 485 |
UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx, |
| 486 |
std::vector<std::unique_ptr<BasicBlock>>* new_blocks) { |
| 487 |
// Look for reference through indexed descriptor. If found, analyze and |
| 488 |
// save components. If not, return. |
| 489 |
ref_analysis ref; |
| 490 |
if (!AnalyzeDescriptorReference(&*ref_inst_itr, &ref)) return; |
| 491 |
Instruction* ptr_inst = get_def_use_mgr()->GetDef(ref.ptr_id); |
| 492 |
if (ptr_inst->opcode() != SpvOp::SpvOpAccessChain) return; |
| 493 |
// If index and bound both compile-time constants and index < bound, |
| 494 |
// return without changing |
| 495 |
Instruction* var_inst = get_def_use_mgr()->GetDef(ref.var_id); |
| 496 |
Instruction* desc_type_inst = GetPointeeTypeInst(var_inst); |
| 497 |
uint32_t length_id = 0; |
| 498 |
if (desc_type_inst->opcode() == SpvOpTypeArray) { |
| 499 |
length_id = |
| 500 |
desc_type_inst->GetSingleWordInOperand(kSpvTypeArrayLengthIdInIdx); |
| 501 |
Instruction* index_inst = get_def_use_mgr()->GetDef(ref.desc_idx_id); |
| 502 |
Instruction* length_inst = get_def_use_mgr()->GetDef(length_id); |
| 503 |
if (index_inst->opcode() == SpvOpConstant && |
| 504 |
length_inst->opcode() == SpvOpConstant && |
| 505 |
index_inst->GetSingleWordInOperand(kSpvConstantValueInIdx) < |
| 506 |
length_inst->GetSingleWordInOperand(kSpvConstantValueInIdx)) |
| 507 |
return; |
| 508 |
} else if (!desc_idx_enabled_ || |
| 509 |
desc_type_inst->opcode() != SpvOpTypeRuntimeArray) { |
| 510 |
return; |
| 511 |
} |
| 512 |
// Move original block's preceding instructions into first new block |
| 513 |
std::unique_ptr<BasicBlock> new_blk_ptr; |
| 514 |
MovePreludeCode(ref_inst_itr, ref_block_itr, &new_blk_ptr); |
| 515 |
InstructionBuilder builder( |
| 516 |
context(), &*new_blk_ptr, |
| 517 |
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping); |
| 518 |
new_blocks->push_back(std::move(new_blk_ptr)); |
| 519 |
uint32_t error_id = builder.GetUintConstantId(kInstErrorBindlessBounds); |
| 520 |
// If length id not yet set, descriptor array is runtime size so |
| 521 |
// generate load of length from stage's debug input buffer. |
| 522 |
if (length_id == 0) { |
| 523 |
assert(desc_type_inst->opcode() == SpvOpTypeRuntimeArray && |
| 524 |
"unexpected bindless type"); |
| 525 |
length_id = GenDebugReadLength(ref.var_id, &builder); |
| 526 |
} |
| 527 |
// Generate full runtime bounds test code with true branch |
| 528 |
// being full reference and false branch being debug output and zero |
| 529 |
// for the referenced value. |
| 530 |
uint32_t desc_idx_32b_id = Gen32BitCvtCode(ref.desc_idx_id, &builder); |
| 531 |
uint32_t length_32b_id = Gen32BitCvtCode(length_id, &builder); |
| 532 |
Instruction* ult_inst = builder.AddBinaryOp(GetBoolId(), SpvOpULessThan, |
| 533 |
desc_idx_32b_id, length_32b_id); |
| 534 |
ref.desc_idx_id = desc_idx_32b_id; |
| 535 |
GenCheckCode(ult_inst->result_id(), error_id, 0u, length_id, stage_idx, &ref, |
| 536 |
new_blocks); |
| 537 |
// Move original block's remaining code into remainder/merge block and add |
| 538 |
// to new blocks |
| 539 |
BasicBlock* back_blk_ptr = &*new_blocks->back(); |
| 540 |
MovePostludeCode(ref_block_itr, back_blk_ptr); |
| 541 |
} |
| 542 |
|
| 543 |
void InstBindlessCheckPass::GenDescInitCheckCode( |
| 544 |
BasicBlock::iterator ref_inst_itr, |
| 545 |
UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx, |
| 546 |
std::vector<std::unique_ptr<BasicBlock>>* new_blocks) { |
| 547 |
// Look for reference through descriptor. If not, return. |
| 548 |
ref_analysis ref; |
| 549 |
if (!AnalyzeDescriptorReference(&*ref_inst_itr, &ref)) return; |
| 550 |
// Determine if we can only do initialization check |
| 551 |
bool init_check = false; |
| 552 |
if (ref.desc_load_id != 0 || !buffer_bounds_enabled_) { |
| 553 |
init_check = true; |
| 554 |
} else { |
| 555 |
// For now, only do bounds check for non-aggregate types. Otherwise |
| 556 |
// just do descriptor initialization check. |
| 557 |
// TODO(greg-lunarg): Do bounds check for aggregate loads and stores |
| 558 |
Instruction* ref_ptr_inst = get_def_use_mgr()->GetDef(ref.ptr_id); |
| 559 |
Instruction* pte_type_inst = GetPointeeTypeInst(ref_ptr_inst); |
| 560 |
uint32_t pte_type_op = pte_type_inst->opcode(); |
| 561 |
if (pte_type_op == SpvOpTypeArray || pte_type_op == SpvOpTypeRuntimeArray || |
| 562 |
pte_type_op == SpvOpTypeStruct) |
| 563 |
init_check = true; |
| 564 |
} |
| 565 |
// If initialization check and not enabled, return |
| 566 |
if (init_check && !desc_init_enabled_) return; |
| 567 |
// Move original block's preceding instructions into first new block |
| 568 |
std::unique_ptr<BasicBlock> new_blk_ptr; |
| 569 |
MovePreludeCode(ref_inst_itr, ref_block_itr, &new_blk_ptr); |
| 570 |
InstructionBuilder builder( |
| 571 |
context(), &*new_blk_ptr, |
| 572 |
IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping); |
| 573 |
new_blocks->push_back(std::move(new_blk_ptr)); |
| 574 |
// If initialization check, use reference value of zero. |
| 575 |
// Else use the index of the last byte referenced. |
| 576 |
uint32_t ref_id = init_check ? builder.GetUintConstantId(0u) |
| 577 |
: GenLastByteIdx(&ref, &builder); |
| 578 |
// Read initialization/bounds from debug input buffer. If index id not yet |
| 579 |
// set, binding is single descriptor, so set index to constant 0. |
| 580 |
if (ref.desc_idx_id == 0) ref.desc_idx_id = builder.GetUintConstantId(0u); |
| 581 |
uint32_t init_id = GenDebugReadInit(ref.var_id, ref.desc_idx_id, &builder); |
| 582 |
// Generate runtime initialization/bounds test code with true branch |
| 583 |
// being full reference and false branch being debug output and zero |
| 584 |
// for the referenced value. |
| 585 |
Instruction* ult_inst = |
| 586 |
builder.AddBinaryOp(GetBoolId(), SpvOpULessThan, ref_id, init_id); |
| 587 |
uint32_t error = |
| 588 |
init_check ? kInstErrorBindlessUninit : kInstErrorBindlessBuffOOB; |
| 589 |
uint32_t error_id = builder.GetUintConstantId(error); |
| 590 |
GenCheckCode(ult_inst->result_id(), error_id, init_check ? 0 : ref_id, |
| 591 |
init_check ? builder.GetUintConstantId(0u) : init_id, stage_idx, |
| 592 |
&ref, new_blocks); |
| 593 |
// Move original block's remaining code into remainder/merge block and add |
| 594 |
// to new blocks |
| 595 |
BasicBlock* back_blk_ptr = &*new_blocks->back(); |
| 596 |
MovePostludeCode(ref_block_itr, back_blk_ptr); |
| 597 |
} |
| 598 |
|
| 599 |
void InstBindlessCheckPass::InitializeInstBindlessCheck() { |
| 600 |
// Initialize base class |
| 601 |
InitializeInstrument(); |
| 602 |
// If runtime array length support enabled, create variable mappings. Length |
| 603 |
// support is always enabled if descriptor init check is enabled. |
| 604 |
if (desc_idx_enabled_ || buffer_bounds_enabled_) |
| 605 |
for (auto& anno : get_module()->annotations()) |
| 606 |
if (anno.opcode() == SpvOpDecorate) { |
| 607 |
if (anno.GetSingleWordInOperand(1u) == SpvDecorationDescriptorSet) |
| 608 |
var2desc_set_[anno.GetSingleWordInOperand(0u)] = |
| 609 |
anno.GetSingleWordInOperand(2u); |
| 610 |
else if (anno.GetSingleWordInOperand(1u) == SpvDecorationBinding) |
| 611 |
var2binding_[anno.GetSingleWordInOperand(0u)] = |
| 612 |
anno.GetSingleWordInOperand(2u); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
Pass::Status InstBindlessCheckPass::ProcessImpl() { |
| 617 |
// Perform bindless bounds check on each entry point function in module |
| 618 |
InstProcessFunction pfn = |
| 619 |
[this](BasicBlock::iterator ref_inst_itr, |
| 620 |
UptrVectorIterator<BasicBlock> ref_block_itr, uint32_t stage_idx, |
| 621 |
std::vector<std::unique_ptr<BasicBlock>>* new_blocks) { |
| 622 |
return GenDescIdxCheckCode(ref_inst_itr, ref_block_itr, stage_idx, |
| 623 |
new_blocks); |
| 624 |
}; |
| 625 |
bool modified = InstProcessEntryPointCallTree(pfn); |
| 626 |
if (desc_init_enabled_ || buffer_bounds_enabled_) { |
| 627 |
// Perform descriptor initialization check on each entry point function in |
| 628 |
// module |
| 629 |
pfn = [this](BasicBlock::iterator ref_inst_itr, |
| 630 |
UptrVectorIterator<BasicBlock> ref_block_itr, |
| 631 |
uint32_t stage_idx, |
| 632 |
std::vector<std::unique_ptr<BasicBlock>>* new_blocks) { |
| 633 |
return GenDescInitCheckCode(ref_inst_itr, ref_block_itr, stage_idx, |
| 634 |
new_blocks); |
| 635 |
}; |
| 636 |
modified |= InstProcessEntryPointCallTree(pfn); |
| 637 |
} |
| 638 |
return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange; |
| 639 |
} |
| 640 |
|
| 641 |
Pass::Status InstBindlessCheckPass::Process() { |
| 642 |
InitializeInstBindlessCheck(); |
| 643 |
return ProcessImpl(); |
| 644 |
} |
| 645 |
|
| 646 |
} // namespace opt |
| 647 |
} // namespace spvtools |