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

[fuzzer-found] [AArch64] Crash at -O2 with `-msve-vector-bits=256 -mstrict-align`: "Cannot select: v2i32,ch = load<..., zext from v2i16>"

Open ubmaxxer opened this issue 1 week ago • 1 comments

With fixed-length SVE of 256 bits or more and strict alignment, converting four under-aligned i16 elements to float crashes instruction selection. After vector legalization the DAG combiner splits zero_extend (load v4i16) into two extending v2i16 to v2i32 loads. Those are marked Custom since #163064, but their custom lowering declines under-aligned loads when strict alignment is on, so they stay as they are and no pattern selects them. clang 22.1.0 to 22.1.8, 23.1.6 and main crash; 21.1.0 compiles the function.

Reproducer

godbolt (armv8-a clang trunk, 23.1.0, 22.1.0 and 21.1.0): https://godbolt.org/z/1aWeP4fK8

void f(float *restrict a, const unsigned short *restrict b) {
  for (int i = 0; i < 4; i++)
    a[i] = b[i];
}
$ clang --target=aarch64-linux-gnu -O2 -march=armv8-a+sve -msve-vector-bits=256 -mstrict-align -c repro.c
fatal error: error in backend: Cannot select: t18: v2i32,ch = load<(load (s32) from %ir.b + 4, align 2, !tbaa !12), zext from v2i16> t0, t17, poison:i64
  t17: i64 = add t4, Constant:i64<4>
    t4: i64,ch = CopyFromReg t0, Register:i64 %1
In function: f
...
4.	Running pass 'AArch64 Instruction Selection' on function '@f'

IR

godbolt (llc trunk, llc 23.1.0, llc 22.1.0 and llc 21.1.0): https://godbolt.org/z/8TfYPW7TG

target triple = "aarch64-unknown-linux-gnu"

define <4 x float> @f(ptr %p) vscale_range(2,2) "target-features"="+sve,+strict-align" {
  %v = load <4 x i16>, ptr %p, align 2
  %c = uitofp <4 x i16> %v to <4 x float>
  ret <4 x float> %c
}
$ llc repro.ll -o /dev/null
LLVM ERROR: Cannot select: t16: v2i32,ch = load<(load (s32) from %ir.p + 4, align 2), zext from v2i16> t0, t15, poison:i64
  t15: i64 = add t2, Constant:i64<4>
    t2: i64,ch = CopyFromReg t0, Register:i64 %0
In function: f

What goes wrong

From llc -debug-only=isel,legalizedag,dagcombine:

  1. Vector legalization turns the conversion into uint_to_fp (zero_extend (load v4i16)).

  2. The DAG combine that follows runs DAGCombiner::CombineExtLoad. AArch64TargetLowering::isVectorLoadExtDesirable returns true for it: the load is not a small-vector load, but useSVEForFixedLengthVectors() is true (this is why -msve-vector-bits of 256 or more is needed) and the source elements are i16. The combine then halves the types until isLoadLegalOrCustom accepts them. v4i16 to v4i32 is Expand; v2i16 to v2i32 is Custom (setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, MVT::v2i16, Custom), added by #163064), so it replaces the node with

    t13: v2i32,ch = load<(load (s32) from %ir.p, align 2), zext from v2i16> t0, t2, poison:i64
      t15: i64 = add t2, Constant:i64<4>
    t16: v2i32,ch = load<(load (s32) from %ir.p + 4, align 2), zext from v2i16> t0, t15, poison:i64
    t19: v4i32 = concat_vectors t13, t16
    t12: v4f32 = uint_to_fp t19
    
  3. Operation legalization visits both loads ("Legalizing extending load operation") and calls the custom lowering. LowerLOAD reaches tryLowerSmallVectorExtLoad, which returns nothing because isEligibleForSmallVectorLoadOpt rejects the load: requiresStrictAlign() is true and the alignment (2) is below the store size of v2i16 (4). The loads are left unchanged.

  4. Instruction selection has no pattern for a v2i32 load that zero-extends from v2i16.

So the legality query in step 2 says Custom without looking at the alignment, and the custom lowering in step 3 then refuses because of the alignment.

Notes

  • Flags, C function above, on main:

    Flags Result
    -O2, -O3 or -Os, -march=armv8-a+sve -msve-vector-bits=256 -mstrict-align crash
    -msve-vector-bits=512, 1024 or 256+ instead of 256 crash
    -march=armv9-a, -mcpu=neoverse-v1 or -mcpu=a64fx (with -msve-vector-bits=512) instead of -march=armv8-a+sve crash
    -mno-unaligned-access instead of -mstrict-align crash
    without -mstrict-align compiles
    without -msve-vector-bits, or with -msve-vector-bits=128 compiles
    -O1 compiles
  • Variants of the IR, llc on main:

    Variant Result
    align 1 on the load crash
    no align on the load (the vector type's own alignment) compiles
    sitofp crash (sext from v2i16)
    vscale_range(4,4), vscale_range(2,16) crash
    vscale_range(1,1) compiles
    without +strict-align compiles
    zext to <4 x i32> returned directly; <4 x i8> source; 2 or 8 elements; <4 x double> result compiles
  • Tested: main at 5a464bdd79c1 (2026-11-27), 9833b0c710ef and 84a7a7d12ccc, the 23.1.6 release, the 22.1.8 release. All crash on the C function and on the IR. On godbolt, clang 22.1.0 crashes and clang 21.1.0, 20.1.0 and 19.1.0 compile the C function; llc 21.1.0 and 20.1.0 compile the IR.

This problem was found with a fuzzer; the C function above was written by hand from its test case. The report was written with LLM assistance.

ubmaxxer avatar Nov 28 '26 09:11 ubmaxxer

@llvm/issue-subscribers-backend-aarch64

Author: Nirmal (ubmaxxer)

With fixed-length SVE of 256 bits or more and strict alignment, converting four under-aligned `i16` elements to `float` crashes instruction selection. After vector legalization the DAG combiner splits `zero_extend (load v4i16)` into two extending `v2i16` to `v2i32` loads. Those are marked Custom since #163064, but their custom lowering declines under-aligned loads when strict alignment is on, so they stay as they are and no pattern selects them. clang 22.1.0 to 22.1.8, 23.1.6 and main crash; 21.1.0 compiles the function.

Reproducer

godbolt (armv8-a clang trunk, 23.1.0, 22.1.0 and 21.1.0): https://godbolt.org/z/1aWeP4fK8

void f(float *restrict a, const unsigned short *restrict b) {
  for (int i = 0; i &lt; 4; i++)
    a[i] = b[i];
}
$ clang --target=aarch64-linux-gnu -O2 -march=armv8-a+sve -msve-vector-bits=256 -mstrict-align -c repro.c
fatal error: error in backend: Cannot select: t18: v2i32,ch = load&lt;(load (s32) from %ir.b + 4, align 2, !tbaa !12), zext from v2i16&gt; t0, t17, poison:i64
  t17: i64 = add t4, Constant:i64&lt;4&gt;
    t4: i64,ch = CopyFromReg t0, Register:i64 %1
In function: f
...
4.	Running pass 'AArch64 Instruction Selection' on function '@<!-- -->f'

IR

godbolt (llc trunk, llc 23.1.0, llc 22.1.0 and llc 21.1.0): https://godbolt.org/z/8TfYPW7TG

target triple = "aarch64-unknown-linux-gnu"

define &lt;4 x float&gt; @<!-- -->f(ptr %p) vscale_range(2,2) "target-features"="+sve,+strict-align" {
  %v = load &lt;4 x i16&gt;, ptr %p, align 2
  %c = uitofp &lt;4 x i16&gt; %v to &lt;4 x float&gt;
  ret &lt;4 x float&gt; %c
}
$ llc repro.ll -o /dev/null
LLVM ERROR: Cannot select: t16: v2i32,ch = load&lt;(load (s32) from %ir.p + 4, align 2), zext from v2i16&gt; t0, t15, poison:i64
  t15: i64 = add t2, Constant:i64&lt;4&gt;
    t2: i64,ch = CopyFromReg t0, Register:i64 %0
In function: f

What goes wrong

From llc -debug-only=isel,legalizedag,dagcombine:

  1. Vector legalization turns the conversion into uint_to_fp (zero_extend (load v4i16)).

  2. The DAG combine that follows runs DAGCombiner::CombineExtLoad. AArch64TargetLowering::isVectorLoadExtDesirable returns true for it: the load is not a small-vector load, but useSVEForFixedLengthVectors() is true (this is why -msve-vector-bits of 256 or more is needed) and the source elements are i16. The combine then halves the types until isLoadLegalOrCustom accepts them. v4i16 to v4i32 is Expand; v2i16 to v2i32 is Custom (setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i32, MVT::v2i16, Custom), added by #163064), so it replaces the node with

    t13: v2i32,ch = load&lt;(load (s32) from %ir.p, align 2), zext from v2i16&gt; t0, t2, poison:i64
      t15: i64 = add t2, Constant:i64&lt;4&gt;
    t16: v2i32,ch = load&lt;(load (s32) from %ir.p + 4, align 2), zext from v2i16&gt; t0, t15, poison:i64
    t19: v4i32 = concat_vectors t13, t16
    t12: v4f32 = uint_to_fp t19
    
  3. Operation legalization visits both loads ("Legalizing extending load operation") and calls the custom lowering. LowerLOAD reaches tryLowerSmallVectorExtLoad, which returns nothing because isEligibleForSmallVectorLoadOpt rejects the load: requiresStrictAlign() is true and the alignment (2) is below the store size of v2i16 (4). The loads are left unchanged.

  4. Instruction selection has no pattern for a v2i32 load that zero-extends from v2i16.

So the legality query in step 2 says Custom without looking at the alignment, and the custom lowering in step 3 then refuses because of the alignment.

Notes

  • Flags, C function above, on main:

    Flags Result
    -O2, -O3 or -Os, -march=armv8-a+sve -msve-vector-bits=256 -mstrict-align crash
    -msve-vector-bits=512, 1024 or 256+ instead of 256 crash
    -march=armv9-a, -mcpu=neoverse-v1 or -mcpu=a64fx (with -msve-vector-bits=512) instead of -march=armv8-a+sve crash
    -mno-unaligned-access instead of -mstrict-align crash
    without -mstrict-align compiles
    without -msve-vector-bits, or with -msve-vector-bits=128 compiles
    -O1 compiles
  • Variants of the IR, llc on main:

    Variant Result
    align 1 on the load crash
    no align on the load (the vector type's own alignment) compiles
    sitofp crash (sext from v2i16)
    vscale_range(4,4), vscale_range(2,16) crash
    vscale_range(1,1) compiles
    without +strict-align compiles
    zext to &lt;4 x i32&gt; returned directly; &lt;4 x i8&gt; source; 2 or 8 elements; &lt;4 x double&gt; result compiles
  • Tested: main at 5a464bdd79c1 (2026-11-27), 9833b0c710ef and 84a7a7d12ccc, the 23.1.6 release, the 22.1.8 release. All crash on the C function and on the IR. On godbolt, clang 22.1.0 crashes and clang 21.1.0, 20.1.0 and 19.1.0 compile the C function; llc 21.1.0 and 20.1.0 compile the IR.

This problem was found with a fuzzer; the C function above was written by hand from its test case. The report was written with LLM assistance.