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

[X86] LLVM 22.1.8 crashes reducing masked <8 x i48> with AVX-512F (PSADBW combine)

Open jdh8 opened this issue 2 months ago • 3 comments

LLVM 22.1.8 segfaults while compiling valid IR that reduces eight masked i48 lanes when AVX-512F is enabled. This was reduced from a Rust/fishnet build failure in shakmaty 0.30.1's Crazyhouse::has_insufficient_material using llvm-extract and llvm-reduce. Neither Rust, Cargo, LTO, nor external dependencies are needed for the reduced crash.

Reproducer

Save as repro.ll:

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

define i48 @sum_masked_i48(<8 x i48> %x) {
  %masked = and <8 x i48> %x, splat (i48 1)
  %sum = call i48 @llvm.vector.reduce.add.v8i48(<8 x i48> %masked)
  ret i48 %sum
}

declare i48 @llvm.vector.reduce.add.v8i48(<8 x i48>)
opt -passes=verify -disable-output repro.ll   # succeeds
llc -O3 -mcpu=x86-64-v4 repro.ll -o /dev/null # SIGSEGV (shell exit 139)

Expected: valid IR compiles without crashing. Actual: SIGSEGV in llvm::SelectionDAG::getNode during X86 DAG-to-DAG instruction selection.

Tested controls

All use the same reduced IR with llc -O3; type/mask substitutions are applied throughout the IR where indicated.

CPU / features Element type Mask Result
x86-64-v3 i48 1 Pass
x86-64-v4 i48 1 SIGSEGV
znver4 i48 1 SIGSEGV
x86-64-v3 -mattr=+avx512f i48 1 SIGSEGV
x86-64-v4 -mattr=-avx512f i48 1 Pass
x86-64-v4 i32 1 Pass
x86-64-v4 i64 1 Pass
x86-64-v4 i48 255 SIGSEGV
x86-64-v4 i48 511 Pass

Suspected faulty transform

The controls point to the PSADBW reduction optimization in combineArithReduction.

The guard accepts element widths from 16 through 64, including the non-simple type i48, when the values are known to be at most 255 and AVX-512 is enabled. The earlier vector-size check also accepts <8 x i48>: 384 is divisible by 128. The path later does:

VecVT = MVT::getVectorVT(VT.getSimpleVT(), 128 / VT.getSizeInBits());
Rdx = DAG.getBitcast(VecVT, Rdx);
return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Rdx, Index);

That assumes a simple element type forming a 128-bit machine vector, which i48 does not satisfy. The exact source-level diagnosis is an inference from the reproducer and source inspection; I have not built a patched LLVM. Restricting this transform to supported element types, or extracting an i64 sum and truncating, may be appropriate.

The corresponding assumption is also present in main at e011bda76aab5f9d59c3e74397416b2aec745c46; main has only been source-inspected, not built/tested.

Environment and crash trace

  • Fedora 44 x86_64, package llvm-22.1.8-4.fc44.x86_64
  • llc --version: LLVM 22.1.8, optimized build, default target x86_64-redhat-linux-gnu
  • Host CPU: AMD Ryzen 7 8700F (native resolves to znver4)
  • Original Rust toolchain: rustc 1.98.1 (48a229cea 2026-09-01) (Fedora 1.98.1-1.fc44), LLVM 22.1.8

Representative stack frames:

 #3 0x00007f0385f66e14 llvm::SelectionDAG::getNode(unsigned int, llvm::SDLoc const&, llvm::EVT, llvm::SDValue, llvm::SDValue, llvm::SDNodeFlags) (/lib64/libLLVM.so.22.1+0x4766e14)
 #4 0x00007f0386812ab3 (/lib64/libLLVM.so.22.1+0x5012ab3)
 #5 0x00007f03867d0d89 (/lib64/libLLVM.so.22.1+0x4fd0d89)
 #6 0x00007f038627d56f (/lib64/libLLVM.so.22.1+0x4a7d56f)
 #7 0x00007f038627ba0b llvm::SelectionDAG::Combine(llvm::CombineLevel, llvm::BatchAAResults*, llvm::CodeGenOptLevel) (/lib64/libLLVM.so.22.1+0x4a7ba0b)
 #8 0x00007f038627634e llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/lib64/libLLVM.so.22.1+0x4a7634e)

The original fishnet release builds successfully with -C target-cpu=x86-64-v3, including its default fat LTO. Merely disabling LTO leaves the crash in the shakmaty dependency. The reduced llc case above is independent of LTO.

jdh8 avatar Sep 30 '26 13:09 jdh8

@llvm/issue-subscribers-backend-x86

Author: Chen-Pang He (jdh8)

LLVM 22.1.8 segfaults while compiling valid IR that reduces eight masked `i48` lanes when AVX-512F is enabled. This was reduced from a Rust/fishnet build failure in `shakmaty` 0.30.1's `Crazyhouse::has_insufficient_material` using `llvm-extract` and `llvm-reduce`. Neither Rust, Cargo, LTO, nor external dependencies are needed for the reduced crash.

Reproducer

Save as repro.ll:

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

define i48 @<!-- -->sum_masked_i48(&lt;8 x i48&gt; %x) {
  %masked = and &lt;8 x i48&gt; %x, splat (i48 1)
  %sum = call i48 @<!-- -->llvm.vector.reduce.add.v8i48(&lt;8 x i48&gt; %masked)
  ret i48 %sum
}

declare i48 @<!-- -->llvm.vector.reduce.add.v8i48(&lt;8 x i48&gt;)
opt -passes=verify -disable-output repro.ll   # succeeds
llc -O3 -mcpu=x86-64-v4 repro.ll -o /dev/null # SIGSEGV (shell exit 139)

Expected: valid IR compiles without crashing. Actual: SIGSEGV in llvm::SelectionDAG::getNode during X86 DAG-to-DAG instruction selection.

Tested controls

All use the same reduced IR with llc -O3; type/mask substitutions are applied throughout the IR where indicated.

CPU / features Element type Mask Result
x86-64-v3 i48 1 Pass
x86-64-v4 i48 1 SIGSEGV
znver4 i48 1 SIGSEGV
x86-64-v3 -mattr=+avx512f i48 1 SIGSEGV
x86-64-v4 -mattr=-avx512f i48 1 Pass
x86-64-v4 i32 1 Pass
x86-64-v4 i64 1 Pass
x86-64-v4 i48 255 SIGSEGV
x86-64-v4 i48 511 Pass

Suspected faulty transform

The controls point to the PSADBW reduction optimization in combineArithReduction.

The guard accepts element widths from 16 through 64, including the non-simple type i48, when the values are known to be at most 255 and AVX-512 is enabled. The earlier vector-size check also accepts &lt;8 x i48&gt;: 384 is divisible by 128. The path later does:

VecVT = MVT::getVectorVT(VT.getSimpleVT(), 128 / VT.getSizeInBits());
Rdx = DAG.getBitcast(VecVT, Rdx);
return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Rdx, Index);

That assumes a simple element type forming a 128-bit machine vector, which i48 does not satisfy. The exact source-level diagnosis is an inference from the reproducer and source inspection; I have not built a patched LLVM. Restricting this transform to supported element types, or extracting an i64 sum and truncating, may be appropriate.

The corresponding assumption is also present in main at e011bda76aab5f9d59c3e74397416b2aec745c46; main has only been source-inspected, not built/tested.

Environment and crash trace

  • Fedora 44 x86_64, package llvm-22.1.8-4.fc44.x86_64
  • llc --version: LLVM 22.1.8, optimized build, default target x86_64-redhat-linux-gnu
  • Host CPU: AMD Ryzen 7 8700F (native resolves to znver4)
  • Original Rust toolchain: rustc 1.98.1 (48a229cea 2026-09-01) (Fedora 1.98.1-1.fc44), LLVM 22.1.8

Representative stack frames:

 #<!-- -->3 0x00007f0385f66e14 llvm::SelectionDAG::getNode(unsigned int, llvm::SDLoc const&amp;, llvm::EVT, llvm::SDValue, llvm::SDValue, llvm::SDNodeFlags) (/lib64/libLLVM.so.22.1+0x4766e14)
 #<!-- -->4 0x00007f0386812ab3 (/lib64/libLLVM.so.22.1+0x5012ab3)
 #<!-- -->5 0x00007f03867d0d89 (/lib64/libLLVM.so.22.1+0x4fd0d89)
 #<!-- -->6 0x00007f038627d56f (/lib64/libLLVM.so.22.1+0x4a7d56f)
 #<!-- -->7 0x00007f038627ba0b llvm::SelectionDAG::Combine(llvm::CombineLevel, llvm::BatchAAResults*, llvm::CodeGenOptLevel) (/lib64/libLLVM.so.22.1+0x4a7ba0b)
 #<!-- -->8 0x00007f038627634e llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/lib64/libLLVM.so.22.1+0x4a7634e)

The original fishnet release builds successfully with -C target-cpu=x86-64-v3, including its default fat LTO. Merely disabling LTO leaves the crash in the shakmaty dependency. The reduced llc case above is independent of LTO.

I'd like to work on this issue.

I can reproduce the crash with the reduced <8 x i48> IR and will investigate combineArithReduction in X86ISelLowering.cpp, particularly the PSADBW path accepting non-simple i48 element types.

My plan is to:

  • confirm the exact failing DAG construction,
  • identify the appropriate type/legality restriction or lowering fix,
  • add a regression test for the <8 x i48> case,
  • verify the fix against the relevant AVX-512 configurations and existing X86 tests.

If no one else is currently working on this, I'd be happy to take it.

Architag1503 avatar Sep 30 '26 14:09 Architag1503

Hopefully this just requires a !isSimple() early out - combineArithReduction will be refactored considerably once I find the time to resume work on VECREDUCE_ADD handling.

RKSimon avatar Oct 01 '26 12:10 RKSimon