Skip to content

Commit

Permalink
Converted all FModuleOp passes to FModuleLike passes
Browse files Browse the repository at this point in the history
  • Loading branch information
dobios committed Jul 27, 2024
1 parent 8de018a commit 852a624
Show file tree
Hide file tree
Showing 11 changed files with 380 additions and 275 deletions.
4 changes: 3 additions & 1 deletion include/circt/Dialect/FIRRTL/FIRRTLIntrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ class IntrinsicLowerings {
}

/// Lowers all intrinsics in a module. Returns number converted or failure.
FailureOr<size_t> lower(FModuleOp mod, bool allowUnknownIntrinsics = false);
template <typename ModuleLikeOp>
FailureOr<size_t> lower(ModuleLikeOp mod,
bool allowUnknownIntrinsics = false);

private:
template <typename T>
Expand Down
19 changes: 18 additions & 1 deletion lib/Dialect/FIRRTL/FIRRTLIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,24 @@ class IntrinsicOpConversion final
// IntrinsicLowerings
//===----------------------------------------------------------------------===//

FailureOr<size_t> IntrinsicLowerings::lower(FModuleOp mod,
// Explicit instanciation of things to avoid implementing template in header
template FailureOr<size_t>
IntrinsicLowerings::lower(FModuleOp mod, bool allowUnknownIntrinsics);
template FailureOr<size_t>
IntrinsicLowerings::lower(FExtModuleOp mod, bool allowUnknownIntrinsics);
template FailureOr<size_t>
IntrinsicLowerings::lower(FIntModuleOp mod, bool allowUnknownIntrinsics);
template FailureOr<size_t>
IntrinsicLowerings::lower(FMemModuleOp mod, bool allowUnknownIntrinsics);
template FailureOr<size_t>
IntrinsicLowerings::lower(ClassOp mod, bool allowUnknownIntrinsics);
template FailureOr<size_t>
IntrinsicLowerings::lower(ExtClassOp mod, bool allowUnknownIntrinsics);
template FailureOr<size_t>
IntrinsicLowerings::lower(FormalOp mod, bool allowUnknownIntrinsics);

template <typename ModuleLikeOp>
FailureOr<size_t> IntrinsicLowerings::lower(ModuleLikeOp mod,
bool allowUnknownIntrinsics) {

ConversionTarget target(*context);
Expand Down
24 changes: 18 additions & 6 deletions lib/Dialect/FIRRTL/Transforms/FlattenMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ using namespace firrtl;
namespace {
struct FlattenMemoryPass
: public circt::firrtl::impl::FlattenMemoryBase<FlattenMemoryPass> {
/// This pass flattens the aggregate data of memory into a UInt, and inserts
/// appropriate bitcasts to access the data.
void runOnOperation() override {
LLVM_DEBUG(llvm::dbgs() << "\n Running lower memory on module:"
<< getOperation().getName());

template <typename Op>
void runOnOp(Op op) {
SmallVector<Operation *> opsToErase;
auto hasSubAnno = [&](MemOp op) -> bool {
for (size_t portIdx = 0, e = op.getNumResults(); portIdx < e; ++portIdx)
Expand All @@ -50,7 +48,8 @@ struct FlattenMemoryPass

return false;
};
getOperation().getBodyBlock()->walk([&](MemOp memOp) {
Block *body = op.getBodyBlock();
body->walk([&](MemOp memOp) {
LLVM_DEBUG(llvm::dbgs() << "\n Memory:" << memOp);
// The vector of leaf elements type after flattening the data.
SmallVector<IntType> flatMemType;
Expand Down Expand Up @@ -196,6 +195,19 @@ struct FlattenMemoryPass
return;
});
}
/// This pass flattens the aggregate data of memory into a UInt, and inserts
/// appropriate bitcasts to access the data.
void runOnOperation() override {
LLVM_DEBUG(llvm::dbgs() << "\n Running lower memory on module:"
<< getOperation().getName());

TypeSwitch<Operation *>(&(*getOperation()))
.Case<FModuleOp, ClassOp, FormalOp>([&](auto op) { runOnOp(op); })
// All other ops are ignored -- particularly ops that don't implement
// the `getBodyBlock()` method. We don't want an error here because the
// pass wasn't designed to run on those ops.
.Default([&](auto) {});
}

private:
// Convert an aggregate type into a flat list of fields.
Expand Down
30 changes: 23 additions & 7 deletions lib/Dialect/FIRRTL/Transforms/LowerCHIRRTL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ struct LowerCHIRRTLPass
Value getConst(unsigned c) {
auto &value = constCache[c];
if (!value) {
auto module = getOperation();
auto builder = OpBuilder::atBlockBegin(module.getBodyBlock());
auto builder = OpBuilder::atBlockBegin(body);
auto u1Type = UIntType::get(builder.getContext(), /*width*/ 1);
value = builder.create<ConstantOp>(module.getLoc(), u1Type, APInt(1, c));
value = builder.create<ConstantOp>(getOperation().getLoc(), u1Type,
APInt(1, c));
}
return value;
}
Expand All @@ -96,6 +96,9 @@ struct LowerCHIRRTLPass

void runOnOperation() override;

template <typename Op>
void runOnOp(Op op);

/// Cached constants.
DenseMap<unsigned, Value> constCache;
DenseMap<Type, Value> invalidCache;
Expand Down Expand Up @@ -124,6 +127,9 @@ struct LowerCHIRRTLPass
Value mode;
};
DenseMap<Value, WDataInfo> wdataValues;

// Internally used data about the operations (avoids template issues)
Block *body;
};
} // end anonymous namespace

Expand Down Expand Up @@ -159,7 +165,7 @@ void LowerCHIRRTLPass::emitInvalid(ImplicitLocOpBuilder &builder, Value value) {
auto type = value.getType();
auto &invalid = invalidCache[type];
if (!invalid) {
auto builder = OpBuilder::atBlockBegin(getOperation().getBodyBlock());
auto builder = OpBuilder::atBlockBegin(body);
invalid = builder.create<InvalidValueOp>(getOperation().getLoc(), type);
}
emitConnect(builder, value, invalid);
Expand Down Expand Up @@ -650,12 +656,13 @@ void LowerCHIRRTLPass::visitUnhandledOp(Operation *op) {
}
}

void LowerCHIRRTLPass::runOnOperation() {
template <typename Op>
void LowerCHIRRTLPass::runOnOp(Op op) {
// Walk the entire body of the module and dispatch the visitor on each
// function. This will replace all CHIRRTL memories and ports, and update all
// uses.
getOperation().getBodyBlock()->walk(
[&](Operation *op) { dispatchCHIRRTLVisitor(op); });
body = op.getBodyBlock();
body->walk([&](Operation *op) { dispatchCHIRRTLVisitor(op); });

// If there are no operations to delete, then we didn't find any CHIRRTL
// memories.
Expand All @@ -670,6 +677,15 @@ void LowerCHIRRTLPass::runOnOperation() {
clear();
}

void LowerCHIRRTLPass::runOnOperation() {
TypeSwitch<Operation *>(&(*getOperation()))
.Case<FModuleOp, ClassOp, FormalOp>([&](auto op) { runOnOp(op); })
// All other ops are ignored -- particularly ops that don't implement
// the `getBodyBlock()` method. We don't want an error here because the
// pass wasn't designed to run on those ops.
.Default([&](auto) {});
}

std::unique_ptr<mlir::Pass> circt::firrtl::createLowerCHIRRTLPass() {
return std::make_unique<LowerCHIRRTLPass>();
}
27 changes: 20 additions & 7 deletions lib/Dialect/FIRRTL/Transforms/LowerIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ using namespace firrtl;
namespace {
struct LowerIntrinsicsPass
: public circt::firrtl::impl::LowerIntrinsicsBase<LowerIntrinsicsPass> {

template <typename Op>
void runOnOp(Op op) {
auto result = lowering->lower(op);
if (failed(result))
return signalPassFailure();

numConverted += *result;

if (*result == 0)
markAllAnalysesPreserved();
}

LogicalResult initialize(MLIRContext *context) override;
void runOnOperation() override;

Expand All @@ -53,14 +66,14 @@ LogicalResult LowerIntrinsicsPass::initialize(MLIRContext *context) {

// This is the main entrypoint for the lowering pass.
void LowerIntrinsicsPass::runOnOperation() {
auto result = lowering->lower(getOperation());
if (failed(result))
return signalPassFailure();

numConverted += *result;

if (*result == 0)
markAllAnalysesPreserved();
TypeSwitch<Operation *>(&(*getOperation()))
.Case<FModuleOp, FExtModuleOp, FIntModuleOp, FMemModuleOp, ClassOp,
ExtClassOp, FormalOp>([&](auto op) { runOnOp(op); })
// All other ops are ignored -- particularly ops that don't implement
// the `getBodyBlock()` method. We don't want an error here because the
// pass wasn't designed to run on those ops.
.Default([&](auto) {});
}

/// This is the pass constructor.
Expand Down
41 changes: 25 additions & 16 deletions lib/Dialect/FIRRTL/Transforms/MaterializeDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,39 @@ namespace {
struct MaterializeDebugInfoPass
: public circt::firrtl::impl::MaterializeDebugInfoBase<
MaterializeDebugInfoPass> {
template <typename Op>
void runOnOp(Op module) {
auto builder = OpBuilder::atBlockBegin(module.getBodyBlock());

// Create DI variables for each port.
for (const auto &[port, value] :
llvm::zip(module.getPorts(), module.getArguments())) {
materializeVariable(builder, port.name, value);
}

// Create DI variables for each declaration in the module body.
module.walk([&](Operation *op) {
TypeSwitch<Operation *>(op).Case<WireOp, NodeOp, RegOp, RegResetOp>(
[&](auto op) {
builder.setInsertionPointAfter(op);
materializeVariable(builder, op.getNameAttr(), op.getResult());
});
});
}
void runOnOperation() override;
void materializeVariable(OpBuilder &builder, StringAttr name, Value value);
Value convertToDebugAggregates(OpBuilder &builder, Value value);
};
} // namespace

void MaterializeDebugInfoPass::runOnOperation() {
auto module = getOperation();
auto builder = OpBuilder::atBlockBegin(module.getBodyBlock());

// Create DI variables for each port.
for (const auto &[port, value] :
llvm::zip(module.getPorts(), module.getArguments())) {
materializeVariable(builder, port.name, value);
}

// Create DI variables for each declaration in the module body.
module.walk([&](Operation *op) {
TypeSwitch<Operation *>(op).Case<WireOp, NodeOp, RegOp, RegResetOp>(
[&](auto op) {
builder.setInsertionPointAfter(op);
materializeVariable(builder, op.getNameAttr(), op.getResult());
});
});
TypeSwitch<Operation *>(&(*getOperation()))
.Case<FModuleOp, ClassOp, FormalOp>([&](auto op) { runOnOp(op); })
// All other ops are ignored -- particularly ops that don't implement
// the `getBodyBlock()` method. We don't want an error here because the
// pass wasn't designed to run on those ops.
.Default([&](auto) {});
}

/// Materialize debug variable ops for a value.
Expand Down
Loading

0 comments on commit 852a624

Please sign in to comment.