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
3 changes: 3 additions & 0 deletions lib/SILGen/ResultPlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ class PackTransformResultPlan final : public ResultPlan {
// Loop over the pack, initializing each value with the appropriate
// element.
SGF.emitDynamicPackLoop(loc, FormalPackType, ComponentIndex, openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue expansionIndex,
SILValue packIndex) {
Expand Down Expand Up @@ -1313,6 +1314,7 @@ ResultPlanBuilder::buildPackExpansionIntoPack(SILValue packAddr,
// we can emit a dynamic loop to do that now.
if (init->canPerformInPlacePackInitialization(openedEnv, eltTy)) {
SGF.emitDynamicPackLoop(loc, formalPackType, componentIndex, openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue expansionPackIndex,
SILValue packIndex) {
Expand All @@ -1334,6 +1336,7 @@ ResultPlanBuilder::buildPackExpansionIntoPack(SILValue packAddr,
SILType::getPrimitiveObjectType(tupleTy));

SGF.emitDynamicPackLoop(loc, formalPackType, componentIndex, openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue expansionPackIndex,
SILValue packIndex) {
Expand Down
1 change: 1 addition & 0 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4345,6 +4345,7 @@ class ArgEmitter {
auto openedElementEnv = expansionExpr->getGenericEnvironment();
SGF.emitDynamicPackLoop(expansionExpr, formalPackType,
packComponentIndex, openedElementEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand Down
1 change: 1 addition & 0 deletions lib/SILGen/SILGenConstructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ static RValue emitImplicitValueConstructorArg(SILGenFunction &SGF,
auto formalPackType = CanPackType::get(SGF.getASTContext(), {type});

SGF.emitDynamicPackLoop(loc, formalPackType, /*component*/0, openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand Down
1 change: 1 addition & 0 deletions lib/SILGen/SILGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ static void copyOrInitPackExpansionInto(SILGenFunction &SGF,
SGF.Cleanups.forwardCleanup(componentCleanup);

SGF.emitDynamicPackLoop(loc, formalPackType, componentIndex, openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGenDestructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,8 @@ void SILGenFunction::emitIsolatingDestructor(DestructorDecl *dd) {
B.createIntegerLiteral(loc, wordTy, 0);

// Schedule isolated execution
B.createApply(loc, swiftDeinitOnExecutorFunc, {},
B.createApply(loc, swiftDeinitOnExecutorFunc,
getForwardingSubstitutionMap(),
{castedSelf, castedDeallocator, executor, flagsInst});
});
}
Expand Down
2 changes: 2 additions & 0 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,7 @@ RValueEmitter::visitPackExpansionExpr(PackExpansionExpr *E,

SGF.emitDynamicPackLoop(E, formalPackType, /*component index*/ 0,
E->getGenericEnvironment(),
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand Down Expand Up @@ -7587,6 +7588,7 @@ static void emitIgnoredPackExpansion(SILGenFunction &SGF,
auto openedElementEnv = E->getGenericEnvironment();
SGF.emitDynamicPackLoop(E, formalPackType, /*component index*/ 0,
openedElementEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand Down
9 changes: 5 additions & 4 deletions lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -3174,6 +3174,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
/// within the loop; can be null to bind no elements
/// \param reverse - if true, iterate the elements in reverse order,
/// starting at index limitWithinComponent - 1
/// \param emitLoopLatch - emit the entry block.
/// \param emitBody - a function that will be called to emit the body of
/// the loop. It's okay if this has paths that exit the body of the loop,
/// but it should leave the insertion point set at the end.
Expand All @@ -3191,20 +3192,20 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
SILLocation loc, CanPackType formalPackType, unsigned componentIndex,
SILValue startingAfterIndexWithinComponent, SILValue limitWithinComponent,
GenericEnvironment *openedElementEnv, bool reverse,
llvm::function_ref<SILBasicBlock *()> emitLoopLatch,
llvm::function_ref<void(SILValue indexWithinComponent,
SILValue packExpansionIndex, SILValue packIndex)>
emitBody,
SILBasicBlock *loopLatch = nullptr);
emitBody);

/// A convenience version of dynamic pack loop that visits an entire
/// pack expansion component in forward order.
void emitDynamicPackLoop(
SILLocation loc, CanPackType formalPackType, unsigned componentIndex,
GenericEnvironment *openedElementEnv,
llvm::function_ref<SILBasicBlock *()> emitLoopLatch,
llvm::function_ref<void(SILValue indexWithinComponent,
SILValue packExpansionIndex, SILValue packIndex)>
emitBody,
SILBasicBlock *loopLatch = nullptr);
emitBody);

/// Emit a transform on each element of a pack-expansion component
/// of a pack, write the result into a pack-expansion component of
Expand Down
21 changes: 15 additions & 6 deletions lib/SILGen/SILGenPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ void SILGenFunction::emitPartialDestroyPack(SILLocation loc, SILValue packAddr,
emitDynamicPackLoop(loc, formalPackType, componentIndex,
/*startAfter*/ SILValue(), limitWithinComponent,
elementEnv, /*reverse*/ true,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand All @@ -548,6 +549,7 @@ void SILGenFunction::emitPartialDestroyRemainingPack(SILLocation loc,
emitDynamicPackLoop(loc, formalPackType, componentIndex,
/*startAfter*/ currentIndexWithinComponent,
/*limit*/ SILValue(), elementEnv, /*reverse*/ false,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand All @@ -570,6 +572,7 @@ void SILGenFunction::emitPartialDestroyTuple(SILLocation loc,
emitDynamicPackLoop(loc, inducedPackType, componentIndex,
/*startAfter*/ SILValue(), limitWithinComponent,
elementEnv, /*reverse*/ true,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand All @@ -592,6 +595,7 @@ void SILGenFunction::emitPartialDestroyRemainingTuple(SILLocation loc,
emitDynamicPackLoop(loc, inducedPackType, componentIndex,
/*startAfter*/ currentIndexWithinComponent,
/*limit*/ SILValue(), elementEnv, /*reverse*/ false,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand Down Expand Up @@ -650,6 +654,7 @@ void SILGenFunction::copyPackElementsToTuple(SILLocation loc,

emitDynamicPackLoop(
loc, formalPackType, /*componentIndex=*/0, elementEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand All @@ -673,6 +678,7 @@ void SILGenFunction::projectTupleElementsToPack(SILLocation loc,

emitDynamicPackLoop(
loc, formalPackType, /*componentIndex=*/0, elementEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue packIndex) {
Expand All @@ -685,24 +691,24 @@ void SILGenFunction::projectTupleElementsToPack(SILLocation loc,
void SILGenFunction::emitDynamicPackLoop(
SILLocation loc, CanPackType formalPackType, unsigned componentIndex,
GenericEnvironment *openedElementEnv,
llvm::function_ref<SILBasicBlock *()> emitLoopLatch,
llvm::function_ref<void(SILValue indexWithinComponent,
SILValue packExpansionIndex, SILValue packIndex)>
emitBody,
SILBasicBlock *loopLatch) {
emitBody) {
return emitDynamicPackLoop(loc, formalPackType, componentIndex,
/*startAfter*/ SILValue(), /*limit*/ SILValue(),
openedElementEnv, /*reverse*/ false, emitBody,
loopLatch);
openedElementEnv, /*reverse*/ false,
emitLoopLatch, emitBody);
}

void SILGenFunction::emitDynamicPackLoop(
SILLocation loc, CanPackType formalPackType, unsigned componentIndex,
SILValue startingAfterIndexInComponent, SILValue limitWithinComponent,
GenericEnvironment *openedElementEnv, bool reverse,
llvm::function_ref<SILBasicBlock *()> emitLoopLatch,
llvm::function_ref<void(SILValue indexWithinComponent,
SILValue packExpansionIndex, SILValue packIndex)>
emitBody,
SILBasicBlock *loopLatch) {
emitBody) {
assert(isa<PackExpansionType>(formalPackType.getElementType(componentIndex)));
assert((!startingAfterIndexInComponent || !reverse) &&
"cannot reverse with a starting index");
Expand All @@ -719,6 +725,8 @@ void SILGenFunction::emitDynamicPackLoop(
if (auto *expansion = loc.getAsASTNode<PackExpansionExpr>())
prepareToEmitPackExpansionExpr(expansion);

auto *loopLatch = emitLoopLatch();

auto wordTy = SILType::getBuiltinWordType(ctx);
auto boolTy = SILType::getBuiltinIntegerType(1, ctx);

Expand Down Expand Up @@ -1073,6 +1081,7 @@ SILGenFunction::emitPackTransform(SILLocation loc,
}

emitDynamicPackLoop(loc, inputFormalPackType, inputComponentIndex, openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue inputPackIndex) {
Expand Down
3 changes: 3 additions & 0 deletions lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4019,6 +4019,7 @@ ManagedValue ResultPlanner::expandPackExpansion(
// expansion in the inner pack.
SGF.emitDynamicPackLoop(Loc, innerFormalPackType, innerComponentIndex,
openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue innerPackIndex) {
Expand Down Expand Up @@ -4164,6 +4165,7 @@ ManagedValue TranslateArguments::expandPackExpansion(
// index.
SGF.emitDynamicPackLoop(Loc, innerFormalPackType, innerComponentIndex,
openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue innerPackIndex) {
Expand Down Expand Up @@ -4300,6 +4302,7 @@ void ResultPlanner::Operation::emitReabstractTupleIntoPackExpansion(

SGF.emitDynamicPackLoop(loc, innerFormalPackType, innerComponentIndex,
openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue packExpansionIndex,
SILValue innerPackIndex) {
Expand Down
8 changes: 5 additions & 3 deletions lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,11 @@ class EmitBBArguments : public CanTypeVisitor<EmitBBArguments,
{&substEltType});

SGF.emitDynamicPackLoop(loc, inducedPackType, packComponentIndex,
openedEnv, [&](SILValue indexWithinComponent,
SILValue expansionPackIndex,
SILValue packIndex) {
openedEnv,
[]() -> SILBasicBlock * { return nullptr; },
[&](SILValue indexWithinComponent,
SILValue expansionPackIndex,
SILValue packIndex) {
componentInit->performPackExpansionInitialization(SGF, loc,
indexWithinComponent,
[&](Initialization *eltInit) {
Expand Down
20 changes: 11 additions & 9 deletions lib/SILGen/SILGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1412,30 +1412,32 @@ void StmtEmitter::visitForEachStmt(ForEachStmt *S) {
PackType::get(SGF.getASTContext(), expansion->getType())
->getCanonicalType());

JumpDest continueDest = createJumpDest(S->getBody());
JumpDest breakDest = createJumpDest(S->getBody());
std::optional<JumpDest> continueDest;
std::optional<JumpDest> breakDest;

SGF.emitDynamicPackLoop(
SILLocation(expansion), formalPackType, 0,
expansion->getGenericEnvironment(),
[&]() -> SILBasicBlock * {
breakDest = createJumpDest(S->getBody());
continueDest = createJumpDest(S->getBody());
return continueDest->getBlock();
},
[&](SILValue indexWithinComponent, SILValue packExpansionIndex,
SILValue packIndex) {
Scope innerForScope(SGF.Cleanups, CleanupLocation(S->getBody()));
auto letValueInit =
SGF.emitPatternBindingInitialization(S->getPattern(), continueDest);
SGF.emitPatternBindingInitialization(S->getPattern(), *continueDest);

SGF.emitExprInto(expansion->getPatternExpr(), letValueInit.get());

// Set the destinations for 'break' and 'continue'.
SGF.BreakContinueDestStack.push_back({S, breakDest, continueDest});
SGF.BreakContinueDestStack.push_back({S, *breakDest, *continueDest});
visit(S->getBody());
SGF.BreakContinueDestStack.pop_back();
});

return;
},
continueDest.getBlock());

emitOrDeleteBlock(SGF, breakDest, S);
emitOrDeleteBlock(SGF, *breakDest, S);

return;
}
Expand Down
43 changes: 43 additions & 0 deletions test/Interpreter/issue-78598.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %target-run-simple-swift(-target %target-swift-5.9-abi-triple) | %FileCheck %s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a // REQUIRES: executable_test else it breaks CI platforms like Android that need those to be explicitly labeled, submitted in #85443.


// https://github.com/swiftlang/swift/issues/78598

var counter = 0

final class Entry<Results> {
var isEmpty: Bool { true }
init() {
counter += 1
}
deinit {
counter -= 1
}
}

struct Foo<each Results> {
private let entry: (repeat Entry<each Results>)

init(_ entry: (repeat Entry<each Results>)) {
self.entry = entry
for entry in repeat each entry {
if entry.isEmpty {
break
}
}
}

var hmmm: Void {
for entry in repeat each entry {
if !entry.isEmpty {
break
}
}
}
}

_ = Foo((
Entry<Any>()
)).hmmm

// CHECK: Counter: 0
print("Counter: \(counter)")
47 changes: 47 additions & 0 deletions test/Interpreter/pack_iteration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// RUN: %target-run-simple-swift

// REQUIRES: executable_test

import StdlibUnittest

var packIter = TestSuite("PackIteration")

packIter.test("break") {
func breakTest<each T: Hashable>(_ ts: (repeat (each T)?)) -> [AnyHashable] {
var result: [AnyHashable] = []

for t in repeat each ts {
if let t {
result.append(t)
} else {
break
}
}

return result
}

let results = breakTest((0, "hello", nil as Bool?, 3.14))
expectEqual(results, [0, "hello"] as [AnyHashable])
}

packIter.test("break") {
func continueTest<each T: Hashable>(_ ts: (repeat (each T)?)) -> [AnyHashable] {
var result: [AnyHashable] = []

for t in repeat each ts {
if let t {
result.append(t)
} else {
continue
}
}

return result
}

let results = continueTest((0, "hello", nil as Bool?, 3.14))
expectEqual(results, [0, "hello", 3.14] as [AnyHashable])
}

runAllTests()
Loading