Skip to content

Commit 43510bc

Browse files
Merge pull request #63897 from aschwaighofer/colocate_type_descriptors
IRGen: Put type descriptors in their own section to collocated them
2 parents 96d988a + 8a7b8dc commit 43510bc

File tree

10 files changed

+47
-11
lines changed

10 files changed

+47
-11
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ class IRGenOptions {
428428
/// Collocate metadata functions in their own section.
429429
unsigned CollocatedMetadataFunctions : 1;
430430

431+
/// Colocate type descriptors in their own section.
432+
unsigned ColocateTypeDescriptors : 1;
433+
431434
/// Use relative (and constant) protocol witness tables.
432435
unsigned UseRelativeProtocolWitnessTables : 1;
433436

@@ -503,6 +506,7 @@ class IRGenOptions {
503506
EmitGenericRODatas(false), NoPreallocatedInstantiationCaches(false),
504507
DisableReadonlyStaticObjects(false),
505508
CollocatedMetadataFunctions(false),
509+
ColocateTypeDescriptors(true),
506510
UseRelativeProtocolWitnessTables(false), CmdArgs(),
507511
SanitizeCoverage(llvm::SanitizerCoverageOptions()),
508512
TypeInfoFilter(TypeInfoDumpFilter::All) {

include/swift/Option/FrontendOptions.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,13 @@ def disable_collocate_metadata_functions :
11321132
Flag<["-"], "disable-collocate-metadata-functions">,
11331133
HelpText<"Disable collocate metadata functions">;
11341134

1135+
def enable_colocate_type_descriptors :
1136+
Flag<["-"], "enable-colocate-type-descriptors">,
1137+
HelpText<"Enable colocate type descriptors">;
1138+
def disable_colocate_type_descriptors :
1139+
Flag<["-"], "disable-colocate-type-descriptors">,
1140+
HelpText<"Disable colocate type descriptors">;
1141+
11351142
def enable_relative_protocol_witness_tables :
11361143
Flag<["-"], "enable-relative-protocol-witness-tables">,
11371144
HelpText<"Enable relative protocol witness tables">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,9 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
25342534
Args.hasFlag(OPT_disable_new_llvm_pass_manager,
25352535
OPT_enable_new_llvm_pass_manager,
25362536
Opts.LegacyPassManager);
2537+
Opts.ColocateTypeDescriptors = Args.hasFlag(OPT_enable_colocate_type_descriptors,
2538+
OPT_disable_colocate_type_descriptors,
2539+
Opts.ColocateTypeDescriptors);
25372540
Opts.CollocatedMetadataFunctions =
25382541
Args.hasFlag(OPT_enable_collocate_metadata_functions,
25392542
OPT_disable_collocate_metadata_functions,

lib/IRGen/GenDecl.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5975,6 +5975,27 @@ IRGenModule::getOrCreateHelperFunction(StringRef fnName, llvm::Type *resultTy,
59755975
return fn;
59765976
}
59775977

5978+
void IRGenModule::setColocateTypeDescriptorSection(llvm::GlobalVariable *v) {
5979+
switch (TargetInfo.OutputObjectFormat) {
5980+
case llvm::Triple::MachO:
5981+
if (IRGen.Opts.ColocateTypeDescriptors)
5982+
v->setSection("__TEXT,__textg_swiftt,regular");
5983+
else
5984+
setTrueConstGlobal(v);
5985+
break;
5986+
case llvm::Triple::DXContainer:
5987+
case llvm::Triple::GOFF:
5988+
case llvm::Triple::SPIRV:
5989+
case llvm::Triple::UnknownObjectFormat:
5990+
case llvm::Triple::Wasm:
5991+
case llvm::Triple::ELF:
5992+
case llvm::Triple::XCOFF:
5993+
case llvm::Triple::COFF:
5994+
setTrueConstGlobal(v);
5995+
break;
5996+
}
5997+
5998+
}
59785999
void IRGenModule::setColocateMetadataSection(llvm::Function *f) {
59796000
if (!IRGen.Opts.CollocatedMetadataFunctions)
59806001
return;

lib/IRGen/GenMeta.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ namespace {
627627
auto var = cast<llvm::GlobalVariable>(addr);
628628

629629
var->setConstant(true);
630-
IGM.setTrueConstGlobal(var);
630+
IGM.setColocateTypeDescriptorSection(var);
631631
}
632632
};
633633

@@ -685,7 +685,7 @@ namespace {
685685
auto var = cast<llvm::GlobalVariable>(addr);
686686

687687
var->setConstant(true);
688-
IGM.setTrueConstGlobal(var);
688+
IGM.setColocateTypeDescriptorSection(var);
689689
}
690690
};
691691

@@ -763,7 +763,7 @@ namespace {
763763
auto var = cast<llvm::GlobalVariable>(addr);
764764

765765
var->setConstant(true);
766-
IGM.setTrueConstGlobal(var);
766+
IGM.setColocateTypeDescriptorSection(var);
767767
}
768768
};
769769

@@ -831,7 +831,7 @@ namespace {
831831
auto var = cast<llvm::GlobalVariable>(addr);
832832

833833
var->setConstant(true);
834-
IGM.setTrueConstGlobal(var);
834+
IGM.setColocateTypeDescriptorSection(var);
835835
}
836836

837837
void addName() {
@@ -1361,7 +1361,7 @@ namespace {
13611361
}
13621362

13631363
var->setConstant(true);
1364-
IGM.setTrueConstGlobal(var);
1364+
IGM.setColocateTypeDescriptorSection(var);
13651365
return var;
13661366
}
13671367

@@ -2172,7 +2172,7 @@ namespace {
21722172
auto var = cast<llvm::GlobalVariable>(addr);
21732173

21742174
var->setConstant(true);
2175-
IGM.setTrueConstGlobal(var);
2175+
IGM.setColocateTypeDescriptorSection(var);
21762176
IGM.emitOpaqueTypeDescriptorAccessor(O);
21772177
}
21782178

lib/IRGen/IRGenModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,7 @@ private: \
15881588
const ProtocolConformance *conformance);
15891589

15901590
void setColocateMetadataSection(llvm::Function *f);
1591+
void setColocateTypeDescriptorSection(llvm::GlobalVariable *v);
15911592

15921593
llvm::Constant *
15931594
getAddrOfTypeMetadata(CanType concreteType,

test/IRGen/class_update_callback_with_stub.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import resilient_objc_class
5454
// CHECK-SAME: @"$s31class_update_callback_with_stub17ResilientSubclassCACycfC"
5555
// -- class stub
5656
// CHECK-SAME: @"$s31class_update_callback_with_stub17ResilientSubclassCMt"
57-
// CHECK-SAME: }>, section "__TEXT,__const", align 4
57+
// CHECK-SAME: }>, section "__TEXT,__textg_swiftt,regular", align 4
5858

5959

6060
// -- Symbols for full stubs; the address point is one word in, and defined below

test/IRGen/foreign_type_metadata.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Foundation
88
// metadata is not required by this TU. Another TU could require it and the
99
// linker could choose the less defined one of the two.
1010

11-
// CHECK: @"$sSo8_NSRangeVMn" = linkonce_odr hidden constant <{ {{.*}}sSo8_NSRangeVMa{{.*}} }>, section "__TEXT,__const"
11+
// CHECK: @"$sSo8_NSRangeVMn" = linkonce_odr hidden constant <{ {{.*}}sSo8_NSRangeVMa{{.*}} }>, section "__TEXT,__textg_swiftt,regular"
1212

1313
func use(_ closure: @escaping (Int) -> ()) {}
1414

test/IRGen/nominal-type-section.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %swift -target x86_64-unknown-linux-gnu -emit-ir -disable-legacy-type-info -parse-stdlib -primary-file %s -module-name main -o - | %FileCheck %s -check-prefix CHECK-ELF
33
// RUN: %swift -target x86_64-unknown-windows-itanium -emit-ir -disable-legacy-type-info -parse-stdlib -primary-file %s -module-name main -o - | %FileCheck %s -check-prefix CHECK-COFF
44

5-
// CHECK-MACHO: @"$s4main1sVMn" = constant {{.*}}, section "__TEXT,__const"
5+
// CHECK-MACHO: @"$s4main1sVMn" = constant {{.*}}, section "__TEXT,__textg_swiftt,regular"
66
// CHECK-ELF: @"$s4main1sVMn" = {{.*}}constant {{.*}}, section ".rodata"
77
// CHECK-COFF: @"$s4main1sVMn" = {{.*}}constant {{.*}}, section ".rdata"
88

test/IRGen/objc_implementation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@
9797
// CHECK: [[_DATA_SwiftSubclass:@[^, ]+]] = internal constant { i32, i32, i32, i32, i8*, i8*, { i32, i32, [2 x { i8*, i8*, i8* }] }*, i8*, i8*, i8*, i8* } { i32 128, i32 24, i32 24, i32 0, i8* null, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.40._TtC19objc_implementation13SwiftSubclass, i64 0, i64 0), { i32, i32, [2 x { i8*, i8*, i8* }] }* [[_INSTANCE_METHODS_SwiftSubclass]], i8* null, i8* null, i8* null, i8* null }, section "__DATA, __objc_data", align 8
9898

9999
// Swift metadata
100-
// CHECK: @"$s19objc_implementationMXM" = linkonce_odr hidden constant <{ i32, i32, i32 }> <{ i32 0, i32 0, i32 trunc (i64 sub (i64 ptrtoint ([20 x i8]* @.str.19.objc_implementation to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32 }>, <{ i32, i32, i32 }>* @"$s19objc_implementationMXM", i32 0, i32 2) to i64)) to i32) }>, section "__TEXT,__const", align 4
100+
// CHECK: @"$s19objc_implementationMXM" = linkonce_odr hidden constant <{ i32, i32, i32 }> <{ i32 0, i32 0, i32 trunc (i64 sub (i64 ptrtoint ([20 x i8]* @.str.19.objc_implementation to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32 }>, <{ i32, i32, i32 }>* @"$s19objc_implementationMXM", i32 0, i32 2) to i64)) to i32) }>, section "__TEXT,__textg_swiftt,regular", align 4
101101
// CHECK: @"symbolic So9ImplClassC" = linkonce_odr hidden constant <{ [13 x i8], i8 }> <{ [13 x i8] c"So9ImplClassC", i8 0 }>, section "__TEXT,__swift5_typeref, regular", align 2
102-
// CHECK: @"$s19objc_implementation13SwiftSubclassCMn" = constant <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }> <{ i32 80, i32 trunc (i64 sub (i64 ptrtoint (<{ i32, i32, i32 }>* @"$s19objc_implementationMXM" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 1) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([14 x i8]* @.str.13.SwiftSubclass to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 2) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (%swift.metadata_response (i64)* @"$s19objc_implementation13SwiftSubclassCMa" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 3) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ({ i32, i32, i16, i16, i32 }* @"$s19objc_implementation13SwiftSubclassCMF" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 4) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (<{ [13 x i8], i8 }>* @"symbolic So9ImplClassC" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 5) to i64)) to i32), i32 3, i32 10, i32 0, i32 0, i32 10 }>, section "__TEXT,__const", align 4
102+
// CHECK: @"$s19objc_implementation13SwiftSubclassCMn" = constant <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }> <{ i32 80, i32 trunc (i64 sub (i64 ptrtoint (<{ i32, i32, i32 }>* @"$s19objc_implementationMXM" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 1) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([14 x i8]* @.str.13.SwiftSubclass to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 2) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (%swift.metadata_response (i64)* @"$s19objc_implementation13SwiftSubclassCMa" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 3) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ({ i32, i32, i16, i16, i32 }* @"$s19objc_implementation13SwiftSubclassCMF" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 4) to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (<{ [13 x i8], i8 }>* @"symbolic So9ImplClassC" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn", i32 0, i32 5) to i64)) to i32), i32 3, i32 10, i32 0, i32 0, i32 10 }>, section "__TEXT,__textg_swiftt,regular", align 4
103103
// CHECK: @"$s19objc_implementation13SwiftSubclassCMf" = internal global <{ i8*, void (%T19objc_implementation13SwiftSubclassC*)*, i8**, i64, %objc_class*, %swift.opaque*, %swift.opaque*, {{i64|%swift.opaque\*}}, i32, i32, i32, i16, i16, i32, i32, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>*, i8* }> <{ i8* null, {{.*}}* @"$s19objc_implementation13SwiftSubclassCfD{{(\.ptrauth)?}}"{{( to void \(.*\)\*\))?}}, i8** @"$sBOWV", i64 ptrtoint (%objc_class* @"OBJC_METACLASS_$__TtC19objc_implementation13SwiftSubclass" to i64), %objc_class* bitcast (<{ i64, %objc_class*, %swift.opaque*, %swift.opaque*, { i32, i32, i32, i32, i8*, i8*, { i32, i32, [5 x { i8*, i8*, i8* }] }*, i8*, { i32, i32, [2 x { i64*, i8*, i8*, i32, i32 }] }*, i8*, { i32, i32, [1 x { i8*, i8* }] }* }* }>* @"OBJC_CLASS_$_ImplClass" to %objc_class*), %swift.opaque* @_objc_empty_cache, %swift.opaque* null, {{i64 add \(i64 ptrtoint|%swift.opaque\* bitcast \(i8\* getelementptr \(i8, i8\* bitcast}} ({ i32, i32, i32, i32, i8*, i8*, { i32, i32, [2 x { i8*, i8*, i8* }] }*, i8*, i8*, i8*, i8* }* @_DATA__TtC19objc_implementation13SwiftSubclass to {{i64\), i64 1|i8\*\), i64 1\) to %swift.opaque\*}}), i32 0, i32 0, i32 24, i16 7, i16 0, i32 104, i32 24, <{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* {{(bitcast \(\{ i8\*, i32, i64, i64 \}\* )?}}@"$s19objc_implementation13SwiftSubclassCMn{{(\.ptrauth)?}}"{{( to <\{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 \}>\*\))?}}, i8* null }>, section "__DATA,__objc_data, regular", align 8
104104
// CHECK: @"symbolic _____ 19objc_implementation13SwiftSubclassC" = linkonce_odr hidden constant <{ i8, i32, i8 }> <{ i8 1, i32 trunc (i64 sub (i64 ptrtoint (<{ i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32 }>* @"$s19objc_implementation13SwiftSubclassCMn" to i64), i64 ptrtoint (i32* getelementptr inbounds (<{ i8, i32, i8 }>, <{ i8, i32, i8 }>* @"symbolic _____ 19objc_implementation13SwiftSubclassC", i32 0, i32 1) to i64)) to i32), i8 0 }>, section "__TEXT,__swift5_typeref, regular", align 2
105105
// CHECK: @"$s19objc_implementation13SwiftSubclassCMF" = internal constant { i32, i32, i16, i16, i32 } { i32 trunc (i64 sub (i64 ptrtoint (<{ i8, i32, i8 }>* @"symbolic _____ 19objc_implementation13SwiftSubclassC" to i64), i64 ptrtoint ({ i32, i32, i16, i16, i32 }* @"$s19objc_implementation13SwiftSubclassCMF" to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint (<{ [13 x i8], i8 }>* @"symbolic So9ImplClassC" to i64), i64 ptrtoint (i32* getelementptr inbounds ({ i32, i32, i16, i16, i32 }, { i32, i32, i16, i16, i32 }* @"$s19objc_implementation13SwiftSubclassCMF", i32 0, i32 1) to i64)) to i32), i16 7, i16 12, i32 0 }, section "__TEXT,__swift5_fieldmd, regular", align 4

0 commit comments

Comments
 (0)