Skip to content

Commit 51645d0

Browse files
authored
Merge pull request #81754 from compnerd/closed
IRGen: honour `-static-libclosure` in block creation
2 parents e910d9b + c0993d4 commit 51645d0

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

lib/IRGen/GenFunc.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@
7878
#include "swift/AST/SubstitutionMap.h"
7979
#include "swift/AST/Types.h"
8080
#include "swift/Basic/Assertions.h"
81+
#include "swift/ClangImporter/ClangImporter.h"
8182
#include "swift/IRGen/Linking.h"
8283
#include "clang/AST/ASTContext.h"
84+
#include "clang/Basic/CodeGenOptions.h"
8385
#include "clang/CodeGen/CodeGenABITypes.h"
8486
#include "llvm/ADT/StringSwitch.h"
8587
#include "llvm/IR/Constants.h"
@@ -2749,14 +2751,17 @@ void irgen::emitBlockHeader(IRGenFunction &IGF,
27492751
= IGF.getTypeInfoForLowered(blockTy).as<BlockStorageTypeInfo>();
27502752

27512753
Address headerAddr = storageTL.projectBlockHeader(IGF, storage);
2752-
2754+
27532755
//
27542756
// Initialize the "isa" pointer, which is _NSConcreteStackBlock.
27552757
auto NSConcreteStackBlock =
27562758
IGF.IGM.getModule()->getOrInsertGlobal("_NSConcreteStackBlock",
27572759
IGF.IGM.ObjCClassStructTy);
2758-
ApplyIRLinkage(IRLinkage::ExternalImport)
2759-
.to(cast<llvm::GlobalVariable>(NSConcreteStackBlock));
2760+
swift::ClangImporter *CI =
2761+
static_cast<ClangImporter *>(IGF.IGM.Context.getClangModuleLoader());
2762+
if (!CI->getCodeGenOpts().StaticClosure)
2763+
ApplyIRLinkage(IRLinkage::ExternalImport)
2764+
.to(cast<llvm::GlobalVariable>(NSConcreteStackBlock));
27602765

27612766
//
27622767
// Set the flags.

lib/IRGen/IRGenModule.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,10 +1170,15 @@ llvm::Constant *swift::getRuntimeFn(
11701170

11711171
if (IGM && useDllStorage(IGM->Triple) && IsExternal) {
11721172
bool bIsImported = true;
1173+
swift::ASTContext &Context = IGM->Context;
11731174
if (IGM->getSwiftModule()->getPublicModuleName(true).str() == ModuleName)
11741175
bIsImported = false;
1175-
else if (ModuleDecl *MD = IGM->Context.getModuleByName(ModuleName))
1176+
else if (ModuleDecl *MD = Context.getModuleByName(ModuleName))
11761177
bIsImported = !MD->isStaticLibrary();
1178+
else if (strcmp(ModuleName, "BlocksRuntime") == 0)
1179+
bIsImported =
1180+
!static_cast<ClangImporter *>(Context.getClangModuleLoader())
1181+
->getCodeGenOpts().StaticClosure;
11771182

11781183
if (bIsImported)
11791184
fn->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);

test/IRGen/static-libclosure.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %swift_frontend_plain -target x86_64-unknown-windows-msvc -primary-file %s -parse-as-library -parse-stdlib -nostdimport -module-name M -emit-ir -Xcc -static-libclosure -o - | %FileCheck %s
2+
3+
public let block: @convention(block) () -> () = {
4+
}
5+
6+
// CHECK-NOT: @_NSConcreteStackBlock = external dllimport global
7+
// CHECK-NOT: declare dllimport ptr @_Block_copy(ptr)
8+
// CHECK: @_NSConcreteStackBlock = external global
9+
// CHECK: declare ptr @_Block_copy(ptr)

0 commit comments

Comments
 (0)