From af0b14e5edfacb8766e446d583e1b43932e877a5 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 4 Jun 2019 15:11:07 -0700 Subject: [PATCH 1/2] SILGen: refactor `#dsohandle` implementation Extract a couple of local variables. Reflow the text and rearrange the flow to support Windows. --- lib/SILGen/SILGenExpr.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/SILGen/SILGenExpr.cpp b/lib/SILGen/SILGenExpr.cpp index c490e36484928..4898309be67b8 100644 --- a/lib/SILGen/SILGenExpr.cpp +++ b/lib/SILGen/SILGenExpr.cpp @@ -3628,20 +3628,22 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) { SGF.getLoweredType(UnsafeRawPointer->getDeclaredInterfaceType()); SILType BuiltinRawPtrTy = SILType::getRawPointerType(SGF.getASTContext()); + SILModule &M = SGF.SGM.M; + SILBuilder &B = SGF.B; - auto DSOGlobal = SGF.SGM.M.lookUpGlobalVariable("__dso_handle"); + auto DSOGlobal = M.lookUpGlobalVariable("__dso_handle"); if (!DSOGlobal) - DSOGlobal = SILGlobalVariable::create(SGF.SGM.M, - SILLinkage::PublicExternal, - IsNotSerialized, "__dso_handle", - BuiltinRawPtrTy); - auto DSOAddr = SGF.B.createGlobalAddr(SILLoc, DSOGlobal); + DSOGlobal = + SILGlobalVariable::create(M, SILLinkage::PublicExternal, + IsNotSerialized, "__dso_handle", + BuiltinRawPtrTy); - auto DSOPointer = SGF.B.createAddressToPointer(SILLoc, DSOAddr, - BuiltinRawPtrTy); + auto DSOAddr = B.createGlobalAddr(SILLoc, DSOGlobal); + auto DSOPointer = + B.createAddressToPointer(SILLoc, DSOAddr, BuiltinRawPtrTy); + auto UnsafeRawPtrStruct = + B.createStruct(SILLoc, UnsafeRawPtrTy, { DSOPointer }); - auto UnsafeRawPtrStruct = SGF.B.createStruct(SILLoc, UnsafeRawPtrTy, - { DSOPointer }); return RValue(SGF, E, ManagedValue::forUnmanaged(UnsafeRawPtrStruct)); } } From 585659177764a603428eded862d2b6202b4c8681 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Tue, 4 Jun 2019 16:20:31 -0700 Subject: [PATCH 2/2] SILGen: make `#dsohandle` work properly on Windows Windows has a special symbol `__ImageBase` whcih provides the constant value of the base of the image. This is roughly equivalent to the `__dso_handle` on the ELF and MachO targets. Use this to construct the value for the `#dsohandle` rather than attempting to use the non-existent symbol `__dso_handle`. This fixes the dsohandle-multi-module validation test on Windows. --- lib/SILGen/SILGenExpr.cpp | 41 +++++++++++++++++++---------- test/SILGen/default_arguments.swift | 2 +- test/SILGen/dso_handle.swift | 2 +- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/lib/SILGen/SILGenExpr.cpp b/lib/SILGen/SILGenExpr.cpp index 4898309be67b8..e1d02a526018d 100644 --- a/lib/SILGen/SILGenExpr.cpp +++ b/lib/SILGen/SILGenExpr.cpp @@ -3631,20 +3631,33 @@ visitMagicIdentifierLiteralExpr(MagicIdentifierLiteralExpr *E, SGFContext C) { SILModule &M = SGF.SGM.M; SILBuilder &B = SGF.B; - auto DSOGlobal = M.lookUpGlobalVariable("__dso_handle"); - if (!DSOGlobal) - DSOGlobal = - SILGlobalVariable::create(M, SILLinkage::PublicExternal, - IsNotSerialized, "__dso_handle", - BuiltinRawPtrTy); - - auto DSOAddr = B.createGlobalAddr(SILLoc, DSOGlobal); - auto DSOPointer = - B.createAddressToPointer(SILLoc, DSOAddr, BuiltinRawPtrTy); - auto UnsafeRawPtrStruct = - B.createStruct(SILLoc, UnsafeRawPtrTy, { DSOPointer }); - - return RValue(SGF, E, ManagedValue::forUnmanaged(UnsafeRawPtrStruct)); + StructInst *S = nullptr; + if (M.getASTContext().LangOpts.Target.isOSWindows()) { + auto ImageBase = M.lookUpGlobalVariable("__ImageBase"); + if (!ImageBase) + ImageBase = + SILGlobalVariable::create(M, SILLinkage::Public, IsNotSerialized, + "__ImageBase", BuiltinRawPtrTy); + + auto ImageBaseAddr = B.createGlobalAddr(SILLoc, ImageBase); + auto ImageBasePointer = + B.createAddressToPointer(SILLoc, ImageBaseAddr, BuiltinRawPtrTy); + S = B.createStruct(SILLoc, UnsafeRawPtrTy, { ImageBasePointer }); + } else { + auto DSOGlobal = M.lookUpGlobalVariable("__dso_handle"); + if (!DSOGlobal) + DSOGlobal = + SILGlobalVariable::create(M, SILLinkage::PublicExternal, + IsNotSerialized, "__dso_handle", + BuiltinRawPtrTy); + + auto DSOAddr = B.createGlobalAddr(SILLoc, DSOGlobal); + auto DSOPointer = + B.createAddressToPointer(SILLoc, DSOAddr, BuiltinRawPtrTy); + S = B.createStruct(SILLoc, UnsafeRawPtrTy, { DSOPointer }); + } + + return RValue(SGF, E, ManagedValue::forUnmanaged(S)); } } diff --git a/test/SILGen/default_arguments.swift b/test/SILGen/default_arguments.swift index aa4b8db3f0d8b..e02df77f39b56 100644 --- a/test/SILGen/default_arguments.swift +++ b/test/SILGen/default_arguments.swift @@ -199,7 +199,7 @@ func takeDSOHandle(_ handle: UnsafeRawPointer = #dsohandle) { } // CHECK-LABEL: sil hidden [ossa] @$s17default_arguments13testDSOHandleyyF func testDSOHandle() { - // CHECK: [[DSO_HANDLE:%[0-9]+]] = global_addr @__dso_handle : $*Builtin.RawPointer + // CHECK: [[DSO_HANDLE:%[0-9]+]] = global_addr {{@__dso_handle|@__ImageBase}} : $*Builtin.RawPointer takeDSOHandle() } diff --git a/test/SILGen/dso_handle.swift b/test/SILGen/dso_handle.swift index e20b3ac3845e6..e7fec3557f27d 100644 --- a/test/SILGen/dso_handle.swift +++ b/test/SILGen/dso_handle.swift @@ -1,6 +1,6 @@ // RUN: %target-swift-emit-silgen -Xllvm -sil-full-demangle %s | %FileCheck %s -// CHECK: sil_global [[DSO:@__dso_handle]] : $Builtin.RawPointer +// CHECK: sil_global [[DSO:@__dso_handle|@__ImageBase]] : $Builtin.RawPointer // CHECK-LABEL: sil [ossa] @main : $@convention(c) // CHECK: bb0