Updated spirv-tools.

This commit is contained in:
Бранимир Караџић
2020-07-10 16:24:17 -07:00
parent 466adb535d
commit 75bbd23e53
45 changed files with 2050 additions and 115 deletions

View File

@@ -112,19 +112,6 @@ bool PhiIdsOkForNewEdge(
return true;
}
uint32_t MaybeGetBoolConstantId(opt::IRContext* context, bool value) {
opt::analysis::Bool bool_type;
auto registered_bool_type =
context->get_type_mgr()->GetRegisteredType(&bool_type);
if (!registered_bool_type) {
return 0;
}
opt::analysis::BoolConstant bool_constant(registered_bool_type->AsBool(),
value);
return context->get_constant_mgr()->FindDeclaredConstant(
&bool_constant, context->get_type_mgr()->GetId(&bool_type));
}
void AddUnreachableEdgeAndUpdateOpPhis(
opt::IRContext* context, opt::BasicBlock* bb_from, opt::BasicBlock* bb_to,
bool condition_value,
@@ -135,7 +122,7 @@ void AddUnreachableEdgeAndUpdateOpPhis(
"Precondition on terminator of bb_from is not satisfied");
// Get the id of the boolean constant to be used as the condition.
uint32_t bool_id = MaybeGetBoolConstantId(context, condition_value);
uint32_t bool_id = MaybeGetBoolConstant(context, condition_value);
assert(
bool_id &&
"Precondition that condition value must be available is not satisfied");
@@ -627,6 +614,7 @@ void AddGlobalVariable(opt::IRContext* context, uint32_t result_id,
context, SpvOpVariable, type_id, result_id, std::move(operands)));
AddVariableIdToEntryPointInterfaces(context, result_id);
UpdateModuleIdBound(context, result_id);
}
void AddLocalVariable(opt::IRContext* context, uint32_t result_id,
@@ -657,6 +645,8 @@ void AddLocalVariable(opt::IRContext* context, uint32_t result_id,
opt::Instruction::OperandList{
{SPV_OPERAND_TYPE_STORAGE_CLASS, {SpvStorageClassFunction}},
{SPV_OPERAND_TYPE_ID, {initializer_id}}}));
UpdateModuleIdBound(context, result_id);
}
bool HasDuplicates(const std::vector<uint32_t>& arr) {
@@ -771,6 +761,199 @@ uint32_t MaybeGetStructType(opt::IRContext* ir_context,
return ir_context->get_type_mgr()->GetId(&type);
}
uint32_t MaybeGetZeroConstant(opt::IRContext* ir_context,
uint32_t scalar_or_composite_type_id) {
const auto* type =
ir_context->get_type_mgr()->GetType(scalar_or_composite_type_id);
assert(type && "|scalar_or_composite_type_id| is invalid");
switch (type->kind()) {
case opt::analysis::Type::kBool:
return MaybeGetBoolConstant(ir_context, false);
case opt::analysis::Type::kFloat:
case opt::analysis::Type::kInteger: {
std::vector<uint32_t> words = {0};
if ((type->AsInteger() && type->AsInteger()->width() > 32) ||
(type->AsFloat() && type->AsFloat()->width() > 32)) {
words.push_back(0);
}
return MaybeGetScalarConstant(ir_context, words,
scalar_or_composite_type_id);
}
case opt::analysis::Type::kStruct: {
std::vector<uint32_t> component_ids;
for (const auto* component_type : type->AsStruct()->element_types()) {
auto component_type_id =
ir_context->get_type_mgr()->GetId(component_type);
assert(component_type_id && "Component type is invalid");
auto component_id = MaybeGetZeroConstant(ir_context, component_type_id);
if (component_id == 0) {
return 0;
}
component_ids.push_back(component_id);
}
return MaybeGetCompositeConstant(ir_context, component_ids,
scalar_or_composite_type_id);
}
case opt::analysis::Type::kMatrix:
case opt::analysis::Type::kVector: {
const auto* component_type = type->AsVector()
? type->AsVector()->element_type()
: type->AsMatrix()->element_type();
auto component_type_id =
ir_context->get_type_mgr()->GetId(component_type);
assert(component_type_id && "Component type is invalid");
if (auto component_id =
MaybeGetZeroConstant(ir_context, component_type_id)) {
auto component_count = type->AsVector()
? type->AsVector()->element_count()
: type->AsMatrix()->element_count();
return MaybeGetCompositeConstant(
ir_context, std::vector<uint32_t>(component_count, component_id),
scalar_or_composite_type_id);
}
return 0;
}
case opt::analysis::Type::kArray: {
auto component_type_id =
ir_context->get_type_mgr()->GetId(type->AsArray()->element_type());
assert(component_type_id && "Component type is invalid");
if (auto component_id =
MaybeGetZeroConstant(ir_context, component_type_id)) {
auto type_id = ir_context->get_type_mgr()->GetId(type);
assert(type_id && "|type| is invalid");
const auto* type_inst = ir_context->get_def_use_mgr()->GetDef(type_id);
assert(type_inst && "Array's type id is invalid");
return MaybeGetCompositeConstant(
ir_context,
std::vector<uint32_t>(GetArraySize(*type_inst, ir_context),
component_id),
scalar_or_composite_type_id);
}
return 0;
}
default:
assert(false && "Type is not supported");
return 0;
}
}
uint32_t MaybeGetScalarConstant(opt::IRContext* ir_context,
const std::vector<uint32_t>& words,
uint32_t scalar_type_id) {
const auto* type = ir_context->get_type_mgr()->GetType(scalar_type_id);
assert(type && "|scalar_type_id| is invalid");
if (const auto* int_type = type->AsInteger()) {
return MaybeGetIntegerConstant(ir_context, words, int_type->width(),
int_type->IsSigned());
} else if (const auto* float_type = type->AsFloat()) {
return MaybeGetFloatConstant(ir_context, words, float_type->width());
} else {
assert(type->AsBool() && words.size() == 1 &&
"|scalar_type_id| doesn't represent a scalar type");
return MaybeGetBoolConstant(ir_context, words[0]);
}
}
uint32_t MaybeGetCompositeConstant(opt::IRContext* ir_context,
const std::vector<uint32_t>& component_ids,
uint32_t composite_type_id) {
std::vector<const opt::analysis::Constant*> constants;
for (auto id : component_ids) {
const auto* component_constant =
ir_context->get_constant_mgr()->FindDeclaredConstant(id);
assert(component_constant && "|id| is invalid");
constants.push_back(component_constant);
}
const auto* type = ir_context->get_type_mgr()->GetType(composite_type_id);
assert(type && "|composite_type_id| is invalid");
std::unique_ptr<opt::analysis::Constant> composite_constant;
switch (type->kind()) {
case opt::analysis::Type::kStruct:
composite_constant = MakeUnique<opt::analysis::StructConstant>(
type->AsStruct(), std::move(constants));
break;
case opt::analysis::Type::kVector:
composite_constant = MakeUnique<opt::analysis::VectorConstant>(
type->AsVector(), std::move(constants));
break;
case opt::analysis::Type::kMatrix:
composite_constant = MakeUnique<opt::analysis::MatrixConstant>(
type->AsMatrix(), std::move(constants));
break;
case opt::analysis::Type::kArray:
composite_constant = MakeUnique<opt::analysis::ArrayConstant>(
type->AsArray(), std::move(constants));
break;
default:
assert(false &&
"|composite_type_id| is not a result id of a composite type");
return 0;
}
return ir_context->get_constant_mgr()->FindDeclaredConstant(
composite_constant.get(), composite_type_id);
}
uint32_t MaybeGetIntegerConstant(opt::IRContext* ir_context,
const std::vector<uint32_t>& words,
uint32_t width, bool is_signed) {
auto type_id = MaybeGetIntegerType(ir_context, width, is_signed);
if (!type_id) {
return 0;
}
const auto* type = ir_context->get_type_mgr()->GetType(type_id);
assert(type && "|type_id| is invalid");
opt::analysis::IntConstant constant(type->AsInteger(), words);
return ir_context->get_constant_mgr()->FindDeclaredConstant(&constant,
type_id);
}
uint32_t MaybeGetFloatConstant(opt::IRContext* ir_context,
const std::vector<uint32_t>& words,
uint32_t width) {
auto type_id = MaybeGetFloatType(ir_context, width);
if (!type_id) {
return 0;
}
const auto* type = ir_context->get_type_mgr()->GetType(type_id);
assert(type && "|type_id| is invalid");
opt::analysis::FloatConstant constant(type->AsFloat(), words);
return ir_context->get_constant_mgr()->FindDeclaredConstant(&constant,
type_id);
}
uint32_t MaybeGetBoolConstant(opt::IRContext* context, bool value) {
opt::analysis::Bool bool_type;
auto registered_bool_type =
context->get_type_mgr()->GetRegisteredType(&bool_type);
if (!registered_bool_type) {
return 0;
}
opt::analysis::BoolConstant bool_constant(registered_bool_type->AsBool(),
value);
return context->get_constant_mgr()->FindDeclaredConstant(
&bool_constant, context->get_type_mgr()->GetId(&bool_type));
}
void AddIntegerType(opt::IRContext* ir_context, uint32_t result_id,
uint32_t width, bool is_signed) {
ir_context->module()->AddType(MakeUnique<opt::Instruction>(