| Issue |
107798
|
| Summary |
Assertion failed in User::getOperand when running opt with specific command and input file
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
khagankhan
|
When I was trying to add new pass in `InstructionCombining.cpp` by adding the following:
```
void cs6475_debug(std::string DbgString) {
if (true)
dbgs() << DbgString;
}
Instruction* cs6475_optimizer(Instruction *I) {
cs6475_debug("\nCS 6475 matcher: running now\n");
// BEGIN KHAGAN KARIMOV
// x - y + y * 2 = x + y
Value *LHS = I->getOperand(0);
Value *RHS = I->getOperand(1);
if (match(LHS, m_Sub(m_Value(X), m_Value(Y)))) {
cs6475_debug("KKK: matched the 'sub'\n");
if(match (RHS, m_Shl(m_Value(Y), m_ConstantInt(C)))){
cs6475_debug("KKK: matched the 'mul'\n");
if (C->isOne()) {
cs6475_debug("KKK: matched the 'shl 1'\n");
}
}
}
return nullptr;
}
```
The program aborted when running: `./bin/opt -O2 add.ll -S -o -`
where add.ll is:
```
define i32 @sum2(i32 %0, i32 %1) local_unnamed_addr #0 {
%3 = sub i32 %0, %1 ; a - b
%4 = shl i32 %1, 1 ; b * 2
%5 = add i32 %3, %4 ; a - b + b * 2 (which is a + b)
ret i32 %5 ; simply return a + b
}
```
I built `llvm` using:
```
cmake -GNinja -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_EH=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS="llvm;clang" ../llvm -DBUILTINS_CMAKE_ARGS=-DCOMPILER_RT_ENABLE_IOS=OFF && ninja -j$(nproc)
```
The version:
`./bin/opt --version`:
```
LLVM (http://llvm.org/):
LLVM version 20.0.0git
Optimized build with assertions.
Default target: x86_64-unknown-linux-gnu
Host CPU: sandybridge
```
The crash backtrace:
```
khan22@node0:~/cs6475/llvm-project/build$ ./bin/opt -O2 add.ll -S -o -
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
KKK: matched the 'sub'
KKK: matched the 'mul'
KKK: matched the 'shl 1'
CS 6475 matcher: running now
opt: /users/khan22/cs6475/llvm-project/llvm/include/llvm/IR/User.h:169: llvm::Value* llvm::User::getOperand(unsigned int) const: Assertion `i < NumUserOperands && "getOperand() out of range!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0. Program arguments: ./bin/opt -O2 add.ll -S -o -
1. Running pass "function<eager-inv>(mem2reg,instcombine<max-iterations=1;no-verify-fixpoint>,simplifycfg<bonus-inst-threshold=1;no-forward-switch-cond;switch-range-to-icmp;no-switch-to-lookup;keep-loops;no-hoist-common-insts;no-hoist-loads-stores-with-cond-faulting;no-sink-common-insts;speculate-blocks;simplify-cond-branch;no-speculate-unpredictables>)" on module "add.ll"
2. Running pass "instcombine<max-iterations=1;no-verify-fixpoint>" on function "sum2"
#0 0x00007f95a20bf920 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/users/khan22/cs6475/llvm-project/build/bin/../lib/libLLVMSupport.so.20.0git+0x20a920)
#1 0x00007f95a20bc92f llvm::sys::RunSignalHandlers() (/users/khan22/cs6475/llvm-project/build/bin/../lib/libLLVMSupport.so.20.0git+0x20792f)
#2 0x00007f95a20bca85 SignalHandler(int) Signals.cpp:0:0
#3 0x00007f95a1cc8520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007f95a1d1c9fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#5 0x00007f95a1cc8476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#6 0x00007f95a1cae7f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#7 0x00007f95a1cae71b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#8 0x00007f95a1cbfe96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#9 0x00007f9599fa054f (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMInstCombine.so.20.0git+0x3354f)
#10 0x00007f9599fa872e cs6475_optimizer(llvm::Instruction*) (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMInstCombine.so.20.0git+0x3b72e)
#11 0x00007f9599fd0f72 llvm::InstCombinerImpl::run() (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMInstCombine.so.20.0git+0x63f72)
#12 0x00007f9599fd1a5b combineInstructionsOverFunction(llvm::Function&, llvm::InstructionWorklist&, llvm::AAResults*, llvm::AssumptionCache&, llvm::TargetLibraryInfo&, llvm::TargetTransformInfo&, llvm::DominatorTree&, llvm::OptimizationRemarkEmitter&, llvm::BlockFrequencyInfo*, llvm::BranchProbabilityInfo*, llvm::ProfileSummaryInfo*, llvm::InstCombineOptions const&) InstructionCombining.cpp:0:0
#13 0x00007f9599fd2812 llvm::InstCombinePass::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMInstCombine.so.20.0git+0x65812)
#14 0x00007f959bdde176 llvm::detail::PassModel<llvm::Function, llvm::InstCombinePass, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMPasses.so.20.0git+0xd5176)
#15 0x00007f9599087baf llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMCore.so.20.0git+0x34ebaf)
#16 0x00007f95a03eb546 llvm::detail::PassModel<llvm::Function, llvm::PassManager<llvm::Function, llvm::AnalysisManager<llvm::Function>>, llvm::AnalysisManager<llvm::Function>>::run(llvm::Function&, llvm::AnalysisManager<llvm::Function>&) (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMAMDGPUCodeGen.so.20.0git+0x3e7546)
#17 0x00007f9599085fe7 llvm::ModuleToFunctionPassAdaptor::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMCore.so.20.0git+0x34cfe7)
#18 0x00007f95a03ea266 llvm::detail::PassModel<llvm::Module, llvm::ModuleToFunctionPassAdaptor, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMAMDGPUCodeGen.so.20.0git+0x3e6266)
#19 0x00007f9599086b95 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/users/khan22/cs6475/llvm-project/build/bin/../lib/../lib/libLLVMCore.so.20.0git+0x34db95)
#20 0x00007f95a21cfb24 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) (/users/khan22/cs6475/llvm-project/build/bin/../lib/libLLVMOptDriver.so.20.0git+0x2fb24)
#21 0x00007f95a21dc82d optMain (/users/khan22/cs6475/llvm-project/build/bin/../lib/libLLVMOptDriver.so.20.0git+0x3c82d)
#22 0x00007f95a1cafd90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#23 0x00007f95a1cafe40 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#24 0x00005622cfbbc095 _start (./bin/opt+0x1095)
Aborted
```
When I do not use `Value *LHS` or `Value *RHS` and use operands directly as following:
```
// x - y + y * 2 = x + y
//Value *LHS = I->getOperand(0);
//Value *RHS = I->getOperand(1);
if (match(I->getOperand(0), m_Sub(m_Value(X), m_Value(Y)))) {
cs6475_debug("KKK: matched the 'sub'\n");
if(match (I->getOperand(1), m_Shl(m_Value(Y), m_ConstantInt(C)))){
cs6475_debug("KKK: matched the 'mul'\n");
if (C->isOne()) {
cs6475_debug("KKK: matched the 'shl 1'\n");
}
}
}
```
It works fine:
```
./bin/opt -O2 add.ll -S -o -
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
KKK: matched the 'sub'
KKK: matched the 'mul'
KKK: matched the 'shl 1'
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
KKK: matched the 'sub'
KKK: matched the 'mul'
KKK: matched the 'shl 1'
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
KKK: matched the 'sub'
KKK: matched the 'mul'
KKK: matched the 'shl 1'
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
KKK: matched the 'sub'
KKK: matched the 'mul'
KKK: matched the 'shl 1'
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
KKK: matched the 'sub'
KKK: matched the 'mul'
KKK: matched the 'shl 1'
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
KKK: matched the 'sub'
KKK: matched the 'mul'
KKK: matched the 'shl 1'
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
KKK: matched the 'sub'
KKK: matched the 'mul'
KKK: matched the 'shl 1'
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
CS 6475 matcher: running now
KKK: matched the 'sub'
KKK: matched the 'mul'
KKK: matched the 'shl 1'
CS 6475 matcher: running now
; ModuleID = 'add.ll'
source_filename = "add.ll"
; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
define i32 @sum2(i32 %0, i32 %1) local_unnamed_addr #0 {
%3 = sub i32 %0, %1
%4 = shl i32 %1, 1
%5 = add i32 %3, %4
ret i32 %5
}
attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) }
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs