IRGen: honour -static-libclosure in block creation

When creating a block, ensure that we correctly associate the DLL
Storage on the `_NSConcreteStackBlock` root object declaration based
upon whether we are generating code with `-static-libclosure` being
passed to the clang importer or not.
This commit is contained in:
Saleem Abdulrasool
2025-05-23 13:07:27 -07:00
parent cc2189bd05
commit c0993d466d
3 changed files with 23 additions and 4 deletions

View File

@@ -78,8 +78,10 @@
#include "swift/AST/SubstitutionMap.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Assertions.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/IRGen/Linking.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/CodeGenOptions.h"
#include "clang/CodeGen/CodeGenABITypes.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/IR/Constants.h"
@@ -2755,6 +2757,9 @@ void irgen::emitBlockHeader(IRGenFunction &IGF,
auto NSConcreteStackBlock =
IGF.IGM.getModule()->getOrInsertGlobal("_NSConcreteStackBlock",
IGF.IGM.ObjCClassStructTy);
swift::ClangImporter *CI =
static_cast<ClangImporter *>(IGF.IGM.Context.getClangModuleLoader());
if (!CI->getCodeGenOpts().StaticClosure)
ApplyIRLinkage(IRLinkage::ExternalImport)
.to(cast<llvm::GlobalVariable>(NSConcreteStackBlock));

View File

@@ -1170,10 +1170,15 @@ llvm::Constant *swift::getRuntimeFn(
if (IGM && useDllStorage(IGM->Triple) && IsExternal) {
bool bIsImported = true;
swift::ASTContext &Context = IGM->Context;
if (IGM->getSwiftModule()->getPublicModuleName(true).str() == ModuleName)
bIsImported = false;
else if (ModuleDecl *MD = IGM->Context.getModuleByName(ModuleName))
else if (ModuleDecl *MD = Context.getModuleByName(ModuleName))
bIsImported = !MD->isStaticLibrary();
else if (strcmp(ModuleName, "BlocksRuntime") == 0)
bIsImported =
!static_cast<ClangImporter *>(Context.getClangModuleLoader())
->getCodeGenOpts().StaticClosure;
if (bIsImported)
fn->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);

View File

@@ -0,0 +1,9 @@
// 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
public let block: @convention(block) () -> () = {
}
// CHECK-NOT: @_NSConcreteStackBlock = external dllimport global
// CHECK-NOT: declare dllimport ptr @_Block_copy(ptr)
// CHECK: @_NSConcreteStackBlock = external global
// CHECK: declare ptr @_Block_copy(ptr)