Skip to content

Commit fe59328

Browse files
author
Joe Shajrawi
authored
Merge pull request #22362 from shajrawi/sr9849
[LoadableByAddress] handle functions that return a closure in a tuple
2 parents 824ab89 + 9e560ce commit fe59328

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

lib/IRGen/LoadableByAddress.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,8 @@ class LoadableByAddress : public SILModuleTransform {
16071607
SmallVectorImpl<SILInstruction *> &Delete);
16081608
bool recreateApply(SILInstruction &I,
16091609
SmallVectorImpl<SILInstruction *> &Delete);
1610+
bool recreateTupleInstr(SILInstruction &I,
1611+
SmallVectorImpl<SILInstruction *> &Delete);
16101612
bool recreateConvInstr(SILInstruction &I,
16111613
SmallVectorImpl<SILInstruction *> &Delete);
16121614
bool recreateBuiltinInstr(SILInstruction &I,
@@ -2617,6 +2619,34 @@ bool LoadableByAddress::fixStoreToBlockStorageInstr(
26172619
return true;
26182620
}
26192621

2622+
bool LoadableByAddress::recreateTupleInstr(
2623+
SILInstruction &I, SmallVectorImpl<SILInstruction *> &Delete) {
2624+
auto *tupleInstr = dyn_cast<TupleInst>(&I);
2625+
if (!tupleInstr)
2626+
return false;
2627+
2628+
// Check if we need to recreate the tuple:
2629+
auto *F = tupleInstr->getFunction();
2630+
auto *currIRMod = getIRGenModule()->IRGen.getGenModule(F);
2631+
GenericEnvironment *genEnv = F->getGenericEnvironment();
2632+
auto resultTy = tupleInstr->getType();
2633+
auto newResultTy = MapperCache.getNewSILType(genEnv, resultTy, *currIRMod);
2634+
if (resultTy == newResultTy)
2635+
return true;
2636+
2637+
// The tuple type have changed based on its members.
2638+
// For example if one or more of them are ‘large’ loadable types
2639+
SILBuilderWithScope tupleBuilder(tupleInstr);
2640+
SmallVector<SILValue, 8> elems;
2641+
for (auto elem : tupleInstr->getElements()) {
2642+
elems.push_back(elem);
2643+
}
2644+
auto *newTuple = tupleBuilder.createTuple(tupleInstr->getLoc(), elems);
2645+
tupleInstr->replaceAllUsesWith(newTuple);
2646+
Delete.push_back(tupleInstr);
2647+
return true;
2648+
}
2649+
26202650
bool LoadableByAddress::recreateConvInstr(SILInstruction &I,
26212651
SmallVectorImpl<SILInstruction *> &Delete) {
26222652
auto *convInstr = dyn_cast<SingleValueInstruction>(&I);
@@ -2855,7 +2885,9 @@ void LoadableByAddress::run() {
28552885
SmallVector<SILInstruction *, 32> Delete;
28562886
for (SILBasicBlock &BB : CurrF) {
28572887
for (SILInstruction &I : BB) {
2858-
if (recreateConvInstr(I, Delete))
2888+
if (recreateTupleInstr(I, Delete))
2889+
continue;
2890+
else if (recreateConvInstr(I, Delete))
28592891
continue;
28602892
else if (recreateBuiltinInstr(I, Delete))
28612893
continue;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-swift-frontend -Xllvm -sil-verify-after-pass=loadable-address -emit-ir -o %t.ll %s
2+
3+
// Just make sure we don't crash.
4+
5+
6+
struct Large {
7+
var a: Int = 1
8+
var b: Int = 1
9+
var c: Int = 1
10+
var d: Int = 1
11+
var e: Int = 1
12+
var f: Int = 1
13+
var g: Int = 1
14+
var h: Int = 1
15+
}
16+
17+
func test(_ x: Large) -> (Large, (Large) -> Large) {
18+
return (x, {$0})
19+
}

0 commit comments

Comments
 (0)