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

[LowerLayers][FIRRTL] Add the ability to sink ops by cloning #8308

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
62 changes: 51 additions & 11 deletions lib/Dialect/FIRRTL/Transforms/AdvancedLayerSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ static bool isAncestor(Block *block, Block *other) {
return block->getParent()->isProperAncestor(other->getParent());
}

//===----------------------------------------------------------------------===//
// Clone-ability.
//===----------------------------------------------------------------------===//

static bool cloneable(Operation *op) {
return isa<ConstantOp, SpecialConstantOp, SubfieldOp, SubindexOp>(op);
}

//===----------------------------------------------------------------------===//
// Effectfulness Analysis.
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -357,6 +365,10 @@ class ModuleLayerSink {
void moveLayersToBack(Operation *op);
void moveLayersToBack(Block *block);

void erase(Operation *op);
void sinkOpByCloning(Operation *op);
void sinkOpByMoving(Operation *op, Block *dest);

FModuleOp moduleOp;
const EffectInfo &effectInfo;
bool changed = false;
Expand Down Expand Up @@ -403,22 +415,50 @@ void ModuleLayerSink::moveLayersToBack(Block *block) {
}
}

void ModuleLayerSink::erase(Operation *op) {
op->erase();
changed = true;
}

void ModuleLayerSink::sinkOpByCloning(Operation *op) {
// Copy the op down to each block where there is a user.
// Use a cache to ensure we don't create more than one copy per block.
DenseMap<Block *, Operation *> cache;
for (unsigned i = 0, e = op->getNumResults(); i < e; ++i) {
for (auto &use : llvm::make_early_inc_range(op->getResult(i).getUses())) {
auto *dest = use.getOwner()->getBlock();
if (dest != op->getBlock()) {
auto &clone = cache[dest];
if (!clone)
clone = OpBuilder::atBlockBegin(dest).clone(*op);
use.set(clone->getResult(i));
changed = true;
}
}
}

// If there are no remaining uses of the original op, erase it.
if (op->use_empty())
erase(op);
}

void ModuleLayerSink::sinkOpByMoving(Operation *op, Block *dest) {
if (dest != op->getBlock()) {
op->moveBefore(dest, dest->begin());
changed = true;
}
}

bool ModuleLayerSink::operator()() {
moveLayersToBack(moduleOp.getBodyBlock());
DemandInfo demandInfo(effectInfo, moduleOp);
walkBwd(moduleOp.getBodyBlock(), [&](Operation *op) {
auto demand = demandInfo.getDemandFor(op);
if (!demand) {
op->erase();
changed = true;
return;
}

if (demand == op->getBlock())
return;

op->moveBefore(demand.block, demand.block->begin());
changed = true;
if (!demand)
return erase(op);
if (cloneable(op))
return sinkOpByCloning(op);
sinkOpByMoving(op, demand.block);
});

return changed;
Expand Down
Loading