Skip to content
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private func constantPropagateCaptures(of partialApply: PartialApplyInst, _ cont
// Escaping closures consume their arguments. Therefore we need to destroy the removed argument values.
addCompensatingDestroys(for: constArgs, context)
}
let newArguments = nonConstArgs.map { $0.value }
let newArguments = Array(nonConstArgs.values)
rewritePartialApply(partialApply, withSpecialized: specializedCallee, arguments: newArguments, context)
}

Expand Down Expand Up @@ -195,8 +195,7 @@ private func cloneArgument(_ argumentOp: Operand,
if partialApply.calleeArgumentConventions[calleeArgIdx].isGuaranteed {
// If the original argument was passed as guaranteed, i.e. is _not_ destroyed in the closure, we have
// to destroy the cloned argument at function exits.
for block in targetFunction.blocks where block.terminator.isFunctionExiting {
let builder = Builder(before: block.terminator, context)
Builder.insertCleanupAtFunctionExits(of: targetFunction, context) { builder in
builder.emitDestroy(of: clonedArg)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ private extension Value {
case is LoadInst:
return true
case is StructInst, is TupleInst:
worklist.pushIfNotVisited(contentsOf: (v as! Instruction).operands.lazy.map { $0.value })
worklist.pushIfNotVisited(contentsOf: (v as! Instruction).operands.values)
case let ei as EnumInst:
if let payload = ei.payload {
worklist.pushIfNotVisited(payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ private extension CondBranchInst {
}
let builder = Builder(before: self, context)
if conditionValue == 0 {
builder.createBranch(to: falseBlock, arguments: Array(falseOperands.map { $0.value }))
builder.createBranch(to: falseBlock, arguments: Array(falseOperands.values))
} else {
builder.createBranch(to: trueBlock, arguments: Array(trueOperands.map { $0.value }))
builder.createBranch(to: trueBlock, arguments: Array(trueOperands.values))
}
context.erase(instruction: self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,9 @@ private struct StackProtectionOptimization {

builder.createCopyAddr(from: argument, to: temporary, takeSource: true, initializeDest: true)

for block in function.blocks {
let terminator = block.terminator
if terminator.isFunctionExiting {
let exitBuilder = Builder(before: terminator, location: terminator.location.asAutoGenerated, context)
exitBuilder.createCopyAddr(from: temporary, to: argument, takeSource: true, initializeDest: true)
exitBuilder.createDeallocStack(temporary)
}
Builder.insertCleanupAtFunctionExits(of: function, context) { builder in
builder.createCopyAddr(from: temporary, to: argument, takeSource: true, initializeDest: true)
builder.createDeallocStack(temporary)
}
log("move addr protection in \(function.name): \(argument)")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,34 @@ struct FunctionPassContext : MutatingContext {
return String(taking: bridgedPassContext.mangleOutlinedVariable(function.bridged))
}

func mangle(withClosureArguments closureArgs: [(argumentIndex: Int, argumentValue: Value)],
enum ClosureArgumentMangling {
case closure(SingleValueInstruction)

/// The argument specializes for the same closure as a previous argument, e.g.
/// ```
/// %1 = partial_apply %closure
/// apply %f(%1, %1) // first argument: `.closure(%1)`
/// // second argument: `.previousArgumentIndex(0)`
case previousArgumentIndex(Int)
}

func mangle(withClosureArguments closureArgs: [(argumentIndex: Int, argumentValue: ClosureArgumentMangling)],
from applySiteCallee: Function
) -> String {
closureArgs.withBridgedArrayRef{ bridgedClosureArgs in
let bridgedArgManglings = closureArgs.map {
switch $0.argumentValue {
case .closure(let closure):
return BridgedPassContext.ClosureArgMangling(argIdx: $0.argumentIndex,
inst: Optional<Instruction>(closure).bridged,
otherArgIdx: -1)
case .previousArgumentIndex(let idx):
return BridgedPassContext.ClosureArgMangling(argIdx: $0.argumentIndex,
inst: OptionalBridgedInstruction(),
otherArgIdx: idx)
}
}

return bridgedArgManglings.withBridgedArrayRef{ bridgedClosureArgs in
String(taking: bridgedPassContext.mangleWithClosureArgs(bridgedClosureArgs, applySiteCallee.bridged))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private func registerSwiftPasses() {
registerPass(tempRValueElimination, { tempRValueElimination.run($0) })
registerPass(mandatoryTempRValueElimination, { mandatoryTempRValueElimination.run($0) })
registerPass(tempLValueElimination, { tempLValueElimination.run($0) })
registerPass(generalClosureSpecialization, { generalClosureSpecialization.run($0) })
registerPass(closureSpecialization, { closureSpecialization.run($0) })
registerPass(autodiffClosureSpecialization, { autodiffClosureSpecialization.run($0) })
registerPass(loopInvariantCodeMotionPass, { loopInvariantCodeMotionPass.run($0) })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public func registerOptimizerTests() {
registerFunctionTests(
addressOwnershipLiveRangeTest,
argumentConventionsTest,
getPullbackClosureInfoTest,
interiorLivenessTest,
lifetimeDependenceRootTest,
lifetimeDependenceScopeTest,
Expand All @@ -52,8 +51,6 @@ public func registerOptimizerTests() {
localVariableReachableUsesTest,
localVariableReachingAssignmentsTest,
rangeOverlapsPathTest,
rewrittenCallerBodyTest,
specializedFunctionSignatureAndBodyTest,
variableIntroducerTest
)

Expand Down
16 changes: 14 additions & 2 deletions SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extension Value {
}
switch v {
case let fw as ForwardingInstruction:
worklist.pushIfNotVisited(contentsOf: fw.definedOperands.lazy.map { $0.value })
worklist.pushIfNotVisited(contentsOf: fw.definedOperands.values)
case let bf as BorrowedFromInst:
worklist.pushIfNotVisited(bf.borrowedValue)
case let bb as BeginBorrowInst:
Expand All @@ -82,7 +82,7 @@ extension Value {
} else if let termResult = TerminatorResult(arg),
let fw = termResult.terminator as? ForwardingInstruction
{
worklist.pushIfNotVisited(contentsOf: fw.definedOperands.lazy.map { $0.value })
worklist.pushIfNotVisited(contentsOf: fw.definedOperands.values)
}
default:
continue
Expand Down Expand Up @@ -262,6 +262,18 @@ extension Builder {
}
}

static func insertCleanupAtFunctionExits(
of function: Function,
_ context: some MutatingContext,
insert: (Builder) -> ()
) {
for exitBlock in function.blocks where exitBlock.terminator.isFunctionExiting {
let terminator = exitBlock.terminator
let builder = Builder(before: terminator, location: terminator.location.asCleanup, context)
insert(builder)
}
}

func destroyCapturedArgs(for paiOnStack: PartialApplyInst) {
precondition(paiOnStack.isOnStack, "Function must only be called for `partial_apply`s on stack!")
self.bridged.destroyCapturedArgs(paiOnStack.bridged)
Expand Down
2 changes: 1 addition & 1 deletion SwiftCompilerSources/Sources/SIL/Argument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public struct Phi {
}

public var incomingValues: LazyMapSequence<LazyMapSequence<PredecessorList, Operand>, Value> {
incomingOperands.lazy.map { $0.value }
incomingOperands.values
}

public var isReborrow: Bool { value.isReborrow }
Expand Down
5 changes: 5 additions & 0 deletions SwiftCompilerSources/Sources/SIL/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ public struct Builder {
return notifyNew(cast.getAs(UncheckedAddrCastInst.self))
}

public func createUncheckedValueCast(from value: Value, to type: Type) -> UncheckedValueCastInst {
let cast = bridged.createUncheckedValueCast(value.bridged, type.bridged)
return notifyNew(cast.getAs(UncheckedValueCastInst.self))
}

public func createUpcast(from value: Value, to type: Type) -> UpcastInst {
let cast = bridged.createUpcast(value.bridged, type.bridged)
return notifyNew(cast.getAs(UpcastInst.self))
Expand Down
7 changes: 6 additions & 1 deletion SwiftCompilerSources/Sources/SIL/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,9 @@ final public class UncheckedTrivialBitCastInst : SingleValueInstruction, UnaryIn
}

final public class UncheckedBitwiseCastInst : SingleValueInstruction, UnaryInstruction {}
final public class UncheckedValueCastInst : SingleValueInstruction, UnaryInstruction {}
final public class UncheckedValueCastInst : SingleValueInstruction, UnaryInstruction {
public var fromValue: Value { operand.value }
}

final public class RefToRawPointerInst : SingleValueInstruction, UnaryInstruction {}
final public class RefToUnmanagedInst : SingleValueInstruction, UnaryInstruction {}
Expand Down Expand Up @@ -1719,6 +1721,9 @@ final public class BeginApplyInst : MultipleValueInstruction, FullApplySite {
public var yieldedValues: Results {
Results(inst: self, numResults: resultCount - (isCalleeAllocated ? 2 : 1))
}

public var isNonThrowing: Bool { bridged.BeginApplyInst_getNonThrowing() }
public var isNonAsync: Bool { bridged.BeginApplyInst_getNonAsync() }
}

final public class EndApplyInst : SingleValueInstruction, UnaryInstruction {
Expand Down
8 changes: 4 additions & 4 deletions SwiftCompilerSources/Sources/SIL/Operand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ public struct OperandArray : RandomAccessCollection, CustomReflectable {
base: OptionalBridgedOperand(op: base.advancedBy(bounds.lowerBound).op),
count: bounds.upperBound - bounds.lowerBound)
}

public var values: LazyMapSequence<LazySequence<OperandArray>.Elements, Value> {
self.lazy.map { $0.value }
}
}

public struct UseList : CollectionLikeSequence {
Expand Down Expand Up @@ -143,6 +139,10 @@ public struct UseList : CollectionLikeSequence {
}

extension Sequence where Element == Operand {
public var values: LazyMapSequence<Self, Value> {
self.lazy.map { $0.value }
}

public var singleUse: Operand? {
var result: Operand? = nil
for op in self {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public final class EnclosingValueIterator : IteratorProtocol {
} else if let forwardingInst = value.forwardingInstruction {
// Recurse through guaranteed forwarding non-phi instructions.
let ops = forwardingInst.forwardedOperands
worklist.pushIfNotVisited(contentsOf: ops.lazy.map { $0.value })
worklist.pushIfNotVisited(contentsOf: ops.values)
} else if value.isGuaranteedApplyResult {
let selfArgument = (value as! ApplyInst).arguments.last!
worklist.pushIfNotVisited(selfArgument)
Expand Down
4 changes: 3 additions & 1 deletion SwiftCompilerSources/Sources/SIL/Utilities/Cloner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public struct Cloner<Context: MutatingContext> {
if let entryBlock = targetFunction.blocks.first {
return entryBlock
}
return targetFunction.appendNewBlock(context)
let entryBlock = targetFunction.appendNewBlock(context)
bridged.setInsertionBlockIfNotSet(entryBlock.bridged)
return entryBlock
}

public func cloneFunctionBody(from originalFunction: Function, entryBlockArguments: [Value]) {
Expand Down
1 change: 1 addition & 0 deletions docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,7 @@ Some kinds need arguments, which precede ``Tf``.
ARG-SPEC-KIND ::= 'n' // Unmodified argument
ARG-SPEC-KIND ::= 'c' // Consumes n 'type' arguments which are closed over types in argument order
// and one 'identifier' argument which is the closure symbol name
ARG-SPEC-KIND ::= 'C' NATURAL-ZERO // the same closure as a previous argument <n>
ARG-SPEC-KIND ::= 'p' CONST-PROP // Constant propagated argument
ARG-SPEC-KIND ::= 'e' 'D'? 'G'? 'X'? // Generic argument, with optional dead, owned=>guaranteed or exploded-specifier
ARG-SPEC-KIND ::= 'd' 'G'? 'X'? // Dead argument, with optional owned=>guaranteed or exploded-specifier
Expand Down
4 changes: 0 additions & 4 deletions include/swift/AST/SILOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,6 @@ class SILOptions {
/// Are we building in embedded Swift + -no-allocations?
bool NoAllocations = false;

/// Should we use the experimental Swift based closure-specialization
/// optimization pass instead of the existing C++ one.
bool EnableExperimentalSwiftBasedClosureSpecialization = false;

/// The name of the file to which the backend should save optimization
/// records.
std::string OptRecordFile;
Expand Down
1 change: 1 addition & 0 deletions include/swift/Demangling/Demangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ enum class FunctionSigSpecializationParamKind : unsigned {
InOutToOut = 8,
ConstantPropKeyPath = 9,
ConstantPropStruct = 10,
ClosurePropPreviousArg = 11,

// Option Set Flags use bits 6-31. This gives us 26 bits to use for option
// flags.
Expand Down
4 changes: 0 additions & 4 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -393,10 +393,6 @@ def public_autolink_library :
// HIDDEN FLAGS
let Flags = [FrontendOption, NoDriverOption, HelpHidden] in {

def enable_experimental_swift_based_closure_specialization :
Flag<["-"], "experimental-swift-based-closure-specialization">,
HelpText<"Use the experimental Swift based closure-specialization optimization pass instead of the existing C++ one">;

def checked_async_objc_bridging : Joined<["-"], "checked-async-objc-bridging=">,
HelpText<"Control whether checked continuations are used when bridging "
"async calls from Swift to ObjC: 'on', 'off' ">;
Expand Down
7 changes: 5 additions & 2 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ BridgedParameterInfoArray SILFunctionType_getParameters(BridgedCanType);

BRIDGED_INLINE bool SILFunctionType_hasSelfParam(BridgedCanType);

BRIDGED_INLINE bool SILFunctionType_isTrivialNoescape(BridgedCanType);

SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
BridgedYieldInfoArray SILFunctionType_getYields(BridgedCanType);

Expand Down Expand Up @@ -816,6 +814,8 @@ struct BridgedInstruction {
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSpecializationInformation ApplyInst_getSpecializationInfo() const;
BRIDGED_INLINE bool TryApplyInst_getNonAsync() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSpecializationInformation TryApplyInst_getSpecializationInfo() const;
BRIDGED_INLINE bool BeginApplyInst_getNonThrowing() const;
BRIDGED_INLINE bool BeginApplyInst_getNonAsync() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclRef ClassMethodInst_getMember() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclRef WitnessMethodInst_getMember() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType WitnessMethodInst_getLookupType() const;
Expand Down Expand Up @@ -1229,6 +1229,8 @@ struct BridgedBuilder{
BridgedType type) const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createUncheckedAddrCast(BridgedValue op,
BridgedType type) const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createUncheckedValueCast(BridgedValue op,
BridgedType type) const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createUpcast(BridgedValue op, BridgedType type) const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createCheckedCastAddrBranch(
BridgedValue source, BridgedCanType sourceFormalType,
Expand Down Expand Up @@ -1545,6 +1547,7 @@ struct BridgedCloner {
void recordClonedInstruction(BridgedInstruction origInst, BridgedInstruction clonedInst) const;
void recordFoldedValue(BridgedValue orig, BridgedValue mapped) const;
BridgedInstruction clone(BridgedInstruction inst) const;
void setInsertionBlockIfNotSet(BridgedBasicBlock block) const;
};

struct BridgedTypeSubstCloner {
Expand Down
17 changes: 13 additions & 4 deletions include/swift/SIL/SILBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,6 @@ bool SILFunctionType_hasSelfParam(BridgedCanType funcTy) {
return funcTy.unbridged()->castTo<swift::SILFunctionType>()->hasSelfParam();
}

bool SILFunctionType_isTrivialNoescape(BridgedCanType funcTy) {
return funcTy.unbridged()->castTo<swift::SILFunctionType>()->isTrivialNoEscape();
}

BridgedYieldInfoArray SILFunctionType_getYields(BridgedCanType funcTy) {
return {funcTy.unbridged()->castTo<swift::SILFunctionType>()->getYields()};
}
Expand Down Expand Up @@ -1349,6 +1345,14 @@ BridgedGenericSpecializationInformation BridgedInstruction::TryApplyInst_getSpec
return {getAs<swift::TryApplyInst>()->getSpecializationInfo()};
}

bool BridgedInstruction::BeginApplyInst_getNonThrowing() const {
return getAs<swift::BeginApplyInst>()->isNonThrowing();
}

bool BridgedInstruction::BeginApplyInst_getNonAsync() const {
return getAs<swift::BeginApplyInst>()->isNonAsync();
}

BridgedDeclRef BridgedInstruction::ClassMethodInst_getMember() const {
return getAs<swift::ClassMethodInst>()->getMember();
}
Expand Down Expand Up @@ -2301,6 +2305,11 @@ BridgedInstruction BridgedBuilder::createUncheckedAddrCast(BridgedValue op, Brid
type.unbridged())};
}

BridgedInstruction BridgedBuilder::createUncheckedValueCast(BridgedValue op, BridgedType type) const {
return {unbridged().createUncheckedValueCast(regularLoc(), op.getSILValue(),
type.unbridged())};
}

BridgedInstruction BridgedBuilder::createUpcast(BridgedValue op, BridgedType type) const {
return {unbridged().createUpcast(regularLoc(), op.getSILValue(),
type.unbridged())};
Expand Down
Loading