CIRCT 22.0.0git
Loading...
Searching...
No Matches
AIG.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
16#include "mlir-c/BuiltinAttributes.h"
17#include "mlir-c/IR.h"
18#include "mlir-c/Support.h"
19#include "mlir/CAPI/IR.h"
20#include "mlir/CAPI/Registration.h"
21#include "mlir/CAPI/Support.h"
22#include "mlir/Pass/AnalysisManager.h"
23#include "llvm/ADT/ImmutableList.h"
24#include "llvm/ADT/PointerUnion.h"
25#include "llvm/Support/JSON.h"
26#include <memory>
27#include <tuple>
28
29using namespace circt;
30using namespace circt::aig;
31
32MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(AIG, aig, circt::aig::AIGDialect)
33
34void registerAIGPasses() { circt::aig::registerPasses(); }
35
36// Wrapper struct to hold both the analysis and the analysis manager
38 std::unique_ptr<mlir::ModuleAnalysisManager> analysisManager;
39 std::unique_ptr<LongestPathAnalysis> analysis;
40};
41
43DEFINE_C_API_PTR_METHODS(AIGLongestPathCollection, LongestPathCollection)
44DEFINE_C_API_PTR_METHODS(AIGLongestPathDataflowPath, DataflowPath)
45DEFINE_C_API_PTR_METHODS(AIGLongestPathHistory,
46 llvm::ImmutableListImpl<DebugPoint>)
47
48// AIGLongestPathObject is a pointer to either an Object or an OutputPort so we
49// can not use DEFINE_C_API_PTR_METHODS.
50llvm::PointerUnion<Object *, DataflowPath::OutputPort *>
51unwrap(AIGLongestPathObject object) {
52 return llvm::PointerUnion<
53 Object *, DataflowPath::OutputPort *>::getFromOpaqueValue(object.ptr);
54}
55
56AIGLongestPathObject
57wrap(llvm::PointerUnion<Object *, DataflowPath::OutputPort *> object) {
58 return AIGLongestPathObject{object.getOpaqueValue()};
59}
60
61AIGLongestPathObject wrap(const Object *object) {
62 auto ptr = llvm::PointerUnion<Object *, DataflowPath::OutputPort *>(
63 const_cast<Object *>(object));
64 return wrap(ptr);
65}
66
67AIGLongestPathObject wrap(const DataflowPath::OutputPort *object) {
68 auto ptr = llvm::PointerUnion<Object *, DataflowPath::OutputPort *>(
69 const_cast<DataflowPath::OutputPort *>(object));
70 return wrap(ptr);
71}
72
73//===----------------------------------------------------------------------===//
74// LongestPathAnalysis C API
75//===----------------------------------------------------------------------===//
76
77AIGLongestPathAnalysis aigLongestPathAnalysisCreate(MlirOperation module,
78 bool traceDebugPoints) {
79 auto *op = unwrap(module);
80 auto *wrapper = new LongestPathAnalysisWrapper();
81 wrapper->analysisManager =
82 std::make_unique<mlir::ModuleAnalysisManager>(op, nullptr);
83 mlir::AnalysisManager am = *wrapper->analysisManager;
84 if (traceDebugPoints)
85 wrapper->analysis = std::make_unique<LongestPathAnalysisWithTrace>(op, am);
86 else
87 wrapper->analysis = std::make_unique<LongestPathAnalysis>(op, am);
88 return wrap(wrapper);
89}
90
91void aigLongestPathAnalysisDestroy(AIGLongestPathAnalysis analysis) {
92 delete unwrap(analysis);
93}
94
95AIGLongestPathCollection
96aigLongestPathAnalysisGetAllPaths(AIGLongestPathAnalysis analysis,
97 MlirStringRef moduleName,
98 bool elaboratePaths) {
99 auto *wrapper = unwrap(analysis);
100 auto *lpa = wrapper->analysis.get();
101 auto moduleNameAttr = StringAttr::get(lpa->getContext(), unwrap(moduleName));
102
103 auto *collection = new LongestPathCollection(lpa->getContext());
104 if (!lpa->isAnalysisAvailable(moduleNameAttr) ||
105 failed(
106 lpa->getAllPaths(moduleNameAttr, collection->paths, elaboratePaths)))
107 return {nullptr};
108
109 collection->sortInDescendingOrder();
110 return wrap(collection);
111}
112
113// ===----------------------------------------------------------------------===//
114// LongestPathCollection
115// ===----------------------------------------------------------------------===//
116
117bool aigLongestPathCollectionIsNull(AIGLongestPathCollection collection) {
118 return !collection.ptr;
119}
120
121void aigLongestPathCollectionDestroy(AIGLongestPathCollection collection) {
122 delete unwrap(collection);
123}
124
125size_t aigLongestPathCollectionGetSize(AIGLongestPathCollection collection) {
126 auto *wrapper = unwrap(collection);
127 return wrapper->paths.size();
128}
129
130// Get a specific path from the collection as DataflowPath object
131AIGLongestPathDataflowPath
132aigLongestPathCollectionGetDataflowPath(AIGLongestPathCollection collection,
133 size_t index) {
134 auto *wrapper = unwrap(collection);
135 auto &path = wrapper->paths[index];
136 return wrap(&path);
137}
138
139//===----------------------------------------------------------------------===//
140// DataflowPath
141//===----------------------------------------------------------------------===//
142
143int64_t aigLongestPathDataflowPathGetDelay(AIGLongestPathDataflowPath path) {
144 auto *wrapper = unwrap(path);
145 return wrapper->getDelay();
146}
147
148AIGLongestPathObject
149aigLongestPathDataflowPathGetFanIn(AIGLongestPathDataflowPath path) {
150 auto *wrapper = unwrap(path);
151 auto &fanIn = wrapper->getFanIn();
152 return wrap(const_cast<Object *>(&fanIn));
153}
154
155AIGLongestPathObject
156aigLongestPathDataflowPathGetFanOut(AIGLongestPathDataflowPath path) {
157 auto *wrapper = unwrap(path);
158 if (auto *object = std::get_if<Object>(&wrapper->getFanOut())) {
159 return wrap(object);
160 }
161 auto *ptr = std::get_if<DataflowPath::OutputPort>(&wrapper->getFanOut());
162 return wrap(ptr);
163}
164
165AIGLongestPathHistory
166aigLongestPathDataflowPathGetHistory(AIGLongestPathDataflowPath path) {
167 auto *wrapper = unwrap(path);
168 return wrap(const_cast<llvm::ImmutableListImpl<DebugPoint> *>(
169 wrapper->getHistory().getInternalPointer()));
170}
171
172MlirOperation
173aigLongestPathDataflowPathGetRoot(AIGLongestPathDataflowPath path) {
174 auto *wrapper = unwrap(path);
175 return wrap(wrapper->getRoot());
176}
177
178//===----------------------------------------------------------------------===//
179// History
180//===----------------------------------------------------------------------===//
181
182bool aigLongestPathHistoryIsEmpty(AIGLongestPathHistory history) {
183 auto *wrapper = unwrap(history);
184 return llvm::ImmutableList<DebugPoint>(wrapper).isEmpty();
185}
186
187void aigLongestPathHistoryGetHead(AIGLongestPathHistory history,
188 AIGLongestPathObject *object, int64_t *delay,
189 MlirStringRef *comment) {
190 auto *wrapper = unwrap(history);
191 auto list = llvm::ImmutableList<DebugPoint>(wrapper);
192
193 auto &head = list.getHead();
194 *object = wrap(&head.object);
195 *delay = head.delay;
196 *comment = mlirStringRefCreate(head.comment.data(), head.comment.size());
197}
198
199AIGLongestPathHistory
200aigLongestPathHistoryGetTail(AIGLongestPathHistory history) {
201 auto *wrapper = unwrap(history);
202 auto list = llvm::ImmutableList<DebugPoint>(wrapper);
203 auto *tail = list.getTail().getInternalPointer();
204 return wrap(const_cast<llvm::ImmutableListImpl<DebugPoint> *>(tail));
205}
206
207//===----------------------------------------------------------------------===//
208// Object
209//===----------------------------------------------------------------------===//
210
212aigLongestPathObjectGetInstancePath(AIGLongestPathObject object) {
213 auto *ptr = dyn_cast<Object *>(unwrap(object));
214 if (ptr) {
215 IgraphInstancePath result;
216 result.ptr = const_cast<igraph::InstanceOpInterface *>(
217 ptr->instancePath.getPath().data());
218 result.size = ptr->instancePath.getPath().size();
219 return result;
220 }
221
222 // This is output port so the instance path is empty.
223 return {nullptr, 0};
224}
225
226MlirStringRef aigLongestPathObjectName(AIGLongestPathObject rawObject) {
227 auto ptr = unwrap(rawObject);
228 if (auto *object = dyn_cast<Object *>(ptr)) {
229 auto name = object->getName();
230 return mlirStringRefCreate(name.data(), name.size());
231 }
232 auto [module, resultNumber, _] = *dyn_cast<DataflowPath::OutputPort *>(ptr);
233 auto name = module.getOutputName(resultNumber);
234 return mlirStringRefCreate(name.data(), name.size());
235}
236
237size_t aigLongestPathObjectBitPos(AIGLongestPathObject rawObject) {
238 auto ptr = unwrap(rawObject);
239 if (auto *object = dyn_cast<Object *>(ptr))
240 return object->bitPos;
241 return std::get<2>(*dyn_cast<DataflowPath::OutputPort *>(ptr));
242}
AIGLongestPathAnalysis aigLongestPathAnalysisCreate(MlirOperation module, bool traceDebugPoints)
Definition AIG.cpp:77
AIGLongestPathHistory aigLongestPathHistoryGetTail(AIGLongestPathHistory history)
Definition AIG.cpp:200
bool aigLongestPathCollectionIsNull(AIGLongestPathCollection collection)
Definition AIG.cpp:117
AIGLongestPathObject aigLongestPathDataflowPathGetFanOut(AIGLongestPathDataflowPath path)
Definition AIG.cpp:156
int64_t aigLongestPathDataflowPathGetDelay(AIGLongestPathDataflowPath path)
Definition AIG.cpp:143
size_t aigLongestPathObjectBitPos(AIGLongestPathObject rawObject)
Definition AIG.cpp:237
AIGLongestPathObject aigLongestPathDataflowPathGetFanIn(AIGLongestPathDataflowPath path)
Definition AIG.cpp:149
bool aigLongestPathHistoryIsEmpty(AIGLongestPathHistory history)
Definition AIG.cpp:182
MlirOperation aigLongestPathDataflowPathGetRoot(AIGLongestPathDataflowPath path)
Definition AIG.cpp:173
MlirStringRef aigLongestPathObjectName(AIGLongestPathObject rawObject)
Definition AIG.cpp:226
AIGLongestPathHistory aigLongestPathDataflowPathGetHistory(AIGLongestPathDataflowPath path)
Definition AIG.cpp:166
AIGLongestPathObject wrap(llvm::PointerUnion< Object *, DataflowPath::OutputPort * > object)
Definition AIG.cpp:57
void registerAIGPasses()
Definition AIG.cpp:34
AIGLongestPathCollection aigLongestPathAnalysisGetAllPaths(AIGLongestPathAnalysis analysis, MlirStringRef moduleName, bool elaboratePaths)
Definition AIG.cpp:96
AIGLongestPathDataflowPath aigLongestPathCollectionGetDataflowPath(AIGLongestPathCollection collection, size_t index)
Definition AIG.cpp:132
void aigLongestPathAnalysisDestroy(AIGLongestPathAnalysis analysis)
Definition AIG.cpp:91
void aigLongestPathCollectionDestroy(AIGLongestPathCollection collection)
Definition AIG.cpp:121
DEFINE_C_API_PTR_METHODS(AIGLongestPathHistory, llvm::ImmutableListImpl< DebugPoint >) llvm
Definition AIG.cpp:45
size_t aigLongestPathCollectionGetSize(AIGLongestPathCollection collection)
Definition AIG.cpp:125
void aigLongestPathHistoryGetHead(AIGLongestPathHistory history, AIGLongestPathObject *object, int64_t *delay, MlirStringRef *comment)
Definition AIG.cpp:187
IgraphInstancePath aigLongestPathObjectGetInstancePath(AIGLongestPathObject object)
Definition AIG.cpp:212
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition OM.cpp:111
std::tuple< hw::HWModuleOp, size_t, size_t > OutputPort
Definition aig.py:1
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
std::unique_ptr< LongestPathAnalysis > analysis
Definition AIG.cpp:39
std::unique_ptr< mlir::ModuleAnalysisManager > analysisManager
Definition AIG.cpp:38