Skip to content

Commit 3c11665

Browse files
committed
[SILGen] Directly silgen access of #dsohandle
Synthesizing a VarDecl for #dsohandle causes some unwanted accessors to be expected, but we really don't need them: this is a global variable for the start of the image. There are only two uses of getDSOHandle: getting the type and emitting the SIL for it. Rather than perform acrobatics to turn off switches, just emit access directly where it's needed. rdar://problem/26565092
1 parent 8cb3581 commit 3c11665

File tree

6 files changed

+40
-39
lines changed

6 files changed

+40
-39
lines changed

include/swift/AST/Module.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,6 @@ class ModuleDecl : public TypeDecl, public DeclContext {
243243
DebugClient = R;
244244
}
245245

246-
/// Retrieve the magic __dso_handle variable.
247-
VarDecl *getDSOHandle();
248-
249246
/// Returns true if this module was or is being compiled for testing.
250247
bool isTestingEnabled() const {
251248
return Flags.TestingEnabled;

lib/AST/Module.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -367,28 +367,6 @@ void Module::removeFile(FileUnit &existingFile) {
367367
Files.erase(I.base());
368368
}
369369

370-
VarDecl *Module::getDSOHandle() {
371-
if (DSOHandle)
372-
return DSOHandle;
373-
374-
auto unsafeMutableRawPtr = getASTContext().getUnsafeMutableRawPointerDecl();
375-
if (!unsafeMutableRawPtr)
376-
return nullptr;
377-
378-
auto &ctx = getASTContext();
379-
auto handleVar = new (ctx) VarDecl(/*IsStatic=*/false, /*IsLet=*/false,
380-
SourceLoc(),
381-
ctx.getIdentifier("__dso_handle"),
382-
unsafeMutableRawPtr->getDeclaredType(),
383-
Files[0]);
384-
handleVar->setImplicit(true);
385-
handleVar->getAttrs().add(
386-
new (ctx) SILGenNameAttr("__dso_handle", /*Implicit=*/true));
387-
handleVar->setAccessibility(Accessibility::Internal);
388-
DSOHandle = handleVar;
389-
return handleVar;
390-
}
391-
392370
#define FORWARD(name, args) \
393371
for (const FileUnit *file : getFiles()) \
394372
file->name args;

lib/SILGen/SILGenExpr.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,8 +2004,27 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) {
20042004
}
20052005

20062006
case MagicIdentifierLiteralExpr::DSOHandle: {
2007-
return SGF.emitRValueForDecl(E, SGF.SGM.SwiftModule->getDSOHandle(),
2008-
E->getType(), AccessSemantics::Ordinary, C);
2007+
auto SILLoc = SILLocation(E);
2008+
auto UnsafeRawPointer = SGF.getASTContext().getUnsafeRawPointerDecl();
2009+
auto UnsafeRawPtrTy =
2010+
SGF.getLoweredType(UnsafeRawPointer->getDeclaredInterfaceType());
2011+
SILType BulitinRawPtrTy = SILType::getRawPointerType(SGF.getASTContext());
2012+
2013+
2014+
auto DSOGlobal = SGF.SGM.M.lookUpGlobalVariable("__dso_handle");
2015+
if (!DSOGlobal)
2016+
DSOGlobal = SILGlobalVariable::create(SGF.SGM.M,
2017+
SILLinkage::HiddenExternal,
2018+
IsNotFragile, "__dso_handle",
2019+
BulitinRawPtrTy);
2020+
auto DSOAddr = SGF.B.createGlobalAddr(SILLoc, DSOGlobal);
2021+
2022+
auto DSOPointer = SGF.B.createAddressToPointer(SILLoc, DSOAddr,
2023+
BulitinRawPtrTy);
2024+
2025+
auto UnsafeRawPtrStruct = SGF.B.createStruct(SILLoc, UnsafeRawPtrTy,
2026+
{ DSOPointer });
2027+
return RValue(SGF, E, ManagedValue::forUnmanaged(UnsafeRawPtrStruct));
20092028
}
20102029
}
20112030
}

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,9 @@ namespace {
12401240
if (tc.requirePointerArgumentIntrinsics(expr->getLoc()))
12411241
return nullptr;
12421242

1243-
return CS.DC->getParentModule()->getDSOHandle()->getInterfaceType();
1243+
auto unsafeRawPointer =
1244+
CS.getASTContext().getUnsafeRawPointerDecl();
1245+
return unsafeRawPointer->getDeclaredType();
12441246
}
12451247
}
12461248
}

test/SILGen/default_arguments.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ func testTakeDefaultArgUnnamed(_ i: Int) {
189189
takeDefaultArgUnnamed(i)
190190
}
191191

192-
func takeDSOHandle(_ handle: UnsafeMutableRawPointer = #dsohandle) { }
192+
func takeDSOHandle(_ handle: UnsafeRawPointer = #dsohandle) { }
193193

194194
// CHECK-LABEL: sil hidden @_TF17default_arguments13testDSOHandleFT_T_
195195
func testDSOHandle() {
196-
// CHECK: [[DSO_HANDLE:%[0-9]+]] = global_addr @__dso_handle : $*UnsafeMutableRawPointer
196+
// CHECK: [[DSO_HANDLE:%[0-9]+]] = global_addr @__dso_handle : $*Builtin.RawPointer
197197
takeDSOHandle()
198198
}
199199

@@ -225,7 +225,7 @@ class ReabstractDefaultArgument<T> {
225225
// CHECK-NEXT: apply [[INITFN]]<Int>(%7,
226226

227227
func testDefaultArgumentReabstraction() {
228-
ReabstractDefaultArgument<Int>()
228+
_ = ReabstractDefaultArgument<Int>()
229229
}
230230

231231
// <rdar://problem/20494437> SILGen crash handling default arguments

test/SILGen/dso_handle.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -emit-silgen %s | FileCheck %s
22

3-
// CHECK: sil_global hidden_external @__dso_handle : $UnsafeMutableRawPointer
3+
// CHECK: sil_global hidden_external [[DSO:@__dso_handle]] : $Builtin.RawPointer
44

55
// CHECK-LABEL: sil @main : $@convention(c)
66
// CHECK: bb0
7-
// CHECK: [[DSO:%[0-9]+]] = global_addr @__dso_handle : $*UnsafeMutableRawPointer
8-
// CHECK: load [[DSO]]
7+
// CHECK: [[DSOAddr:%[0-9]+]] = global_addr [[DSO]] : $*Builtin.RawPointer
8+
// CHECK-NEXT: [[DSOPtr:%[0-9]+]] = address_to_pointer [[DSOAddr]] : $*Builtin.RawPointer to $Builtin.RawPointer
9+
// CHECK-NEXT: [[DSOPtrStruct:[0-9]+]] = struct $UnsafeRawPointer ([[DSOPtr]] : $Builtin.RawPointer)
910

10-
// CHECK-LABEL: sil hidden @_TIF10dso_handle14printDSOHandleFT3dsoSv_SvA_
11-
// CHECK: [[DSO:%[0-9]+]] = global_addr @__dso_handle : $*UnsafeMutableRawPointer
12-
// CHECK: load [[DSO]]
13-
func printDSOHandle(dso: UnsafeMutableRawPointer = #dsohandle) -> UnsafeMutableRawPointer {
11+
12+
// CHECK-LABEL: sil hidden @_TIF10dso_handle14printDSOHandleFT3dsoSV_SVA_
13+
// CHECK: [[DSOAddr:%[0-9]+]] = global_addr [[DSO]] : $*Builtin.RawPointer
14+
// CHECK-NEXT: [[DSOPtr:%[0-9]+]] = address_to_pointer [[DSOAddr]] : $*Builtin.RawPointer to $Builtin.RawPointer
15+
// CHECK-NEXT: [[DSOPtrStruct:%[0-9]+]] = struct $UnsafeRawPointer ([[DSOPtr]] : $Builtin.RawPointer)
16+
// CHECK-NEXT: return [[DSOPtrStruct]] : $UnsafeRawPointer
17+
func printDSOHandle(dso: UnsafeRawPointer = #dsohandle) -> UnsafeRawPointer {
1418
print(dso)
19+
return dso
1520
}
1621

17-
printDSOHandle()
22+
_ = printDSOHandle()
1823

0 commit comments

Comments
 (0)