https://gcc.gnu.org/g:bb55bb62fc8a8eabb928653fc38cbe1fde3ab773
commit r17-5808-gbb55bb62fc8a8eabb928653fc38cbe1fde3ab773 Author: Kyrylo Tkachov <[email protected]> Date: Thu Oct 15 20:15:30 2026 +0200 RISC-V: Avoid vsetvl insertion on shared table edges [PR middle-end/126236] Late vsetvl PRE can insert a configuration on an edge from a shared jump-table dispatch. Such an edge cannot be split for one dispatch without also changing the other users of the table. Keep the local configuration in blocks with incoming shared-table edges, using the existing invalid_opt_bb_p check. RISC-V shares jump tables, and without this patch the new test fails with an ICE in rtl_split_edge. With it, the test passes at VLEN 128 and 256. The test also checks that its dispatches share their tables. gcc/ChangeLog: PR middle-end/126236 * config/riscv/riscv-vsetvl.cc (invalid_opt_bb_p): Keep local configurations in blocks with incoming shared-table edges. gcc/testsuite/ChangeLog: PR middle-end/126236 * gcc.target/riscv/rvv/vsetvl/pr126236.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]> Diff: --- gcc/config/riscv/riscv-vsetvl.cc | 7 +- .../gcc.target/riscv/rvv/vsetvl/pr126236.c | 117 +++++++++++++++++++++ 2 files changed, 122 insertions(+), 2 deletions(-) diff --git a/gcc/config/riscv/riscv-vsetvl.cc b/gcc/config/riscv/riscv-vsetvl.cc index 4db4349a6e1d..342087e8ab10 100644 --- a/gcc/config/riscv/riscv-vsetvl.cc +++ b/gcc/config/riscv/riscv-vsetvl.cc @@ -781,9 +781,12 @@ invalid_opt_bb_p (basic_block cfg_bb) edge e; edge_iterator ei; - /* We don't do LCM optimizations on complex edges. */ + /* Keep local configurations when an incoming edge cannot be split for + a vsetvl insertion. */ FOR_EACH_EDGE (e, ei, cfg_bb->preds) - if (e->flags & EDGE_COMPLEX) + if ((e->flags & EDGE_COMPLEX) + || (e->src != ENTRY_BLOCK_PTR_FOR_FN (cfun) + && shared_tablejump_p (BB_END (e->src), nullptr, nullptr))) return true; /* We only do LCM optimizations on blocks that are post dominated by diff --git a/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c new file mode 100644 index 000000000000..9e84d5837af2 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/vsetvl/pr126236.c @@ -0,0 +1,117 @@ +/* PR middle-end/126236 */ +/* { dg-do run { target { riscv_v } } } */ +/* { dg-additional-options "-std=c99 -O2 -fduplicate-switch-dispatch -mno-explicit-relocs -fdump-rtl-compgotos" } */ + +/* Execute each ordered pair of handlers with different vector configurations. + The reference uses the same unsigned operations without vectorization. */ + +struct state +{ + unsigned char byte[128]; + unsigned short half[64]; + unsigned int word[32]; + unsigned long long wide[16]; +}; + +static struct state actual __attribute__((aligned (64))); +static struct state expected __attribute__((aligned (64))); + +#define DISPATCH_BODY \ + for (;;) \ + switch (*pc++) \ + { \ + case 0: return; \ + case 1: \ + for (unsigned i = 0; i < 128; ++i) \ + s->byte[i] += 3; \ + break; \ + case 2: \ + for (unsigned i = 0; i < 64; ++i) \ + s->half[i] ^= 0x1357; \ + break; \ + case 3: \ + for (unsigned i = 0; i < 32; ++i) \ + s->word[i] += 0x1234567u; \ + break; \ + case 4: \ + for (unsigned i = 0; i < 16; ++i) \ + s->wide[i] ^= 0x13579bdf02468aceull; \ + break; \ + case 5: \ + for (unsigned i = 0; i < 57; ++i) \ + s->byte[i] ^= 0x5a; \ + break; \ + case 6: \ + for (unsigned i = 0; i < 31; ++i) \ + s->half[i] += 71; \ + break; \ + case 7: \ + for (unsigned i = 0; i < 13; ++i) \ + s->word[i] ^= 0x2468ace0u; \ + break; \ + case 8: \ + for (unsigned i = 0; i < 5; ++i) \ + s->wide[i] += 0x102030405060708ull; \ + break; \ + default: return; \ + } + +__attribute__((noinline, noclone)) +static void +vector_dispatch (struct state *s, const unsigned char *pc) +{ + DISPATCH_BODY +} + +__attribute__((noinline, noclone, optimize ("no-tree-vectorize"))) +static void +scalar_dispatch (struct state *s, const unsigned char *pc) +{ + DISPATCH_BODY +} + +int +main (void) +{ + unsigned char code[129]; + unsigned pos = 0; + for (unsigned first = 1; first <= 8; ++first) + for (unsigned second = 1; second <= 8; ++second) + { + code[pos++] = first; + code[pos++] = second; + } + code[pos] = 0; + + for (unsigned i = 0; i < 128; ++i) + actual.byte[i] = expected.byte[i] = i * 7 + 1; + for (unsigned i = 0; i < 64; ++i) + actual.half[i] = expected.half[i] = i * 257 + 3; + for (unsigned i = 0; i < 32; ++i) + actual.word[i] = expected.word[i] = i * 0x1234567u + 5; + for (unsigned i = 0; i < 16; ++i) + actual.wide[i] = expected.wide[i] = i * 0x123456789abcdefull + 7; + + for (unsigned round = 0; round < 12; ++round) + { + vector_dispatch (&actual, code); + scalar_dispatch (&expected, code); + } + + for (unsigned i = 0; i < 128; ++i) + if (actual.byte[i] != expected.byte[i]) + return 1; + for (unsigned i = 0; i < 64; ++i) + if (actual.half[i] != expected.half[i]) + return 2; + for (unsigned i = 0; i < 32; ++i) + if (actual.word[i] != expected.word[i]) + return 3; + for (unsigned i = 0; i < 16; ++i) + if (actual.wide[i] != expected.wide[i]) + return 4; + return 0; +} + +/* { dg-final { scan-rtl-dump "Duplicating dispatch" "compgotos" { target { no-opts "-flto" } } } } */ +/* { dg-final { scan-rtl-dump {addr_(?:diff_)?vec/u} "compgotos" { target { no-opts "-flto" } } } } */
