From b7af10449bc237b54d27833e65a25ce80dab8167 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Fri, 10 Sep 2021 16:00:25 +0200 Subject: [PATCH] aco/ra: create nested affinities for loop header phis Totals from 875 (0.58% of 150170) affected shaders: (GFX10.3) CodeSize: 6084528 -> 6066628 (-0.29%); split: -0.32%, +0.02% Instrs: 1136497 -> 1133565 (-0.26%); split: -0.28%, +0.02% Latency: 23355051 -> 22952592 (-1.72%); split: -1.83%, +0.10% InvThroughput: 13028151 -> 12859628 (-1.29%); split: -1.38%, +0.09% Copies: 85673 -> 82790 (-3.37%); split: -3.62%, +0.26% Branches: 25049 -> 25098 (+0.20%); split: -0.08%, +0.28% Reviewed-by: Rhys Perry Part-of: --- src/amd/compiler/aco_register_allocation.cpp | 36 ++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/amd/compiler/aco_register_allocation.cpp b/src/amd/compiler/aco_register_allocation.cpp index 829104cf6ed..a8d4668426d 100644 --- a/src/amd/compiler/aco_register_allocation.cpp +++ b/src/amd/compiler/aco_register_allocation.cpp @@ -2386,14 +2386,46 @@ get_affinities(ra_ctx& ctx, std::vector& live_out_per_block) affinity_related = &phi_ressources.back(); } - for (unsigned i = 0; i < instr->operands.size(); i++) { - const Operand& op = instr->operands[i]; + for (const Operand& op : instr->operands) { if (op.isTemp() && op.isKill() && op.regClass() == instr->definitions[0].regClass()) { affinity_related->emplace_back(op.getTemp()); + if (block.kind & block_kind_loop_header) + continue; temp_to_phi_ressources[op.tempId()] = index; } } } + + /* visit the loop header phis first in order to create nested affinities */ + if (block.kind & block_kind_loop_exit) { + /* find loop header */ + auto header_rit = block_rit; + while ((header_rit + 1)->loop_nest_depth > block.loop_nest_depth) + header_rit++; + + for (aco_ptr& phi : header_rit->instructions) { + if (!is_phi(phi)) + break; + if (phi->definitions[0].isKill() || phi->definitions[0].isFixed()) + continue; + + /* create an (empty) merge-set for the phi-related variables */ + auto it = temp_to_phi_ressources.find(phi->definitions[0].tempId()); + unsigned index = phi_ressources.size(); + if (it == temp_to_phi_ressources.end()) { + temp_to_phi_ressources[phi->definitions[0].tempId()] = index; + phi_ressources.emplace_back(std::vector{phi->definitions[0].getTemp()}); + } else { + index = it->second; + } + for (unsigned i = 1; i < phi->operands.size(); i++) { + const Operand& op = phi->operands[i]; + if (op.isTemp() && op.isKill() && op.regClass() == phi->definitions[0].regClass()) { + temp_to_phi_ressources[op.tempId()] = index; + } + } + } + } } /* create affinities */ for (std::vector& vec : phi_ressources) {