Skip to content

Commit f167273

Browse files
committed
Create HeapLayout destructors with internal linkage
This makes these symbols visible in backtraces, at the cost of binary size. The long-term goal is to generate these functions with LinkOnceODRLinkage, using the contents of the HeapLayout to produce a mangled name, so they can be deduplicated across modules. In order to make the symbols more meaningful when they appear in backtraces, use the name parameter passed to emitUnmanagedAlloc to produce a destructor name with the format __swift_{name}_destructor. rdar://149084103
1 parent 536518b commit f167273

5 files changed

Lines changed: 23 additions & 21 deletions

File tree

lib/IRGen/GenHeap.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,11 @@ void irgen::emitDeallocatePartialClassInstance(IRGenFunction &IGF,
423423

424424
/// Create the destructor function for a layout.
425425
/// TODO: give this some reasonable name and possibly linkage.
426-
static llvm::Function *createDtorFn(IRGenModule &IGM,
427-
const HeapLayout &layout) {
428-
llvm::Function *fn =
429-
llvm::Function::Create(IGM.DeallocatingDtorTy,
430-
llvm::Function::PrivateLinkage,
431-
"objectdestroy", &IGM.Module);
426+
static llvm::Function *createDtorFn(IRGenModule &IGM, const HeapLayout &layout,
427+
const llvm::Twine &layoutName) {
428+
llvm::Function *fn = llvm::Function::Create(
429+
IGM.DeallocatingDtorTy, llvm::Function::InternalLinkage,
430+
"__swift_" + layoutName + "_destructor", &IGM.Module);
432431
auto attrs = IGM.constructInitialAttributes();
433432
IGM.addSwiftSelfAttributes(attrs, 0);
434433
fn->setAttributes(attrs);
@@ -556,11 +555,12 @@ static llvm::Constant *buildPrivateMetadata(IRGenModule &IGM,
556555

557556
llvm::Constant *
558557
HeapLayout::getPrivateMetadata(IRGenModule &IGM,
559-
llvm::Constant *captureDescriptor) const {
558+
llvm::Constant *captureDescriptor,
559+
const llvm::Twine &name) const {
560560
if (!privateMetadata)
561-
privateMetadata = buildPrivateMetadata(IGM, *this, createDtorFn(IGM, *this),
562-
captureDescriptor,
563-
MetadataKind::HeapLocalVariable);
561+
privateMetadata = buildPrivateMetadata(
562+
IGM, *this, createDtorFn(IGM, *this, name), captureDescriptor,
563+
MetadataKind::HeapLocalVariable);
564564
return privateMetadata;
565565
}
566566

@@ -573,7 +573,8 @@ llvm::Value *IRGenFunction::emitUnmanagedAlloc(const HeapLayout &layout,
573573
return IGM.RefCountedNull;
574574
}
575575

576-
llvm::Value *metadata = layout.getPrivateMetadata(IGM, captureDescriptor);
576+
llvm::Value *metadata =
577+
layout.getPrivateMetadata(IGM, captureDescriptor, name);
577578
llvm::Value *size, *alignMask;
578579
if (offsets) {
579580
size = offsets->getSize();

lib/IRGen/GenHeap.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class HeapLayout : public StructLayout {
7676
/// As a convenience, build a metadata object with internal linkage
7777
/// consisting solely of the standard heap metadata.
7878
llvm::Constant *getPrivateMetadata(IRGenModule &IGM,
79-
llvm::Constant *captureDescriptor) const;
79+
llvm::Constant *captureDescriptor,
80+
const llvm::Twine &name) const;
8081
};
8182

8283
class HeapNonFixedOffsets : public NonFixedOffsetsImpl {

test/IRGen/closure.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
// -- partial_apply context metadata
1111

12-
// CHECK-DAG: [[METADATA:@.*]] = private constant %swift.full_boxmetadata { ptr {{.*}}@objectdestroy{{(\.ptrauth.*)?}}, ptr null, %swift.type { i64 1024 }, i32 16, ptr @"\01l__swift5_reflection_descriptor" }
12+
// CHECK-DAG: [[METADATA:@.*]] = private constant %swift.full_boxmetadata { ptr {{.*}}@__swift_closure_destructor{{(\.ptrauth.*)?}}, ptr null, %swift.type { i64 1024 }, i32 16, ptr @"\01l__swift5_reflection_descriptor" }
1313

1414
func a(i i: Int) -> (Int) -> Int {
1515
return { x in i }

test/IRGen/empty-noncopyable-with-deinit.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// RUN: %target-swift-frontend -emit-irgen %s | %FileCheck %s
22

3-
// CHECK: [[BOX_1:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_1:@[A-Za-z0-9.]+]],
4-
// CHECK: [[BOX_2:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_2:@[A-Za-z0-9.]+]],
5-
// CHECK: [[BOX_3:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_3:@[A-Za-z0-9.]+]],
6-
// CHECK: [[BOX_4:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_4:@[A-Za-z0-9.]+]],
3+
// CHECK: [[BOX_1:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_1:@[A-Za-z0-9._]+]],
4+
// CHECK: [[BOX_2:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_2:@[A-Za-z0-9._]+]],
5+
// CHECK: [[BOX_3:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_3:@[A-Za-z0-9._]+]],
6+
// CHECK: [[BOX_4:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_4:@[A-Za-z0-9._]+]],
77

88
// We don't really need to test arm64e, and doing so would mean tweaking the
99
// test to cope with ptrauth.

test/IRGen/fixed-noncopyable-with-deinit.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// RUN: %target-swift-frontend -emit-irgen %s | %FileCheck %s
22

3-
// CHECK: [[BOX_1:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_1:@[A-Za-z0-9.]+]],
4-
// CHECK: [[BOX_2:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_2:@[A-Za-z0-9.]+]],
5-
// CHECK: [[BOX_3:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_3:@[A-Za-z0-9.]+]],
6-
// CHECK: [[BOX_4:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_4:@[A-Za-z0-9.]+]],
3+
// CHECK: [[BOX_1:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_1:@[A-Za-z0-9._]+]],
4+
// CHECK: [[BOX_2:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_2:@[A-Za-z0-9._]+]],
5+
// CHECK: [[BOX_3:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_3:@[A-Za-z0-9._]+]],
6+
// CHECK: [[BOX_4:@[A-Za-z0-9.]+]] = private constant %swift.full_boxmetadata { ptr [[DESTROY_BOX_4:@[A-Za-z0-9._]+]],
77

88
// We don't really need to test arm64e, and doing so would mean tweaking the
99
// test to cope with ptrauth.

0 commit comments

Comments
 (0)