CIRCT 23.0.0git
Loading...
Searching...
No Matches
SynthOps.h
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 defines the operations of the Synth dialect.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef CIRCT_DIALECT_SYNTH_SYNTHOPS_H
14#define CIRCT_DIALECT_SYNTH_SYNTHOPS_H
15
18#include "circt/Support/LLVM.h"
19#include "mlir/IR/Attributes.h"
20#include "mlir/IR/Builders.h"
21#include "mlir/IR/BuiltinOps.h"
22#include "mlir/IR/BuiltinTypes.h"
23#include "mlir/IR/Dialect.h"
24#include "mlir/IR/Operation.h"
25#include "mlir/Interfaces/InferTypeOpInterface.h"
26#include "mlir/Interfaces/SideEffectInterfaces.h"
27#include "mlir/Rewrite/PatternApplicator.h"
28
29#define GET_OP_CLASSES
30#include "circt/Dialect/Synth/Synth.h.inc"
31
32#include "llvm/ADT/PriorityQueue.h"
33
34namespace circt {
35namespace synth {
37 mlir::RewritePatternSet &patterns);
39 mlir::RewritePatternSet &patterns);
40
41/// This function performs a topological sort on the operations within each
42/// block of graph regions in the given operation. It uses MLIR's topological
43/// sort utility as a wrapper, ensuring that operations are ordered such that
44/// all operands are defined before their uses. The `isOperandReady` callback
45/// allows customization of when an operand is considered ready for sorting.
47 mlir::Operation *op,
48 llvm::function_ref<bool(mlir::Value, mlir::Operation *)> isOperandReady);
49
50//===----------------------------------------------------------------------===//
51// Delay-Aware Tree Building Utilities
52//===----------------------------------------------------------------------===//
53
54/// Helper class for delay-aware tree building.
55/// Stores a value along with its arrival time and inversion flag.
57 /// The value and an optional inversion flag packed together.
58 /// The inversion flag is used for AndInverterOp lowering.
59 llvm::PointerIntPair<mlir::Value, 1, bool> value;
60
61 /// The arrival time (delay) of this value in the circuit.
62 int64_t arrivalTime;
63
64 /// Value numbering for deterministic ordering when arrival times are equal.
65 /// This ensures consistent results across runs when multiple values have
66 /// the same delay.
67 size_t valueNumbering = 0;
68
69public:
70 ValueWithArrivalTime(mlir::Value value, int64_t arrivalTime, bool invert,
71 size_t valueNumbering = 0)
74
75 mlir::Value getValue() const { return value.getPointer(); }
76 bool isInverted() const { return value.getInt(); }
77 int64_t getArrivalTime() const { return arrivalTime; }
78
80 value.setInt(!value.getInt());
81 return *this;
82 }
83
84 /// Comparison operator for priority queue. Values with earlier arrival times
85 /// have higher priority. When arrival times are equal, use value numbering
86 /// for determinism.
87 bool operator>(const ValueWithArrivalTime &other) const {
88 return std::tie(arrivalTime, valueNumbering) >
89 std::tie(other.arrivalTime, other.valueNumbering);
90 }
91};
92
93/// Build a balanced binary tree using a priority queue to greedily pair
94/// elements with earliest arrival times. This minimizes the critical path
95/// delay.
96///
97/// Template parameters:
98/// T - The element type (must have operator> defined)
99///
100/// The algorithm uses a min-heap to repeatedly combine the two elements with
101/// the earliest arrival times, which is optimal for minimizing maximum delay.
102template <typename T>
103T buildBalancedTreeWithArrivalTimes(llvm::ArrayRef<T> elements,
104 llvm::function_ref<T(T, T)> combine) {
105 assert(!elements.empty() && "Cannot build tree from empty elements");
106
107 if (elements.size() == 1)
108 return elements[0];
109 if (elements.size() == 2)
110 return combine(elements[0], elements[1]);
111
112 // Min-heap priority queue ordered by operator>
113 llvm::PriorityQueue<T, std::vector<T>, std::greater<T>> pq(elements.begin(),
114 elements.end());
115
116 // Greedily pair the two earliest-arriving elements
117 while (pq.size() > 1) {
118 T e1 = pq.top();
119 pq.pop();
120 T e2 = pq.top();
121 pq.pop();
122
123 // Combine the two elements
124 T combined = combine(e1, e2);
125 pq.push(combined);
126 }
127
128 return pq.top();
129}
130
131} // namespace synth
132} // namespace circt
133
134#endif // CIRCT_DIALECT_SYNTH_SYNTHOPS_H
assert(baseType &&"element must be base type")
Helper class for delay-aware tree building.
Definition SynthOps.h:56
ValueWithArrivalTime(mlir::Value value, int64_t arrivalTime, bool invert, size_t valueNumbering=0)
Definition SynthOps.h:70
mlir::Value getValue() const
Definition SynthOps.h:75
ValueWithArrivalTime & flipInversion()
Definition SynthOps.h:79
size_t valueNumbering
Value numbering for deterministic ordering when arrival times are equal.
Definition SynthOps.h:67
int64_t arrivalTime
The arrival time (delay) of this value in the circuit.
Definition SynthOps.h:62
llvm::PointerIntPair< mlir::Value, 1, bool > value
The value and an optional inversion flag packed together.
Definition SynthOps.h:59
bool operator>(const ValueWithArrivalTime &other) const
Comparison operator for priority queue.
Definition SynthOps.h:87
void populateVariadicXorInverterLoweringPatterns(mlir::RewritePatternSet &patterns)
LogicalResult topologicallySortGraphRegionBlocks(mlir::Operation *op, llvm::function_ref< bool(mlir::Value, mlir::Operation *)> isOperandReady)
This function performs a topological sort on the operations within each block of graph regions in the...
Definition SynthOps.cpp:376
T buildBalancedTreeWithArrivalTimes(llvm::ArrayRef< T > elements, llvm::function_ref< T(T, T)> combine)
Build a balanced binary tree using a priority queue to greedily pair elements with earliest arrival t...
Definition SynthOps.h:103
void populateVariadicAndInverterLoweringPatterns(mlir::RewritePatternSet &patterns)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition synth.py:1