[llvm] [BPF] Handle unreachable with a kfunc call (PR #131731)

via llvm-commits llvm-commits at lists.llvm.org
Thu May 22 09:18:06 PDT 2025


================
@@ -320,6 +320,7 @@ struct BPFMIPreEmitPeephole : public MachineFunctionPass {
   bool adjustBranch();
   bool insertMissingCallerSavedSpills();
   bool removeMayGotoZero();
+  bool addExitAfterUnreachable();
----------------
yonghong-song wrote:

The above seems working but it will add 'exit' after __every__ bpf_unreachable() calls. For example, in IR, if we have
```
<...>
unreachable
<...>
unreachable
<...>
unreachable
```

The above code will generate
```
<...>
bpf_unreachable()
exit
<...>
bpf_unreachable()
exit
<...>
bpf_unreachable()
exit
```

I would like to have
```
<...>
bpf_unreachable()
<...>
bpf_unreachable()
<...>
bpf_unreachable()
exit
```

I will be a trivial change on top of my current patch:
```
diff --git a/llvm/lib/Target/BPF/BPFMIPeephole.cpp b/llvm/lib/Target/BPF/BPFMIPeephole.cpp
index a949d98e6de5..bf8188cc2528 100644
--- a/llvm/lib/Target/BPF/BPFMIPeephole.cpp
+++ b/llvm/lib/Target/BPF/BPFMIPeephole.cpp
@@ -742,8 +742,8 @@ bool BPFMIPreEmitPeephole::removeMayGotoZero() {
 bool BPFMIPreEmitPeephole::addExitAfterUnreachable() {
   MachineBasicBlock &MBB = MF->back();
   MachineInstr &MI = MBB.back();
-  if (MI.getOpcode() != BPF::JAL || !MI.getOperand(0).isSymbol() ||
-      strcmp(MI.getOperand(0).getSymbolName(), BPF_UNREACHABLE))
+  if (MI.getOpcode() != BPF::JAL || !MI.getOperand(0).isGlobal() ||
+      MI.getOperand(0).getGlobal()->getName() != BPF_UNREACHABLE)
     return false;
 
   BuildMI(&MBB, MI.getDebugLoc(), TII->get(BPF::RET));
```


https://github.com/llvm/llvm-project/pull/131731


More information about the llvm-commits mailing list