Skip to content

Commit 9d61e7a

Browse files
author
Joshua Turcotti
authored
Merge pull request #67302 from JTurcotti/with-memutils
Increase robustness of SendNonSendable SIL pass with better address tracking
2 parents 284639c + 1d13039 commit 9d61e7a

File tree

3 files changed

+333
-115
lines changed

3 files changed

+333
-115
lines changed

include/swift/SILOptimizer/Utils/PartitionUtils.h

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,37 @@ class PartitionOp {
8686
}
8787

8888
void dump() const LLVM_ATTRIBUTE_USED {
89+
raw_ostream &os = llvm::errs();
8990
switch (OpKind) {
9091
case PartitionOpKind::Assign:
91-
llvm::dbgs() << "assign %" << OpArgs[0] << " = %" << OpArgs[1] << "\n";
92+
os.changeColor(llvm::raw_ostream::CYAN, true);
93+
os << "assign";
94+
os.resetColor();
95+
os << " %%" << OpArgs[0] << " = %%" << OpArgs[1] << "\n";
9296
break;
9397
case PartitionOpKind::AssignFresh:
94-
llvm::dbgs() << "assign_fresh %" << OpArgs[0] << "\n";
98+
os.changeColor(llvm::raw_ostream::GREEN, true);
99+
os << "assign_fresh";
100+
os.resetColor();
101+
os << " %%" << OpArgs[0] << "\n";
95102
break;
96103
case PartitionOpKind::Consume:
97-
llvm::dbgs() << "consume %" << OpArgs[0] << "\n";
104+
os.changeColor(llvm::raw_ostream::RED, true);
105+
os << "consume";
106+
os.resetColor();
107+
os << " %%" << OpArgs[0] << "\n";
98108
break;
99109
case PartitionOpKind::Merge:
100-
llvm::dbgs() << "merge %" << OpArgs[0] << " with %" << OpArgs[1] << "\n";
110+
os.changeColor(llvm::raw_ostream::BLUE, true);
111+
os << "merge";
112+
os.resetColor();
113+
os << " %%" << OpArgs[0] << " with %%" << OpArgs[1] << "\n";
101114
break;
102115
case PartitionOpKind::Require:
103-
llvm::dbgs() << "require %" << OpArgs[0] << "\n";
116+
os.changeColor(llvm::raw_ostream::YELLOW, true);
117+
os << "require";
118+
os.resetColor();
119+
os << " %%" << OpArgs[0] << "\n";
104120
break;
105121
}
106122
}
@@ -160,10 +176,10 @@ class Partition {
160176
if (label < 0) continue;
161177

162178
// this label should not exceed fresh_label
163-
if (label >= fresh_label) return fail(i, 0);
179+
if ((unsigned) label >= fresh_label) return fail(i, 0);
164180

165181
// the label of a region should be at most as large as each index in it
166-
if (i < label) return fail(i, 1);
182+
if ((unsigned) label > i) return fail(i, 1);
167183

168184
// each region label should refer to an index in that region
169185
if (labels[label] != label) return fail(i, 2);
@@ -299,10 +315,21 @@ class Partition {
299315
PartitionOp op,
300316
llvm::function_ref<void(const PartitionOp&, unsigned)>
301317
handleFailure = [](const PartitionOp&, unsigned) {},
302-
std::vector<unsigned> nonconsumables = {},
318+
319+
std::vector<unsigned>
320+
nonconsumables = {},
321+
303322
llvm::function_ref<void(const PartitionOp&, unsigned)>
304-
handleConsumeNonConsumable = [](const PartitionOp&, unsigned) {}
323+
handleConsumeNonConsumable = [](const PartitionOp&, unsigned) {},
324+
325+
bool reviveAfterFailure = false
305326
) {
327+
auto handleFailureAndRevive =
328+
[&](const PartitionOp& partitionOp, unsigned consumedVal) {
329+
if (reviveAfterFailure)
330+
horizontalUpdate(labels, consumedVal, fresh_label++);
331+
handleFailure(partitionOp, consumedVal);
332+
};
306333
switch (op.OpKind) {
307334
case PartitionOpKind::Assign:
308335
assert(op.OpArgs.size() == 2 &&
@@ -311,7 +338,7 @@ class Partition {
311338
"Assign PartitionOp's source argument should be already tracked");
312339
// if assigning to a missing region, handle the failure
313340
if (labels[op.OpArgs[1]] < 0)
314-
handleFailure(op, op.OpArgs[1]);
341+
handleFailureAndRevive(op, op.OpArgs[1]);
315342

316343
labels[op.OpArgs[0]] = labels[op.OpArgs[1]];
317344

@@ -322,8 +349,6 @@ class Partition {
322349
case PartitionOpKind::AssignFresh:
323350
assert(op.OpArgs.size() == 1 &&
324351
"AssignFresh PartitionOp should be passed 1 argument");
325-
assert(!labels.count(op.OpArgs[0]) &&
326-
"AssignFresh PartitionOp's argument should NOT already be tracked");
327352

328353
// map index op.OpArgs[0] to a fresh label
329354
labels[op.OpArgs[0]] = fresh_label++;
@@ -337,7 +362,7 @@ class Partition {
337362

338363
// if attempting to consume a consumed region, handle the failure
339364
if (labels[op.OpArgs[0]] < 0)
340-
handleFailure(op, op.OpArgs[0]);
365+
handleFailureAndRevive(op, op.OpArgs[0]);
341366

342367
// mark region as consumed
343368
horizontalUpdate(labels, op.OpArgs[0], -1);
@@ -360,11 +385,12 @@ class Partition {
360385
"Merge PartitionOp should be passed 2 arguments");
361386
assert(labels.count(op.OpArgs[0]) && labels.count(op.OpArgs[1]) &&
362387
"Merge PartitionOp's arguments should already be tracked");
388+
363389
// if attempting to merge a consumed region, handle the failure
364390
if (labels[op.OpArgs[0]] < 0)
365-
handleFailure(op, op.OpArgs[0]);
391+
handleFailureAndRevive(op, op.OpArgs[0]);
366392
if (labels[op.OpArgs[1]] < 0)
367-
handleFailure(op, op.OpArgs[1]);
393+
handleFailureAndRevive(op, op.OpArgs[1]);
368394

369395
merge(op.OpArgs[0], op.OpArgs[1]);
370396
break;
@@ -374,7 +400,7 @@ class Partition {
374400
assert(labels.count(op.OpArgs[0]) &&
375401
"Require PartitionOp's argument should already be tracked");
376402
if (labels[op.OpArgs[0]] < 0)
377-
handleFailure(op, op.OpArgs[0]);
403+
handleFailureAndRevive(op, op.OpArgs[0]);
378404
}
379405

380406
assert(is_canonical_correct());
@@ -406,9 +432,7 @@ class Partition {
406432
}
407433
llvm::dbgs() << (label < 0 ? "}" : ")");
408434
}
409-
llvm::dbgs() << "] | ";
410-
411-
dump_labels();
435+
llvm::dbgs() << "]";
412436
}
413437
};
414438
}

0 commit comments

Comments
 (0)