File tree Expand file tree Collapse file tree 6 files changed +46
-1
lines changed
Inputs/multithread_module Expand file tree Collapse file tree 6 files changed +46
-1
lines changed Original file line number Diff line number Diff line change @@ -863,6 +863,10 @@ class SILFunction
863
863
validateSubclassScope (getClassSubclassScope (), isThunk (), Info);
864
864
SpecializationInfo = Info;
865
865
}
866
+
867
+ // / If this function is a specialization, return the original function from
868
+ // / which this function was specialized.
869
+ const SILFunction *getOriginOfSpecialization () const ;
866
870
867
871
// / Retrieve the generic environment containing the mapping from interface
868
872
// / types to context archetypes for this function. Only present if the
Original file line number Diff line number Diff line change @@ -1209,6 +1209,16 @@ void IRGenerator::addLazyFunction(SILFunction *f) {
1209
1209
assert (!FinishedEmittingLazyDefinitions);
1210
1210
LazyFunctionDefinitions.push_back (f);
1211
1211
1212
+ if (const SILFunction *orig = f->getOriginOfSpecialization ()) {
1213
+ // f is a specialization. Try to emit all specializations of the same
1214
+ // original function into the same IGM. This increases the chances that
1215
+ // specializations are merged by LLVM's function merging.
1216
+ auto iter =
1217
+ IGMForSpecializations.insert (std::make_pair (orig, CurrentIGM)).first ;
1218
+ DefaultIGMForFunction.insert (std::make_pair (f, iter->second ));
1219
+ return ;
1220
+ }
1221
+
1212
1222
if (auto *dc = f->getDeclContext ())
1213
1223
if (dc->getParentSourceFile ())
1214
1224
return ;
Original file line number Diff line number Diff line change @@ -204,7 +204,13 @@ class IRGenerator {
204
204
// Stores the IGM from which a function is referenced the first time.
205
205
// It is used if a function has no source-file association.
206
206
llvm::DenseMap<SILFunction *, IRGenModule *> DefaultIGMForFunction;
207
-
207
+
208
+ // The IGMs where sepecializations of functions are emitted. The key is the
209
+ // non-specialized function.
210
+ // Storing all specializations of a function in the same IGM increases the
211
+ // chances of function merging.
212
+ llvm::DenseMap<const SILFunction *, IRGenModule *> IGMForSpecializations;
213
+
208
214
// The IGM of the first source file.
209
215
IRGenModule *PrimaryIGM = nullptr ;
210
216
Original file line number Diff line number Diff line change @@ -167,6 +167,17 @@ bool SILFunction::hasForeignBody() const {
167
167
return SILDeclRef::isClangGenerated (getClangNode ());
168
168
}
169
169
170
+ const SILFunction *SILFunction::getOriginOfSpecialization () const {
171
+ if (!isSpecialization ())
172
+ return nullptr ;
173
+
174
+ const SILFunction *p = getSpecializationInfo ()->getParent ();
175
+ while (p->isSpecialization ()) {
176
+ p = p->getSpecializationInfo ()->getParent ();
177
+ }
178
+ return p;
179
+ }
180
+
170
181
void SILFunction::numberValues (llvm::DenseMap<const SILNode*, unsigned > &
171
182
ValueToNumberMap) const {
172
183
unsigned idx = 0 ;
Original file line number Diff line number Diff line change @@ -23,6 +23,10 @@ public struct MyStruct : MyProto {
23
23
return x + 3
24
24
}
25
25
26
+ public func mutateMyStructArray( _ arr: inout [ MyStruct ] , _ x: MyStruct ) {
27
+ arr. append ( x)
28
+ }
29
+
26
30
public var g1 = 234
27
31
28
32
let i = testit ( 27 )
Original file line number Diff line number Diff line change @@ -58,8 +58,18 @@ func callproto(_ p: MyProto) {
58
58
print ( p. protofunc ( ) )
59
59
}
60
60
61
+ public func mutateBaseArray( _ arr: inout [ Base ] , _ x: Base ) {
62
+ arr. append ( x)
63
+ }
64
+
65
+
61
66
// Check the llvm IR files:
62
67
68
+ // Check if all specializations from stdlib functions are created in the same LLVM module.
69
+
70
+ // CHECK-MAINLL-DAG: define {{.*}} @"$sSa16_createNewBuffer14bufferIsUnique15minimumCapacity13growForAppendySb_SiSbtF4test8MyStructV_Tg5"
71
+ // CHECK-MAINLL-DAG: define {{.*}} @"$sSa16_createNewBuffer14bufferIsUnique15minimumCapacity13growForAppendySb_SiSbtF4test4BaseC_Tg5"
72
+
63
73
// Check if the DI filename is correct and not "<unknown>".
64
74
65
75
// CHECK-MAINLL: [[F:![0-9]+]] = !DIFile(filename: "{{.*}}IRGen/Inputs/multithread_module/main.swift", directory: "{{.*}}")
You can’t perform that action at this time.
0 commit comments