[clang] [llvm] [BPF] Lower unwinding to unwind_goto and unwind_exit (PR #233649)
Matt Arsenault via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 21:11:20 PST 2026
================
@@ -0,0 +1,315 @@
+//===-- BPFUnwind.cpp - Lower unwinding to unwind_goto and unwind_exit ----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// There are two ways to leave a frame, and a call that may unwind is followed
+// by an insn that tells them apart:
+// ret exit
+// invoke f() unwind pad call f; unwind_goto pad
+// resume r0 = 0; unwind_exit
+// call _Unwind_RaiseException r0 = 0; unwind_exit
+// invoke _Unwind_RaiseException goto pad
+// unwind_goto is taken when the callee left with unwind_exit. No register says
+// so: how one insn tells the other is up to the JIT. Otherwise unwind_exit is
+// exit, hence r0, and r2 as well in a function that returns more than 8 bytes.
+// A pad expects the exception object in r0. There is none, so a pad that looks
+// at it starts with r0 = 0.
+//
+// _Unwind_RaiseException() is how a language runtime starts an unwind with the
+// Itanium ABI, e.g. at the end of a panic handler. To start one and to carry
+// one on is the same thing here: leave the frame with unwind_exit, after its
+// own pad if it has one. It cannot fail, so it does not return.
+//
+// There are no tables and nothing for a loader to do.
+//
+//===----------------------------------------------------------------------===//
+
+#include "BPF.h"
+#include "BPFInstrInfo.h"
+#include "BPFSubtarget.h"
+#include "llvm/ADT/DepthFirstIterator.h"
+#include "llvm/ADT/SetVector.h"
+#include "llvm/CodeGen/LivePhysRegs.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/IR/DiagnosticInfo.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/InstIterator.h"
+#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Transforms/Utils/Local.h"
+
+using namespace llvm;
+
+/// On other targets it is a function of the unwinder library, which is why
+/// nothing else in LLVM knows the name.
+static constexpr StringLiteral UnwindRaise = "_Unwind_RaiseException";
----------------
arsenm wrote:
This is hacky. This is relying on a function declaration which shouldn't actually be added to the target's runtime libraries being incorrectly inserted, then relying on rewriting it later in codegen.
The ultimate form should be directly emitted without relying on incorrect library functions
https://github.com/llvm/llvm-project/pull/233649
More information about the cfe-commits
mailing list