| Issue |
203753
|
| Summary |
[MLIR][Math] ASSERTION failure in math-expand-ops: type.hasStaticShape() && "type must have static shape
|
| Labels |
|
| Assignees |
|
| Reporter |
Kiritobl
|
### Version
git commit hash: b3df06651
#### Bug Description
`mlir-opt --math-expand-ops` crashes with an **assertion failure** when expanding math dialect operations (e.g., `math.tanh`, `math.exp2`, `math.round`, `math.roundeven`) on tensors with **dynamic shapes** (e.g., `tensor<?xf32>`).
#### Crash Chain & Root Cause
**Location**: `mlir/lib/Dialect/Math/Transforms/ExpandOps.cpp` → `mlir/lib/IR/BuiltinAttributes.cpp:1219`
When `--math-expand-ops` encounters a math operation with a dynamically-shaped tensor result type, the expand function (e.g., `convertTanhOp`, `convertRoundEvenOp`) calls the `createFloatConst()` helper to generate floating-point constants. This helper unconditionally passes the operation's result `ShapedType` to `DenseElementsAttr::get()`, which internally calls `DenseTypedElementsAttr::getRaw()`. The latter asserts that the type must have a static shape — but for dynamic tensors (`tensor<?xf32>`), it does not.
```
math.tanh(%arg0 : tensor<?xf32>)
→ convertTanhOp()
→ createFloatConst(loc, resultType /* tensor<?xf32> */, value, builder)
→ DenseElementsAttr::get(cast<ShapedType>(type), value)
→ DenseTypedElementsAttr::getRaw(type, data)
→ assert(type.hasStaticShape()) // ASSERTION FAILURE
```
Affected operations include all math ops that go through `createFloatConst`:
- `math.tanh` → `convertTanhOp`
- `math.exp2` → `convertExp2fOp`
- `math.round` → `convertRoundOp`
- `math.roundeven` → `convertRoundEvenOp`
- and any other expansion that uses this shared helper
#### Steps to Reproduce
Run the following command with the provided reproducer MLIR:
```bash
mlir-opt --math-expand-ops reproducer.mlir -o /dev/null
```
#### Reproducer (`reproducer.mlir`)
```mlir
// Minimal reproducer for ASSERTION failure in --math-expand-ops
// Crash: BuiltinAttributes.cpp:1219
// Assert: type.hasStaticShape() && "type must have static shape"
//
// Trigger: math.tanh on a dynamic-shaped tensor — the expand pass
// calls createFloatConst() with tensor<?xf32>, which reaches
// DenseTypedElementsAttr::getRaw() and fails the static-shape check.
func.func @math_expand_dynamic_shape_crash(%arg0: tensor<?xf32>) -> tensor<?xf32> {
%0 = math.tanh %arg0 : tensor<?xf32>
return %0 : tensor<?xf32>
}
```
#### Stack Dump
```text
Stack dump:
0. Program arguments: mlir-opt --math-expand-ops reproducer.mlir -o /dev/null
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
mlir-opt: …/mlir/lib/IR/BuiltinAttributes.cpp:1219: static DenseElementsAttr mlir::DenseTypedElementsAttr::getRaw(ShapedType, ArrayRef<char>): Assertion `type.hasStaticShape() && "type must have static shape"' failed.
#0 0x00005f99009fd518 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x1f8c518)
#1 0x00005f99009fa3b5 llvm::sys::RunSignalHandlers() (mlir-opt+0x1f893b5)
#2 0x00005f99009fe641 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007deb74c42520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007deb74c969fc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#5 0x00007deb74c969fc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#6 0x00007deb74c969fc pthread_kill ./nptl/pthread_kill.c:89:10
#7 0x00007deb74c42476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007deb74c287f3 abort ./stdlib/abort.c:81:7
#9 0x00007deb74c2871b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007deb74c39e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x00005f99090bc2fe mlir::DenseTypedElementsAttr::getRaw(mlir::ShapedType, llvm::ArrayRef<char>) (mlir-opt+0xa64b2fe)
#12 0x00005f99090bbe6c mlir::DenseElementsAttr::get(mlir::ShapedType, llvm::ArrayRef<mlir::Attribute>) (mlir-opt+0xa64ae6c)
#13 0x00005f9901ebc9ed createFloatConst(mlir::Location, mlir::Type, llvm::APFloat, mlir::OpBuilder&) ExpandOps.cpp:0:0
#14 0x00005f9901ebb8f0 convertRoundEvenOp(mlir::math::RoundEvenOp, mlir::PatternRewriter&) ExpandOps.cpp:0:0
#15 0x00005f9908dfebc3 void llvm::function_ref<…>::callback_fn<mlir::PatternApplicator::matchAndRewrite(…)::$_0>(long) PatternApplicator.cpp:0:0
#16 0x00005f9908dfb41a mlir::PatternApplicator::matchAndRewrite(…) (mlir-opt+0xa38a41a)
#17 0x00005f9908dd3296 (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#18 0x00005f9908dd2392 …::RegionPatternRewriteDriver::simplify(bool*) &&::$_2>(long) GreedyPatternRewriteDriver.cpp:0:0
#19 0x00005f9908dcf3b5 mlir::applyPatternsGreedily(…) (mlir-opt+0xa35e3b5)
#20 0x00005f9901ebe318 (anonymous namespace)::MathExpandOpsPass::runOnOperation() ExpandOps.cpp:0:0
#21 0x00005f990e25e9c8 mlir::detail::OpToOpPassAdaptor::run(…) (mlir-opt+0xa3eb9c8)
#22 0x00005f990e25f7a4 mlir::detail::OpToOpPassAdaptor::runPipeline(…) (mlir-opt+0xa3ec7a4)
#23 0x00005f990e266b32 mlir::PassManager::runPasses(…) (mlir-opt+0xa3f3b32)
#24 0x00005f990e26620a mlir::PassManager::run(…) (mlir-opt+0xa3f320a)
#25 0x00005f99009dcf70 performActions(…) MlirOptMain.cpp:0:0
#26 0x00005f99009dc2e8 …::MlirOptMain(…)::$_0>(…) MlirOptMain.cpp:0:0
#27 0x00005f990e72e145 mlir::splitAndProcessBuffer(…) (mlir-opt+0xa735145)
#28 0x00005f99009d18e5 mlir::MlirOptMain(…) (mlir-opt+0x20348e5)
#29 0x00005f99009d1cb1 mlir::MlirOptMain(int, char**, …) (mlir-opt+0x2034cb1)
#30 0x00005f99009d1f1b mlir::MlirOptMain(int, char**, …) (mlir-opt+0x2034f1b)
#31 0x00005f990090f632 main (mlir-opt+0x1f72632)
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs