SILGen: Support mutable consuming parameters.

Emit a box like we would for a local variable, and move the parameter value into the box, as
part of the prolog.
This commit is contained in:
Joe Groff
2023-01-25 12:40:29 -08:00
parent 704a4363d7
commit 2a187f2606
4 changed files with 45 additions and 14 deletions

View File

@@ -6387,9 +6387,9 @@ Type VarDecl::getType() const {
/// is a let member in an initializer.
bool VarDecl::isSettable(const DeclContext *UseDC,
const DeclRefExpr *base) const {
// Only inout parameters are settable.
// Parameters are settable or not depending on their ownership convention.
if (auto *PD = dyn_cast<ParamDecl>(this))
return PD->isInOut();
return !PD->isImmutableInFunctionBody();
// If this is a 'var' decl, then we're settable if we have storage or a
// setter.

View File

@@ -429,18 +429,20 @@ struct ArgumentInitHelper {
ManagedValue argrv = makeArgument(ty, pd->isInOut(), isNoImplicitCopy,
lifetimeAnnotation, parent, loc);
SILValue value = argrv.getValue();
if (pd->isInOut()) {
assert(argrv.getType().isAddress() && "expected inout to be address");
} else {
#warning "todo"
assert(pd->isImmutableInFunctionBody()
&& "consuming mutable params not implemented yet");
// If the variable is immutable, we can bind the value as is.
// Leave the cleanup on the argument, if any, in place to consume the
// argument if we're responsible for it.
} else if (!pd->isImmutableInFunctionBody()) {
// If it's a locally mutable parameter, then we need to move the argument
// value into a local box to hold the mutated value.
auto mutableBox = SGF.emitLocalVariableWithCleanup(pd,
MarkUninitializedInst::Var);
argrv.ensurePlusOne(SGF, loc).forwardInto(SGF, loc, mutableBox.get());
return;
}
SILValue value = argrv.getValue();
#warning "todo"
// If the variable is immutable, we can bind the value as is.
// Leave the cleanup on the argument, if any, in place to consume the
// argument if we're responsible for it.
SILDebugVariable varinfo(pd->isImmutableInFunctionBody(), ArgNo);
if (!argrv.getType().isAddress()) {
// NOTE: We setup SGF.VarLocs[pd] in updateArgumentValueForBinding.

View File

@@ -3358,9 +3358,9 @@ StorageImplInfoRequest::evaluate(Evaluator &evaluator,
AbstractStorageDecl *storage) const {
if (auto *param = dyn_cast<ParamDecl>(storage)) {
return StorageImplInfo::getSimpleStored(
param->isInOut()
? StorageIsMutable
: StorageIsNotMutable);
param->isImmutableInFunctionBody()
? StorageIsNotMutable
: StorageIsMutable);
}
if (auto *var = dyn_cast<VarDecl>(storage)) {

View File

@@ -0,0 +1,29 @@
// RUN: %target-swift-emit-silgen %s | %FileCheck %s
func bar(_: String) {}
// CHECK-LABEL: sil {{.*}} @${{.*}}3foo
func foo(y: consuming String, z: String) -> () -> String {
// CHECK: bb0(%0 : @owned $String, %1 : @guaranteed $String):
// CHECK: [[BOX:%.*]] = alloc_box ${ var String }
// CHECK: [[BOX0:%.*]] = mark_uninitialized [var] [[BOX]]
// CHECK: [[BOX1:%.*]] = begin_borrow [lexical] [[BOX0]]
// CHECK: [[Y:%.*]] = project_box [[BOX1]]
// CHECK: store %0 to [init] [[Y]]
// CHECK: [[YCAPTURE:%.*]] = copy_value [[BOX1]]
// CHECK: partial_apply {{.*}} {{%.*}}([[YCAPTURE]])
let r = { y }
// CHECK: [[ZCOPY:%.*]] = copy_value %1
// CHECK: [[YACCESS:%.*]] = begin_access [modify] [unknown] [[Y]]
// CHECK: assign [[ZCOPY]] to [[YACCESS]]
y = z
// CHECK: [[YACCESS:%.*]] = begin_access [read] [unknown] [[Y]]
// CHECK: [[YVAL:%.*]] = load [copy] [[YACCESS]]
// CHECK: apply {{%.*}}([[YVAL]]
bar(y)
return r
}