Skip to content

Commit

Permalink
Fix array size calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
s-perron committed Nov 1, 2023
1 parent c87755b commit 54bb64a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/opt/folding_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2079,21 +2079,22 @@ uint32_t GetNumberOfElements(const analysis::Type* type) {
return static_cast<uint32_t>(struct_type->element_types().size());
}
if (auto* array_type = type->AsArray()) {
// STEVEN: This is incorrect
return array_type->length_info().words[0];
}
return 0;
}

// Returns a map with the set of values that were inserted into an object by
// the chain of OpCompositeInsertInstruction starting with |inst|.
// The map will map the index to the value inserted at that index.
// The map will map the index to the value inserted at that index. An empty map will be returned if the map could not be properly generated.
std::map<uint32_t, uint32_t> GetInsertedValues(Instruction* inst) {
analysis::DefUseManager* def_use_mgr = inst->context()->get_def_use_mgr();
std::map<uint32_t, uint32_t> values_inserted;
Instruction* current_inst = inst;
while (current_inst->opcode() == spv::Op::OpCompositeInsert) {
if (current_inst->NumInOperands() > inst->NumInOperands()) {
// This is the catch the case
// This is to catch the case
// %2 = OpCompositeInsert %m2x2int %v2int_1_0 %m2x2int_undef 0
// %3 = OpCompositeInsert %m2x2int %int_4 %2 0 0
// %4 = OpCompositeInsert %m2x2int %v2int_2_3 %3 1
Expand Down Expand Up @@ -2124,6 +2125,8 @@ bool DoInsertedValuesCoverEntireObject(
return false;
}

std::cerr << container_size << ", " << values_inserted.size() << "\n";

if (values_inserted.rbegin()->first >= container_size) {
return false;
}
Expand Down Expand Up @@ -2183,6 +2186,10 @@ bool CompositeInsertToCompositeConstruct(
if (inst->NumInOperands() < 3) return false;

std::map<uint32_t, uint32_t> values_inserted = GetInsertedValues(inst);
if (values_inserted.empty()) {
// return false;
}

const analysis::Type* container_type = GetContainerType(inst);
if (container_type == nullptr) {
return false;
Expand Down

0 comments on commit 54bb64a

Please sign in to comment.