CIRCT 22.0.0git
Loading...
Searching...
No Matches
GenericLUTMapper.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 implements a generic LUT mapper pass using the cut-based rewriting
10// framework. It performs technology mapping by converting combinational logic
11// networks into implementations using K-input lookup tables (LUTs).
12//
13//===----------------------------------------------------------------------===//
14
19#include "circt/Support/LLVM.h"
21#include "mlir/IR/Builders.h"
22#include "mlir/Pass/Pass.h"
23#include "llvm/Support/Debug.h"
24
25#define DEBUG_TYPE "synth-generic-lut-mapper"
26
27using namespace circt;
28using namespace circt::synth;
29
30namespace circt {
31namespace synth {
32#define GEN_PASS_DEF_GENERICLUTMAPPER
33#include "circt/Dialect/Synth/Transforms/SynthPasses.h.inc"
34} // namespace synth
35} // namespace circt
36
37//===----------------------------------------------------------------------===//
38// Generic LUT Pattern
39//===----------------------------------------------------------------------===//
40
41/// A generic K-input LUT pattern that can implement any boolean function
42/// with up to K inputs using a lookup table.
44 unsigned k; // Maximum number of inputs for this LUT
45 SmallVector<DelayType, 8> cachedDelays; // Pre-computed unit delays
46
47 GenericLUT(mlir::MLIRContext *context, unsigned k)
49
50 std::optional<MatchResult> match(CutEnumerator &enumerator,
51 const Cut &cut) const override {
52 // This pattern can implement any cut with at most k inputs
53 if (cut.getInputSize() > k || cut.getOutputSize() != 1)
54 return std::nullopt;
55
56 // Create match result with a reference to cached delays
57 return MatchResult(
58 1.0, ArrayRef<DelayType>(cachedDelays).take_front(cut.getInputSize()));
59 }
60
61 llvm::FailureOr<Operation *> rewrite(mlir::OpBuilder &rewriter,
62 CutEnumerator &enumerator,
63 const Cut &cut) const override {
64 // NOTE: Don't use NPN since it's unnecessary.
65 auto truthTable = cut.getTruthTable();
66 LLVM_DEBUG({
67 llvm::dbgs() << "Rewriting cut with " << cut.getInputSize()
68 << " inputs and " << cut.getInputSize()
69 << " operations to a generic LUT with " << k << " inputs.\n";
70 cut.dump(llvm::dbgs());
71 llvm::dbgs() << "Truth table details:\n";
72 truthTable.dump(llvm::dbgs());
73 });
74
75 SmallVector<bool> lutTable;
76 // Convert the truth table to a LUT table
77 for (uint32_t i = 0; i < truthTable.table.getBitWidth(); ++i)
78 lutTable.push_back(truthTable.table[i]);
79
80 // Reverse the inputs to match the LUT input order
81 SmallVector<Value> lutInputs(cut.inputs.rbegin(), cut.inputs.rend());
82
83 // Generate comb.truth table operation.
84 auto truthTableOp = comb::TruthTableOp::create(
85 rewriter, cut.getRoot()->getLoc(), lutInputs, lutTable);
86
87 // Replace the root operation with the truth table operation
88 return truthTableOp.getOperation();
89 }
90
91 unsigned getNumOutputs() const override { return 1; }
92
93 StringRef getPatternName() const override { return "GenericLUT"; }
94};
95
96//===----------------------------------------------------------------------===//
97// Generic LUT Mapper Pass
98//===----------------------------------------------------------------------===//
99
101 : public impl::GenericLutMapperBase<GenericLUTMapperPass> {
102 using GenericLutMapperBase<GenericLUTMapperPass>::GenericLutMapperBase;
103
104 void runOnOperation() override {
105 auto module = getOperation();
106 // Create the cut rewriter options
107 CutRewriterOptions options;
108 options.strategy = strategy;
109 options.maxCutInputSize = maxLutSize;
110 options.maxCutSizePerRoot = maxCutsPerRoot;
111 options.allowNoMatch = false;
112 options.attachDebugTiming = test;
113
114 // Create the pattern for generic K-LUT
115 SmallVector<std::unique_ptr<CutRewritePattern>, 4> patterns;
116 patterns.push_back(
117 std::make_unique<GenericLUT>(module->getContext(), maxLutSize));
118
119 // Create the pattern set
120 CutRewritePatternSet patternSet(std::move(patterns));
121
122 // Create the cut rewriter
123 CutRewriter rewriter(options, patternSet);
124
125 // Apply the rewriting
126 if (failed(rewriter.run(module)))
127 return signalPassFailure();
128 }
129};
Strategy strategy
Cut enumeration engine for combinational logic networks.
Manages a collection of rewriting patterns for combinational logic optimization.
Main cut-based rewriting algorithm for combinational logic optimization.
LogicalResult run(Operation *topOp)
Execute the complete cut-based rewriting algorithm.
Represents a cut in the combinational logic network.
unsigned getOutputSize() const
Get the number of outputs from root operation.
mlir::Operation * getRoot() const
Get the root operation of this cut.
llvm::SmallSetVector< mlir::Value, 4 > inputs
External inputs to this cut (cut boundary).
void dump(llvm::raw_ostream &os) const
const BinaryTruthTable & getTruthTable() const
Get the truth table for this cut.
unsigned getInputSize() const
Get the number of inputs to this cut.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition synth.py:1
void runOnOperation() override
A generic K-input LUT pattern that can implement any boolean function with up to K inputs using a loo...
std::optional< MatchResult > match(CutEnumerator &enumerator, const Cut &cut) const override
Check if a cut matches this pattern and compute area/delay metrics.
unsigned getNumOutputs() const override
Get the number of outputs this pattern produces.
SmallVector< DelayType, 8 > cachedDelays
StringRef getPatternName() const override
Get the name of this pattern. Used for debugging.
llvm::FailureOr< Operation * > rewrite(mlir::OpBuilder &rewriter, CutEnumerator &enumerator, const Cut &cut) const override
Return a new operation that replaces the matched cut.
GenericLUT(mlir::MLIRContext *context, unsigned k)
Base class for cut rewriting patterns used in combinational logic optimization.
mlir::MLIRContext * context
Configuration options for the cut-based rewriting algorithm.
unsigned maxCutInputSize
Maximum number of inputs allowed for any cut.
unsigned maxCutSizePerRoot
Maximum number of cuts to maintain per logic node.
bool allowNoMatch
Fail if there is a root operation that has no matching pattern.
bool attachDebugTiming
Put arrival times to rewritten operations.
OptimizationStrategy strategy
Optimization strategy (area vs. timing).
Result of matching a cut against a pattern.
Definition CutRewriter.h:74