18#include "mlir/IR/PatternMatch.h"
22#define GEN_PASS_DEF_LOWERUNIQUELABELSPASS
23#include "circt/Dialect/RTG/Transforms/RTGPasses.h.inc"
36struct LowerUniqueLabelsPass
37 :
public rtg::impl::LowerUniqueLabelsPassBase<LowerUniqueLabelsPass> {
40 void runOnOperation()
override;
45 if (
auto constOp = op.getNamePrefix().getDefiningOp<ConstantOp>())
46 return dyn_cast<StringAttr>(constOp.getValue());
48 op->emitError(
"label_unique_decl name prefix must be a constant string");
52void LowerUniqueLabelsPass::runOnOperation() {
56 auto result = getOperation()->walk([&](Operation *op) -> WalkResult {
57 if (isa<StringToLabelOp>(op))
59 "string_to_label operations must be elaborated before "
60 "lowering unique labels");
62 if (
auto constOp = dyn_cast<ConstantOp>(op)) {
63 if (
auto labelAttr = dyn_cast<LabelAttr>(constOp.getValue()))
64 labelNames.
add(labelAttr.getName());
65 }
else if (
auto labelDecl = dyn_cast<LabelUniqueDeclOp>(op)) {
68 return WalkResult::interrupt();
72 return WalkResult::advance();
75 if (result.wasInterrupted())
76 return signalPassFailure();
79 getOperation()->walk([&](LabelUniqueDeclOp op) {
83 IRRewriter rewriter(op);
85 rewriter.replaceOpWithNewOp<ConstantOp>(
86 op, LabelAttr::get(rewriter.getContext(), newName));
static StringAttr getLabelName(LabelUniqueDeclOp op)
A namespace that is used to store existing names and generate new names in some scope within the IR.
void add(mlir::ModuleOp module)
StringRef newName(const Twine &name)
Return a unique name, derived from the input name, and add the new name to the internal namespace.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.