[llvm] [BPF] Remove 'may_goto 0' instructions (PR #123482)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 24 13:52:46 PST 2025
================
@@ -682,6 +685,56 @@ bool BPFMIPreEmitPeephole::insertMissingCallerSavedSpills() {
return Changed;
}
+bool BPFMIPreEmitPeephole::removeMayGotoZero() {
+ bool Changed = false;
+ MachineBasicBlock *Prev_MBB, *Curr_MBB = nullptr;
+
+ for (MachineBasicBlock &MBB : make_early_inc_range(reverse(*MF))) {
+ Prev_MBB = Curr_MBB;
+ Curr_MBB = &MBB;
+ if (Prev_MBB == nullptr)
+ continue;
+
+ MachineInstr &MI = MBB.back();
+ if (MI.getOpcode() != TargetOpcode::INLINEASM_BR)
+ continue;
+
+ const char *AsmStr = MI.getOperand(0).getSymbolName();
+ SmallVector<StringRef, 4> AsmPieces;
+ SplitString(AsmStr, AsmPieces, ";\n");
+
+ // Do not support multiple insns in one inline asm.
+ if (AsmPieces.size() != 1)
+ continue;
+
+ // The asm insn must be a may_goto insn.
+ SmallVector<StringRef, 4> AsmOpPieces;
+ SplitString(AsmPieces[0], AsmOpPieces, " ");
+ if (AsmOpPieces[0] != "may_goto")
+ continue;
+
+ // Get the may_goto branch target.
+ MachineOperand &MO = MI.getOperand(InlineAsm::MIOp_FirstOperand + 1);
+ if (MO.getMBB() != Prev_MBB)
----------------
eddyz87 wrote:
I think that `MO.isMBB()` is needed, otherwise this would abort on (bogus) input as below:
```
$ cat test.c
void bar(int i) {
asm volatile goto ("may_goto %l[lbl];"::"r"(i)::lbl);
lbl:
asm volatile("r0 = 42;");
return;
}
$ clang -O2 --target=bpf -S test.c -o - 2>&1 | head -n 20
clang: /home/eddy/work/llvm-project/llvm/include/llvm/CodeGen/MachineOperand.h:572: MachineBasicBlock *llvm::MachineOperand::getMBB() const: Assertion `isMBB() && "Wrong MachineOperand accessor"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: clang -O2 --target=bpf -S test.c -o -
1. <eof> parser at end of file
2. Code generation
3. Running pass 'Function Pass Manager' on module 'test.c'.
4. Running pass 'BPF PreEmit Peephole Optimization' on function '@bar'
#0 0x00007f8711bfe388 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/eddy/work/llvm-project/llvm/lib/Support/Unix/Signals.inc:794:13
#1 0x00007f8711bfc260 llvm::sys::RunSignalHandlers() /home/eddy/work/llvm-project/llvm/lib/Support/Signals.cpp:106:18
#2 0x00007f8711b3e7d6 (anonymous namespace)::CrashRecoveryContextImpl::HandleCrash(int, unsigned long) /home/eddy/work/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:73:5
#3 0x00007f8711b3e7d6 CrashRecoverySignalHandler(int) /home/eddy/work/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp:390:51
#4 0x00007f8711426090 __restore_rt (/lib64/libc.so.6+0x1a090)
#5 0x00007f871147f0f4 __pthread_kill_implementation /usr/src/debug/glibc-2.40-17.fc41.x86_64/nptl/pthread_kill.c:44:76
#6 0x00007f8711425fde gsignal /usr/src/debug/glibc-2.40-17.fc41.x86_64/signal/../sysdeps/posix/raise.c:27:6
#7 0x00007f871140d942 abort /usr/src/debug/glibc-2.40-17.fc41.x86_64/stdlib/abort.c:81:7
#8 0x00007f871140d85e _nl_load_domain.cold /usr/src/debug/glibc-2.40-17.fc41.x86_64/intl/loadmsgcat.c:1177:9
#9 0x00007f871141e107 (/lib64/libc.so.6+0x12107)
#10 0x00007f8718304657 llvm::MachineOperand::getSymbolName() const /home/eddy/work/llvm-project/llvm/include/llvm/CodeGen/MachineOperand.h:638:5
#11 0x00007f8718304657 (anonymous namespace)::BPFMIPreEmitPeephole::removeMayGotoZero() /home/eddy/work/llvm-project/llvm/lib/Target/BPF/BPFMIPeephole.cpp:702:43
```
https://github.com/llvm/llvm-project/pull/123482
More information about the llvm-commits
mailing list