Skip to content

[NFC] OSSACanonicalizeOwned: Simplify Def definition. #79205

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 2 commits into from
Feb 7, 2025
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
66 changes: 11 additions & 55 deletions include/swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
#define SWIFT_SILOPTIMIZER_UTILS_CANONICALOSSALIFETIME_H

#include "swift/Basic/SmallPtrSetVector.h"
#include "swift/Basic/TaggedUnion.h"
#include "swift/SIL/PrunedLiveness.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
Expand Down Expand Up @@ -279,68 +278,25 @@ class CanonicalizeOSSALifetime final {
/// outside the pruned liveness at the time it is discovered.
llvm::SmallPtrSet<DebugValueInst *, 8> debugValues;

class Def {
struct Root {
SILValue value;
};
struct Copy {
CopyValueInst *cvi;
};
struct BorrowedFrom {
BorrowedFromInst *bfi;
};
struct Reborrow {
SILArgument *argument;
};
using Payload = TaggedUnion<Root, Copy, BorrowedFrom, Reborrow>;
Payload payload;
Def(Payload payload) : payload(payload) {}

public:
enum class Kind {
struct Def {
enum Kind {
Root,
Copy,
BorrowedFrom,
Reborrow,
};
Kind getKind() const {
if (payload.isa<Root>()) {
return Kind::Root;
}
if (payload.isa<Copy>()) {
return Kind::Copy;
}
if (payload.isa<BorrowedFrom>()) {
return Kind::BorrowedFrom;
}
assert(payload.isa<Reborrow>());
return Kind::Reborrow;
}
operator Kind() const { return getKind(); }
bool operator==(Def rhs) const {
return getKind() == rhs.getKind() && getValue() == rhs.getValue();
}
static Def root(SILValue value) { return {Root{value}}; }
static Def copy(CopyValueInst *cvi) { return {Copy{cvi}}; }
const Kind kind;
const SILValue value;
static Def root(SILValue value) { return {Root, value}; }
static Def copy(CopyValueInst *cvi) { return {Copy, cvi}; }
static Def borrowedFrom(BorrowedFromInst *bfi) {
return {BorrowedFrom{bfi}};
}
static Def reborrow(SILArgument *argument) { return {Reborrow{argument}}; }
SILValue getValue() const {
switch (*this) {
case Kind::Root:
return payload.get<Root>().value;
case Kind::Copy:
return payload.get<Copy>().cvi;
case Kind::BorrowedFrom:
return payload.get<BorrowedFrom>().bfi;
case Kind::Reborrow:
return payload.get<Reborrow>().argument;
}
llvm_unreachable("covered switch");
return {BorrowedFrom, bfi};
}
static Def reborrow(SILArgument *argument) { return {Reborrow, argument}; }

private:
Def(Kind kind, SILValue value) : kind(kind), value(value) {}
};
friend llvm::DenseMapInfo<Def>;

/// The defs derived from currentDef whose uses are added to liveness.
SmallVector<Def, 8> discoveredDefs;
Expand Down
32 changes: 16 additions & 16 deletions lib/SILOptimizer/Utils/CanonicalizeOSSALifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ bool CanonicalizeOSSALifetime::computeCanonicalLiveness() {
SmallVector<unsigned, 8> indexWorklist;
ValueSet visitedDefs(getCurrentDef()->getFunction());
auto addDefToWorklist = [&](Def def) {
if (!visitedDefs.insert(def.getValue()))
if (!visitedDefs.insert(def.value))
return;
discoveredDefs.push_back(def);
indexWorklist.push_back(discoveredDefs.size() - 1);
Expand All @@ -154,25 +154,25 @@ bool CanonicalizeOSSALifetime::computeCanonicalLiveness() {
while (!indexWorklist.empty()) {
auto index = indexWorklist.pop_back_val();
auto def = discoveredDefs[index];
auto value = def.getValue();
auto value = def.value;
LLVM_DEBUG(llvm::dbgs() << " Uses of value:\n";
value->print(llvm::dbgs()));

for (Operand *use : value->getUses()) {
LLVM_DEBUG(llvm::dbgs() << " Use:\n";
use->getUser()->print(llvm::dbgs()));

auto *user = use->getUser();
// Recurse through copies.
if (auto *copy = dyn_cast<CopyValueInst>(user)) {
// Don't recurse through copies of borrowed-froms or reborrows.
switch (def) {
case Def::Kind::Root:
case Def::Kind::Copy:
switch (def.kind) {
case Def::Root:
case Def::Copy:
addDefToWorklist(Def::copy(copy));
break;
case Def::Kind::Reborrow:
case Def::Kind::BorrowedFrom:
case Def::Reborrow:
case Def::BorrowedFrom:
break;
}
continue;
Expand Down Expand Up @@ -1251,15 +1251,15 @@ void CanonicalizeOSSALifetime::rewriteCopies(

// Perform a def-use traversal, visiting each use operand.
for (auto def : discoveredDefs) {
switch (def) {
case Def::Kind::BorrowedFrom:
case Def::Kind::Reborrow:
switch (def.kind) {
case Def::BorrowedFrom:
case Def::Reborrow:
// Direct uses of these defs never need to be rewritten. Being guaranteed
// values, none of their direct uses consume an owned value.
assert(def.getValue()->getOwnershipKind() == OwnershipKind::Guaranteed);
assert(def.value->getOwnershipKind() == OwnershipKind::Guaranteed);
break;
case Def::Kind::Root: {
SILValue value = def.getValue();
case Def::Root: {
SILValue value = def.value;
for (auto useIter = value->use_begin(), endIter = value->use_end();
useIter != endIter;) {
Operand *use = *useIter++;
Expand All @@ -1269,8 +1269,8 @@ void CanonicalizeOSSALifetime::rewriteCopies(
}
break;
}
case Def::Kind::Copy: {
SILValue value = def.getValue();
case Def::Copy: {
SILValue value = def.value;
CopyValueInst *srcCopy = cast<CopyValueInst>(value);
// Recurse through copies while replacing their uses.
Operand *reusedCopyOp = nullptr;
Expand Down