Skip to content

Commit

Permalink
[LowerTypes] A few NFC cleanups.
Browse files Browse the repository at this point in the history
- Use a vector instead of a deque, deque is a lot less efficient and
  isn't needed in this case.
- Use early exit to reduce nesting in a function.
- Rotate a loop to avoid duplicating some code.
  • Loading branch information
lattner committed Nov 1, 2021
1 parent 11431bf commit 50b3d8f
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions lib/Dialect/FIRRTL/Transforms/LowerTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "mlir/IR/ImplicitLocOpBuilder.h"
#include "mlir/IR/Threading.h"
#include "llvm/ADT/APSInt.h"
#include <deque>

using namespace circt;
using namespace firrtl;
Expand Down Expand Up @@ -335,42 +334,39 @@ Value TypeLoweringVisitor::getSubWhatever(Value val, size_t index) {
bool TypeLoweringVisitor::processSAPath(Operation *op) {
// Does this LHS have a subaccessop?
SmallVector<Operation *> writePath = getSAWritePath(op);
if (!writePath.empty()) {
lowerSAWritePath(op, writePath);
// Unhook the writePath from the connect. This isn't the right type, but we
// are deleting the op anyway.
op->eraseOperands(0, 2);
// See how far up the tree we can delete things.
for (size_t i = 0; i < writePath.size(); ++i) {
if (writePath[i]->use_empty()) {
writePath[i]->erase();
} else {
break;
}
if (writePath.empty())
return false;

lowerSAWritePath(op, writePath);
// Unhook the writePath from the connect. This isn't the right type, but we
// are deleting the op anyway.
op->eraseOperands(0, 2);
// See how far up the tree we can delete things.
for (size_t i = 0; i < writePath.size(); ++i) {
if (writePath[i]->use_empty()) {
writePath[i]->erase();
} else {
break;
}
opsToRemove.push_back(op);
return true;
}
return false;
opsToRemove.push_back(op);
return true;
}

void TypeLoweringVisitor::lowerBlock(Block *block) {
// Lower the operations.
for (auto &iop : llvm::reverse(*block)) {
// We erase old ops eagerly so we don't have dangling uses we've already
// Lower the operations bottom up.
for (auto it = block->rbegin(), e = block->rend(); it != e;) {
auto &iop = *it;
builder->setInsertionPoint(&iop);
builder->setLoc(iop.getLoc());
dispatchVisitor(&iop);
++it;
// Erase old ops eagerly so we don't have dangling uses we've already
// lowered.
for (auto *op : opsToRemove)
op->erase();
opsToRemove.clear();

builder->setInsertionPoint(&iop);
builder->setLoc(iop.getLoc());
dispatchVisitor(&iop);
}

for (auto *op : opsToRemove)
op->erase();
opsToRemove.clear();
}

void TypeLoweringVisitor::lowerProducer(
Expand Down Expand Up @@ -1218,7 +1214,7 @@ struct LowerTypesPass : public LowerFIRRTLTypesBase<LowerTypesPass> {

// This is the main entrypoint for the lowering pass.
void LowerTypesPass::runOnOperation() {
std::deque<Operation *> ops;
std::vector<Operation *> ops;
llvm::for_each(getOperation().getBody()->getOperations(),
[&](Operation &op) { ops.push_back(&op); });

Expand Down

0 comments on commit 50b3d8f

Please sign in to comment.