Skip to content

Commit 8955950

Browse files
committed
Exception support for basic block sections
This is part of the Propeller framework to do post link code layout optimizations. Please see the RFC here: https://groups.google.com/forum/#!msg/llvm-dev/ef3mKzAdJ7U/1shV64BYBAAJ and the detailed RFC doc here: https://github.com/google/llvm-propeller/blob/plo-dev/Propeller_RFC.pdf This patch provides exception support for basic block sections by splitting the call-site table into call-site ranges corresponding to different basic block sections. Still all landing pads must reside in the same basic block section (which is guaranteed by the the core basic block section patch D73674 (ExceptionSection) ). Each call-site table will refer to the landing pad fragment by explicitly specifying @lpstart (which is omitted in the normal non-basic-block section case). All these call-site tables will share their action and type tables. The C++ ABI somehow assumes that no landing pads point directly to LPStart (which works in the normal case since the function begin is never a landing pad), and uses LP.offset = 0 to specify no landing pad. In the case of basic block section where one section contains all the landing pads, the landing pad offset relative to LPStart could actually be zero. Thus, we avoid zero-offset landing pads by inserting a **nop** operation as the first non-CFI instruction in the exception section. **Background on Exception Handling in C++ ABI** https://github.com/itanium-cxx-abi/cxx-abi/blob/master/exceptions.pdf Compiler emits an exception table for every function. When an exception is thrown, the stack unwinding library queries the unwind table (which includes the start and end of each function) to locate the exception table for that function. The exception table includes a call site table for the function, which is used to guide the exception handling runtime to take the appropriate action upon an exception. Each call site record in this table is structured as follows: | CallSite | --> Position of the call site (relative to the function entry) | CallSite length | --> Length of the call site. | Landing Pad | --> Position of the landing pad (relative to the landing pad fragment’s begin label) | Action record offset | --> Position of the first action record The call site records partition a function into different pieces and describe what action must be taken for each callsite. The callsite fields are relative to the start of the function (as captured in the unwind table). The landing pad entry is a reference into the function and corresponds roughly to the catch block of a try/catch statement. When execution resumes at a landing pad, it receives an exception structure and a selector value corresponding to the type of the exception thrown, and executes similar to a switch-case statement. The landing pad field is relative to the beginning of the procedure fragment which includes all the landing pads (@lpstart). The C++ ABI requires all landing pads to be in the same fragment. Nonetheless, without basic block sections, @lpstart is the same as the function @start (found in the unwind table) and can be omitted. The action record offset is an index into the action table which includes information about which exception types are caught. **C++ Exceptions with Basic Block Sections** Basic block sections break the contiguity of a function fragment. Therefore, call sites must be specified relative to the beginning of the basic block section. Furthermore, the unwinding library should be able to find the corresponding callsites for each section. To do so, the .cfi_lsda directive for a section must point to the range of call-sites for that section. This patch introduces a new **CallSiteRange** structure which specifies the range of call-sites which correspond to every section: `struct CallSiteRange { // Symbol marking the beginning of the precedure fragment. MCSymbol *FragmentBeginLabel = nullptr; // Symbol marking the end of the procedure fragment. MCSymbol *FragmentEndLabel = nullptr; // LSDA symbol for this call-site range. MCSymbol *ExceptionLabel = nullptr; // Index of the first call-site entry in the call-site table which // belongs to this range. size_t CallSiteBeginIdx = 0; // Index just after the last call-site entry in the call-site table which // belongs to this range. size_t CallSiteEndIdx = 0; // Whether this is the call-site range containing all the landing pads. bool IsLPRange = false; };` With N basic-block-sections, the call-site table is partitioned into N call-site ranges. Conceptually, we emit the call-site ranges for sections sequentially in the exception table as if each section has its own exception table. In the example below, two sections result in the two call site ranges (denoted by LSDA1 and LSDA2) placed next to each other. However, their call-sites will refer to records in the shared Action Table. We also emit the header fields (@lpstart and CallSite Table Length) for each call site range in order to place the call site ranges in separate LSDAs. We note that with -basic-block-sections, The CallSiteTableLength will not actually represent the length of the call site table, but rather the reference to the action table. Since the only purpose of this field is to locate the action table, correctness is guaranteed. Finally, every call site range has one @lpstart pointer so the landing pads of each section must all reside in one section (not necessarily the same section). To make this easier, we decide to place all landing pads of the function in one section (hence the `IsLPRange` field in CallSiteRange). | @lpstart | ---> Landing pad fragment ( LSDA1 points here) | CallSite Table Length | ---> Used to find the action table. | CallSites | | … | | … | | @lpstart | ---> Landing pad fragment ( LSDA2 points here) | CallSite Table Length | | CallSites | | … | | … | … … | Action Table | | Types Table | Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D73739
1 parent afc277b commit 8955950

12 files changed

+541
-89
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,11 @@ class AsmPrinter : public MachineFunctionPass {
141141

142142
private:
143143
MCSymbol *CurrentFnEnd = nullptr;
144-
MCSymbol *CurExceptionSym = nullptr;
144+
145+
/// Map a basic block section ID to the exception symbol associated with that
146+
/// section. Map entries are assigned and looked up via
147+
/// AsmPrinter::getMBBExceptionSym.
148+
DenseMap<unsigned, MCSymbol *> MBBSectionExceptionSyms;
145149

146150
// The symbol used to represent the start of the current BB section of the
147151
// function. This is used to calculate the size of the BB section.
@@ -238,7 +242,10 @@ class AsmPrinter : public MachineFunctionPass {
238242

239243
MCSymbol *getFunctionBegin() const { return CurrentFnBegin; }
240244
MCSymbol *getFunctionEnd() const { return CurrentFnEnd; }
241-
MCSymbol *getCurExceptionSym();
245+
246+
// Return the exception symbol associated with the MBB section containing a
247+
// given basic block.
248+
MCSymbol *getMBBExceptionSym(const MachineBasicBlock &MBB);
242249

243250
/// Return information about object file lowering.
244251
const TargetLoweringObjectFile &getObjFileLowering() const;

llvm/include/llvm/CodeGen/AsmPrinterHandler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class MachineFunction;
2424
class MachineInstr;
2525
class MCSymbol;
2626

27-
typedef MCSymbol *ExceptionSymbolProvider(AsmPrinter *Asm);
27+
typedef MCSymbol *ExceptionSymbolProvider(AsmPrinter *Asm,
28+
const MachineBasicBlock *MBB);
2829

2930
/// Collects and handles AsmPrinter objects required to build debug
3031
/// or EH information.

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,10 +1763,11 @@ bool AsmPrinter::doFinalization(Module &M) {
17631763
return false;
17641764
}
17651765

1766-
MCSymbol *AsmPrinter::getCurExceptionSym() {
1767-
if (!CurExceptionSym)
1768-
CurExceptionSym = createTempSymbol("exception");
1769-
return CurExceptionSym;
1766+
MCSymbol *AsmPrinter::getMBBExceptionSym(const MachineBasicBlock &MBB) {
1767+
auto Res = MBBSectionExceptionSyms.try_emplace(MBB.getSectionIDNum());
1768+
if (Res.second)
1769+
Res.first->second = createTempSymbol("exception");
1770+
return Res.first->second;
17701771
}
17711772

17721773
void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
@@ -1793,7 +1794,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
17931794
CurrentFnBegin = nullptr;
17941795
CurrentSectionBeginSym = nullptr;
17951796
MBBSectionRanges.clear();
1796-
CurExceptionSym = nullptr;
1797+
MBBSectionExceptionSyms.clear();
17971798
bool NeedsLocalForSize = MAI->needsLocalForSize();
17981799
if (F.hasFnAttribute("patchable-function-entry") ||
17991800
F.hasFnAttribute("function-instrument") ||

llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ void DwarfCFIException::endModule() {
8181
}
8282
}
8383

84-
static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
85-
return Asm->getCurExceptionSym();
84+
static MCSymbol *getExceptionSym(AsmPrinter *Asm,
85+
const MachineBasicBlock *MBB) {
86+
return Asm->getMBBExceptionSym(*MBB);
8687
}
8788

8889
void DwarfCFIException::beginFunction(const MachineFunction *MF) {
@@ -161,7 +162,7 @@ void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
161162

162163
// Provide LSDA information.
163164
if (shouldEmitLSDA)
164-
Asm->OutStreamer->emitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
165+
Asm->OutStreamer->emitCFILsda(ESP(Asm, MBB), TLOF.getLSDAEncoding());
165166
}
166167

167168
/// endFunction - Gather and emit post-function exception information.

0 commit comments

Comments
 (0)