[X86][GlobalISel] Legalizer livelocks on a <2 x i32> G_BUILD_VECTOR instead of reporting it
Summary
llc -global-isel never returns when legalizing a <2 x i32> vector multiply
on a default x86_64 triple. Unlike the G_BUILD_VECTOR failures in #216655
this is not an abort: the legalizer livelocks, so -global-isel-abort=1/2
cannot cut it and no diagnostic is ever printed.
Reproducer
define void @f(ptr %p, ptr %q) {
%a = load <2 x i32>, ptr %p
%b = load <2 x i32>, ptr %q
%r = mul <2 x i32> %a, %b
store <2 x i32> %r, ptr %p
ret void
}
$ llc -mtriple=x86_64-unknown-linux-gnu -global-isel -O0 repro.ll -o /dev/null
# never returns
Measured on upstream/main faba310ff3e76fb8fd9d279d01d7af98943ef044
(LLVM 24.0.0git, assertions on):
| command | rc |
|---|---|
-global-isel -O0 |
124 (killed by timeout 60) |
-global-isel -O0 -global-isel-abort=1 |
124 |
-global-isel -O0 -global-isel-abort=2 |
124 |
SelectionDAG, same input (-O0) |
0 |
-global-isel -O0 -stop-before=legalizer |
0 (the MIR reaching the legalizer is fine) |
-mtriple=aarch64-unknown-linux-gnu -global-isel -O0 |
0 |
-mtriple=riscv64-unknown-linux-gnu -global-isel -O0 |
1 (unable to legalize) |
The subtarget decides whether it hangs or errors cleanly:
-mattr |
result |
|---|---|
| (default: SSE2) | hang |
+sse2 |
hang |
+sse4.1 / +avx / +avx2 / +avx512f |
1, unable to legalize instruction: %8:_(<4 x s32>) = G_BUILD_VECTOR ... |
What the legalizer does
-run-pass=legalizer -debug-only=legalizer, 30 s cap: 12,947 iterations,
12,946 rewrites of the same <2 x s32> G_BUILD_VECTOR, and
Increase number of elements 12,947 times against Reduce number of elements
once (the G_MUL, at the very start). The 30 s trace is 31 MB.
One cycle, verbatim:
Legalizing: %v2:_(<2 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32)
.. Increase number of elements
.. .. Erasing: %v2:_(<2 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32)
.. .. New MI: %34:_(s32) = G_IMPLICIT_DEF
.. .. New MI: %35:_(<4 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32), %34:_(s32), %34:_(s32)
.. .. New MI: %36:_(s32), %37:_(s32), %38:_(s32), %39:_(s32) = G_UNMERGE_VALUES %35:_(<4 x s32>)
.. .. New MI: %v2:_(<2 x s32>) = G_BUILD_VECTOR %36:_(s32), %37:_(s32)
Trying to combine: %36:_(s32), %37:_(s32), %38:_(s32), %39:_(s32) = G_UNMERGE_VALUES %35:_(<4 x s32>)
.. .. Changing MI: %v2:_(<2 x s32>) = G_BUILD_VECTOR %36:_(s32), %37:_(s32)
.. .. Changed MI: %v2:_(<2 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32)
.. .. Erasing: %35:_(<4 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32), %34:_(s32), %34:_(s32)
.. .. Erasing: %34:_(s32) = G_IMPLICIT_DEF
=== New Iteration === <-- and round we go
Why it never terminates
- X86's
G_BUILD_VECTORrules (X86LegalizerInfo.cpp, around line 551) push<2 x s32>up to<4 x s32>:clampNumElements(0, v4s32, s32MaxVector)supplies the minimum number of elements, andcustomIfnames only the exact legal widths (v4s32, ...), so<2 x s32>itself matches no rule that answers it. LegalizerHelper::moreElementsVectorforG_BUILD_VECTOR(LegalizerHelper.cpp, thecase TargetOpcode::G_BUILD_VECTORbranch in themoreElementsVectorswitch) pads withundefto<4 x s32>and callsMIRBuilder.buildDeleteTrailingVectorElements, which emitsG_UNMERGE_VALUESplus a new<2 x s32>G_BUILD_VECTOR.LegalizationArtifactCombinerseesG_UNMERGE_VALUESof that paddedG_BUILD_VECTORand folds its sources straight back, which rewrites the new<2 x s32>build vector to exactly the instruction step 2 erased. The padded<4 x s32>build vector then has no users and is erased too —grep 'Legalizing: %.*_(<4 x s32>) = G_BUILD_VECTOR'over the trace returns nothing, so it is never legalized.
Net effect: the legalization step produces no change, and the worklist feeds the
same query back forever. The existing no-progress guard does not see it:
hasNoSimpleLoops() inspects only the rule's mutation (it compares the
requested type against the query's own type), and here the mutation really is a
widening. The cycle closes one phase later, in the artifact combiner.
A smaller MIR-only form
The loop needs no vector multiply. This MIR livelocks on its own:
--- |
target datalayout = "e-m:e-i64:64-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define void @f() { ret void }
---
name: f
body: |
bb.0:
%0:_(s32) = G_IMPLICIT_DEF
%1:_(s32) = G_IMPLICIT_DEF
%2:_(<2 x s32>) = G_BUILD_VECTOR %0:_(s32), %1:_(s32)
%3:_(p0) = G_IMPLICIT_DEF
G_STORE %2:_(<2 x s32>), %3:_(p0) :: (store (<2 x s32>), align 8)
$ llc -mtriple=x86_64-unknown-linux-gnu -global-isel -run-pass=legalizer repro.mir -o /dev/null
# never returns (over 9,000 iterations in 20 s)
Scope / related issues
- #216655 is the same family (
G_MULscalarize →G_BUILD_VECTOR) but a different symptom: it aborts withunable to legalize, and its proposed fix #225265 does not touch this path. I built #225265: this reproducer is still rc=124 there. Same with #235976 applied on top ofupstream/main. - #143099 ("X86
legalizeBuildVectoronly handles constant build vectors") is the missing capability underneath, but the livelock is a separate defect: the legalizer should report that it cannot legalize, not spin.
Possible directions
Either stop moreElementsVector from choosing an encoding the artifact combiner
immediately undoes (keep the widened vector live, or make
buildDeleteTrailingVectorElements not re-create the same query), or give X86 a
rule for the narrow <2 x s32> / <2 x s64> build vector so the query is
answered instead of widened. Either way a default -O0 x86_64 triple must not
livelock.
Found while sweeping X86 GlobalISel legality queries with a model-checking tool.
The IR above was verified well formed: SelectionDAG compiles it and
-stop-before=legalizer produces the expected MIR.
@llvm/issue-subscribers-backend-x86
Author: nodecoda (nodecoda)
llc -global-isel never returns when legalizing a <2 x i32> vector multiply
on a default x86_64 triple. Unlike the G_BUILD_VECTOR failures in #216655
this is not an abort: the legalizer livelocks, so -global-isel-abort=1/2
cannot cut it and no diagnostic is ever printed.
Reproducer
define void @<!-- -->f(ptr %p, ptr %q) {
%a = load <2 x i32>, ptr %p
%b = load <2 x i32>, ptr %q
%r = mul <2 x i32> %a, %b
store <2 x i32> %r, ptr %p
ret void
}
$ llc -mtriple=x86_64-unknown-linux-gnu -global-isel -O0 repro.ll -o /dev/null
# never returns
Measured on upstream/main faba310ff3e76fb8fd9d279d01d7af98943ef044
(LLVM 24.0.0git, assertions on):
| command | rc |
|---|---|
-global-isel -O0 |
124 (killed by timeout 60) |
-global-isel -O0 -global-isel-abort=1 |
124 |
-global-isel -O0 -global-isel-abort=2 |
124 |
SelectionDAG, same input (-O0) |
0 |
-global-isel -O0 -stop-before=legalizer |
0 (the MIR reaching the legalizer is fine) |
-mtriple=aarch64-unknown-linux-gnu -global-isel -O0 |
0 |
-mtriple=riscv64-unknown-linux-gnu -global-isel -O0 |
1 (unable to legalize) |
The subtarget decides whether it hangs or errors cleanly:
-mattr |
result |
|---|---|
| (default: SSE2) | hang |
+sse2 |
hang |
+sse4.1 / +avx / +avx2 / +avx512f |
1, unable to legalize instruction: %8:_(<4 x s32>) = G_BUILD_VECTOR ... |
What the legalizer does
-run-pass=legalizer -debug-only=legalizer, 30 s cap: 12,947 iterations,
12,946 rewrites of the same <2 x s32> G_BUILD_VECTOR, and
Increase number of elements 12,947 times against Reduce number of elements
once (the G_MUL, at the very start). The 30 s trace is 31 MB.
One cycle, verbatim:
Legalizing: %v2:_(<2 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32)
.. Increase number of elements
.. .. Erasing: %v2:_(<2 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32)
.. .. New MI: %34:_(s32) = G_IMPLICIT_DEF
.. .. New MI: %35:_(<4 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32), %34:_(s32), %34:_(s32)
.. .. New MI: %36:_(s32), %37:_(s32), %38:_(s32), %39:_(s32) = G_UNMERGE_VALUES %35:_(<4 x s32>)
.. .. New MI: %v2:_(<2 x s32>) = G_BUILD_VECTOR %36:_(s32), %37:_(s32)
Trying to combine: %36:_(s32), %37:_(s32), %38:_(s32), %39:_(s32) = G_UNMERGE_VALUES %35:_(<4 x s32>)
.. .. Changing MI: %v2:_(<2 x s32>) = G_BUILD_VECTOR %36:_(s32), %37:_(s32)
.. .. Changed MI: %v2:_(<2 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32)
.. .. Erasing: %35:_(<4 x s32>) = G_BUILD_VECTOR %24:_(s32), %25:_(s32), %34:_(s32), %34:_(s32)
.. .. Erasing: %34:_(s32) = G_IMPLICIT_DEF
=== New Iteration === <-- and round we go
Why it never terminates
- X86's
G_BUILD_VECTORrules (X86LegalizerInfo.cpp, around line 551) push<2 x s32>up to<4 x s32>:clampNumElements(0, v4s32, s32MaxVector)supplies the minimum number of elements, andcustomIfnames only the exact legal widths (v4s32, ...), so<2 x s32>itself matches no rule that answers it. LegalizerHelper::moreElementsVectorforG_BUILD_VECTOR(LegalizerHelper.cpp, thecase TargetOpcode::G_BUILD_VECTORbranch in themoreElementsVectorswitch) pads withundefto<4 x s32>and callsMIRBuilder.buildDeleteTrailingVectorElements, which emitsG_UNMERGE_VALUESplus a new<2 x s32>G_BUILD_VECTOR.LegalizationArtifactCombinerseesG_UNMERGE_VALUESof that paddedG_BUILD_VECTORand folds its sources straight back, which rewrites the new<2 x s32>build vector to exactly the instruction step 2 erased. The padded<4 x s32>build vector then has no users and is erased too —grep 'Legalizing: %.*_(<4 x s32>) = G_BUILD_VECTOR'over the trace returns nothing, so it is never legalized.
Net effect: the legalization step produces no change, and the worklist feeds the
same query back forever. The existing no-progress guard does not see it:
hasNoSimpleLoops() inspects only the rule's mutation (it compares the
requested type against the query's own type), and here the mutation really is a
widening. The cycle closes one phase later, in the artifact combiner.
A smaller MIR-only form
The loop needs no vector multiply. This MIR livelocks on its own:
--- |
target datalayout = "e-m:e-i64:64-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define void @<!-- -->f() { ret void }
---
name: f
body: |
bb.0:
%0:_(s32) = G_IMPLICIT_DEF
%1:_(s32) = G_IMPLICIT_DEF
%2:_(<2 x s32>) = G_BUILD_VECTOR %0:_(s32), %1:_(s32)
%3:_(p0) = G_IMPLICIT_DEF
G_STORE %2:_(<2 x s32>), %3:_(p0) :: (store (<2 x s32>), align 8)
$ llc -mtriple=x86_64-unknown-linux-gnu -global-isel -run-pass=legalizer repro.mir -o /dev/null
# never returns (over 9,000 iterations in 20 s)
Scope / related issues
- #216655 is the same family (
G_MULscalarize →G_BUILD_VECTOR) but a different symptom: it aborts withunable to legalize, and its proposed fix #225265 does not touch this path. I built #225265: this reproducer is still rc=124 there. Same with #235976 applied on top ofupstream/main. - #143099 ("X86
legalizeBuildVectoronly handles constant build vectors") is the missing capability underneath, but the livelock is a separate defect: the legalizer should report that it cannot legalize, not spin.
Possible directions
Either stop moreElementsVector from choosing an encoding the artifact combiner
immediately undoes (keep the widened vector live, or make
buildDeleteTrailingVectorElements not re-create the same query), or give X86 a
rule for the narrow <2 x s32> / <2 x s64> build vector so the query is
answered instead of widened. Either way a default -O0 x86_64 triple must not
livelock.
Found while sweeping X86 GlobalISel legality queries with a model-checking tool.
The IR above was verified well formed: SelectionDAG compiles it and
-stop-before=legalizer produces the expected MIR.
Additional data on the same rule gap, measured on ae47e0668aed (LLVM 24.0.0git, assertions on), x86_64-unknown-linux-gnu, llc -O0 -global-isel.
A register-only reproducer aborts instead of hanging, so the symptom depends on the shape of the MIR, not only on the subtarget:
define <2 x i32> @f(<2 x i32> %a, <2 x i32> %b) {
%m = mul <2 x i32> %a, %b
ret <2 x i32> %m
}
$ llc -global-isel -O0 f.ll -o /dev/null
LLVM ERROR: unable to legalize instruction: %7:_(<4 x s32>) = G_BUILD_VECTOR %30:_(s32), %31:_(s32), %10:_(s32), %10:_(s32) (in function: f)
rc=1
No timeout needed — it returns immediately. Drop the loads/stores and the livelock becomes a clean abort.
Scope over element widths. One function per file, timeout 25, same build:
| IR | result |
|---|---|
add/sub/and/or/xor on <2 x i32>, <4 x i32>, <8 x i16> |
rc=0 |
mul <4 x i16>, mul <8 x i16> |
rc=0 |
mul <4 x i32> |
rc=1, G_BUILD_VECTOR (<4 x s32>) |
mul <2 x i32> |
rc=1, G_BUILD_VECTOR (<4 x s32>) |
mul <2 x i64> |
rc=1, G_BUILD_VECTOR (<2 x s64>) |
mul <8 x i8> |
rc=1, G_BUILD_VECTOR (<16 x s8>) |
mul <16 x i8> |
rc=1, G_BUILD_VECTOR (<16 x s8>) |
The failing set is exactly the lane counts with no packed multiply instruction: 32-bit lanes at 2 and 4, 64-bit at 2, 8-bit at 8 and 16. <8 x i16> has pmullw and works. That makes this the same root as #216655, generalised past byte lanes: the G_BUILD_VECTOR the legalizer creates while reassembling the widened or scalarized lanes has no legality rule of its own.
G_SDIV and G_SHL also fail for every one of these types in the same build, but those look like separate gaps and I have not investigated them.
Not a regression: two independently built binaries from the same source behave identically here.
(Written with AI assistance; every command above is reproducible as shown.)