From: Mikko Rasa Date: Sat, 9 Oct 2021 16:58:51 +0000 (+0300) Subject: Also break up arrays which are only indexed by constants X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=49e89e55692ecdbccd98b93578d29f89669a9fc0;p=libs%2Fgl.git Also break up arrays which are only indexed by constants --- diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 768cfd5d..42e4a86e 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -532,11 +532,19 @@ bool AggregateDismantler::apply(Stage &stage) for(const AggregateMember &m: kvp.second.members) { + string name; + if(m.declaration) + name = format("%s_%s", kvp.second.declaration->name, m.declaration->name); + else + name = format("%s_%d", kvp.second.declaration->name, m.index); + VariableDeclaration *var = new VariableDeclaration; var->source = kvp.first->source; var->line = kvp.first->line; - var->name = get_unused_variable_name(*kvp.second.decl_scope, format("%s_%s", kvp.second.declaration->name, m.declaration->name)); - var->type = m.declaration->type; + var->name = get_unused_variable_name(*kvp.second.decl_scope, name); + /* XXX This is kind of brittle and depends on the array declaration's + textual type not having brackets in it. */ + var->type = (m.declaration ? m.declaration : kvp.second.declaration)->type; if(m.initializer) var->init_expression = m.initializer->clone(); @@ -570,10 +578,17 @@ void AggregateDismantler::visit(RefPtr &expr) { r_aggregate_ref = 0; expr->visit(*this); - if(r_aggregate_ref && r_reference.chain_len==1 && (r_reference.chain[0]&0x3F)!=0x3F) + if(r_aggregate_ref && r_reference.chain_len==1) { - r_aggregate_ref->members[r_reference.chain[0]&0x3F].references.push_back(&expr); - r_aggregate_ref->members_referenced = true; + if((r_reference.chain[0]&0x3F)!=0x3F) + { + r_aggregate_ref->members[r_reference.chain[0]&0x3F].references.push_back(&expr); + r_aggregate_ref->members_referenced = true; + } + else + /* If the accessed member is not known, mark the entire aggregate as + referenced. */ + r_aggregate_ref->referenced = true; } r_aggregate_ref = 0; } @@ -584,6 +599,8 @@ void AggregateDismantler::visit(VariableReference &var) r_reference.declaration = var.declaration; else { + /* If an aggregate variable is referenced as a whole, it should not be + dismantled. */ auto i = aggregates.find(var.declaration); if(i!=aggregates.end()) i->second.referenced = true; @@ -624,7 +641,19 @@ void AggregateDismantler::visit(BinaryExpression &binary) visit(binary.right); } - add_to_chain(r_reference, Assignment::Target::ARRAY, 0x3F); + unsigned index = 0x3F; + if(Literal *literal_subscript = dynamic_cast(binary.right.get())) + if(literal_subscript->value.check_type()) + index = literal_subscript->value.value(); + add_to_chain(r_reference, Assignment::Target::ARRAY, index); + + if(r_reference.declaration && r_reference.chain_len==1) + { + auto i = aggregates.find(r_reference.declaration); + r_aggregate_ref = (i!=aggregates.end() ? &i->second : 0); + } + else + r_aggregate_ref = 0; } else { @@ -638,6 +667,7 @@ void AggregateDismantler::visit(VariableDeclaration &var) TraversingVisitor::visit(var); if(var.interface.empty()) + { if(const StructDeclaration *strct = dynamic_cast(var.type_declaration)) { const FunctionCall *init_call = dynamic_cast(var.init_expression.get()); @@ -656,6 +686,7 @@ void AggregateDismantler::visit(VariableDeclaration &var) { AggregateMember member; member.declaration = mem_decl; + member.index = i; if(init_call) member.initializer = init_call->arguments[i]; aggre.members.push_back(member); @@ -664,6 +695,26 @@ void AggregateDismantler::visit(VariableDeclaration &var) } } } + else if(const Literal *literal_size = dynamic_cast(var.array_size.get())) + { + if(literal_size->value.check_type()) + { + Aggregate &aggre = aggregates[&var]; + aggre.declaration = &var; + aggre.decl_scope = current_block; + aggre.insert_point = insert_point; + + int size = literal_size->value.value(); + for(int i=0; i initializer; std::vector *> references; }; diff --git a/tests/glsl/unary_operators.glsl b/tests/glsl/unary_operators.glsl index bd9803b2..baef6451 100644 --- a/tests/glsl/unary_operators.glsl +++ b/tests/glsl/unary_operators.glsl @@ -22,7 +22,7 @@ void main() --i; i++; i--; - int arr[3]; - ++arr[0]; + int arr_0; + ++arr_0; } */