Skip to content

Commit 623bcfe

Browse files
authored
Merge pull request swiftlang#8771 from bnbarham/revert-for-tsan-failures
[rebranch] Revert "Cleanup unwind table emission code a bit."
2 parents 23ba989 + ff76102 commit 623bcfe

13 files changed

+171
-45
lines changed

llvm/include/llvm/CodeGen/MachineFunction.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,10 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
11991199
/// Find or create an LandingPadInfo for the specified MachineBasicBlock.
12001200
LandingPadInfo &getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad);
12011201

1202+
/// Remap landing pad labels and remove any deleted landing pads.
1203+
void tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap = nullptr,
1204+
bool TidyIfNoBeginLabels = true);
1205+
12021206
/// Return a reference to the landing pad info for the current function.
12031207
const std::vector<LandingPadInfo> &getLandingPads() const {
12041208
return LandingPads;
@@ -1214,11 +1218,22 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
12141218
/// entry.
12151219
MCSymbol *addLandingPad(MachineBasicBlock *LandingPad);
12161220

1221+
/// Provide the catch typeinfo for a landing pad.
1222+
void addCatchTypeInfo(MachineBasicBlock *LandingPad,
1223+
ArrayRef<const GlobalValue *> TyInfo);
1224+
1225+
/// Provide the filter typeinfo for a landing pad.
1226+
void addFilterTypeInfo(MachineBasicBlock *LandingPad,
1227+
ArrayRef<const GlobalValue *> TyInfo);
1228+
1229+
/// Add a cleanup action for a landing pad.
1230+
void addCleanup(MachineBasicBlock *LandingPad);
1231+
12171232
/// Return the type id for the specified typeinfo. This is function wide.
12181233
unsigned getTypeIDFor(const GlobalValue *TI);
12191234

12201235
/// Return the id of the filter encoded by TyIds. This is function wide.
1221-
int getFilterIDFor(ArrayRef<unsigned> TyIds);
1236+
int getFilterIDFor(std::vector<unsigned> &TyIds);
12221237

12231238
/// Map the landing pad's EH symbol to the call site indexes.
12241239
void setCallSiteLandingPad(MCSymbol *Sym, ArrayRef<unsigned> Sites);

llvm/lib/CodeGen/AsmPrinter/AIXException.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@
2121

2222
namespace llvm {
2323

24-
AIXException::AIXException(AsmPrinter *A) : EHStreamer(A) {}
24+
AIXException::AIXException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
25+
26+
// This overrides 'DwarfCFIExceptionBase::markFunctionEnd', to avoid the call to
27+
// tidyLandingPads. This is necessary, because the
28+
// 'PPCAIXAsmPrinter::emitFunctionBodyEnd' function already checked whether we
29+
// need ehinfo, and emitted a traceback table with the bits set to indicate that
30+
// we will be emitting it, if so. Thus, if we remove it now -- so late in the
31+
// process -- we'll end up having emitted a reference to __ehinfo.N symbol, but
32+
// not emitting a definition for said symbol.
33+
void AIXException::markFunctionEnd() {}
2534

2635
void AIXException::emitExceptionInfoTable(const MCSymbol *LSDA,
2736
const MCSymbol *PerSym) {

llvm/lib/CodeGen/AsmPrinter/ARMException.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "llvm/MC/MCStreamer.h"
2020
using namespace llvm;
2121

22-
ARMException::ARMException(AsmPrinter *A) : EHStreamer(A) {}
22+
ARMException::ARMException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
2323

2424
ARMException::~ARMException() = default;
2525

@@ -51,6 +51,7 @@ void ARMException::beginFunction(const MachineFunction *MF) {
5151
void ARMException::markFunctionEnd() {
5252
if (shouldEmitCFI)
5353
Asm->OutStreamer->emitCFIEndProc();
54+
DwarfCFIExceptionBase::markFunctionEnd();
5455
}
5556

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

llvm/lib/CodeGen/AsmPrinter/DwarfCFIException.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@
2323
#include "llvm/Target/TargetOptions.h"
2424
using namespace llvm;
2525

26-
DwarfCFIException::DwarfCFIException(AsmPrinter *A) : EHStreamer(A) {}
26+
DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A) : EHStreamer(A) {}
27+
28+
void DwarfCFIExceptionBase::markFunctionEnd() {
29+
// Map all labels and get rid of any dead landing pads.
30+
if (!Asm->MF->getLandingPads().empty()) {
31+
MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
32+
NonConstMF->tidyLandingPads();
33+
}
34+
}
35+
36+
DwarfCFIException::DwarfCFIException(AsmPrinter *A)
37+
: DwarfCFIExceptionBase(A) {}
2738

2839
DwarfCFIException::~DwarfCFIException() = default;
2940

llvm/lib/CodeGen/AsmPrinter/DwarfException.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ namespace llvm {
2121
class MachineFunction;
2222
class ARMTargetStreamer;
2323

24-
class LLVM_LIBRARY_VISIBILITY DwarfCFIException : public EHStreamer {
24+
class LLVM_LIBRARY_VISIBILITY DwarfCFIExceptionBase : public EHStreamer {
25+
protected:
26+
DwarfCFIExceptionBase(AsmPrinter *A);
27+
28+
/// Per-function flag to indicate if frame CFI info should be emitted.
29+
bool shouldEmitCFI = false;
30+
/// Per-module flag to indicate if .cfi_section has beeen emitted.
31+
bool hasEmittedCFISections = false;
32+
33+
void markFunctionEnd() override;
34+
};
35+
36+
class LLVM_LIBRARY_VISIBILITY DwarfCFIException : public DwarfCFIExceptionBase {
2537
/// Per-function flag to indicate if .cfi_personality should be emitted.
2638
bool shouldEmitPersonality = false;
2739

@@ -63,13 +75,7 @@ class LLVM_LIBRARY_VISIBILITY DwarfCFIException : public EHStreamer {
6375
void endBasicBlockSection(const MachineBasicBlock &MBB) override;
6476
};
6577

66-
class LLVM_LIBRARY_VISIBILITY ARMException : public EHStreamer {
67-
/// Per-function flag to indicate if frame CFI info should be emitted.
68-
bool shouldEmitCFI = false;
69-
70-
/// Per-module flag to indicate if .cfi_section has beeen emitted.
71-
bool hasEmittedCFISections = false;
72-
78+
class LLVM_LIBRARY_VISIBILITY ARMException : public DwarfCFIExceptionBase {
7379
void emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) override;
7480
ARMTargetStreamer &getTargetStreamer();
7581

@@ -93,14 +99,16 @@ class LLVM_LIBRARY_VISIBILITY ARMException : public EHStreamer {
9399
void markFunctionEnd() override;
94100
};
95101

96-
class LLVM_LIBRARY_VISIBILITY AIXException : public EHStreamer {
102+
class LLVM_LIBRARY_VISIBILITY AIXException : public DwarfCFIExceptionBase {
97103
/// This is AIX's compat unwind section, which unwinder would use
98104
/// to find the location of LSDA area and personality rountine.
99105
void emitExceptionInfoTable(const MCSymbol *LSDA, const MCSymbol *PerSym);
100106

101107
public:
102108
AIXException(AsmPrinter *A);
103109

110+
void markFunctionEnd() override;
111+
104112
void endModule() override {}
105113
void beginFunction(const MachineFunction *MF) override {}
106114
void endFunction(const MachineFunction *MF) override;

llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,6 @@ void EHStreamer::computePadMap(
194194
const LandingPadInfo *LandingPad = LandingPads[i];
195195
for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
196196
MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
197-
MCSymbol *EndLabel = LandingPad->BeginLabels[j];
198-
// If we have deleted the code for a given invoke after registering it in
199-
// the LandingPad label list, the associated symbols will not have been
200-
// emitted. In that case, ignore this callsite entry.
201-
if (!BeginLabel->isDefined() || !EndLabel->isDefined())
202-
continue;
203197
assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
204198
PadRange P = { i, j };
205199
PadMap[BeginLabel] = P;
@@ -388,14 +382,8 @@ MCSymbol *EHStreamer::emitExceptionTable() {
388382
SmallVector<const LandingPadInfo *, 64> LandingPads;
389383
LandingPads.reserve(PadInfos.size());
390384

391-
for (const LandingPadInfo &LPI : PadInfos) {
392-
// If a landing-pad has an associated label, but the label wasn't ever
393-
// emitted, then skip it. (This can occur if the landingpad's MBB was
394-
// deleted).
395-
if (LPI.LandingPadLabel && !LPI.LandingPadLabel->isDefined())
396-
continue;
385+
for (const LandingPadInfo &LPI : PadInfos)
397386
LandingPads.push_back(&LPI);
398-
}
399387

400388
// Order landing pads lexicographically by type id.
401389
llvm::sort(LandingPads, [](const LandingPadInfo *L, const LandingPadInfo *R) {

llvm/lib/CodeGen/AsmPrinter/WasmException.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ void WasmException::endModule() {
4242
}
4343
}
4444

45+
void WasmException::markFunctionEnd() {
46+
// Get rid of any dead landing pads.
47+
if (!Asm->MF->getLandingPads().empty()) {
48+
auto *NonConstMF = const_cast<MachineFunction *>(Asm->MF);
49+
// Wasm does not set BeginLabel and EndLabel information for landing pads,
50+
// so we should set the second argument false.
51+
NonConstMF->tidyLandingPads(nullptr, /* TidyIfNoBeginLabels */ false);
52+
}
53+
}
54+
4555
void WasmException::endFunction(const MachineFunction *MF) {
4656
bool ShouldEmitExceptionTable = false;
4757
for (const LandingPadInfo &Info : MF->getLandingPads()) {

llvm/lib/CodeGen/AsmPrinter/WasmException.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class LLVM_LIBRARY_VISIBILITY WasmException : public EHStreamer {
2828

2929
void endModule() override;
3030
void beginFunction(const MachineFunction *MF) override {}
31+
void markFunctionEnd() override;
3132
void endFunction(const MachineFunction *MF) override;
3233

3334
protected:

llvm/lib/CodeGen/AsmPrinter/WinException.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ void WinException::endFunction(const MachineFunction *MF) {
130130
if (F.hasPersonalityFn())
131131
Per = classifyEHPersonality(F.getPersonalityFn()->stripPointerCasts());
132132

133+
// Get rid of any dead landing pads if we're not using funclets. In funclet
134+
// schemes, the landing pad is not actually reachable. It only exists so
135+
// that we can emit the right table data.
136+
if (!isFuncletEHPersonality(Per)) {
137+
MachineFunction *NonConstMF = const_cast<MachineFunction*>(MF);
138+
NonConstMF->tidyLandingPads();
139+
}
140+
133141
endFuncletImpl();
134142

135143
// endFunclet will emit the necessary .xdata tables for table-based SEH.

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 78 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -799,31 +799,32 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
799799
if (LPI->isCleanup() && LPI->getNumClauses() != 0)
800800
LP.TypeIds.push_back(0);
801801

802+
if (LPI->isCleanup())
803+
addCleanup(LandingPad);
804+
802805
// FIXME: New EH - Add the clauses in reverse order. This isn't 100%
803806
// correct, but we need to do it this way because of how the DWARF EH
804807
// emitter processes the clauses.
805808
for (unsigned I = LPI->getNumClauses(); I != 0; --I) {
806809
Value *Val = LPI->getClause(I - 1);
807810
if (LPI->isCatch(I - 1)) {
808-
LP.TypeIds.push_back(
809-
getTypeIDFor(dyn_cast<GlobalValue>(Val->stripPointerCasts())));
811+
addCatchTypeInfo(LandingPad,
812+
dyn_cast<GlobalValue>(Val->stripPointerCasts()));
810813
} else {
811814
// Add filters in a list.
812815
auto *CVal = cast<Constant>(Val);
813-
SmallVector<unsigned, 4> FilterList;
816+
SmallVector<const GlobalValue *, 4> FilterList;
814817
for (const Use &U : CVal->operands())
815-
FilterList.push_back(
816-
getTypeIDFor(cast<GlobalValue>(U->stripPointerCasts())));
818+
FilterList.push_back(cast<GlobalValue>(U->stripPointerCasts()));
817819

818-
LP.TypeIds.push_back(getFilterIDFor(FilterList));
820+
addFilterTypeInfo(LandingPad, FilterList);
819821
}
820822
}
821823

822824
} else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) {
823825
for (unsigned I = CPI->arg_size(); I != 0; --I) {
824-
auto *TypeInfo =
825-
dyn_cast<GlobalValue>(CPI->getArgOperand(I - 1)->stripPointerCasts());
826-
LP.TypeIds.push_back(getTypeIDFor(TypeInfo));
826+
Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts();
827+
addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo));
827828
}
828829

829830
} else {
@@ -833,6 +834,73 @@ MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
833834
return LandingPadLabel;
834835
}
835836

837+
void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
838+
ArrayRef<const GlobalValue *> TyInfo) {
839+
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
840+
for (const GlobalValue *GV : llvm::reverse(TyInfo))
841+
LP.TypeIds.push_back(getTypeIDFor(GV));
842+
}
843+
844+
void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
845+
ArrayRef<const GlobalValue *> TyInfo) {
846+
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
847+
std::vector<unsigned> IdsInFilter(TyInfo.size());
848+
for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
849+
IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
850+
LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
851+
}
852+
853+
void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap,
854+
bool TidyIfNoBeginLabels) {
855+
for (unsigned i = 0; i != LandingPads.size(); ) {
856+
LandingPadInfo &LandingPad = LandingPads[i];
857+
if (LandingPad.LandingPadLabel &&
858+
!LandingPad.LandingPadLabel->isDefined() &&
859+
(!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
860+
LandingPad.LandingPadLabel = nullptr;
861+
862+
// Special case: we *should* emit LPs with null LP MBB. This indicates
863+
// "nounwind" case.
864+
if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
865+
LandingPads.erase(LandingPads.begin() + i);
866+
continue;
867+
}
868+
869+
if (TidyIfNoBeginLabels) {
870+
for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
871+
MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
872+
MCSymbol *EndLabel = LandingPad.EndLabels[j];
873+
if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) &&
874+
(EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0)))
875+
continue;
876+
877+
LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
878+
LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
879+
--j;
880+
--e;
881+
}
882+
883+
// Remove landing pads with no try-ranges.
884+
if (LandingPads[i].BeginLabels.empty()) {
885+
LandingPads.erase(LandingPads.begin() + i);
886+
continue;
887+
}
888+
}
889+
890+
// If there is no landing pad, ensure that the list of typeids is empty.
891+
// If the only typeid is a cleanup, this is the same as having no typeids.
892+
if (!LandingPad.LandingPadBlock ||
893+
(LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
894+
LandingPad.TypeIds.clear();
895+
++i;
896+
}
897+
}
898+
899+
void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
900+
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
901+
LP.TypeIds.push_back(0);
902+
}
903+
836904
void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
837905
ArrayRef<unsigned> Sites) {
838906
LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
@@ -846,7 +914,7 @@ unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
846914
return TypeInfos.size();
847915
}
848916

849-
int MachineFunction::getFilterIDFor(ArrayRef<unsigned> TyIds) {
917+
int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
850918
// If the new filter coincides with the tail of an existing filter, then
851919
// re-use the existing filter. Folding filters more than this requires
852920
// re-ordering filters and/or their elements - probably not worth it.

llvm/test/CodeGen/X86/gcc_except_table_bb_sections.ll

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ declare i32 @__gxx_personality_v0(...)
148148
; CHECK-NEXT: .uleb128 .Ltmp0-.Lfunc_begin0 # >> Call Site 1 <<
149149
; CHECK-NEXT: .uleb128 .Ltmp1-.Ltmp0 # Call between .Ltmp0 and .Ltmp1
150150
; CHECK-NEXT: .uleb128 .Ltmp2-main.__part.2 # jumps to .Ltmp2
151-
; CHECK-NEXT: .byte 3 # On action: 2
151+
; CHECK-NEXT: .byte 5 # On action: 3
152152
; CHECK-NEXT: .p2align 2
153153
; CHECK-NEXT: .Lexception1:
154154

@@ -207,9 +207,12 @@ declare i32 @__gxx_personality_v0(...)
207207
; CHECK-NEXT: .byte 0 # >> Action Record 1 <<
208208
; CHECK-NEXT: # Cleanup
209209
; CHECK-NEXT: .byte 0 # No further actions
210-
; CHECK-NEXT: .byte 1 # >> Action Record 2 <<
211-
; CHECK-NEXT: # Catch TypeInfo 1
212-
; CHECK-NEXT: .byte 125 # Continue to action 1
210+
; CHECK-NEXT: .byte 0 # >> Action Record 2 <<
211+
; CHECK-NEXT: # Cleanup
212+
; CHECK-NEXT: .byte 125 # Continue to action 1
213+
; CHECK-NEXT: .byte 1 # >> Action Record 3 <<
214+
; CHECK-NEXT: # Catch TypeInfo 1
215+
; CHECK-NEXT: .byte 125 # Continue to action 2
213216
; CHECK-NEXT: .p2align 2
214217
; CHECK-NEXT: # >> Catch TypeInfos <<
215218

llvm/test/CodeGen/X86/gcc_except_table_bb_sections_ehpad_groups_with_cold.ll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ declare i32 @__gxx_personality_v0(...)
6666
; CHECK-NEXT: .uleb128 .Ltmp0-.Lfunc_begin0 # >> Call Site 1 <<
6767
; CHECK-NEXT: .uleb128 .Ltmp1-.Ltmp0 # Call between .Ltmp0 and .Ltmp1
6868
; CHECK-NEXT: .uleb128 .Ltmp2-main.cold # jumps to .Ltmp2
69-
; CHECK-NEXT: .byte 3 # On action: 2
69+
; CHECK-NEXT: .byte 5 # On action: 3
7070
; CHECK-NEXT: .p2align 2
7171
; CHECK-NEXT: .Lexception1:
7272
; CHECK-NEXT: .byte 0 # @LPStart Encoding = absptr
@@ -85,9 +85,12 @@ declare i32 @__gxx_personality_v0(...)
8585
; CHECK-NEXT: .byte 0 # >> Action Record 1 <<
8686
; CHECK-NEXT: # Cleanup
8787
; CHECK-NEXT: .byte 0 # No further actions
88-
; CHECK-NEXT: .byte 1 # >> Action Record 2 <<
89-
; CHECK-NEXT: # Catch TypeInfo 1
88+
; CHECK-NEXT: .byte 0 # >> Action Record 2 <<
89+
; CHECK-NEXT: # Cleanup
9090
; CHECK-NEXT: .byte 125 # Continue to action 1
91+
; CHECK-NEXT: .byte 1 # >> Action Record 3 <<
92+
; CHECK-NEXT: # Catch TypeInfo 1
93+
; CHECK-NEXT: .byte 125 # Continue to action 2
9194
; CHECK-NEXT: .p2align 2
9295
; CHECK-NEXT: # >> Catch TypeInfos <<
9396
; CHECK-NEXT: .long _ZTIi # TypeInfo 1

0 commit comments

Comments
 (0)