Skip to content

Commit f991732

Browse files
authored
Merge pull request #2974 from swiftwasm/main
[pull] swiftwasm from main
2 parents 37eb246 + 4d69559 commit f991732

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ using DomTreeLevelMap = llvm::DenseMap<DomTreeNode *, unsigned>;
5959
// Utilities
6060
//===----------------------------------------------------------------------===//
6161

62-
static void replaceDestroy(DestroyAddrInst *dai, SILValue newValue) {
62+
static void replaceDestroy(DestroyAddrInst *dai, SILValue newValue,
63+
SILBuilderContext &ctx) {
6364
SILFunction *f = dai->getFunction();
6465
auto ty = dai->getOperand()->getType();
6566

@@ -68,7 +69,7 @@ static void replaceDestroy(DestroyAddrInst *dai, SILValue newValue) {
6869
assert(newValue ||
6970
(ty.is<TupleType>() && ty.getAs<TupleType>()->getNumElements() == 0));
7071

71-
SILBuilderWithScope builder(dai);
72+
SILBuilderWithScope builder(dai, ctx);
7273

7374
auto &typeLowering = f->getTypeLowering(ty);
7475

@@ -84,21 +85,22 @@ static void replaceDestroy(DestroyAddrInst *dai, SILValue newValue) {
8485

8586
/// Promote a DebugValueAddr to a DebugValue of the given value.
8687
static void promoteDebugValueAddr(DebugValueAddrInst *dvai, SILValue value,
87-
SILBuilder &b) {
88+
SILBuilderContext &ctx) {
8889
assert(dvai->getOperand()->getType().isLoadable(*dvai->getFunction()) &&
8990
"Unexpected promotion of address-only type!");
9091
assert(value && "Expected valid value");
9192
// Avoid inserting the same debug_value twice.
92-
for (auto *use : value->getUses())
93-
if (auto *dvi = dyn_cast<DebugValueInst>(use->getUser()))
93+
for (auto *use : value->getUses()) {
94+
if (auto *dvi = dyn_cast<DebugValueInst>(use->getUser())) {
9495
if (*dvi->getVarInfo() == *dvai->getVarInfo()) {
9596
dvai->eraseFromParent();
9697
return;
9798
}
98-
b.setInsertionPoint(dvai);
99-
b.setCurrentDebugScope(dvai->getDebugScope());
100-
b.createDebugValue(dvai->getLoc(), value, *dvai->getVarInfo());
99+
}
100+
}
101101

102+
SILBuilderWithScope b(dvai, ctx);
103+
b.createDebugValue(dvai->getLoc(), value, *dvai->getVarInfo());
102104
dvai->eraseFromParent();
103105
}
104106

@@ -135,10 +137,11 @@ static void collectLoads(SILInstruction *i,
135137
}
136138
}
137139

138-
static void replaceLoad(LoadInst *li, SILValue newValue, AllocStackInst *asi) {
140+
static void replaceLoad(LoadInst *li, SILValue newValue, AllocStackInst *asi,
141+
SILBuilderContext &ctx) {
139142
ProjectionPath projections(newValue->getType());
140143
SILValue op = li->getOperand();
141-
SILBuilderWithScope builder(li);
144+
SILBuilderWithScope builder(li, ctx);
142145

143146
while (op != asi) {
144147
assert(isa<UncheckedAddrCastInst>(op) || isa<StructElementAddrInst>(op) ||
@@ -200,14 +203,16 @@ static void replaceLoad(LoadInst *li, SILValue newValue, AllocStackInst *asi) {
200203

201204
/// Create a tuple value for an empty tuple or a tuple of empty tuples.
202205
static SILValue createValueForEmptyTuple(SILType ty,
203-
SILInstruction *insertionPoint) {
206+
SILInstruction *insertionPoint,
207+
SILBuilderContext &ctx) {
204208
auto tupleTy = ty.castTo<TupleType>();
205209
SmallVector<SILValue, 4> elements;
206210
for (unsigned idx : range(tupleTy->getNumElements())) {
207211
SILType elementTy = ty.getTupleElementType(idx);
208-
elements.push_back(createValueForEmptyTuple(elementTy, insertionPoint));
212+
elements.push_back(
213+
createValueForEmptyTuple(elementTy, insertionPoint, ctx));
209214
}
210-
SILBuilderWithScope builder(insertionPoint);
215+
SILBuilderWithScope builder(insertionPoint, ctx);
211216
return builder.createTuple(insertionPoint->getLoc(), ty, elements);
212217
}
213218

@@ -242,8 +247,9 @@ class StackAllocationPromoter {
242247
/// Map from dominator tree node to tree level.
243248
DomTreeLevelMap &domTreeLevels;
244249

245-
/// The builder used to create new instructions during register promotion.
246-
SILBuilder &b;
250+
/// The SIL builder used when creating new instructions during register
251+
/// promotion.
252+
SILBuilderContext &ctx;
247253

248254
/// Records the last store instruction in each block for a specific
249255
/// AllocStackInst.
@@ -253,9 +259,9 @@ class StackAllocationPromoter {
253259
/// C'tor.
254260
StackAllocationPromoter(AllocStackInst *inputASI, DominanceInfo *inputDomInfo,
255261
DomTreeLevelMap &inputDomTreeLevels,
256-
SILBuilder &inputB)
262+
SILBuilderContext &inputCtx)
257263
: asi(inputASI), dsi(nullptr), domInfo(inputDomInfo),
258-
domTreeLevels(inputDomTreeLevels), b(inputB) {
264+
domTreeLevels(inputDomTreeLevels), ctx(inputCtx) {
259265
// Scan the users in search of a deallocation instruction.
260266
for (auto *use : asi->getUses()) {
261267
if (auto *foundDealloc = dyn_cast<DeallocStackInst>(use->getUser())) {
@@ -336,7 +342,7 @@ StoreInst *StackAllocationPromoter::promoteAllocationInBlock(
336342
// If we are loading from the AllocStackInst and we already know the
337343
// content of the Alloca then use it.
338344
LLVM_DEBUG(llvm::dbgs() << "*** Promoting load: " << *li);
339-
replaceLoad(li, runningVal, asi);
345+
replaceLoad(li, runningVal, asi, ctx);
340346
++NumInstRemoved;
341347
} else if (li->getOperand() == asi &&
342348
li->getOwnershipQualifier() != LoadOwnershipQualifier::Copy) {
@@ -361,9 +367,10 @@ StoreInst *StackAllocationPromoter::promoteAllocationInBlock(
361367
// simplifies further processing.
362368
if (si->getOwnershipQualifier() == StoreOwnershipQualifier::Assign) {
363369
if (runningVal) {
364-
SILBuilderWithScope(si).createDestroyValue(si->getLoc(), runningVal);
370+
SILBuilderWithScope(si, ctx).createDestroyValue(si->getLoc(),
371+
runningVal);
365372
} else {
366-
SILBuilderWithScope localBuilder(si);
373+
SILBuilderWithScope localBuilder(si, ctx);
367374
auto *newLoad = localBuilder.createLoad(si->getLoc(), asi,
368375
LoadOwnershipQualifier::Take);
369376
localBuilder.createDestroyValue(si->getLoc(), newLoad);
@@ -395,14 +402,14 @@ StoreInst *StackAllocationPromoter::promoteAllocationInBlock(
395402
// promote this when we deal with hooking up phis.
396403
if (auto *dvai = dyn_cast<DebugValueAddrInst>(inst)) {
397404
if (dvai->getOperand() == asi && runningVal)
398-
promoteDebugValueAddr(dvai, runningVal, b);
405+
promoteDebugValueAddr(dvai, runningVal, ctx);
399406
continue;
400407
}
401408

402409
// Replace destroys with a release of the value.
403-
if (auto *DAI = dyn_cast<DestroyAddrInst>(inst)) {
404-
if (DAI->getOperand() == asi && runningVal) {
405-
replaceDestroy(DAI, runningVal);
410+
if (auto *dai = dyn_cast<DestroyAddrInst>(inst)) {
411+
if (dai->getOperand() == asi && runningVal) {
412+
replaceDestroy(dai, runningVal, ctx);
406413
}
407414
continue;
408415
}
@@ -536,7 +543,7 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSet &phiBlocks) {
536543
<< "*** Replacing " << *li << " with Def " << *def);
537544

538545
// Replace the load with the definition that we found.
539-
replaceLoad(li, def, asi);
546+
replaceLoad(li, def, asi, ctx);
540547
removedUser = true;
541548
++NumInstRemoved;
542549
}
@@ -552,15 +559,15 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSet &phiBlocks) {
552559
if (auto *dvai = dyn_cast<DebugValueAddrInst>(user)) {
553560
// Replace DebugValueAddr with DebugValue.
554561
SILValue def = getLiveInValue(phiBlocks, userBlock);
555-
promoteDebugValueAddr(dvai, def, b);
562+
promoteDebugValueAddr(dvai, def, ctx);
556563
++NumInstRemoved;
557564
continue;
558565
}
559566

560567
// Replace destroys with a release of the value.
561-
if (auto *DAI = dyn_cast<DestroyAddrInst>(user)) {
562-
SILValue Def = getLiveInValue(phiBlocks, userBlock);
563-
replaceDestroy(DAI, Def);
568+
if (auto *dai = dyn_cast<DestroyAddrInst>(user)) {
569+
SILValue def = getLiveInValue(phiBlocks, userBlock);
570+
replaceDestroy(dai, def, ctx);
564571
continue;
565572
}
566573
}
@@ -742,8 +749,9 @@ class MemoryToRegisters {
742749
/// Dominators.
743750
DominanceInfo *domInfo;
744751

745-
/// The builder used to create new instructions during register promotion.
746-
SILBuilder b;
752+
/// The builder context used when creating new instructions during register
753+
/// promotion.
754+
SILBuilderContext ctx;
747755

748756
/// Check if the AllocStackInst \p ASI is only written into.
749757
bool isWriteOnlyAllocation(AllocStackInst *asi);
@@ -763,7 +771,7 @@ class MemoryToRegisters {
763771
public:
764772
/// C'tor
765773
MemoryToRegisters(SILFunction &inputFunc, DominanceInfo *inputDomInfo)
766-
: f(inputFunc), domInfo(inputDomInfo), b(inputFunc) {}
774+
: f(inputFunc), domInfo(inputDomInfo), ctx(inputFunc.getModule()) {}
767775

768776
/// Promote memory to registers. Return True on change.
769777
bool run();
@@ -925,9 +933,9 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
925933
if (!runningVal) {
926934
// Loading without a previous store is only acceptable if the type is
927935
// Void (= empty tuple) or a tuple of Voids.
928-
runningVal = createValueForEmptyTuple(asi->getElementType(), inst);
936+
runningVal = createValueForEmptyTuple(asi->getElementType(), inst, ctx);
929937
}
930-
replaceLoad(cast<LoadInst>(inst), runningVal, asi);
938+
replaceLoad(cast<LoadInst>(inst), runningVal, asi, ctx);
931939
++NumInstRemoved;
932940
continue;
933941
}
@@ -938,7 +946,8 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
938946
if (si->getDest() == asi) {
939947
if (si->getOwnershipQualifier() == StoreOwnershipQualifier::Assign) {
940948
assert(runningVal);
941-
SILBuilderWithScope(si).createDestroyValue(si->getLoc(), runningVal);
949+
SILBuilderWithScope(si, ctx).createDestroyValue(si->getLoc(),
950+
runningVal);
942951
}
943952
runningVal = si->getSrc();
944953
inst->eraseFromParent();
@@ -951,7 +960,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
951960
if (auto *dvai = dyn_cast<DebugValueAddrInst>(inst)) {
952961
if (dvai->getOperand() == asi) {
953962
if (runningVal) {
954-
promoteDebugValueAddr(dvai, runningVal, b);
963+
promoteDebugValueAddr(dvai, runningVal, ctx);
955964
} else {
956965
// Drop debug_value_addr of uninitialized void values.
957966
assert(asi->getElementType().isVoid() &&
@@ -963,9 +972,9 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
963972
}
964973

965974
// Replace destroys with a release of the value.
966-
if (auto *DAI = dyn_cast<DestroyAddrInst>(inst)) {
967-
if (DAI->getOperand() == asi) {
968-
replaceDestroy(DAI, runningVal);
975+
if (auto *dai = dyn_cast<DestroyAddrInst>(inst)) {
976+
if (dai->getOperand() == asi) {
977+
replaceDestroy(dai, runningVal, ctx);
969978
}
970979
continue;
971980
}
@@ -1048,16 +1057,17 @@ bool MemoryToRegisters::promoteSingleAllocation(
10481057
// This can come up if the source contains a withUnsafePointer where
10491058
// the pointer escapes. It's illegal code but we should not crash.
10501059
// Re-insert a dealloc_stack so that the verifier is happy.
1051-
b.setInsertionPoint(std::next(alloc->getIterator()));
1052-
b.createDeallocStack(alloc->getLoc(), alloc);
1060+
auto *next = alloc->getNextInstruction();
1061+
SILBuilderWithScope b(next, ctx);
1062+
b.createDeallocStack(next->getLoc(), alloc);
10531063
}
10541064
return true;
10551065
}
10561066

10571067
LLVM_DEBUG(llvm::dbgs() << "*** Need to insert BB arguments for " << *alloc);
10581068

10591069
// Promote this allocation.
1060-
StackAllocationPromoter(alloc, domInfo, domTreeLevels, b).run();
1070+
StackAllocationPromoter(alloc, domInfo, domTreeLevels, ctx).run();
10611071

10621072
// Make sure that all of the allocations were promoted into registers.
10631073
assert(isWriteOnlyAllocation(alloc) && "Non-write uses left behind");
@@ -1106,7 +1116,6 @@ bool MemoryToRegisters::run() {
11061116
namespace {
11071117

11081118
class SILMem2Reg : public SILFunctionTransform {
1109-
11101119
void run() override {
11111120
SILFunction *f = getFunction();
11121121

test/Concurrency/Runtime/async_task_yield.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// REQUIRES: executable_test
44
// REQUIRES: concurrency
55

6-
// REQUIRES: rdar76274257
7-
86
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
97
protocol Go: Actor {
108
func go(times: Int) async -> Int
@@ -45,5 +43,6 @@ func yielding() async {
4543
static func main() async {
4644
await yielding()
4745
// TODO: No idea for a good test for this... Open to ideas?
46+
// CHECK: Two @ 100
4847
}
4948
}

0 commit comments

Comments
 (0)