Loading [MathJax]/extensions/tex2jax.js
CIRCT 22.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DropName.cpp
Go to the documentation of this file.
1//===- DropName.cpp - Drop Names -----------------------------------------===//
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 file defines the DropName pass.
10//
11//===----------------------------------------------------------------------===//
12
16#include "mlir/Pass/Pass.h"
17
18namespace circt {
19namespace firrtl {
20#define GEN_PASS_DEF_DROPNAME
21#include "circt/Dialect/FIRRTL/Passes.h.inc"
22} // namespace firrtl
23} // namespace circt
24
25using namespace circt;
26using namespace firrtl;
27
28namespace {
29struct DropNamesPass : public circt::firrtl::impl::DropNameBase<DropNamesPass> {
30 using Base::Base;
31
32 enum ModAction { Drop, Keep, Demote };
33
34 void runOnOperation() override {
35 size_t namesDropped = 0;
36 size_t namesChanged = 0;
37 if (preserveMode == PreserveValues::None) {
38 // Drop all names.
39 dropNamesIf(namesChanged, namesDropped, [](FNamableOp op) {
40 if (isUselessName(op.getName()))
41 return ModAction::Drop;
42 return ModAction::Demote;
43 });
44 } else if (preserveMode == PreserveValues::Strip) {
45 // Strip all names.
46 dropNamesIf(namesChanged, namesDropped,
47 [](FNamableOp op) { return ModAction::Drop; });
48 } else if (preserveMode == PreserveValues::Named) {
49 // Drop the name if it isn't considered meaningful.
50 dropNamesIf(namesChanged, namesDropped, [](FNamableOp op) {
51 auto name = op.getName();
52 if (isUselessName(name))
53 return ModAction::Drop;
54 if (name.starts_with("_"))
55 return ModAction::Demote;
56 return ModAction::Keep;
57 });
58 } else if (preserveMode == PreserveValues::All) {
59 // Drop the name if it isn't considered meaningful.
60 dropNamesIf(namesChanged, namesDropped, [](FNamableOp op) {
61 if (isUselessName(op.getName()))
62 return ModAction::Demote;
63 return ModAction::Keep;
64 });
65 }
66 numNamesConverted += namesChanged;
67 numNamesDropped += namesDropped;
68 }
69
70private:
71 size_t dropNamesIf(size_t &namesChanged, size_t &namesDropped,
72 llvm::function_ref<ModAction(FNamableOp)> pred) {
73 size_t changedNames = 0;
74 auto emptyNameAttr = StringAttr::get(&getContext(), "");
75 auto droppableNameAttr =
76 NameKindEnumAttr::get(&getContext(), NameKindEnum::DroppableName);
77 getOperation()->walk([&](FNamableOp op) {
78 switch (pred(op)) {
79 case ModAction::Drop:
80 op.setNameAttr(emptyNameAttr);
81 op.setNameKindAttr(droppableNameAttr);
82 ++namesDropped;
83 break;
84 case ModAction::Demote:
85 op.setNameKindAttr(droppableNameAttr);
86 ++namesChanged;
87 break;
88 default:
89 break;
90 }
91 });
92 return changedNames;
93 }
94};
95
96} // end anonymous namespace
@ None
Don't explicitly preserve any named values.
Definition Passes.h:52
@ Strip
Strip all names. No name on declaration is preserved.
Definition Passes.h:48
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
bool isUselessName(StringRef name)
Return true if this is a possibly useless temporary name.
Definition Naming.cpp:16