Issue 185153
Summary [Matrix] Crash with `-fuse-matrix-tile-size=0` after automatic loop-tiling selection
Labels new issue
Assignees
Reporter cardigan1008
    Here is a crash case when reviewing https://github.com/llvm/llvm-project/pull/179325:

```llvm
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "aarch64-apple-ios"

define void @multiply_8x8x8(ptr noalias %A, ptr noalias %B, ptr noalias %C) {
entry:
  %a = load <64 x double>, ptr %A, align 8
  %b = load <64 x double>, ptr %B, align 8
  %c = call <64 x double> @llvm.matrix.multiply.v64f64.v64f64.v64f64(<64 x double> %a, <64 x double> %b, i32 8, i32 8, i32 8)
  store <64 x double> %c, ptr %C, align 8
 ret void
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <64 x double> @llvm.matrix.multiply.v64f64.v64f64.v64f64(<64 x double>, <64 x double>, i32 immarg, i32 immarg, i32 immarg) #0

attributes #0 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
```

Compiler Explorer: https://alive2.llvm.org/ce/z/JVg9Bk

Crash:

```sh
opt: /home/ce/llvm-project/llvm/lib/IR/Type.cpp:803: static llvm::FixedVectorType* llvm::FixedVectorType::get(llvm::Type*, unsigned int): Assertion `NumElts > 0 && "#Elements of a VectorType must be greater than 0"' failed.
```

Backtrace:

```sh
0.	Program arguments: bin/opt -S -S -passes=lower-matrix-intrinsics -matrix-allow-contract -force-fuse-matrix -fuse-matrix-tile-size=0 /data/tmp/tmpmw76wyqn/orig.ll
1.	Running pass "function(lower-matrix-intrinsics<>)" on module "/data/tmp/tmpmw76wyqn/orig.ll"
2.	Running pass "lower-matrix-intrinsics<>" on function "multiply_8x8x8"
 #0 0x00007d88da135c18 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (bin/../lib/libLLVMSupport.so.23.0git+0x335c18)
 #1 0x00007d88da13608b PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #2 0x00007d88da132856 llvm::sys::RunSignalHandlers() (bin/../lib/libLLVMSupport.so.23.0git+0x332856)
 #3 0x00007d88da134c90 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #4 0x00007d88d9a45330 (/lib/w  "components": [
x86_64-linux-gnu/libc.so.6+0x45330)
 #5 0x00007d88d1427cf6 (anonymous namespace)::LowerMatrixIntrinsics::emitSIMDTiling(llvm::CallInst*, llvm::LoadInst*, llvm::LoadInst*, llvm::StoreInst*, llvm::SmallPtrSetImpl<llvm::Instruction*>&) LowerMatrixIntrinsics.cpp:0:0
 #6 0x00007d88d142927a (anonymous namespace)::LowerMatrixIntrinsics::LowerMatrixMultiplyFused(llvm::CallInst*, llvm::SmallPtrSetImpl<llvm::Instruction*>&, llvm::SmallVector<llvm::IntrinsicInst*, 16u>&) LowerMatrixIntrinsics.cpp:0:0
 #7 0x00007d88d142160b (anonymous namespace)::LowerMatrixIntrinsics::Visit() LowerMatrixIntrinsics.cpp:0:0
 #8 0x00007d88d142da00 llvm::LowerMatrixIntrinsicsPass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (bin/../lib/../lib/libLLVMScalarOpts.so.23.0git+0x42da00)
 #9 0x00007d88d502d3b1 llvm::detail::PassModel<llvm::Function, llvm::LowerMatrixIntrinsicsPass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (bin/../lib/../lib/libLLVMPasses.so.23.0git+0x22d3b1)
#10 0x00007d88ce35738b llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (bin/../lib/../lib/libLLVMCore.so.23.0git+0x55738b)
#11 0x00007d88d8d5b92b llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (bin/../lib/../lib/libLLVMX86CodeGen.so.23.0git+0x15b92b)
#12 0x00007d88ce359bde llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (bin/../lib/../lib/libLLVMCore.so.23.0git+0x559bde)
#13 0x00007d88da3fd8cb llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (bin/../lib/libLLVMOptDriver.so.23.0git+0x538cb)
#14 0x00007d88ce356f4b llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (bin/../lib/../lib/libLLVMCore.so.23.0git+0x556f4b)
#15 0x00007d88da3d303a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool, bool) (bin/../lib/libLLVMOptDriver.so.23.0git+0x2903a)
#16 0x00007d88da40a656 optMain (bin/../lib/libLLVMOptDriver.so.23.0git+0x60656)
#17 0x0000596bc87081e8 main (bin/opt+0x11e8)
#18 0x00007d88d9a2a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#19 0x00007d88d9a2a28b call_init ./csu/../csu/libc-start.c:128:20
#20 0x00007d88d9a2a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#21 0x0000596bc87080a5 _start (bin/opt+0x10a5)
```

> Note: This is a review assisted with a self-built agent. The reproducer was validated manually. Please let me know if anything is wrong.

**Analysis**

This is a real crash in `lower-matrix-intrinsics` caused by accepting an invalid hidden option value: `-fuse-matrix-tile-size=0`.

After this patch, large matrix multiplies may automatically use the loop-based tiling path when the estimated number of native vector ops exceeds `TileLoopsThreshold`. For the reproducer (`8x8x8` double matmul), that path becomes reachable under `-force-fuse-matrix`.

However, the tiled lowering code assumes `TileSize > 0`. In particular, it constructs a fixed-width vector type using `TileSize`, and `FixedVectorType::get(..., 0)` is invalid and triggers the assertion `#Elements of a VectorType must be greater than 0`.

**Suggested fix**

Reject `TileSize == 0` before entering tiled lowering, or enforce `TileSize >= 1` when parsing the command-line option.

cc @fhahn 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to