CIRCT 21.0.0git
Loading...
Searching...
No Matches
LowerUniqueLabelsPass.cpp
Go to the documentation of this file.
1//===- LowerUniqueLabelsPass.cpp - RTG LowerUniqueLabelsPass impl. --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass lowers label_unique_decl operations to label_decl operations by
10// creating a unique label string based on all the existing unique and
11// non-unique label declarations in the module.
12//
13//===----------------------------------------------------------------------===//
14
18#include "mlir/IR/PatternMatch.h"
19
20namespace circt {
21namespace rtg {
22#define GEN_PASS_DEF_LOWERUNIQUELABELSPASS
23#include "circt/Dialect/RTG/Transforms/RTGPasses.h.inc"
24} // namespace rtg
25} // namespace circt
26
27using namespace mlir;
28using namespace circt;
29using namespace circt::rtg;
30
31//===----------------------------------------------------------------------===//
32// Lower Unique Labels Pass
33//===----------------------------------------------------------------------===//
34
35namespace {
36struct LowerUniqueLabelsPass
37 : public rtg::impl::LowerUniqueLabelsPassBase<LowerUniqueLabelsPass> {
38 using Base::Base;
39
40 void runOnOperation() override;
41};
42} // namespace
43
44void LowerUniqueLabelsPass::runOnOperation() {
45 auto moduleOp = getOperation();
46 Namespace labelNames;
47
48 // Collect all the label names in a first iteration.
49 moduleOp.walk([&](Operation *op) {
50 if (auto labelDecl = dyn_cast<LabelDeclOp>(op))
51 labelNames.add(labelDecl.getFormatString());
52 else if (auto labelDecl = dyn_cast<LabelUniqueDeclOp>(op))
53 labelNames.add(labelDecl.getFormatString());
54 });
55
56 // Lower the unique labels in a second iteration.
57 moduleOp.walk([&](LabelUniqueDeclOp op) {
58 // Convert 'rtg.label_unique_decl' to 'rtg.label_decl' by choosing a unique
59 // name based on the set of names we collected during elaboration.
60 IRRewriter rewriter(op);
61 auto newName = labelNames.newName(op.getFormatString());
62 rewriter.replaceOpWithNewOp<LabelDeclOp>(op, newName, ValueRange());
63 ++numLabelsLowered;
64 });
65}
A namespace that is used to store existing names and generate new names in some scope within the IR.
Definition Namespace.h:30
void add(mlir::ModuleOp module)
Definition Namespace.h:48
StringRef newName(const Twine &name)
Return a unique name, derived from the input name, and add the new name to the internal namespace.
Definition Namespace.h:87
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition rtg.py:1