Skip to content

Scope arg transforms 1 #8285

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 4 commits into from
Mar 22, 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
27 changes: 16 additions & 11 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2566,16 +2566,18 @@ namespace {
SILType loweredSubstArgType,
AbstractionPattern origParamType,
SILParameterInfo param) {
Scope scope(SGF, arg.getLocation());

// TODO: We should take the opportunity to peephole certain abstraction
// changes here, for instance, directly emitting a closure literal at the
// callee's expected abstraction level instead of emitting it maximally
// substituted and thunking.
auto emitted = emitArgumentFromSource(std::move(arg), loweredSubstArgType,
origParamType, param);
return SGF.emitSubstToOrigValue(emitted.loc,
std::move(emitted.value).getScalarValue(),
origParamType, emitted.value.getType(),
emitted.contextForReabstraction);
ManagedValue result = SGF.emitSubstToOrigValue(
emitted.loc, std::move(emitted.value).getScalarValue(), origParamType,
emitted.value.getType(), emitted.contextForReabstraction);
return scope.popPreservingValue(result);
}

CanType getAnyObjectType() {
Expand All @@ -2592,6 +2594,8 @@ namespace {
SILType loweredSubstArgType,
AbstractionPattern origParamType,
SILParameterInfo param) {
Scope scope(SGF, arg.getLocation());

// If we're bridging a concrete type to `id` via Any, skip the Any
// boxing.

Expand All @@ -2601,17 +2605,18 @@ namespace {
if (auto objTy = paramObjTy.getAnyOptionalObjectType())
paramObjTy = objTy;
if (isAnyObjectType(paramObjTy) && !arg.isRValue()) {
return emitNativeToBridgedObjectArgument(std::move(arg).asKnownExpr(),
loweredSubstArgType,
origParamType, param);
return scope.popPreservingValue(emitNativeToBridgedObjectArgument(
std::move(arg).asKnownExpr(), loweredSubstArgType, origParamType,
param));
}

auto emitted = emitArgumentFromSource(std::move(arg), loweredSubstArgType,
origParamType, param);

return SGF.emitNativeToBridgedValue(emitted.loc,
std::move(emitted.value).getAsSingleValue(SGF, emitted.loc),
Rep, param.getType());

return scope.popPreservingValue(SGF.emitNativeToBridgedValue(
emitted.loc,
std::move(emitted.value).getAsSingleValue(SGF, emitted.loc), Rep,
param.getType()));
}

enum class ExistentialPeepholeOptionality {
Expand Down
40 changes: 13 additions & 27 deletions lib/SILGen/SILGenBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,24 @@ using namespace swift;
using namespace Lowering;

//===----------------------------------------------------------------------===//
// Cleanup Forwarder
// CleanupCloner
//===----------------------------------------------------------------------===//

namespace {
class CleanupCloner {
SILGenFunction &SGF;
bool hasCleanup;
bool isLValue;
ValueOwnershipKind ownershipKind;

public:
CleanupCloner(SILGenBuilder &Builder, ManagedValue M)
: SGF(Builder.getSILGenFunction()), hasCleanup(M.hasCleanup()),
isLValue(M.isLValue()), ownershipKind(M.getOwnershipKind()) {}

ManagedValue clone(SILValue value) {
if (isLValue) {
return ManagedValue::forLValue(value);
}

if (!hasCleanup) {
return ManagedValue::forUnmanaged(value);
}
ManagedValue CleanupCloner::clone(SILValue value) const {
if (isLValue) {
return ManagedValue::forLValue(value);
}

if (value->getType().isAddress()) {
return SGF.emitManagedBufferWithCleanup(value);
}
if (!hasCleanup) {
return ManagedValue::forUnmanaged(value);
}

return SGF.emitManagedRValueWithCleanup(value);
if (value->getType().isAddress()) {
return SGF.emitManagedBufferWithCleanup(value);
}
};
} // end anonymous namespace

return SGF.emitManagedRValueWithCleanup(value);
}

//===----------------------------------------------------------------------===//
// Utility Methods
Expand Down
17 changes: 17 additions & 0 deletions lib/SILGen/SILGenBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,23 @@ class SwitchEnumBuilder {
SILGenFunction &getSGF() const { return builder.getSILGenFunction(); }
};

class CleanupCloner {
SILGenFunction &SGF;
bool hasCleanup;
bool isLValue;
ValueOwnershipKind ownershipKind;

public:
CleanupCloner(SILGenBuilder &builder, ManagedValue mv)
: CleanupCloner(builder.getSILGenFunction(), mv) {}

CleanupCloner(SILGenFunction &SGF, ManagedValue mv)
: SGF(SGF), hasCleanup(mv.hasCleanup()), isLValue(mv.isLValue()),
ownershipKind(mv.getOwnershipKind()) {}

ManagedValue clone(SILValue value) const;
};

} // namespace Lowering
} // namespace swift

Expand Down
14 changes: 14 additions & 0 deletions lib/SILGen/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class LLVM_LIBRARY_VISIBILITY Scope {
cleanups.innermostScope = depth;
}

explicit Scope(SILGenFunction &SGF, SILLocation loc)
: Scope(SGF.Cleanups, CleanupLocation::get(loc)) {}

void pop() {
assert(depth.isValid() && "popping a scope twice!");
popImpl();
Expand All @@ -54,6 +57,17 @@ class LLVM_LIBRARY_VISIBILITY Scope {

bool isValid() const { return depth.isValid(); }

ManagedValue popPreservingValue(ManagedValue mv) {
// If we have a value, make sure that it is an object. The reason why is
// that we want to make sure that we are not forwarding a cleanup for a
// stack location that will be destroyed by this scope.
assert(!mv.getValue() || mv.getType().isObject());
CleanupCloner cloner(cleanups.SGF, mv);
SILValue value = mv.forward(cleanups.SGF);
pop();
return cloner.clone(value);
}

private:
void popImpl() {
cleanups.stack.checkIterator(depth);
Expand Down
4 changes: 2 additions & 2 deletions test/SILGen/objc_bridging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,10 @@ func applyStringBlock(_ f: @convention(block) (String) -> String, x: String) ->
// CHECK: [[STRING_TO_NSSTRING:%.*]] = function_ref @_T0SS10FoundationE19_bridgeToObjectiveCSo8NSStringCyF
// CHECK: [[BORROWED_STRING_COPY:%.*]] = begin_borrow [[STRING_COPY]]
// CHECK: [[NSSTR:%.*]] = apply [[STRING_TO_NSSTRING]]([[BORROWED_STRING_COPY]]) : $@convention(method) (@guaranteed String)
// CHECK: [[RESULT_NSSTR:%.*]] = apply [[BLOCK_COPY_COPY]]([[NSSTR]]) : $@convention(block) (NSString) -> @autoreleased NSString
// CHECK: destroy_value [[NSSTR]]
// CHECK: end_borrow [[BORROWED_STRING_COPY]] from [[STRING_COPY]]
// CHECK: destroy_value [[STRING_COPY]]
// CHECK: [[RESULT_NSSTR:%.*]] = apply [[BLOCK_COPY_COPY]]([[NSSTR]]) : $@convention(block) (NSString) -> @autoreleased NSString
// CHECK: destroy_value [[NSSTR]]
// CHECK: [[FINAL_BRIDGE:%.*]] = function_ref @_T0SS10FoundationE36_unconditionallyBridgeFromObjectiveCSSSo8NSStringCSgFZ
// CHECK: [[OPTIONAL_NSSTR:%.*]] = enum $Optional<NSString>, #Optional.some!enumelt.1, [[RESULT_NSSTR]]
// CHECK: [[RESULT:%.*]] = apply [[FINAL_BRIDGE]]([[OPTIONAL_NSSTR]], {{.*}}) : $@convention(method) (@owned Optional<NSString>, @thin String.Type) -> @owned String
Expand Down
Loading