llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[X86][GlobalISel] Legalizer crashes legalizing its own G_BUILD_VECTOR after scalarizing byte-lane G_MUL

Open isanbard opened this issue 3 months ago • 4 comments

Description

llc -mtriple=x86_64-* -global-isel aborts with "unable to legalize instruction" on an ordinary byte-lane vector multiply. G_MUL correctly resolves to FewerElements/scalarize for <16 x i8> (X86 has no packed byte-multiply instruction), but the G_BUILD_VECTOR the Legalizer itself creates to reassemble the scalarized lanes has no legality rule and the Legalizer crashes trying to legalize its own output.

Reproducer

define <16 x i8> @vecmul16(<16 x i8> %a, <16 x i8> %b) {
  %r = mul <16 x i8> %a, %b
  ret <16 x i8> %r
}
$ llc -mtriple=x86_64-unknown-linux-gnu -global-isel repro.ll -o -
LLVM ERROR: unable to legalize instruction: %2:_(<16 x s8>) = G_BUILD_VECTOR %35:_(s8), %36:_(s8), %37:_(s8), %38:_(s8), %39:_(s8), %40:_(s8), %41:_(s8), %42:_(s8), %43:_(s8), %44:_(s8), %45:_(s8), %46:_(s8), %47:_(s8), %48:_(s8), %49:_(s8), %50:_(s8) (in function: vecmul16)

Not specific to <16 x i8> — a smaller vector like <4 x i8> hits the same crash after argument-lowering widens it up to 16 lanes:

define <4 x i8> @vecmul4(<4 x i8> %a, <4 x i8> %b) {
  %r = mul <4 x i8> %a, %b
  ret <4 x i8> %r
}
LLVM ERROR: unable to legalize instruction: %11:_(<16 x s8>) = G_BUILD_VECTOR ...

Analysis

  • Traced the rule chain in X86LegalizerInfo.cpp: G_MUL's rules (legalFor/clampMinNumElements/clampMaxNumElements) never mention an s8 element type at all (x86 has no packed byte-lane multiply), so scalarize(0) — the catch-all isVector rule — fires for byte-element vectors. That correctly produces G_UNMERGE_VALUES → 16x scalar G_MUL → G_BUILD_VECTOR. But G_BUILD_VECTOR itself apparently has no legality rule for a 16-lane s8 destination, so the Legalizer's own worklist, now processing the instruction it just inserted, aborts.
  • I independently confirmed a correct instruction sequence for this exact case does exist and is reachable: an experimental MLIR-based instruction-selection frontend I'm working on (llc -enable-mlir-isel, in an unrelated fork) produces the same initial G_UNMERGE_VALUES/16x G_MUL/G_BUILD_VECTOR shape via a different front-end path, and the same downstream Legalizer/Combiner/RegBankSelect/InstructionSelect passes turn it into correct code (the standard SSE2 byte-multiply idiom: widen each byte to a word via punpcklbw/punpckhbw, pmullw, mask with pand, pack back down with packuswb). So the target-specific pieces needed to finish legalizing/selecting this shape exist and work when reached — something about the Legalizer's own pass reaching G_BUILD_VECTOR from its own scalarize action specifically hits the crash, rather than the shape being fundamentally unsupported.
  • With graceful fallback (-global-isel-abort=0), this silently and correctly falls back to SelectionDAG for the affected function — no user-visible failure. The hard abort above only reproduces under strict abort-on-failure mode, which is llc -global-isel's default (same caveat as issue #216648).

Filing this as a heads-up for anyone working on X86 GlobalISel's vector legalization, not as an urgent/user-facing issue — I don't believe this affects any real user today given the point above.

Environment

Reproduced at commit 513a02ee150335a84c39ab2e2d0f965f1b0e260e (llvm-project main). Confirmed via git diff that llvm/lib/Target/X86/GISel/, llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp, and llvm/lib/CodeGen/GlobalISel/LegalizerInfo.cpp are unmodified relative to that commit in the branch this was found on, so this should reproduce unmodified on upstream main.

isanbard avatar Aug 17 '26 08:08 isanbard

@llvm/issue-subscribers-backend-x86

Author: Bill Wendling (isanbard)

## Description

llc -mtriple=x86_64-* -global-isel aborts with "unable to legalize instruction" on an ordinary byte-lane vector multiply. G_MUL correctly resolves to FewerElements/scalarize for &lt;16 x i8&gt; (X86 has no packed byte-multiply instruction), but the G_BUILD_VECTOR the Legalizer itself creates to reassemble the scalarized lanes has no legality rule and the Legalizer crashes trying to legalize its own output.

Reproducer

define &lt;16 x i8&gt; @<!-- -->vecmul16(&lt;16 x i8&gt; %a, &lt;16 x i8&gt; %b) {
  %r = mul &lt;16 x i8&gt; %a, %b
  ret &lt;16 x i8&gt; %r
}
$ llc -mtriple=x86_64-unknown-linux-gnu -global-isel repro.ll -o -
LLVM ERROR: unable to legalize instruction: %2:_(&lt;16 x s8&gt;) = G_BUILD_VECTOR %35:_(s8), %36:_(s8), %37:_(s8), %38:_(s8), %39:_(s8), %40:_(s8), %41:_(s8), %42:_(s8), %43:_(s8), %44:_(s8), %45:_(s8), %46:_(s8), %47:_(s8), %48:_(s8), %49:_(s8), %50:_(s8) (in function: vecmul16)

Not specific to &lt;16 x i8&gt; — a smaller vector like &lt;4 x i8&gt; hits the same crash after argument-lowering widens it up to 16 lanes:

define &lt;4 x i8&gt; @<!-- -->vecmul4(&lt;4 x i8&gt; %a, &lt;4 x i8&gt; %b) {
  %r = mul &lt;4 x i8&gt; %a, %b
  ret &lt;4 x i8&gt; %r
}
LLVM ERROR: unable to legalize instruction: %11:_(&lt;16 x s8&gt;) = G_BUILD_VECTOR ...

Analysis

  • Traced the rule chain in X86LegalizerInfo.cpp: G_MUL's rules (legalFor/clampMinNumElements/clampMaxNumElements) never mention an s8 element type at all (x86 has no packed byte-lane multiply), so scalarize(0) — the catch-all isVector rule — fires for byte-element vectors. That correctly produces G_UNMERGE_VALUES → 16x scalar G_MUL → G_BUILD_VECTOR. But G_BUILD_VECTOR itself apparently has no legality rule for a 16-lane s8 destination, so the Legalizer's own worklist, now processing the instruction it just inserted, aborts.
  • I independently confirmed a correct instruction sequence for this exact case does exist and is reachable: an experimental MLIR-based instruction-selection frontend I'm working on (llc -enable-mlir-isel, in an unrelated fork) produces the same initial G_UNMERGE_VALUES/16x G_MUL/G_BUILD_VECTOR shape via a different front-end path, and the same downstream Legalizer/Combiner/RegBankSelect/InstructionSelect passes turn it into correct code (the standard SSE2 byte-multiply idiom: widen each byte to a word via punpcklbw/punpckhbw, pmullw, mask with pand, pack back down with packuswb). So the target-specific pieces needed to finish legalizing/selecting this shape exist and work when reached — something about the Legalizer's own pass reaching G_BUILD_VECTOR from its own scalarize action specifically hits the crash, rather than the shape being fundamentally unsupported.
  • With graceful fallback (-global-isel-abort=0), this silently and correctly falls back to SelectionDAG for the affected function — no user-visible failure. The hard abort above only reproduces under strict abort-on-failure mode, which is llc -global-isel's default (same caveat as issue #216648).

Filing this as a heads-up for anyone working on X86 GlobalISel's vector legalization, not as an urgent/user-facing issue — I don't believe this affects any real user today given the point above.

Environment

Reproduced at commit 513a02ee150335a84c39ab2e2d0f965f1b0e260e (llvm-project main). Confirmed via git diff that llvm/lib/Target/X86/GISel/, llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp, and llvm/lib/CodeGen/GlobalISel/LegalizerInfo.cpp are unmodified relative to that commit in the branch this was found on, so this should reproduce unmodified on upstream main.

Opened a fix: null

Root cause: G_MUL of <16 x i8> scalarized into lane muls plus a non-constant G_BUILD_VECTOR, which custom legalization previously rejected. After a stack fallback for that BUILD_VECTOR, selection still failed on G_UNMERGE_VALUES of <16 x s8>.

The PR custom-lowers v16s8 G_MUL through a defined stack lane-wise path (and keeps the BUILD_VECTOR fallback as defense in depth). Issue repro now passes with -global-isel-abort=1.

agourakis82 avatar Sep 22 '26 02:09 agourakis82

Correction / concrete PR link: https://github.com/llvm/llvm-project/pull/225265

Root cause: G_MUL of <16 x i8> scalarized into lane muls plus a non-constant G_BUILD_VECTOR, which custom legalization previously rejected. After a stack fallback for that BUILD_VECTOR, selection still failed on G_UNMERGE_VALUES of <16 x s8>.

PR #225265 custom-lowers v16s8 G_MUL through a defined stack lane-wise path (and keeps the BUILD_VECTOR fallback as defense in depth). The issue repro now passes with -global-isel-abort=1.

agourakis82 avatar Sep 22 '26 02:09 agourakis82

A related form of the same failure, from plain IR rather than MIR, and it does not abort — it spins. This matters because -global-isel-abort=* cannot cut it, and hasNoSimpleLoops() does not see it.

Reproducer

define void @f(ptr %pa, ptr %pb, ptr %out) {
  %a = load <4 x i8>, ptr %pa
  %b = load <4 x i8>, ptr %pb
  %r = mul <4 x i8> %a, %b
  store <4 x i8> %r, ptr %out
  ret void
}
$ llc -mtriple=x86_64-unknown-linux-gnu -global-isel -O0 repro.ll -o /dev/null
# never returns

The IR is fine: SelectionDAG compiles it (rc=0), and -stop-before=legalizer produces the expected MIR for it.

What the legalizer does

-debug-only=legalizer, 20s cap:

rc=124
rewrites of the same G_BUILD_VECTOR: 27732
   2320 .. Already legal
   2312 .. Increase number of elements
      1 .. Reduce number of elements

So G_MUL scalarizes <4 x s8> once (Reduce number of elements, 1 occurrence), which produces G_BUILD_VECTOR <4 x s8>. That vector is then legalized by Increase number of elements to <16 x s8>, which emits a new G_BUILD_VECTOR <16 x s8> plus G_UNMERGE_VALUES, and the reassembly of the low four lanes produces G_BUILD_VECTOR <4 x s8> again — with a fresh G_IMPLICIT_DEF filler register each round. The instruction is rewritten thousands of times and the function never reaches a fixed point.

Note the asymmetry: MoreElements fires 2312 times and FewerElements once, i.e. the chain does not oscillate between two actions, it climbs. hasNoSimpleLoops() guards against a single step being undone, and a changeElementCountTo mutation on a vector does not undo a previous step, so it does not fire here.

How this differs from the MIR repro in the description

The reported repro fails cleanly (LLVM ERROR: unable to legalize instruction: %2:_(<16 x s8>) = G_BUILD_VECTOR ...) because <16 x i8> arguments are already 16 lanes. Going through load/store keeps the value at 4 lanes, which is what gets the legalizer into the <4 x s8> -> <16 x s8> reassembly cycle.

Also worth recording for whoever fixes this: PR #225265 fixes vecmul16 (it now compiles, rc=0), but this 4-lane form still hangs on top of that patch (tested at df33f8396582), and mul <4 x i8> via arguments still fails, just later, with cannot select: ... = G_UNMERGE_VALUES. So the byte-lane custom lowering and the legalizer cycle look like two separate things.

Environment: upstream/main faba310ff3e7, LLVM version 24.0.0git, Release+assertions, X86 only, -mtriple=x86_64-unknown-linux-gnu.

nodecoda avatar Oct 29 '26 03:10 nodecoda