CIRCT 22.0.0git
Loading...
Searching...
No Matches
Synth.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
10
18#include "mlir-c/BuiltinAttributes.h"
19#include "mlir-c/IR.h"
20#include "mlir-c/Support.h"
21#include "mlir/CAPI/IR.h"
22#include "mlir/CAPI/Registration.h"
23#include "mlir/CAPI/Support.h"
24#include "mlir/Pass/AnalysisManager.h"
25#include "llvm/ADT/ImmutableList.h"
26#include "llvm/ADT/PointerUnion.h"
27#include "llvm/Support/JSON.h"
28#include <memory>
29#include <tuple>
30
31using namespace circt;
32using namespace circt::synth;
33
35
36MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(Synth, synth, circt::synth::SynthDialect)
37
38void registerSynthPasses() { circt::synth::registerPasses(); }
39
40// Wrapper struct to hold both the analysis and the analysis manager
42 std::unique_ptr<mlir::ModuleAnalysisManager> analysisManager;
43 std::unique_ptr<LongestPathAnalysis> analysis;
44};
45
47DEFINE_C_API_PTR_METHODS(SynthLongestPathCollection, LongestPathCollection)
48DEFINE_C_API_PTR_METHODS(SynthLongestPathDataflowPath, DataflowPath)
49DEFINE_C_API_PTR_METHODS(SynthLongestPathHistory,
50 llvm::ImmutableListImpl<DebugPoint>)
51
52// SynthLongestPathObject is a pointer to either an Object or an OutputPort so
53// we can not use DEFINE_C_API_PTR_METHODS.
54llvm::PointerUnion<Object *, DataflowPath::OutputPort *>
55unwrap(SynthLongestPathObject object) {
56 return llvm::PointerUnion<
57 Object *, DataflowPath::OutputPort *>::getFromOpaqueValue(object.ptr);
58}
59
60SynthLongestPathObject
61wrap(llvm::PointerUnion<Object *, DataflowPath::OutputPort *> object) {
62 return SynthLongestPathObject{object.getOpaqueValue()};
63}
64
65SynthLongestPathObject wrap(const Object *object) {
66 auto ptr = llvm::PointerUnion<Object *, DataflowPath::OutputPort *>(
67 const_cast<Object *>(object));
68 return wrap(ptr);
69}
70
71SynthLongestPathObject wrap(const DataflowPath::OutputPort *object) {
72 auto ptr = llvm::PointerUnion<Object *, DataflowPath::OutputPort *>(
73 const_cast<DataflowPath::OutputPort *>(object));
74 return wrap(ptr);
75}
76
77//===----------------------------------------------------------------------===//
78// LongestPathAnalysis C API
79//===----------------------------------------------------------------------===//
80
81SynthLongestPathAnalysis
82synthLongestPathAnalysisCreate(MlirOperation module, bool collectDebugInfo,
83 bool keepOnlyMaxDelayPaths, bool lazyComputation,
84 MlirStringRef topModuleName) {
85 auto *op = unwrap(module);
86 auto *wrapper = new LongestPathAnalysisWrapper();
87 wrapper->analysisManager =
88 std::make_unique<mlir::ModuleAnalysisManager>(op, nullptr);
89 mlir::AnalysisManager am = *wrapper->analysisManager;
90 auto topModuleNameAttr =
91 StringAttr::get(op->getContext(), unwrap(topModuleName));
92 wrapper->analysis = std::make_unique<LongestPathAnalysis>(
93 op, am,
94 LongestPathAnalysisOptions(collectDebugInfo, lazyComputation,
95 keepOnlyMaxDelayPaths, topModuleNameAttr));
96 return wrap(wrapper);
97}
98
99void synthLongestPathAnalysisDestroy(SynthLongestPathAnalysis analysis) {
100 delete unwrap(analysis);
101}
102
103SynthLongestPathCollection
104synthLongestPathAnalysisGetPaths(SynthLongestPathAnalysis analysis,
105 MlirValue value, int64_t bitPos,
106 bool elaboratePaths) {
107 auto *wrapper = unwrap(analysis);
108 auto *lpa = wrapper->analysis.get();
109 auto *collection = new LongestPathCollection(lpa->getContext());
110 auto result =
111 lpa->computeGlobalPaths(unwrap(value), bitPos, collection->paths);
112 if (failed(result))
113 return {nullptr};
114 collection->sortInDescendingOrder();
115 return wrap(collection);
116}
117
118SynthLongestPathCollection
119synthLongestPathAnalysisGetAllPaths(SynthLongestPathAnalysis analysis,
120 MlirStringRef moduleName,
121 bool elaboratePaths) {
122 auto *wrapper = unwrap(analysis);
123 auto *lpa = wrapper->analysis.get();
124 auto moduleNameAttr = StringAttr::get(lpa->getContext(), unwrap(moduleName));
125
126 auto *collection = new LongestPathCollection(lpa->getContext());
127 if (!lpa->isAnalysisAvailable(moduleNameAttr) ||
128 failed(
129 lpa->getAllPaths(moduleNameAttr, collection->paths, elaboratePaths)))
130 return {nullptr};
131
132 collection->sortInDescendingOrder();
133 return wrap(collection);
134}
135
136// ===----------------------------------------------------------------------===//
137// LongestPathCollection
138// ===----------------------------------------------------------------------===//
139
140bool synthLongestPathCollectionIsNull(SynthLongestPathCollection collection) {
141 return !collection.ptr;
142}
143
144void synthLongestPathCollectionDestroy(SynthLongestPathCollection collection) {
145 delete unwrap(collection);
146}
147
148size_t
149synthLongestPathCollectionGetSize(SynthLongestPathCollection collection) {
150 auto *wrapper = unwrap(collection);
151 return wrapper->paths.size();
152}
153
154// Get a specific path from the collection as DataflowPath object
155SynthLongestPathDataflowPath
156synthLongestPathCollectionGetDataflowPath(SynthLongestPathCollection collection,
157 size_t index) {
158 auto *wrapper = unwrap(collection);
159 auto &path = wrapper->paths[index];
160 return wrap(&path);
161}
162
163void synthLongestPathCollectionMerge(SynthLongestPathCollection dest,
164 SynthLongestPathCollection src) {
165 auto *destWrapper = unwrap(dest);
166 auto *srcWrapper = unwrap(src);
167 destWrapper->merge(*srcWrapper);
168}
169
170//===----------------------------------------------------------------------===//
171// DataflowPath
172//===----------------------------------------------------------------------===//
173
174int64_t
175synthLongestPathDataflowPathGetDelay(SynthLongestPathDataflowPath path) {
176 auto *wrapper = unwrap(path);
177 return wrapper->getDelay();
178}
179
180SynthLongestPathObject
181synthLongestPathDataflowPathGetStartPoint(SynthLongestPathDataflowPath path) {
182 auto *wrapper = unwrap(path);
183 auto &startPoint = wrapper->getStartPoint();
184 return wrap(const_cast<Object *>(&startPoint));
185}
186
187SynthLongestPathObject
188synthLongestPathDataflowPathGetEndPoint(SynthLongestPathDataflowPath path) {
189 auto *wrapper = unwrap(path);
190 if (auto *object = std::get_if<Object>(&wrapper->getEndPoint())) {
191 return wrap(object);
192 }
193 auto *ptr = std::get_if<DataflowPath::OutputPort>(&wrapper->getEndPoint());
194 return wrap(ptr);
195}
196
197SynthLongestPathHistory
198synthLongestPathDataflowPathGetHistory(SynthLongestPathDataflowPath path) {
199 auto *wrapper = unwrap(path);
200 return wrap(const_cast<llvm::ImmutableListImpl<DebugPoint> *>(
201 wrapper->getHistory().getInternalPointer()));
202}
203
204MlirOperation
205synthLongestPathDataflowPathGetRoot(SynthLongestPathDataflowPath path) {
206 auto *wrapper = unwrap(path);
207 return wrap(wrapper->getRoot());
208}
209
210//===----------------------------------------------------------------------===//
211// History
212//===----------------------------------------------------------------------===//
213
214bool synthLongestPathHistoryIsEmpty(SynthLongestPathHistory history) {
215 auto *wrapper = unwrap(history);
216 return llvm::ImmutableList<DebugPoint>(wrapper).isEmpty();
217}
218
219void synthLongestPathHistoryGetHead(SynthLongestPathHistory history,
220 SynthLongestPathObject *object,
221 int64_t *delay, MlirStringRef *comment) {
222 auto *wrapper = unwrap(history);
223 auto list = llvm::ImmutableList<DebugPoint>(wrapper);
224
225 auto &head = list.getHead();
226 *object = wrap(&head.object);
227 *delay = head.delay;
228 *comment = mlirStringRefCreate(head.comment.data(), head.comment.size());
229}
230
231SynthLongestPathHistory
232synthLongestPathHistoryGetTail(SynthLongestPathHistory history) {
233 auto *wrapper = unwrap(history);
234 auto list = llvm::ImmutableList<DebugPoint>(wrapper);
235 auto *tail = list.getTail().getInternalPointer();
236 return wrap(const_cast<llvm::ImmutableListImpl<DebugPoint> *>(tail));
237}
238
239//===----------------------------------------------------------------------===//
240// Object
241//===----------------------------------------------------------------------===//
242
244synthLongestPathObjectGetInstancePath(SynthLongestPathObject object) {
245 auto *ptr = dyn_cast<Object *>(unwrap(object));
246 if (ptr) {
247 IgraphInstancePath result;
248 result.ptr = const_cast<igraph::InstanceOpInterface *>(
249 ptr->instancePath.getPath().data());
250 result.size = ptr->instancePath.getPath().size();
251 return result;
252 }
253
254 // This is output port so the instance path is empty.
255 return {nullptr, 0};
256}
257
258MlirStringRef synthLongestPathObjectName(SynthLongestPathObject rawObject) {
259 auto ptr = unwrap(rawObject);
260 if (auto *object = dyn_cast<Object *>(ptr)) {
261 auto name = object->getName();
262 return mlirStringRefCreate(name.data(), name.size());
263 }
264 auto [module, resultNumber, _] = *dyn_cast<DataflowPath::OutputPort *>(ptr);
265 auto name = module.getOutputName(resultNumber);
266 return mlirStringRefCreate(name.data(), name.size());
267}
268
269size_t synthLongestPathObjectBitPos(SynthLongestPathObject rawObject) {
270 auto ptr = unwrap(rawObject);
271 if (auto *object = dyn_cast<Object *>(ptr))
272 return object->bitPos;
273 return std::get<2>(*dyn_cast<DataflowPath::OutputPort *>(ptr));
274}
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition OM.cpp:111
size_t synthLongestPathCollectionGetSize(SynthLongestPathCollection collection)
Definition Synth.cpp:149
MlirStringRef synthLongestPathObjectName(SynthLongestPathObject rawObject)
Definition Synth.cpp:258
void synthLongestPathHistoryGetHead(SynthLongestPathHistory history, SynthLongestPathObject *object, int64_t *delay, MlirStringRef *comment)
Definition Synth.cpp:219
IgraphInstancePath synthLongestPathObjectGetInstancePath(SynthLongestPathObject object)
Definition Synth.cpp:244
void synthLongestPathAnalysisDestroy(SynthLongestPathAnalysis analysis)
Definition Synth.cpp:99
void synthLongestPathCollectionDestroy(SynthLongestPathCollection collection)
Definition Synth.cpp:144
SynthLongestPathObject synthLongestPathDataflowPathGetStartPoint(SynthLongestPathDataflowPath path)
Definition Synth.cpp:181
SynthLongestPathCollection synthLongestPathAnalysisGetAllPaths(SynthLongestPathAnalysis analysis, MlirStringRef moduleName, bool elaboratePaths)
Definition Synth.cpp:119
SynthLongestPathCollection synthLongestPathAnalysisGetPaths(SynthLongestPathAnalysis analysis, MlirValue value, int64_t bitPos, bool elaboratePaths)
Definition Synth.cpp:104
SynthLongestPathHistory synthLongestPathDataflowPathGetHistory(SynthLongestPathDataflowPath path)
Definition Synth.cpp:198
SynthLongestPathAnalysis synthLongestPathAnalysisCreate(MlirOperation module, bool collectDebugInfo, bool keepOnlyMaxDelayPaths, bool lazyComputation, MlirStringRef topModuleName)
Definition Synth.cpp:82
SynthLongestPathObject synthLongestPathDataflowPathGetEndPoint(SynthLongestPathDataflowPath path)
Definition Synth.cpp:188
bool synthLongestPathCollectionIsNull(SynthLongestPathCollection collection)
Definition Synth.cpp:140
void synthLongestPathCollectionMerge(SynthLongestPathCollection dest, SynthLongestPathCollection src)
Definition Synth.cpp:163
DEFINE_C_API_PTR_METHODS(SynthLongestPathHistory, llvm::ImmutableListImpl< DebugPoint >) llvm
Definition Synth.cpp:49
SynthLongestPathDataflowPath synthLongestPathCollectionGetDataflowPath(SynthLongestPathCollection collection, size_t index)
Definition Synth.cpp:156
int64_t synthLongestPathDataflowPathGetDelay(SynthLongestPathDataflowPath path)
Definition Synth.cpp:175
void registerSynthPasses()
Definition Synth.cpp:38
bool synthLongestPathHistoryIsEmpty(SynthLongestPathHistory history)
Definition Synth.cpp:214
SynthLongestPathObject wrap(llvm::PointerUnion< Object *, DataflowPath::OutputPort * > object)
Definition Synth.cpp:61
MlirOperation synthLongestPathDataflowPathGetRoot(SynthLongestPathDataflowPath path)
Definition Synth.cpp:205
SynthLongestPathHistory synthLongestPathHistoryGetTail(SynthLongestPathHistory history)
Definition Synth.cpp:232
size_t synthLongestPathObjectBitPos(SynthLongestPathObject rawObject)
Definition Synth.cpp:269
std::tuple< hw::HWModuleOp, size_t, size_t > OutputPort
void registerSynthesisPipeline()
Register the synthesis pipelines.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition synth.py:1
std::unique_ptr< LongestPathAnalysis > analysis
Definition Synth.cpp:43
std::unique_ptr< mlir::ModuleAnalysisManager > analysisManager
Definition Synth.cpp:42
Configuration options for the longest path analysis.