Hi Richard, > On 4 Nov 2026, at 14:59, Richard Biener <[email protected]> wrote: > > On Tue, Nov 3, 2026 at 3:45 PM <[email protected]> wrote: >> >> From: Kyrylo Tkachov <[email protected]> >> >> vect_bb_vectorization_profitable_p requires the code of each loop of a >> BB SLP region to be profitable on its own. A loop can run any number of >> times. The loop of the region entry is the exception. Within a loop >> the function adds up the costs as if all of its code ran on every >> iteration. >> >> In the switch interpreter of bb-slp-pr127246-2.c, BB SLP keeps >> next_instr and stack_pointer in one vector. So every handler stores >> both with one vector store (slp1 dump, simplified): >> >> latch: >> _99 = {_104, _110}; >> dispatch: >> # vect_next_instr_82 = PHI <_99(3), vect_next_instr_88(2)> >> _66 = BIT_FIELD_REF <vect_next_instr_82, 64, 64>; >> _77 = BIT_FIELD_REF <vect_next_instr_82, 64, 0>; >> switch (_143) <...> >> handler: >> MEM <vector(2) long unsigned int> [frame] = vect_next_instr_82; >> >> Each iteration runs one handler. But the costing uses the savings of >> the stores of all handlers to pay for the vector code that runs on every >> iteration. This code is the construction in the latch and the lane >> extracts in the dispatch block. The loop costs 24 for the vector and 42 >> for the scalar code on aarch64 with generic tuning. As a result, the >> interpreter moves its state between general and vector registers on >> every dispatch. For gcc.target/aarch64/pr127246.c, which includes this >> test, trunk emits 21 umov and 61 fmov instructions, and none with this >> patch. >> >> The computed goto form of the test, bb-slp-pr127246-1.c, is the form of >> CPython's evaluator. It no longer has this problem since r17-5469 >> (PR128069): BB SLP starts a new region at each block with an abnormal >> predecessor, so every handler is a region of its own. With trunk, >> CPython 3.14.5 built with PGO and LTO for Neoverse V2 has no moves >> between general and vector registers in its evaluator, and this patch >> does not change the evaluator. A switch has no abnormal edges. >> >> The code of a block runs together with the code of the blocks dominating >> it. So also require each block with costs in a loop to be profitable >> together with its dominators in that loop. The check adds up the >> unweighted costs that the target returns for their code. It walks up >> the dominators of each block with costs to the nearest common dominator >> of these blocks. It keeps the sums of the blocks on the way. >> >> In the example on aarch64 the dispatch block saves 2. This is because >> aarch64 does not cost the vector PHI and its lane extracts for BB SLP. >> A handler saves 1 with its vector store. The latch adds the >> construction, 4, so the latch fails the check with 2 more (8 more with >> -mcpu=neoverse-v2). On x86_64 the dispatch block costs 28 more, a >> handler saves 12 and the latch costs 28 more, so every block fails. >> >> The check also rejects a loop whose first exit comes before the code >> that pays for the vector code. It also rejects a loop part in which >> only the code after a join pays for the vector code before the join. >> Examples are the vectors that the arms of a branch build for a PHI. >> Trunk can vectorize such a loop part. A block whose vector code costs >> more than its scalar code still passes when its dominators save more, as >> in bb-slp-pr127246-13.c. >> >> Like the existing check, the new check leaves out the loop of the region >> entry. It also does not apply to loops whose costs are all in one >> block. This is because the costs that the target returns for single >> stmts do not include its final adjustments. >> >> For loops with costs in several blocks the check does not see these >> adjustments either. For example, aarch64 can give the prologue a cost >> of 0 (PR109072). It does this when the vector code only builds and >> stores vectors, and one store is to a decl that a vld1 loads. The check >> can reject such a loop part. >> >> The previous patch places vector code in the blocks of the scalar code >> that it replaces. So the check does not charge the vector code of one >> arm to the block before the branch. This does not hold for permutes, >> for nodes without scalar stmts and for externals that several nodes use. >> An example is a node that mixes additions and subtractions of values >> defined before the branch. Its blend, its two halves and the vectors of >> their operands stay before the branch. The check can then reject a loop >> part that trunk vectorizes. >> >> Bootstrapped and regression tested on aarch64-unknown-linux-gnu and >> x86_64-pc-linux-gnu. >> >> gcc/ChangeLog: >> >> PR tree-optimization/127246 >> * tree-vectorizer.h (vector_costs::add_slp_cost): Document the >> return value. >> * tree-vect-slp.cc (add_slp_costs): Return the sum of the costs. >> (li_cost): New struct. >> (li_cost_vec_cmp): Use li_cost. >> (vect_bb_slp_dominator_cost): New function. >> (vect_bb_vectorization_profitable_p): Record the vector minus the >> scalar cost of each block of a loop part and require each block >> with costs to be profitable together with its dominators in the >> loop. >> >> gcc/testsuite/ChangeLog: >> >> PR tree-optimization/127246 >> * gcc.dg/vect/bb-slp-pr127246-1.c: New test. >> * gcc.dg/vect/bb-slp-pr127246-2.c: New test. >> * gcc.dg/vect/bb-slp-pr127246-9.c: New test. >> * gcc.dg/vect/bb-slp-pr127246-13.c: New test. >> * gcc.dg/vect/bb-slp-pr127246-5.c: Expect the loop part to be >> vectorized. >> * gcc.dg/vect/bb-slp-pr127246-17.c: Likewise. >> * gcc.target/aarch64/pr127246.c: New test. >> >> Signed-off-by: Kyrylo Tkachov <[email protected]> >> --- >> gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-1.c | 54 ++++++ >> .../gcc.dg/vect/bb-slp-pr127246-13.c | 31 ++++ >> .../gcc.dg/vect/bb-slp-pr127246-17.c | 4 +- >> gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-2.c | 50 ++++++ >> gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c | 4 +- >> gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-9.c | 25 +++ >> gcc/testsuite/gcc.target/aarch64/pr127246.c | 16 ++ >> gcc/tree-vect-slp.cc | 160 ++++++++++++++---- >> gcc/tree-vectorizer.h | 4 +- >> 9 files changed, 311 insertions(+), 37 deletions(-) >> create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-1.c >> create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-13.c >> create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-2.c >> create mode 100644 gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-9.c >> create mode 100644 gcc/testsuite/gcc.target/aarch64/pr127246.c >> >> diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-1.c >> b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-1.c >> new file mode 100644 >> index 00000000000..e5856ecd736 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-1.c >> @@ -0,0 +1,54 @@ >> +/* PR tree-optimization/127246 */ >> +/* { dg-do compile } */ >> +/* { dg-require-effective-target label_values } */ >> +/* { dg-require-effective-target indirect_jumps } */ >> +/* { dg-additional-options "-fvect-cost-model=dynamic" } */ >> +/* { dg-additional-options "-mtune=generic" { target aarch64*-*-* i?86-*-* >> x86_64-*-* } } */ >> + >> +/* Each dispatch of the threaded interpreter runs one handler. Keeping >> + next_instr and stack_pointer in one vector, so that a handler can store >> + both with one vector store, costs a vector construction and two lane >> + extracts on every dispatch, more than the stores of one handler save. */ >> + >> +struct frame >> +{ >> + const unsigned short *instr_ptr; >> + long *stackpointer; >> +}; >> + >> +/* Opcode number and stack delta. */ >> +#define OPS(X) \ >> + X (0, -1) X (1, 0) X (2, -1) X (3, 0) X (4, -1) X (5, 0) \ >> + X (6, -1) X (7, 0) X (8, -1) X (9, 0) X (10, -1) X (11, 0) \ >> + X (12, -1) X (13, 0) X (14, -1) X (15, 0) X (16, -1) X (17, 0) \ >> + X (18, -1) X (19, 0) >> + >> +extern long op (long); >> + >> +#define DISPATCH() goto *targets[*next_instr & 0xff] >> + >> +#define HANDLER(N, SD) \ >> + L_##N: \ >> + frame->instr_ptr = next_instr; \ >> + next_instr += 1; \ >> + frame->stackpointer = stack_pointer; \ >> + stack_pointer[-1] = op (N); \ >> + stack_pointer += (SD); \ >> + DISPATCH (); >> + >> +#define ENTRY(N, SD) &&L_##N, >> + >> +void >> +interp (struct frame *frame) >> +{ >> + static const void *const targets[] = { OPS (ENTRY) }; >> + const unsigned short *next_instr = frame->instr_ptr; >> + long *stack_pointer = frame->stackpointer; >> + >> + DISPATCH (); >> + >> + OPS (HANDLER) >> +} >> + >> +/* { dg-final { scan-tree-dump "vectorization is not profitable" "slp1" { >> target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> +/* { dg-final { scan-tree-dump-not "optimized: basic block" "slp1" { target >> { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-13.c >> b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-13.c >> new file mode 100644 >> index 00000000000..54bf0053cb2 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-13.c >> @@ -0,0 +1,31 @@ >> +/* PR tree-optimization/127246 */ >> +/* { dg-do compile } */ >> +/* { dg-additional-options "-fvect-cost-model=dynamic >> -fno-tree-loop-vectorize" } */ >> +/* { dg-additional-options "-mtune=generic" { target aarch64*-*-* i?86-*-* >> x86_64-*-* } } */ >> + >> +/* The vector code of the conditional block costs more than the scalar >> + code it replaces, but the vector code of the block that dominates it >> + saves more, so the loop body stays vectorized. */ >> + >> +extern void use (long, long); >> + >> +void >> +f (long *restrict p, long *restrict q, long *restrict r, long *restrict s, >> + long *restrict t, int *restrict c, int n) >> +{ >> + for (int i = 0; i < n; i++) >> + { >> + long a = q[2 * i] + s[2 * i], b = q[2 * i + 1] + s[2 * i + 1]; >> + p[2 * i] = a; >> + p[2 * i + 1] = b; >> + if (c[i]) >> + { >> + long x = a ^ t[2 * i], y = b ^ t[2 * i + 1]; >> + r[2 * i] = x; >> + r[2 * i + 1] = y; >> + use (x, y); >> + } >> + } >> +} >> + >> +/* { dg-final { scan-tree-dump-times "optimized: basic block part >> vectorized" 2 "slp1" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* >> } && lp64 } } } } */ >> diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-17.c >> b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-17.c >> index a8440b4c478..f99d3ed270c 100644 >> --- a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-17.c >> +++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-17.c >> @@ -5,7 +5,8 @@ >> >> /* The vector of t and t2 is used only by the additions in the conditional >> block, so it is placed with them and not after the calls in the loop >> - header. */ >> + header. So the check of each block of the loop with its dominators does >> + not charge it to the loop header. */ >> >> extern int foo (int), bar (int); >> >> @@ -26,3 +27,4 @@ f (int *restrict out, int *restrict a, int *restrict c, >> int n) >> } >> >> /* { dg-final { scan-tree-dump "placing node \[^\n\r\]* with node" "slp1" { >> target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> +/* { dg-final { scan-tree-dump "optimized: basic block part vectorized" >> "slp1" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } >> } } */ >> diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-2.c >> b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-2.c >> new file mode 100644 >> index 00000000000..18e95e58ca7 >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-2.c >> @@ -0,0 +1,50 @@ >> +/* PR tree-optimization/127246 */ >> +/* { dg-do compile } */ >> +/* { dg-additional-options "-fvect-cost-model=dynamic" } */ >> +/* { dg-additional-options "-mtune=generic" { target aarch64*-*-* i?86-*-* >> x86_64-*-* } } */ >> + >> +/* The switch version of bb-slp-pr127246-1.c. Each iteration runs one case, >> + and the vector construction and the lane extracts of every iteration cost >> + more than the stores of one case save. */ >> + >> +struct frame >> +{ >> + const unsigned short *instr_ptr; >> + long *stackpointer; >> +}; >> + >> +/* Opcode number and stack delta. */ >> +#define OPS(X) \ >> + X (0, -1) X (1, 0) X (2, -1) X (3, 0) X (4, -1) X (5, 0) \ >> + X (6, -1) X (7, 0) X (8, -1) X (9, 0) X (10, -1) X (11, 0) \ >> + X (12, -1) X (13, 0) X (14, -1) X (15, 0) X (16, -1) X (17, 0) \ >> + X (18, -1) X (19, 0) >> + >> +extern long op (long); >> + >> +#define HANDLER(N, SD) \ >> + case N: \ >> + frame->instr_ptr = next_instr; \ >> + next_instr += 1; \ >> + frame->stackpointer = stack_pointer; \ >> + stack_pointer[-1] = op (N); \ >> + stack_pointer += (SD); \ >> + break; >> + >> +long >> +interp (struct frame *frame) >> +{ >> + const unsigned short *next_instr = frame->instr_ptr; >> + long *stack_pointer = frame->stackpointer; >> + for (;;) >> + switch (*next_instr & 0xff) >> + { >> + OPS (HANDLER) >> + default: >> + return stack_pointer[0]; >> + } >> +} >> + >> +/* { dg-final { scan-tree-dump "vectorization is not profitable" "slp1" { >> target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> +/* { dg-final { scan-tree-dump-not "optimized: basic block" "slp1" { target >> { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> +/* { dg-final { scan-tree-dump "and its dominators costs" "slp1" { target { >> { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c >> b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c >> index de2b001a2f5..4a6b63a51f7 100644 >> --- a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c >> +++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-5.c >> @@ -5,7 +5,8 @@ >> >> /* The additions only run in the conditional block, so their vector code >> and the vector of x and y go there and not after the calls in the loop >> - header. */ >> + header. So the check of each block of the loop with its dominators does >> + not charge them to the loop header. */ >> >> extern unsigned short foo (unsigned short); >> >> @@ -31,3 +32,4 @@ f (unsigned short *restrict out, unsigned short *a, int >> *c, int n) >> >> /* { dg-final { scan-tree-dump "placing node \[^\n\r\]* before" "slp1" { >> target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> /* { dg-final { scan-tree-dump "placing node \[^\n\r\]* with node" "slp1" { >> target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> +/* { dg-final { scan-tree-dump "optimized: basic block part vectorized" >> "slp1" { target { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } >> } } */ >> diff --git a/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-9.c >> b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-9.c >> new file mode 100644 >> index 00000000000..99269f3c77f >> --- /dev/null >> +++ b/gcc/testsuite/gcc.dg/vect/bb-slp-pr127246-9.c >> @@ -0,0 +1,25 @@ >> +/* PR tree-optimization/127246 */ >> +/* { dg-do compile } */ >> +/* { dg-additional-options "-fvect-cost-model=dynamic >> -fno-tree-loop-vectorize" } */ >> +/* { dg-additional-options "-mtune=generic" { target aarch64*-*-* i?86-*-* >> x86_64-*-* } } */ >> + >> +/* The loop can leave through its first exit before it reaches the stores. >> + On that path the vector load of a and b and the extraction of both >> + lanes for the exit test are not paid for by the code after it. */ >> + >> +void >> +f (long *restrict p, long *restrict q, long *restrict r, int n) >> +{ >> + for (int i = 0; i < n; i++) >> + { >> + long a = q[2 * i], b = q[2 * i + 1]; >> + if (a == b) >> + break; >> + long c = r[2 * i], d = r[2 * i + 1]; >> + p[2 * i] = ((a + c) ^ (a - c)) + ((a | c) & (a ^ c)); >> + p[2 * i + 1] = ((b + d) ^ (b - d)) + ((b | d) & (b ^ d)); >> + } >> +} >> + >> +/* { dg-final { scan-tree-dump "and its dominators costs" "slp1" { target { >> { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> +/* { dg-final { scan-tree-dump-not "optimized: basic block" "slp1" { target >> { { { aarch64*-*-* || i?86-*-* } || x86_64-*-* } && lp64 } } } } */ >> diff --git a/gcc/testsuite/gcc.target/aarch64/pr127246.c >> b/gcc/testsuite/gcc.target/aarch64/pr127246.c >> new file mode 100644 >> index 00000000000..dfe41393d6e >> --- /dev/null >> +++ b/gcc/testsuite/gcc.target/aarch64/pr127246.c >> @@ -0,0 +1,16 @@ >> +/* PR tree-optimization/127246 */ >> +/* { dg-do compile } */ >> +/* { dg-require-effective-target lp64 } */ >> +/* { dg-options "-O2 -mtune=generic" } */ >> + >> +#pragma GCC target "+nosve" >> + >> +/* Do not move the interpreter state between general and vector registers >> + on every dispatch. */ >> + >> +#include "../../gcc.dg/vect/bb-slp-pr127246-2.c" >> + >> +/* { dg-final { scan-assembler-not {\tins\t} } } */ >> +/* { dg-final { scan-assembler-not {\tumov\t} } } */ >> +/* { dg-final { scan-assembler-not {\tfmov\t} } } */ >> +/* { dg-final { scan-assembler-not {\tdup\t} } } */ >> diff --git a/gcc/tree-vect-slp.cc b/gcc/tree-vect-slp.cc >> index 6fb0b185c18..09d4e2e9b49 100644 >> --- a/gcc/tree-vect-slp.cc >> +++ b/gcc/tree-vect-slp.cc >> @@ -9491,11 +9491,13 @@ vect_slp_prune_covered_roots (slp_tree node, >> hash_set<stmt_vec_info> &roots, >> vect_slp_prune_covered_roots (child, roots, visited); >> } >> >> -/* Hand over COST_VEC to the target COSTS grouped by SLP node. */ >> +/* Hand over COST_VEC to the target COSTS grouped by SLP node. Return the >> + sum of the costs the target records. */ >> >> -static void >> +static unsigned >> add_slp_costs (vector_costs *costs, stmt_vector_for_cost& cost_vec) >> { >> + unsigned sum = 0; >> for (unsigned start = 0; start < cost_vec.length ();) >> { >> unsigned end = start + 1; >> @@ -9503,15 +9505,16 @@ add_slp_costs (vector_costs *costs, >> stmt_vector_for_cost& cost_vec) >> && cost_vec[start].node == cost_vec[end].node) >> end++; >> if (cost_vec[start].node) >> - costs->add_slp_cost (cost_vec[start].node, >> - array_slice<stmt_info_for_cost> >> - (cost_vec.begin () + start, end - start)); >> + sum += costs->add_slp_cost (cost_vec[start].node, >> + array_slice<stmt_info_for_cost> >> + (cost_vec.begin () + start, end - >> start)); > > This assumes that the return value of vector_costs::add_slp_cost (and > ::add_stmt_cost) > is something overall meaningful. But this is only useful for dumps as > the actual > cost is determined only later by targets as overall cost in > ::finish_cost. So I believe > any usage of the return value for actual costing is flawed. > > I know this is a bit unfortunate given how you attempt to use > "dominator costs" below, > and splitting up target costing bits to individual BBs has its own > limitations (PHIs for > one, but lack of a big picture from the target costing side as well). > > There's a good connection between the SLP graph and the SSA graph but less > so between the CFG the SSA graph is layed ontop and the SLP graph before > placing stmts (a SLP node can have defs from multiple BBs), so maybe we have > to abstract from that a bit and instead look to cost paths through the > value graph > rather than the CFG? > > I know you're looking for a solution to an actual performance issue. I know > the > existing code is far from perfect, but I have my doubts this patch or even > [1/2] > makes us go towards a universally better solution. That said, I'm > still thinking > of how the perfect solution would look like, also just having seen an > internal RFC > from Michal who proposes to schedule a SLP node to _different_ places > (OK, for loop vect), allowing dependence distance < VF for a subset of DDRs > by keeping the scalar iteration order for a subset of vector stmts, aka only > interleaving the virtually-VF-unrolled body partly. >
So it turns out that the performance problem in PR127246 has been “fixed” by your commit dd32b9d9d1d74650c0e67dce5c5eef539846638e i.e. Cpython now doesn’t try to vectorize the interpreter cases. So I’m not stressed about landing these patches and am happy for Michal to take this forward properly. From this PR the only patch I’d like to land is the profile info improvement for computed goto edges: https://gcc.gnu.org/pipermail/gcc-patches/2026-October/733193.html But I guess that’s for Honza. Thanks, Kyrill > Richard. > >> else >> - costs->vector_costs::add_slp_cost (cost_vec[start].node, >> - array_slice<stmt_info_for_cost> >> - (cost_vec.begin () + start, end - start)); >> + sum += costs->vector_costs::add_slp_cost (cost_vec[start].node, >> + array_slice<stmt_info_for_cost> >> + (cost_vec.begin () + start, end - >> start)); >> start = end; >> } >> + return sum; >> } >> >> /* Analyze statements in SLP instances of VINFO. Return true if the >> @@ -9860,20 +9863,63 @@ vect_bb_slp_scalar_cost (bb_vec_info vinfo, >> } >> >> >> +/* A cost entry together with the index of the loop and the block containing >> + the code it describes. */ >> + >> +struct li_cost >> +{ >> + unsigned loop; >> + basic_block bb; >> + stmt_info_for_cost *cost; >> +}; >> + >> /* Comparator for the loop-index sorted cost vectors. */ >> >> static int >> li_cost_vec_cmp (const void *a_, const void *b_, void *) >> { >> - auto *a = (const std::pair<unsigned, stmt_info_for_cost *> *)a_; >> - auto *b = (const std::pair<unsigned, stmt_info_for_cost *> *)b_; >> - if (a->first < b->first) >> + auto *a = (const li_cost *)a_; >> + auto *b = (const li_cost *)b_; >> + if (a->loop < b->loop) >> return -1; >> - else if (a->first == b->first) >> + else if (a->loop == b->loop) >> return 0; >> return 1; >> } >> >> +/* Return the vector minus the scalar cost of the code of BB and of the >> + blocks dominating it up to and including TOP, the nearest common >> + dominator of the blocks with costs. BB_COSTS holds that difference for >> + each block with costs. ACC memoizes the result for the blocks visited. >> */ >> + >> +static int >> +vect_bb_slp_dominator_cost (basic_block bb, basic_block top, >> + hash_map<basic_block, int> &bb_costs, >> + hash_map<basic_block, int> &acc) >> +{ >> + auto_vec<basic_block, 16> chain; >> + int cost = 0; >> + for (basic_block b = bb; ; b = get_immediate_dominator (CDI_DOMINATORS, >> b)) >> + { >> + if (int *a = acc.get (b)) >> + { >> + cost = *a; >> + break; >> + } >> + chain.safe_push (b); >> + if (b == top) >> + break; >> + } >> + while (!chain.is_empty ()) >> + { >> + basic_block b = chain.pop (); >> + if (int *c = bb_costs.get (b)) >> + cost += *c; >> + acc.put (b, cost); >> + } >> + return cost; >> +} >> + >> /* Check if vectorization of the basic block is profitable for the >> subgraph denoted by SLP_INSTANCES. */ >> >> @@ -9936,33 +9982,33 @@ vect_bb_vectorization_profitable_p (bb_vec_info >> bb_vinfo, >> loops stmts separately. */ >> >> /* First produce cost vectors sorted by loop index. */ >> - auto_vec<std::pair<unsigned, stmt_info_for_cost *> > >> - li_scalar_costs (scalar_costs.length ()); >> - auto_vec<std::pair<unsigned, stmt_info_for_cost *> > >> - li_vector_costs (vector_costs.length ()); >> + auto_vec<li_cost> li_scalar_costs (scalar_costs.length ()); >> + auto_vec<li_cost> li_vector_costs (vector_costs.length ()); >> stmt_info_for_cost *cost; >> FOR_EACH_VEC_ELT (scalar_costs, i, cost) >> { >> - unsigned l = gimple_bb (cost->stmt_info->stmt)->loop_father->num; >> - li_scalar_costs.quick_push (std::make_pair (l, cost)); >> + basic_block bb = gimple_bb (cost->stmt_info->stmt); >> + li_scalar_costs.quick_push ({ (unsigned) bb->loop_father->num, bb, >> + cost }); >> } >> - /* Use a random used loop as fallback in case the first vector_costs >> + /* Use a random used block as fallback in case the first vector_costs >> entry does not have a location associated with it. */ >> - unsigned l = li_scalar_costs[0].first; >> + basic_block bb = li_scalar_costs[0].bb; >> FOR_EACH_VEC_ELT (vector_costs, i, cost) >> { >> /* Use SLP node placement according to the computed schedule. */ >> if (cost->node && cost->node->si) >> - l = gimple_bb (cost->node->si)->loop_father->num; >> - /* For schedules at region boundary use the region entry loop. */ >> + bb = gimple_bb (cost->node->si); >> + /* For schedules at region boundary use the region entry block. */ >> else if (cost->node) >> - l = bb_vinfo->bbs[0]->loop_father->num; >> + bb = bb_vinfo->bbs[0]; >> /* SLP instance root stmts do not have an associated SLP node. */ >> else if (cost->stmt_info) >> - l = gimple_bb (cost->stmt_info->stmt)->loop_father->num; >> + bb = gimple_bb (cost->stmt_info->stmt); >> /* And since vect_prologue_cost_for_slp can end up costing with >> neither, inherit from the previous node. */ >> - li_vector_costs.quick_push (std::make_pair (l, cost)); >> + li_vector_costs.quick_push ({ (unsigned) bb->loop_father->num, bb, >> + cost }); >> } >> li_scalar_costs.stablesort (li_cost_vec_cmp, NULL); >> li_vector_costs.stablesort (li_cost_vec_cmp, NULL); >> @@ -9979,9 +10025,12 @@ vect_bb_vectorization_profitable_p (bb_vec_info >> bb_vinfo, >> || vi < li_vector_costs.length ()) >> { >> unsigned sl >> - = si < li_scalar_costs.length () ? li_scalar_costs[si].first : -1U; >> + = si < li_scalar_costs.length () ? li_scalar_costs[si].loop : -1U; >> unsigned vl >> - = vi < li_vector_costs.length () ? li_vector_costs[vi].first : -1U; >> + = vi < li_vector_costs.length () ? li_vector_costs[vi].loop : -1U; >> + /* The vector minus the scalar cost of the code of each block of the >> + part. */ >> + hash_map<basic_block, int> bb_costs; >> >> class vector_costs *scalar_target_cost_data = nullptr; >> scalar_cost = 0; >> @@ -9993,12 +10042,13 @@ vect_bb_vectorization_profitable_p (bb_vec_info >> bb_vinfo, >> scalar_target_cost_data = init_cost (bb_vinfo, true); >> do >> { >> - add_stmt_cost (scalar_target_cost_data, >> - li_scalar_costs[si].second); >> + bb_costs.get_or_insert (li_scalar_costs[si].bb) >> + -= (int) add_stmt_cost (scalar_target_cost_data, >> + li_scalar_costs[si].cost); >> si++; >> } >> while (si < li_scalar_costs.length () >> - && li_scalar_costs[si].first == sl); >> + && li_scalar_costs[si].loop == sl); >> scalar_target_cost_data->finish_cost (nullptr); >> scalar_cost = scalar_target_cost_data->body_cost (); >> total_scalar_cost += scalar_cost; >> @@ -10021,12 +10071,19 @@ vect_bb_vectorization_profitable_p (bb_vec_info >> bb_vinfo, >> auto_vec<stmt_info_for_cost> tem; >> do >> { >> - tem.safe_push (*li_vector_costs[vi].second); >> - vi++; >> + /* Hand over the costs of one block at a time to record their sum >> + in BB_COSTS. */ >> + bb = li_vector_costs[vi].bb; >> + tem.truncate (0); >> + do >> + tem.safe_push (*li_vector_costs[vi++].cost); >> + while (vi < li_vector_costs.length () >> + && li_vector_costs[vi].bb == bb); >> + bb_costs.get_or_insert (bb) >> + += (int) add_slp_costs (vect_target_cost_data, tem); >> } >> while (vi < li_vector_costs.length () >> - && li_vector_costs[vi].first == vl); >> - add_slp_costs (vect_target_cost_data, tem); >> + && li_vector_costs[vi].loop == vl); >> vect_target_cost_data->finish_cost (scalar_target_cost_data); >> vec_prologue_cost = vect_target_cost_data->prologue_cost (); >> vec_inside_cost = vect_target_cost_data->body_cost (); >> @@ -10067,6 +10124,41 @@ vect_bb_vectorization_profitable_p (bb_vec_info >> bb_vinfo, >> example). */ >> if (vec_outside_cost + vec_inside_cost > scalar_cost) >> profitable = false; >> + /* The code of a block in the loop runs together with the code of the >> + blocks dominating it in the loop, but not necessarily with the code >> + of other blocks of the loop. So require each block with costs to be >> + profitable together with its dominators in the loop. The costs the >> + target records for single stmts do not include its final >> + adjustments, so leave parts with costs in one block to the check >> + above. */ >> + else if (bb_costs.elements () > 1) >> + { >> + basic_block top = NULL; >> + for (auto p : bb_costs) >> + top = nearest_common_dominator (CDI_DOMINATORS, top, p.first); >> + hash_map<basic_block, int> acc; >> + basic_block fail_bb = NULL; >> + int fail_cost = 0; >> + for (auto p : bb_costs) >> + { >> + int excess = vect_bb_slp_dominator_cost (p.first, top, >> bb_costs, >> + acc); >> + if (excess > 0 && (!fail_bb || p.first->index < >> fail_bb->index)) >> + { >> + fail_bb = p.first; >> + fail_cost = excess; >> + } >> + } >> + if (fail_bb) >> + { >> + if (dump_enabled_p ()) >> + dump_printf_loc (MSG_NOTE, vect_location, >> + " The vector code of bb%d and its >> dominators " >> + "costs %d more than the scalar code\n", >> + fail_bb->index, fail_cost); >> + profitable = false; >> + } >> + } >> } >> >> if (dump_enabled_p ()) >> diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h >> index 19b8cfafed0..c2fd8bd04cc 100644 >> --- a/gcc/tree-vectorizer.h >> +++ b/gcc/tree-vectorizer.h >> @@ -1817,7 +1817,9 @@ public: >> vect_cost_model_location where); >> >> /* Update the costs in response to adding costs in V which are all from >> - vectorizing NODE to the respective part. */ >> + vectorizing NODE to the respective part. Return the calculated cost >> + as well as recording it. BB vectorization adds up the returned costs >> + for each block. */ >> virtual unsigned int add_slp_cost (slp_tree node, >> const array_slice<stmt_info_for_cost> >> &v); >> >> -- >> 2.54.0 (Apple Git-157)
