21#include "mlir/Analysis/TopologicalSortUtils.h"
22#include "mlir/IR/BuiltinAttributes.h"
23#include "mlir/IR/Operation.h"
24#include "mlir/IR/Visitors.h"
25#include "mlir/Support/LLVM.h"
26#include "mlir/Transforms/RegionUtils.h"
27#include "llvm/ADT/DenseMap.h"
28#include "llvm/ADT/DenseMapInfo.h"
29#include "llvm/ADT/PointerIntPair.h"
30#include "llvm/ADT/TypeSwitch.h"
31#include "llvm/Support/DebugLog.h"
32#include "llvm/Support/LogicalResult.h"
34#define DEBUG_TYPE "synth-structural-hash"
38#define GEN_PASS_DEF_STRUCTURALHASH
39#include "circt/Dialect/Synth/Transforms/SynthPasses.h.inc"
56 llvm::SmallVector<llvm::PointerIntPair<Value, 1>, 3> inps)
76 hash = llvm::hash_combine(hash, operand.getOpaqueValue());
77 return static_cast<unsigned>(hash);
89struct StructuralHashPass
90 :
public impl::StructuralHashBase<StructuralHashPass> {
91 void runOnOperation()
override;
101class StructuralHashDriver {
103 StructuralHashDriver() =
default;
104 void visitOp(BooleanLogicOpInterface op);
105 void visitUnaryOp(BooleanLogicOpInterface op);
106 void visitVariadicOp(BooleanLogicOpInterface op);
107 uint64_t getNumber(Value v);
112 llvm::LogicalResult
run(Operation *op);
116 DenseMap<Value, uint64_t> valueNumber;
117 uint64_t constantCounter = 0;
120 DenseMap<StructuralHashKey, BooleanLogicOpInterface> hashTable;
130 DenseMap<Value, Value> inversion;
134void StructuralHashDriver::visitOp(BooleanLogicOpInterface op) {
138 if (op.getInputs().size() == 1) {
149void StructuralHashDriver::visitUnaryOp(BooleanLogicOpInterface logicOp) {
150 Operation *op = logicOp.getOperation();
151 auto [input, inverted] = logicOp.getInputPair(0);
153 op->replaceAllUsesWith(ArrayRef<Value>{input});
157 auto it = inversion.find(input);
158 if (it != inversion.end()) {
160 op->replaceAllUsesWith(ArrayRef<Value>{it->second});
164 inversion[logicOp.getResult()] = input;
170void StructuralHashDriver::visitVariadicOp(BooleanLogicOpInterface logicOp) {
171 Operation *op = logicOp.getOperation();
172 auto inversions = logicOp.getInverted();
176 for (
auto [input, inverted] :
llvm::zip(op->getOperands(), inversions)) {
177 bool isInverted = inverted;
179 auto it = inversion.find(input);
180 if (it != inversion.end()) {
183 isInverted = !isInverted;
186 key.operandPairs.push_back(
187 llvm::PointerIntPair<Value, 1>(input, isInverted));
190 (void)getNumber(input);
195 if (logicOp.areInputsPermutationInvariant()) {
196 llvm::sort(key.operandPairs, [&](
auto a,
auto b) {
197 size_t aNum = getNumber(a.getPointer());
198 size_t bNum = getNumber(b.getPointer());
201 return a.getInt() < b.getInt();
206 auto [it, inserted] = hashTable.try_emplace(key, logicOp);
209 op->setOperands(llvm::to_vector<3>(llvm::map_range(
210 key.operandPairs, [](
auto p) { return p.getPointer(); })));
211 SmallVector<bool, 3> newInversion(
212 llvm::map_range(key.operandPairs, [](
auto p) { return p.getInt(); }));
213 logicOp.setInverted(newInversion);
215 (void)getNumber(logicOp.getResult());
217 LDBG() <<
"Structural Hash: Replacing " << *op <<
" with " << *(it->second)
222 if (name && !it->second->hasAttr(
"sv.namehint"))
223 it->second->setAttr(
"sv.namehint", name);
224 op->replaceAllUsesWith(it->second);
231uint64_t StructuralHashDriver::getNumber(Value v) {
232 auto it = valueNumber.find(v);
233 if (it != valueNumber.end())
238 if (
auto *op = v.getDefiningOp();
239 op && op->hasTrait<mlir::OpTrait::ConstantLike>()) {
240 auto [it, inserted] = valueNumber.try_emplace(
241 v, std::numeric_limits<uint64_t>::max() - constantCounter++);
245 return valueNumber.try_emplace(v, valueNumber.size() - constantCounter)
249llvm::LogicalResult StructuralHashDriver::run(Operation *moduleOp) {
250 auto isOperationReady = [&](Value value, Operation *op) ->
bool {
252 return !isa<BooleanLogicOpInterface>(op);
255 if (moduleOp->getNumRegions() != 1 ||
256 moduleOp->getRegion(0).getBlocks().size() != 1)
257 return llvm::success();
259 Block &block = moduleOp->getRegion(0).front();
260 if (!mlir::sortTopologically(&block, isOperationReady))
263 for (
auto arg : block.getArguments())
264 (void)getNumber(arg);
270 llvm::make_early_inc_range(block.getOps<BooleanLogicOpInterface>())) {
275 mlir::PatternRewriter rewriter(moduleOp->getContext());
276 (void)mlir::runRegionDCE(rewriter, moduleOp->getRegions());
277 return mlir::success();
280void StructuralHashPass::runOnOperation() {
281 auto *topOp = getOperation();
282 StructuralHashDriver driver;
283 if (failed(driver.run(topOp)))
284 return signalPassFailure();
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
StringRef chooseName(StringRef a, StringRef b)
Choose a good name for an item from two options.
int run(Type[Generator] generator=CppGenerator, cmdline_args=sys.argv)
llvm::hash_code hash_value(const DenseSet< T > &set)
A struct that represents the key used for structural hashing.
StructuralHashKey(OperationName name, llvm::SmallVector< llvm::PointerIntPair< Value, 1 >, 3 > inps)
Constructor.
llvm::SmallVector< llvm::PointerIntPair< Value, 1 >, 3 > operandPairs
static StructuralHashKey getTombstoneKey()
static bool isEqual(const StructuralHashKey &lhs, const StructuralHashKey &rhs)
static StructuralHashKey getEmptyKey()
static unsigned getHashValue(const StructuralHashKey &key)