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