Skip to content

Commit 443e244

Browse files
committed
[sil-opaque-values] NFC, Allow SIL conventions to be set within SILBuilder.
This keeps the `create*` API simple for *normal* users and forces AddressLowering to declare conventions when creating a builder.
1 parent 3da3bcb commit 443e244

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class SILBuilder {
3636
SILFunction *F;
3737
SILModule &Mod;
3838

39+
/// Allow the SIL module conventions to be overriden within the builder.
40+
/// This supports passes that lower SIL to a new stage.
41+
SILModuleConventions silConv = SILModuleConventions(Mod);
42+
3943
/// If this is non-null, the instruction is inserted in the specified
4044
/// basic block, at the specified InsertPt. If null, created instructions
4145
/// are not auto-inserted.
@@ -103,6 +107,13 @@ class SILBuilder {
103107
setInsertionPoint(BB, InsertPt);
104108
}
105109

110+
// Allow a pass to override the current SIL module conventions. This should
111+
// only be done by a pass responsible for lowering SIL to a new stage
112+
// (e.g. AddressLowering).
113+
void setSILConventions(SILModuleConventions silConv) {
114+
this->silConv = silConv;
115+
}
116+
106117
SILFunction &getFunction() const {
107118
assert(F && "cannot create this instruction without a function context");
108119
return *F;
@@ -333,10 +344,9 @@ class SILBuilder {
333344
ApplyInst *createApply(
334345
SILLocation Loc, SILValue Fn, SubstitutionList Subs,
335346
ArrayRef<SILValue> Args, bool isNonThrowing,
336-
const GenericSpecializationInformation *SpecializationInfo = nullptr,
337-
Optional<SILModuleConventions> ModuleConventions = None) {
347+
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
338348
return insert(ApplyInst::create(getSILDebugLocation(Loc), Fn, Subs, Args,
339-
isNonThrowing, ModuleConventions, *F,
349+
isNonThrowing, silConv, *F,
340350
OpenedArchetypes, SpecializationInfo));
341351
}
342352

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ void OpaqueStorageAllocation::allocateOpaqueStorage() {
411411
void OpaqueStorageAllocation::convertIndirectFunctionArgs() {
412412
// Insert temporary argument loads at the top of the function.
413413
SILBuilder argBuilder(pass.F->getEntryBlock()->begin());
414+
argBuilder.setSILConventions(
415+
SILModuleConventions::getLoweredAddressConventions());
414416

415417
auto fnConv = pass.F->getConventions();
416418
unsigned argIdx = fnConv.getSILArgIndexOfFirstParam();
@@ -572,6 +574,8 @@ void OpaqueStorageAllocation::allocateForValue(SILValue value,
572574
}
573575

574576
SILBuilder allocBuilder(pass.F->begin()->begin());
577+
allocBuilder.setSILConventions(
578+
SILModuleConventions::getLoweredAddressConventions());
575579
AllocStackInst *allocInstr =
576580
allocBuilder.createAllocStack(getLocForValue(value), value->getType());
577581

@@ -580,6 +584,8 @@ void OpaqueStorageAllocation::allocateForValue(SILValue value,
580584
// Insert stack deallocations.
581585
for (TermInst *termInst : pass.returnInsts) {
582586
SILBuilder deallocBuilder(termInst);
587+
deallocBuilder.setSILConventions(
588+
SILModuleConventions::getLoweredAddressConventions());
583589
deallocBuilder.createDeallocStack(allocInstr->getLoc(), allocInstr);
584590
}
585591
}
@@ -713,7 +719,10 @@ class ApplyRewriter {
713719

714720
public:
715721
ApplyRewriter(SILInstruction *origCall, AddressLoweringState &pass)
716-
: pass(pass), apply(origCall), argBuilder(origCall) {}
722+
: pass(pass), apply(origCall), argBuilder(origCall) {
723+
argBuilder.setSILConventions(
724+
SILModuleConventions::getLoweredAddressConventions());
725+
}
717726

718727
void rewriteParameters();
719728
void rewriteIndirectParameter(Operand *operand);
@@ -755,6 +764,8 @@ static void insertStackDeallocationAtCall(AllocStackInst *allocInst,
755764
switch (applyInst->getKind()) {
756765
case ValueKind::ApplyInst: {
757766
SILBuilder deallocBuilder(&*std::next(lastUse->getIterator()));
767+
deallocBuilder.setSILConventions(
768+
SILModuleConventions::getLoweredAddressConventions());
758769
deallocBuilder.createDeallocStack(allocInst->getLoc(), allocInst);
759770
break;
760771
}
@@ -824,11 +835,14 @@ void ApplyRewriter::canonicalizeResults(
824835
SILInstruction *result = directResultValues[resultIdx];
825836
if (!result) {
826837
SILBuilder resultBuilder(std::next(SILBasicBlock::iterator(applyInst)));
838+
resultBuilder.setSILConventions(
839+
SILModuleConventions::getLoweredAddressConventions());
827840
result = resultBuilder.createTupleExtract(applyInst->getLoc(),
828841
applyInst, resultIdx);
829842
directResultValues[resultIdx] = result;
830843
}
831844
SILBuilder B(destroyInst);
845+
B.setSILConventions(SILModuleConventions::getLoweredAddressConventions());
832846
auto &TL = pass.F->getModule().getTypeLowering(result->getType());
833847
TL.emitDestroyValue(B, destroyInst->getLoc(), result);
834848
}
@@ -860,6 +874,8 @@ SILValue ApplyRewriter::materializeIndirectResultAddress(
860874
// TODO: Find the try_apply's result block.
861875
// Build results outside-in to next stack allocations.
862876
SILBuilder resultBuilder(std::next(SILBasicBlock::iterator(origCallInst)));
877+
resultBuilder.setSILConventions(
878+
SILModuleConventions::getLoweredAddressConventions());
863879
// This is a formally indirect argument, but is loadable.
864880
loadInst = resultBuilder.createLoad(loc, allocInst,
865881
LoadOwnershipQualifier::Unqualified);
@@ -907,6 +923,8 @@ void ApplyRewriter::convertApplyWithIndirectResults() {
907923
// Prepare to emit a new call instruction.
908924
SILLocation loc = origCallInst->getLoc();
909925
SILBuilder callBuilder(origCallInst);
926+
callBuilder.setSILConventions(
927+
SILModuleConventions::getLoweredAddressConventions());
910928

911929
// The new call instruction's SIL calling convention.
912930
SILFunctionConventions loweredCalleeConv(
@@ -966,9 +984,7 @@ void ApplyRewriter::convertApplyWithIndirectResults() {
966984
case ValueKind::ApplyInst:
967985
newCallInst = callBuilder.createApply(
968986
loc, apply.getCallee(), apply.getSubstitutions(), newCallArgs,
969-
cast<ApplyInst>(origCallInst)->isNonThrowing(),
970-
nullptr,
971-
SILModuleConventions::getLoweredAddressConventions());
987+
cast<ApplyInst>(origCallInst)->isNonThrowing(), nullptr);
972988
break;
973989
case ValueKind::TryApplyInst:
974990
// TODO: insert dealloc in the catch block.
@@ -984,6 +1000,9 @@ void ApplyRewriter::convertApplyWithIndirectResults() {
9841000
// TODO: handle bbargs from try_apply.
9851001
SILBuilder resultBuilder(
9861002
std::next(SILBasicBlock::iterator(origCallInst)));
1003+
resultBuilder.setSILConventions(
1004+
SILModuleConventions::getLoweredAddressConventions());
1005+
9871006
SmallVector<Operand*, 8> origUses(origCallInst->getUses());
9881007
for (Operand *operand : origUses) {
9891008
auto *extractInst = dyn_cast<TupleExtractInst>(operand->getUser());
@@ -1057,6 +1076,7 @@ void ReturnRewriter::rewriteReturn(ReturnInst *returnInst) {
10571076
break;
10581077
}
10591078
SILBuilder B(insertPt);
1079+
B.setSILConventions(SILModuleConventions::getLoweredAddressConventions());
10601080

10611081
// Gather direct function results.
10621082
unsigned numOrigDirectResults =
@@ -1152,7 +1172,9 @@ class AddressOnlyUseRewriter
11521172

11531173
public:
11541174
explicit AddressOnlyUseRewriter(AddressLoweringState &pass)
1155-
: pass(pass), B(*pass.F), addrMat(pass, B) {}
1175+
: pass(pass), B(*pass.F), addrMat(pass, B) {
1176+
B.setSILConventions(SILModuleConventions::getLoweredAddressConventions());
1177+
}
11561178

11571179
void visitOperand(Operand *operand) {
11581180
currOper = operand;
@@ -1288,7 +1310,9 @@ class AddressOnlyDefRewriter
12881310

12891311
public:
12901312
explicit AddressOnlyDefRewriter(AddressLoweringState &pass)
1291-
: pass(pass), B(*pass.F), addrMat(pass, B) {}
1313+
: pass(pass), B(*pass.F), addrMat(pass, B) {
1314+
B.setSILConventions(SILModuleConventions::getLoweredAddressConventions());
1315+
}
12921316

12931317
void visitInst(SILInstruction *inst) { visit(inst); }
12941318

0 commit comments

Comments
 (0)