llvm-project
llvm-project copied to clipboard
Null pointer exception bug in the debug mode for the ‘scf’ Dialect
Hello,
We think we have discovered a potential null-pointer exception bug in the mlir-opt debug mode. This problem leads to a potential compiler crash.
This bug is related to the scf.for operation when converting the scf dialect to the std dialect; please see the attached test case and execution command.
We think the issue is due to the operation clean-up process in the debugging mode, described as follows.
During the dialect lowering process, the scf.for op in FuncOP will be erased by calling replaceOP’. As a result, the scf.forop will become a null pointer.
This is not a problem in the release mode because all the illegal instructions that appear after the matchAndRewrite’ method will be removed once calling finalize(rewriter). As such, these illegal instructions/ops will not appear in the FuncOP.
However, in the debug mode (when using LLVM_DEBUG(logSucessfulPatternApplication(dumpRootOp)) to dump FuncOP), the scf.forops will not be removed. Sincescf.for` points to a null pointer, the compilation command will trigger a null pointer exception, leading to a program crash.
Attached below is a test case to replicate the behavior described above.
version
llvm-project-release-14.x
git hash: f28c006a5895fc0e329fe15fead81e37457cb1d1(https://github.com/llvm/llvm-project/tree/f28c006a5895fc0e329fe15fead81e37457cb1d1)
Operation System
Ubuntu version: 16.04
Linux Kernel version: 4.15.0-142-generic
test case
// RUN: mlir-opt -allow-unregistered-dialect -convert-scf-to-std %s | FileCheck %s
func @simple_std_for_loop(%arg0 : index, %arg1 : index, %arg2 : index) {
scf.for %i0 = %arg0 to %arg1 step %arg2 {
%c1 = arith.constant 1 : index
}
return
}
Execution command Save the above IR as convert-to-cfg.mlir, then run:
mlir-opt convert-to-cfg.mlir -allow-unregistered-dialect -convert-scf-to-std -print-ir-after-all -debug
This leads a crash dump given below.
Crash dump:
//===-------------------------------------------===//
Legalizing operation : 'scf.for'(0x6a632c0) {
* Fold {
} -> FAILURE : unable to fold
* Pattern : 'scf.for -> ()' {
Trying to match "(anonymous namespace)::ForLowering"
** Insert : 'arith.addi'(0x6a5eee0)
** Insert : 'std.br'(0x6a67c90)
** Erase : 'scf.yield'(0x6a606b0)
** Insert : 'std.br'(0x6a67d30)
** Insert : 'arith.cmpi'(0x6a5ef90)
** Insert : 'std.cond_br'(0x6a6d090)
** Replace : 'scf.for'(0x6a632c0)
"(anonymous namespace)::ForLowering" result 1
//===-------------------------------------------===//
other op Legalizing ...
//===-------------------------------------------===//
} -> SUCCESS : pattern applied successfully
// *** IR Dump After Pattern Application ***
func @simple_std_for_loop(%arg0: index, %arg1: index, %arg2: index) {
br ^bb1(%arg0 : index)
^bb1(%0: index): // 2 preds: ^bb0, ^bb2
%1 = arith.cmpi slt, %0, %arg1 : index
cond_br %1, ^bb2, ^bb3
^bb2: // pred: ^bb1
%c1 = arith.constant 1 : index
scf.yield
%2 = arith.addi %0, %arg2 : index
br ^bb1(%2 : index)
^bb3: // pred: ^bb1
scf.for mlir-opt: /home/mlir/llvm/include/llvm/ADT/ilist_iterator.h:138:
llvm::ilist_iterator::reference llvm::ilist_iterator<llvm::ilist_detail::node_options<mlir::Block, true, false, void>, false, false>::operator*() const [OptionsT = llvm::ilist_detail::node_options<mlir::Block, true, false, void>, IsReverse = false, IsConst = false]: Assertion `!NodePtr->isKnownSentinel()' failed.
@llvm/issue-subscribers-mlir
Anyone who can reproduce this? It looks like something similar doesn't crash on current main (fe01c084249969518bcd69aa2fedffc9f017f43a):
$ mlir-opt temp.mlir --convert-scf-to-cf --debug
[...]
module {
func.func @simple_std_for_loop(%arg0: index, %arg1: index, %arg2: index) {
cf.br ^bb1(%arg0 : index)
^bb1(%0: index): // 2 preds: ^bb0, ^bb2
%1 = arith.cmpi slt, %0, %arg1 : index
cf.cond_br %1, ^bb2, ^bb3
^bb2: // pred: ^bb1
%c1 = arith.constant 1 : index
%2 = arith.addi %0, %arg2 : index
cf.br ^bb1(%2 : index)
^bb3: // pred: ^bb1
return
}
}
With temp.mlir containing:
func.func @simple_std_for_loop(%arg0 : index, %arg1 : index, %arg2 : index) {
scf.for %i0 = %arg0 to %arg1 step %arg2 {
%c1 = arith.constant 1 : index
}
return
}
Converting to std like in OP's example doesn't work anymore as far as I understand because of the removal of std (e.g., https://discourse.llvm.org/t/rfc-splitting-the-standard-dialect/2312).