CIRCT  20.0.0git
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 "circt/Support/Naming.h"
17 #include "mlir/Pass/Pass.h"
18 
19 namespace circt {
20 namespace firrtl {
21 #define GEN_PASS_DEF_DROPNAME
22 #include "circt/Dialect/FIRRTL/Passes.h.inc"
23 } // namespace firrtl
24 } // namespace circt
25 
26 using namespace circt;
27 using namespace firrtl;
28 
29 namespace {
30 struct DropNamesPass : public circt::firrtl::impl::DropNameBase<DropNamesPass> {
31  DropNamesPass(PreserveValues::PreserveMode preserveMode) {
32  this->preserveMode = preserveMode;
33  }
34 
35  enum ModAction { Drop, Keep, Demote };
36 
37  void runOnOperation() override {
38  size_t namesDropped = 0;
39  size_t namesChanged = 0;
40  if (preserveMode == PreserveValues::None) {
41  // Drop all names.
42  dropNamesIf(namesChanged, namesDropped, [](FNamableOp op) {
43  if (isUselessName(op.getName()))
44  return ModAction::Drop;
45  return ModAction::Demote;
46  });
47  } else if (preserveMode == PreserveValues::Strip) {
48  // Strip all names.
49  dropNamesIf(namesChanged, namesDropped,
50  [](FNamableOp op) { return ModAction::Drop; });
51  } else if (preserveMode == PreserveValues::Named) {
52  // Drop the name if it isn't considered meaningful.
53  dropNamesIf(namesChanged, namesDropped, [](FNamableOp op) {
54  auto name = op.getName();
55  if (isUselessName(name))
56  return ModAction::Drop;
57  if (name.starts_with("_"))
58  return ModAction::Demote;
59  return ModAction::Keep;
60  });
61  } else if (preserveMode == PreserveValues::All) {
62  // Drop the name if it isn't considered meaningful.
63  dropNamesIf(namesChanged, namesDropped, [](FNamableOp op) {
64  if (isUselessName(op.getName()))
65  return ModAction::Demote;
66  return ModAction::Keep;
67  });
68  }
69  numNamesConverted += namesChanged;
70  numNamesDropped += namesDropped;
71  }
72 
73 private:
74  size_t dropNamesIf(size_t &namesChanged, size_t &namesDropped,
75  llvm::function_ref<ModAction(FNamableOp)> pred) {
76  size_t changedNames = 0;
77  auto emptyNameAttr = StringAttr::get(&getContext(), "");
78  auto droppableNameAttr =
79  NameKindEnumAttr::get(&getContext(), NameKindEnum::DroppableName);
80  getOperation()->walk([&](FNamableOp op) {
81  switch (pred(op)) {
82  case ModAction::Drop:
83  op.setNameAttr(emptyNameAttr);
84  op.setNameKindAttr(droppableNameAttr);
85  ++namesDropped;
86  break;
87  case ModAction::Demote:
88  op.setNameKindAttr(droppableNameAttr);
89  ++namesChanged;
90  break;
91  default:
92  break;
93  }
94  });
95  return changedNames;
96  }
97 };
98 
99 } // end anonymous namespace
100 
101 std::unique_ptr<mlir::Pass>
103  return std::make_unique<DropNamesPass>(mode);
104 }
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:55
@ None
Don't explicitly preserve any named values.
Definition: Passes.h:54
@ Strip
Strip all names. No name on declaration is preserved.
Definition: Passes.h:50
std::unique_ptr< mlir::Pass > createDropNamesPass(PreserveValues::PreserveMode mode=PreserveValues::None)
Definition: DropName.cpp:102
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
bool isUselessName(StringRef name)
Return true if this is a possibly useless temporary name.
Definition: Naming.cpp:16