Skip to content

Commit 5593f27

Browse files
committed
Revise unwind-table emission, enabling them for most popular targets
Based on the clang 19 logic (but not 100% accurate). There's an interesting special case - Darwin on arm64 apparently uses *synchronous* tables.
1 parent e0e7fed commit 5593f27

16 files changed

+59
-28
lines changed

gen/abi/aarch64.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ struct AArch64TargetABI : TargetABI {
5757
public:
5858
AArch64TargetABI() {}
5959

60+
llvm::UWTableKind defaultUnwindTableKind() override {
61+
return isDarwin() ? llvm::UWTableKind::Sync : llvm::UWTableKind::Async;
62+
}
63+
6064
bool returnInArg(TypeFunction *tf, bool) override {
6165
Type *rt = tf->next->toBasetype();
6266
if (rt->ty == TY::Tstruct || rt->ty == TY::Tsarray) {

gen/abi/abi.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ llvm::CallingConv::ID TargetABI::callingConv(FuncDeclaration *fdecl) {
155155
return callingConv(tf, fdecl->needThis() || fdecl->isNested());
156156
}
157157

158+
void TargetABI::setUnwindTableKind(llvm::Function *fn) {
159+
llvm::UWTableKind kind = defaultUnwindTableKind();
160+
if (kind != llvm::UWTableKind::None) {
161+
#if LDC_LLVM_VER >= 1600
162+
fn->setUWTableKind(kind);
163+
#else
164+
fn->setUWTableKind(kind);
165+
#endif
166+
}
167+
}
168+
158169
//////////////////////////////////////////////////////////////////////////////
159170

160171
bool TargetABI::returnInArg(TypeFunction *tf, bool needsThis) {

gen/abi/abi.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "dmd/globals.h"
2020
#include "gen/dvalue.h"
2121
#include "llvm/IR/CallingConv.h"
22+
#include "llvm/Support/CodeGen.h" // for UWTableKind
2223
#include <vector>
2324

2425
class Type;
@@ -33,6 +34,7 @@ namespace llvm {
3334
class Type;
3435
class Value;
3536
class FunctionType;
37+
class Function;
3638
}
3739

3840
/// Transforms function arguments and return values.
@@ -110,14 +112,14 @@ struct TargetABI {
110112
return name;
111113
}
112114

113-
/// Returns true if all functions require the LLVM uwtable attribute.
114-
virtual bool needsUnwindTables() {
115-
// Condensed logic of Clang implementations of
116-
// `clang::ToolChain::IsUnwindTablesDefault()` based on early Clang 5.0.
117-
return global.params.targetTriple->getArch() == llvm::Triple::x86_64 ||
118-
global.params.targetTriple->getOS() == llvm::Triple::NetBSD;
115+
/// Returns the default unwind-table kind for all functions.
116+
/// Analogous to clang's ToolChain::getDefaultUnwindTableLevel().
117+
virtual llvm::UWTableKind defaultUnwindTableKind() {
118+
return llvm::UWTableKind::None;
119119
}
120120

121+
void setUnwindTableKind(llvm::Function *fn);
122+
121123
/// Returns true if the target is darwin-based.
122124
bool isDarwin() {
123125
return global.params.targetTriple->isOSDarwin();

gen/abi/arm.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ struct ArmTargetABI : TargetABI {
2626
CompositeToArray64 compositeToArray64;
2727
IntegerRewrite integerRewrite;
2828

29+
llvm::UWTableKind defaultUnwindTableKind() override {
30+
const auto &triple = *global.params.targetTriple;
31+
return triple.isOSLinux() || triple.isOSOpenBSD()
32+
? llvm::UWTableKind::None
33+
: llvm::UWTableKind::Async;
34+
}
35+
2936
bool returnInArg(TypeFunction *tf, bool) override {
3037
// AAPCS 5.4 wants composites > 4-bytes returned by arg except for
3138
// Homogeneous Aggregates of up-to 4 float types (6.1.2.1) - an HFA.

gen/abi/nvptx.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ struct NVPTXTargetABI : TargetABI {
4444
pointerRewite.applyTo(arg);
4545
}
4646
}
47-
// There are no exceptions at all, so no need for unwind tables.
48-
bool needsUnwindTables() override {
49-
return false;
50-
}
5147
};
5248

5349
TargetABI *createNVPTXABI() { return new NVPTXTargetABI(); }

gen/abi/ppc.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ struct PPCTargetABI : TargetABI {
3636

3737
explicit PPCTargetABI(const bool Is64Bit) : Is64Bit(Is64Bit) {}
3838

39+
llvm::UWTableKind defaultUnwindTableKind() override {
40+
return llvm::UWTableKind::Async;
41+
}
42+
3943
bool returnInArg(TypeFunction *tf, bool) override {
4044
Type *rt = tf->next->toBasetype();
4145

gen/abi/ppc64le.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ struct PPC64LETargetABI : TargetABI {
2929

3030
explicit PPC64LETargetABI() : hfvaToArray(8) {}
3131

32+
llvm::UWTableKind defaultUnwindTableKind() override {
33+
return llvm::UWTableKind::Async;
34+
}
35+
3236
bool passByVal(TypeFunction *, Type *t) override {
3337
t = t->toBasetype();
3438
return isPOD(t) &&

gen/abi/riscv64.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ struct RISCV64TargetABI : TargetABI {
158158
IntegerRewrite integerRewrite;
159159

160160
public:
161+
llvm::UWTableKind defaultUnwindTableKind() override {
162+
return global.params.targetTriple->isOSLinux() ? llvm::UWTableKind::Async
163+
: llvm::UWTableKind::None;
164+
}
165+
161166
Type *vaListType() override {
162167
// va_list is void*
163168
return pointerTo(Type::tvoid);

gen/abi/spirv.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ struct SPIRVTargetABI : TargetABI {
5050
pointerRewite.applyTo(arg);
5151
}
5252
}
53-
// There are no exceptions at all, so no need for unwind tables.
54-
bool needsUnwindTables() override {
55-
return false;
56-
}
5753
};
5854

5955
TargetABI *createSPIRVABI() { return new SPIRVTargetABI(); }

gen/abi/win64.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ struct Win64TargetABI : TargetABI {
116116
return name;
117117
}
118118

119+
llvm::UWTableKind defaultUnwindTableKind() override {
120+
return llvm::UWTableKind::Async;
121+
}
122+
119123
bool returnInArg(TypeFunction *tf, bool needsThis) override {
120124
Type *rt = tf->next->toBasetype();
121125

0 commit comments

Comments
 (0)