Skip to content

SIL cast comment corrections. #11849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class SILBuilder {
SILFunction *F;
SILModule &Mod;

/// Allow the SIL module conventions to be overriden within the builder.
/// This supports passes that lower SIL to a new stage.
SILModuleConventions silConv = SILModuleConventions(Mod);

/// If this is non-null, the instruction is inserted in the specified
/// basic block, at the specified InsertPt. If null, created instructions
/// are not auto-inserted.
Expand Down Expand Up @@ -103,6 +107,13 @@ class SILBuilder {
setInsertionPoint(BB, InsertPt);
}

// Allow a pass to override the current SIL module conventions. This should
// only be done by a pass responsible for lowering SIL to a new stage
// (e.g. AddressLowering).
void setSILConventions(SILModuleConventions silConv) {
this->silConv = silConv;
}

SILFunction &getFunction() const {
assert(F && "cannot create this instruction without a function context");
return *F;
Expand Down Expand Up @@ -333,10 +344,9 @@ class SILBuilder {
ApplyInst *createApply(
SILLocation Loc, SILValue Fn, SubstitutionList Subs,
ArrayRef<SILValue> Args, bool isNonThrowing,
const GenericSpecializationInformation *SpecializationInfo = nullptr,
Optional<SILModuleConventions> ModuleConventions = None) {
const GenericSpecializationInformation *SpecializationInfo = nullptr) {
return insert(ApplyInst::create(getSILDebugLocation(Loc), Fn, Subs, Args,
isNonThrowing, ModuleConventions, *F,
isNonThrowing, silConv, *F,
OpenedArchetypes, SpecializationInfo));
}

Expand Down
17 changes: 13 additions & 4 deletions include/swift/SIL/SILNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,20 @@ ABSTRACT_VALUE(SILInstruction, ValueBase)
INST(ObjCMetatypeToObjectInst, ConversionInst, objc_metatype_to_object, None, DoesNotRelease)
INST(ObjCExistentialMetatypeToObjectInst, ConversionInst, objc_existential_metatype_to_object, None,
DoesNotRelease)
// We have to use a MayRead here to block moving the ultimate release
// accross the cast (which reads the type metadata). With Semantic SIL we
// should be able to move this back to None - it should model the scoping
// and forbid the compiler from moving the ultimate retain.
// unconditional_checked_cast_value reads the source value and produces
// a new value with a potentially different representation.
INST(UnconditionalCheckedCastValueInst, ConversionInst, unconditional_checked_cast_value, MayRead, MayRelease)
// unconditional_checked_cast_inst is only MayRead to prevent a subsequent
// release of the cast's source from being hoisted above the cast:
// retain X
// Y = unconditional_checked_cast_inst X
// _ = Y
// release X // This release cannot be reordered with the cast.
//
// With Semantic SIL, this pattern of unbalanced retain/release
// should never happen. Since unconditional_checked_cast is a
// scalar cast that doesn't affect the value's representation, its
// side effect can then be modeled as None.
INST(UnconditionalCheckedCastInst, ConversionInst, unconditional_checked_cast, MayRead, DoesNotRelease)
VALUE_RANGE(ConversionInst, UpcastInst, UnconditionalCheckedCastInst)
INST(IsNonnullInst, SILInstruction, is_nonnull, None, DoesNotRelease)
Expand Down
36 changes: 30 additions & 6 deletions lib/SILOptimizer/Mandatory/AddressLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ void OpaqueStorageAllocation::allocateOpaqueStorage() {
void OpaqueStorageAllocation::convertIndirectFunctionArgs() {
// Insert temporary argument loads at the top of the function.
SILBuilder argBuilder(pass.F->getEntryBlock()->begin());
argBuilder.setSILConventions(
SILModuleConventions::getLoweredAddressConventions());

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

SILBuilder allocBuilder(pass.F->begin()->begin());
allocBuilder.setSILConventions(
SILModuleConventions::getLoweredAddressConventions());
AllocStackInst *allocInstr =
allocBuilder.createAllocStack(getLocForValue(value), value->getType());

Expand All @@ -580,6 +584,8 @@ void OpaqueStorageAllocation::allocateForValue(SILValue value,
// Insert stack deallocations.
for (TermInst *termInst : pass.returnInsts) {
SILBuilder deallocBuilder(termInst);
deallocBuilder.setSILConventions(
SILModuleConventions::getLoweredAddressConventions());
deallocBuilder.createDeallocStack(allocInstr->getLoc(), allocInstr);
}
}
Expand Down Expand Up @@ -713,7 +719,10 @@ class ApplyRewriter {

public:
ApplyRewriter(SILInstruction *origCall, AddressLoweringState &pass)
: pass(pass), apply(origCall), argBuilder(origCall) {}
: pass(pass), apply(origCall), argBuilder(origCall) {
argBuilder.setSILConventions(
SILModuleConventions::getLoweredAddressConventions());
}

void rewriteParameters();
void rewriteIndirectParameter(Operand *operand);
Expand Down Expand Up @@ -755,6 +764,8 @@ static void insertStackDeallocationAtCall(AllocStackInst *allocInst,
switch (applyInst->getKind()) {
case ValueKind::ApplyInst: {
SILBuilder deallocBuilder(&*std::next(lastUse->getIterator()));
deallocBuilder.setSILConventions(
SILModuleConventions::getLoweredAddressConventions());
deallocBuilder.createDeallocStack(allocInst->getLoc(), allocInst);
break;
}
Expand Down Expand Up @@ -824,11 +835,14 @@ void ApplyRewriter::canonicalizeResults(
SILInstruction *result = directResultValues[resultIdx];
if (!result) {
SILBuilder resultBuilder(std::next(SILBasicBlock::iterator(applyInst)));
resultBuilder.setSILConventions(
SILModuleConventions::getLoweredAddressConventions());
result = resultBuilder.createTupleExtract(applyInst->getLoc(),
applyInst, resultIdx);
directResultValues[resultIdx] = result;
}
SILBuilder B(destroyInst);
B.setSILConventions(SILModuleConventions::getLoweredAddressConventions());
auto &TL = pass.F->getModule().getTypeLowering(result->getType());
TL.emitDestroyValue(B, destroyInst->getLoc(), result);
}
Expand Down Expand Up @@ -860,6 +874,8 @@ SILValue ApplyRewriter::materializeIndirectResultAddress(
// TODO: Find the try_apply's result block.
// Build results outside-in to next stack allocations.
SILBuilder resultBuilder(std::next(SILBasicBlock::iterator(origCallInst)));
resultBuilder.setSILConventions(
SILModuleConventions::getLoweredAddressConventions());
// This is a formally indirect argument, but is loadable.
loadInst = resultBuilder.createLoad(loc, allocInst,
LoadOwnershipQualifier::Unqualified);
Expand Down Expand Up @@ -907,6 +923,8 @@ void ApplyRewriter::convertApplyWithIndirectResults() {
// Prepare to emit a new call instruction.
SILLocation loc = origCallInst->getLoc();
SILBuilder callBuilder(origCallInst);
callBuilder.setSILConventions(
SILModuleConventions::getLoweredAddressConventions());

// The new call instruction's SIL calling convention.
SILFunctionConventions loweredCalleeConv(
Expand Down Expand Up @@ -966,9 +984,7 @@ void ApplyRewriter::convertApplyWithIndirectResults() {
case ValueKind::ApplyInst:
newCallInst = callBuilder.createApply(
loc, apply.getCallee(), apply.getSubstitutions(), newCallArgs,
cast<ApplyInst>(origCallInst)->isNonThrowing(),
nullptr,
SILModuleConventions::getLoweredAddressConventions());
cast<ApplyInst>(origCallInst)->isNonThrowing(), nullptr);
break;
case ValueKind::TryApplyInst:
// TODO: insert dealloc in the catch block.
Expand All @@ -984,6 +1000,9 @@ void ApplyRewriter::convertApplyWithIndirectResults() {
// TODO: handle bbargs from try_apply.
SILBuilder resultBuilder(
std::next(SILBasicBlock::iterator(origCallInst)));
resultBuilder.setSILConventions(
SILModuleConventions::getLoweredAddressConventions());

SmallVector<Operand*, 8> origUses(origCallInst->getUses());
for (Operand *operand : origUses) {
auto *extractInst = dyn_cast<TupleExtractInst>(operand->getUser());
Expand Down Expand Up @@ -1057,6 +1076,7 @@ void ReturnRewriter::rewriteReturn(ReturnInst *returnInst) {
break;
}
SILBuilder B(insertPt);
B.setSILConventions(SILModuleConventions::getLoweredAddressConventions());

// Gather direct function results.
unsigned numOrigDirectResults =
Expand Down Expand Up @@ -1152,7 +1172,9 @@ class AddressOnlyUseRewriter

public:
explicit AddressOnlyUseRewriter(AddressLoweringState &pass)
: pass(pass), B(*pass.F), addrMat(pass, B) {}
: pass(pass), B(*pass.F), addrMat(pass, B) {
B.setSILConventions(SILModuleConventions::getLoweredAddressConventions());
}

void visitOperand(Operand *operand) {
currOper = operand;
Expand Down Expand Up @@ -1288,7 +1310,9 @@ class AddressOnlyDefRewriter

public:
explicit AddressOnlyDefRewriter(AddressLoweringState &pass)
: pass(pass), B(*pass.F), addrMat(pass, B) {}
: pass(pass), B(*pass.F), addrMat(pass, B) {
B.setSILConventions(SILModuleConventions::getLoweredAddressConventions());
}

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

Expand Down