-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HW] Select the better name when dropping wires (#6559)
- Loading branch information
Showing
11 changed files
with
126 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//===- Naming.h - Utilities for handling names ------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CIRCT_SUPPORT_NAMING_H | ||
#define CIRCT_SUPPORT_NAMING_H | ||
|
||
#include "circt/Support/LLVM.h" | ||
|
||
namespace circt { | ||
|
||
/// Return true if this is a possibly useless temporary name. | ||
/// This method is FIRRTL-centric, dropping useless temporaries. | ||
bool isUselessName(StringRef name); | ||
|
||
/// Choose a good name for an item from two options. | ||
StringRef chooseName(StringRef a, StringRef b); | ||
|
||
/// Choose a good name for an item from two options. | ||
StringAttr chooseName(StringAttr a, StringAttr b); | ||
|
||
/// Choose the better name between two ops. Picks the "name" attribute as first | ||
/// preference, using "sv.namehint" as an alternative. | ||
StringAttr chooseName(Operation *a, Operation *b); | ||
|
||
} // namespace circt | ||
|
||
#endif // CIRCT_SUPPORT_NAMING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
//===- Naming.cpp - Utilities for handling names ----------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "circt/Support/Naming.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "mlir/IR/Operation.h" | ||
#include "llvm/ADT/StringRef.h" | ||
|
||
using namespace circt; | ||
|
||
bool circt::isUselessName(StringRef name) { | ||
if (name.empty()) | ||
return true; | ||
// Ignore _.* | ||
return name.starts_with("_T") || name.starts_with("_WIRE"); | ||
} | ||
|
||
// Heuristic to pick the best name. | ||
// Good names are not useless, don't start with an underscore, minimize | ||
// underscores in them, and are short. This function deterministically favors | ||
// the second name on ties. | ||
static bool isNameBetter(StringRef a, StringRef b) { | ||
if (a.empty()) | ||
return false; | ||
if (b.empty()) | ||
return true; | ||
if (isUselessName(a)) | ||
return false; | ||
if (isUselessName(b)) | ||
return true; | ||
if (a.starts_with("_")) | ||
return false; | ||
if (b.starts_with("_")) | ||
return true; | ||
if (b.count('_') < a.count('_')) | ||
return false; | ||
if (b.count('_') > a.count('_')) | ||
return true; | ||
return a.size() <= b.size(); | ||
} | ||
|
||
StringRef circt::chooseName(StringRef a, StringRef b) { | ||
return isNameBetter(a, b) ? a : b; | ||
} | ||
|
||
StringAttr circt::chooseName(StringAttr a, StringAttr b) { | ||
if (!a) | ||
return b; | ||
if (!b) | ||
return a; | ||
return isNameBetter(a.getValue(), b.getValue()) ? a : b; | ||
} | ||
|
||
static StringAttr getNameOrHint(Operation *a) { | ||
StringAttr name = a->getAttrOfType<StringAttr>("name"); | ||
if (!name || name.getValue().empty()) | ||
return a->getAttrOfType<StringAttr>("sv.namehint"); | ||
return name; | ||
} | ||
|
||
StringAttr circt::chooseName(Operation *a, Operation *b) { | ||
return chooseName(getNameOrHint(a), getNameOrHint(b)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters