2021-10-26 21:20:58 +00:00
|
|
|
// Copyright (c) 2021 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "source/opt/desc_sroa_util.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
namespace {
|
2022-11-17 18:02:50 +00:00
|
|
|
constexpr uint32_t kOpAccessChainInOperandIndexes = 1;
|
2021-10-26 21:20:58 +00:00
|
|
|
|
|
|
|
// Returns the length of array type |type|.
|
|
|
|
uint32_t GetLengthOfArrayType(IRContext* context, Instruction* type) {
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(type->opcode() == spv::Op::OpTypeArray && "type must be array");
|
2021-10-26 21:20:58 +00:00
|
|
|
uint32_t length_id = type->GetSingleWordInOperand(1);
|
|
|
|
const analysis::Constant* length_const =
|
|
|
|
context->get_constant_mgr()->FindDeclaredConstant(length_id);
|
|
|
|
assert(length_const != nullptr);
|
|
|
|
return length_const->GetU32();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace descsroautil {
|
|
|
|
|
|
|
|
bool IsDescriptorArray(IRContext* context, Instruction* var) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (var->opcode() != spv::Op::OpVariable) {
|
2021-10-26 21:20:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ptr_type_id = var->type_id();
|
|
|
|
Instruction* ptr_type_inst = context->get_def_use_mgr()->GetDef(ptr_type_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (ptr_type_inst->opcode() != spv::Op::OpTypePointer) {
|
2021-10-26 21:20:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t var_type_id = ptr_type_inst->GetSingleWordInOperand(1);
|
|
|
|
Instruction* var_type_inst = context->get_def_use_mgr()->GetDef(var_type_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (var_type_inst->opcode() != spv::Op::OpTypeArray &&
|
|
|
|
var_type_inst->opcode() != spv::Op::OpTypeStruct) {
|
2021-10-26 21:20:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All structures with descriptor assignments must be replaced by variables,
|
|
|
|
// one for each of their members - with the exceptions of buffers.
|
|
|
|
if (IsTypeOfStructuredBuffer(context, var_type_inst)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!context->get_decoration_mgr()->HasDecoration(
|
2022-11-04 21:27:10 +00:00
|
|
|
var->result_id(), uint32_t(spv::Decoration::DescriptorSet))) {
|
2021-10-26 21:20:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-04 21:27:10 +00:00
|
|
|
return context->get_decoration_mgr()->HasDecoration(
|
|
|
|
var->result_id(), uint32_t(spv::Decoration::Binding));
|
2021-10-26 21:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsTypeOfStructuredBuffer(IRContext* context, const Instruction* type) {
|
2022-11-04 21:27:10 +00:00
|
|
|
if (type->opcode() != spv::Op::OpTypeStruct) {
|
2021-10-26 21:20:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All buffers have offset decorations for members of their structure types.
|
|
|
|
// This is how we distinguish it from a structure of descriptors.
|
2022-11-04 21:27:10 +00:00
|
|
|
return context->get_decoration_mgr()->HasDecoration(
|
|
|
|
type->result_id(), uint32_t(spv::Decoration::Offset));
|
2021-10-26 21:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const analysis::Constant* GetAccessChainIndexAsConst(
|
|
|
|
IRContext* context, Instruction* access_chain) {
|
|
|
|
if (access_chain->NumInOperands() <= 1) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
uint32_t idx_id = GetFirstIndexOfAccessChain(access_chain);
|
|
|
|
const analysis::Constant* idx_const =
|
|
|
|
context->get_constant_mgr()->FindDeclaredConstant(idx_id);
|
|
|
|
return idx_const;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetFirstIndexOfAccessChain(Instruction* access_chain) {
|
|
|
|
assert(access_chain->NumInOperands() > 1 &&
|
|
|
|
"OpAccessChain does not have Indexes operand");
|
|
|
|
return access_chain->GetSingleWordInOperand(kOpAccessChainInOperandIndexes);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetNumberOfElementsForArrayOrStruct(IRContext* context,
|
|
|
|
Instruction* var) {
|
|
|
|
uint32_t ptr_type_id = var->type_id();
|
|
|
|
Instruction* ptr_type_inst = context->get_def_use_mgr()->GetDef(ptr_type_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(ptr_type_inst->opcode() == spv::Op::OpTypePointer &&
|
2021-10-26 21:20:58 +00:00
|
|
|
"Variable should be a pointer to an array or structure.");
|
|
|
|
uint32_t pointee_type_id = ptr_type_inst->GetSingleWordInOperand(1);
|
|
|
|
Instruction* pointee_type_inst =
|
|
|
|
context->get_def_use_mgr()->GetDef(pointee_type_id);
|
2022-11-04 21:27:10 +00:00
|
|
|
if (pointee_type_inst->opcode() == spv::Op::OpTypeArray) {
|
2021-10-26 21:20:58 +00:00
|
|
|
return GetLengthOfArrayType(context, pointee_type_inst);
|
|
|
|
}
|
2022-11-04 21:27:10 +00:00
|
|
|
assert(pointee_type_inst->opcode() == spv::Op::OpTypeStruct &&
|
2021-10-26 21:20:58 +00:00
|
|
|
"Variable should be a pointer to an array or structure.");
|
|
|
|
return pointee_type_inst->NumInOperands();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace descsroautil
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|