Skip to content

Commit 3ca4a6e

Browse files
Merge pull request #62403 from aschwaighofer/collocate_section_the_3rd
IRGen: Co-locate metadata instatiation/completions/accessor functions in a special section
2 parents e80e7e3 + 39e5e39 commit 3ca4a6e

File tree

76 files changed

+147
-92
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+147
-92
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,9 @@ class IRGenOptions {
419419

420420
unsigned DisableReadonlyStaticObjects : 1;
421421

422+
/// Collocate metadata functions in their own section.
423+
unsigned CollocatedMetadataFunctions : 1;
424+
422425
/// The number of threads for multi-threaded code generation.
423426
unsigned NumThreads = 0;
424427

@@ -489,7 +492,8 @@ class IRGenOptions {
489492
WitnessMethodElimination(false), ConditionalRuntimeRecords(false),
490493
InternalizeAtLink(false), InternalizeSymbols(false),
491494
EmitGenericRODatas(false), NoPreallocatedInstantiationCaches(false),
492-
DisableReadonlyStaticObjects(false), CmdArgs(),
495+
DisableReadonlyStaticObjects(false),
496+
CollocatedMetadataFunctions(false), CmdArgs(),
493497
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
494498
TypeInfoFilter(TypeInfoDumpFilter::All) {
495499
#ifndef NDEBUG

include/swift/Option/FrontendOptions.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,13 @@ def enable_move_inout_stack_protector :
11051105
Flag<["-"], "enable-move-inout-stack-protector">,
11061106
HelpText<"Enable the stack protector by moving values to temporaries">;
11071107

1108+
def enable_collocate_metadata_functions :
1109+
Flag<["-"], "enable-collocate-metadata-functions">,
1110+
HelpText<"Enable collocate metadata functions">;
1111+
def disable_collocate_metadata_functions :
1112+
Flag<["-"], "disable-collocate-metadata-functions">,
1113+
HelpText<"Disable collocate metadata functions">;
1114+
11081115
def enable_new_llvm_pass_manager :
11091116
Flag<["-"], "enable-new-llvm-pass-manager">,
11101117
HelpText<"Enable the new llvm pass manager">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,10 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
24722472
Args.hasFlag(OPT_disable_new_llvm_pass_manager,
24732473
OPT_enable_new_llvm_pass_manager,
24742474
Opts.LegacyPassManager);
2475-
2475+
Opts.CollocatedMetadataFunctions =
2476+
Args.hasFlag(OPT_enable_collocate_metadata_functions,
2477+
OPT_disable_collocate_metadata_functions,
2478+
Opts.CollocatedMetadataFunctions);
24762479
return false;
24772480
}
24782481

lib/IRGen/GenClass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,7 @@ static llvm::Function *emitObjCMetadataUpdateFunction(IRGenModule &IGM,
25112511
llvm::Function *f =
25122512
IGM.getAddrOfObjCMetadataUpdateFunction(D, ForDefinition);
25132513
f->setAttributes(IGM.constructInitialAttributes());
2514+
IGM.setColocateMetadataSection(f);
25142515

25152516
IRGenFunction IGF(IGM, f);
25162517
if (IGM.DebugInfo)

lib/IRGen/GenDecl.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,7 @@ void IRGenerator::emitEagerClassInitialization() {
19791979
IGM->DebugInfo->emitArtificialFunction(RegisterIGF, RegisterIGF.CurFn);
19801980
RegisterFn->setAttributes(IGM->constructInitialAttributes());
19811981
RegisterFn->setCallingConv(IGM->DefaultCC);
1982+
IGM->setColocateMetadataSection(RegisterFn);
19821983

19831984
for (ClassDecl *CD : ClassesForEagerInitialization) {
19841985
auto Ty = CD->getDeclaredType()->getCanonicalType();
@@ -2022,6 +2023,7 @@ void IRGenerator::emitObjCActorsNeedingSuperclassSwizzle() {
20222023
IGM->DebugInfo->emitArtificialFunction(RegisterIGF, RegisterIGF.CurFn);
20232024
RegisterFn->setAttributes(IGM->constructInitialAttributes());
20242025
RegisterFn->setCallingConv(IGM->DefaultCC);
2026+
IGM->setColocateMetadataSection(RegisterFn);
20252027

20262028
// Look up the SwiftNativeNSObject class.
20272029
auto swiftNativeNSObjectName =
@@ -5858,3 +5860,23 @@ IRGenModule::getOrCreateHelperFunction(StringRef fnName, llvm::Type *resultTy,
58585860

58595861
return fn;
58605862
}
5863+
5864+
void IRGenModule::setColocateMetadataSection(llvm::Function *f) {
5865+
if (!IRGen.Opts.CollocatedMetadataFunctions)
5866+
return;
5867+
5868+
switch (TargetInfo.OutputObjectFormat) {
5869+
case llvm::Triple::MachO:
5870+
f->setSection("__TEXT, __textg_swiftm, regular, pure_instructions");
5871+
break;
5872+
case llvm::Triple::DXContainer:
5873+
case llvm::Triple::GOFF:
5874+
case llvm::Triple::SPIRV:
5875+
case llvm::Triple::UnknownObjectFormat:
5876+
case llvm::Triple::Wasm:
5877+
case llvm::Triple::ELF:
5878+
case llvm::Triple::XCOFF:
5879+
case llvm::Triple::COFF:
5880+
break;
5881+
}
5882+
}

lib/IRGen/GenMeta.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ static void emitMetadataCompletionFunction(IRGenModule &IGM,
206206
f->setAttributes(IGM.constructInitialAttributes());
207207
f->setDoesNotThrow();
208208
IGM.setHasNoFramePointer(f);
209+
IGM.setColocateMetadataSection(f);
209210

210211
IRGenFunction IGF(IGM, f);
211212

@@ -2969,6 +2970,7 @@ namespace {
29692970
f->setAttributes(IGM.constructInitialAttributes());
29702971
f->setDoesNotThrow();
29712972
IGM.setHasNoFramePointer(f);
2973+
IGM.setColocateMetadataSection(f);
29722974

29732975
IRGenFunction IGF(IGM, f);
29742976

lib/IRGen/IRGenModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,8 @@ private: \
15721572
void appendLLVMUsedConditionalEntry(llvm::GlobalVariable *var,
15731573
const ProtocolConformance *conformance);
15741574

1575+
void setColocateMetadataSection(llvm::Function *f);
1576+
15751577
llvm::Constant *
15761578
getAddrOfTypeMetadata(CanType concreteType,
15771579
TypeMetadataCanonicality canonicality =

lib/IRGen/MetadataRequest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2052,6 +2052,7 @@ void irgen::emitCacheAccessFunction(IRGenModule &IGM, llvm::Function *accessor,
20522052
accessor->addFnAttr(llvm::Attribute::NoInline);
20532053
// Accessor functions don't need frame pointers.
20542054
IGM.setHasNoFramePointer(accessor);
2055+
IGM.setColocateMetadataSection(accessor);
20552056

20562057
// This function is logically 'readnone': the caller does not need
20572058
// to reason about any side effects or stores it might perform.
@@ -2404,6 +2405,7 @@ MetadataResponse irgen::emitGenericTypeMetadataAccessFunction(
24042405
generateThunkFn,
24052406
/*noinline*/ true));
24062407
}
2408+
IGM.setColocateMetadataSection(thunkFn);
24072409

24082410
// Call out to the helper.
24092411
auto arg0 = numArguments >= 1
@@ -2674,6 +2676,8 @@ irgen::getGenericTypeMetadataAccessFunction(IRGenModule &IGM,
26742676
llvm::Function *accessor =
26752677
IGM.getAddrOfGenericTypeMetadataAccessFunction(
26762678
nominal, genericArgs.Types, shouldDefine);
2679+
if (shouldDefine)
2680+
IGM.setColocateMetadataSection(accessor);
26772681

26782682
// If we're not supposed to define the accessor, or if we already
26792683
// have defined it, just return the pointer.
@@ -2917,6 +2921,7 @@ emitMetadataAccessByMangledName(IRGenFunction &IGF, CanType type,
29172921
IGM.setHasNoFramePointer(subIGF.CurFn);
29182922
if (IGM.DebugInfo)
29192923
IGM.DebugInfo->emitArtificialFunction(subIGF, subIGF.CurFn);
2924+
IGM.setColocateMetadataSection(subIGF.CurFn);
29202925

29212926
auto params = subIGF.collectParameters();
29222927
auto cache = params.claimNext();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -enable-collocate-metadata-functions | %FileCheck %s
2+
3+
// REQUIRES: OS=macosx
4+
5+
// CHECK: define{{.*}} swiftcc %swift.metadata_response @"$s28collocate_metadata_functions13GenericStructVMr"({{.*}} section "__TEXT, __textg_swiftm, regular, pure_instructions"
6+
7+
public struct GenericStruct<T> {
8+
var field: T?
9+
}

test/IRGen/generic_classes.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,5 +415,5 @@ entry(%c : $RootGeneric<Int32>):
415415
// CHECK: ret %swift.metadata_response [[T1]]
416416
// CHECK: }
417417

418-
// OSIZE: define hidden swiftcc %swift.metadata_response @"$s15generic_classes11RootGenericCMa"(i64 %0, %swift.type* {{.*}}) [[ATTRS:#[0-9]+]] {
418+
// OSIZE: define hidden swiftcc %swift.metadata_response @"$s15generic_classes11RootGenericCMa"(i64 %0, %swift.type* {{.*}}) [[ATTRS:#[0-9]+]] {{(section)?.*}}{
419419
// OSIZE: [[ATTRS]] = {{{.*}}noinline

0 commit comments

Comments
 (0)