CIRCT 22.0.0git
Loading...
Searching...
No Matches
ESI.cpp
Go to the documentation of this file.
1//===- ESI.cpp - C interface for the ESI dialect --------------------------===//
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
13#include "mlir/CAPI/IR.h"
14#include "mlir/CAPI/Registration.h"
15#include "mlir/CAPI/Support.h"
16#include "mlir/CAPI/Utils.h"
17#include "mlir/IR/Builders.h"
18#include "mlir/IR/SymbolTable.h"
19#include "mlir/Parser/Parser.h"
20#include "mlir/Support/FileUtilities.h"
21
22using namespace circt;
23using namespace circt::esi;
24
25MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(ESI, esi, circt::esi::ESIDialect)
26
28
29bool circtESITypeIsAChannelType(MlirType type) {
30 return isa<ChannelType>(unwrap(type));
31}
32
33MlirType circtESIChannelTypeGet(MlirType inner, uint32_t signaling,
34 uint64_t dataDelay) {
35 auto signalEnum = symbolizeChannelSignaling(signaling);
36 if (!signalEnum)
37 return {};
38 auto cppInner = unwrap(inner);
39 return wrap(ChannelType::get(cppInner.getContext(), cppInner, *signalEnum,
40 dataDelay));
41}
42
43MlirType circtESIChannelGetInner(MlirType channelType) {
44 return wrap(cast<ChannelType>(unwrap(channelType)).getInner());
45}
46uint32_t circtESIChannelGetSignaling(MlirType channelType) {
47 return (uint32_t)cast<ChannelType>(unwrap(channelType)).getSignaling();
48}
49uint64_t circtESIChannelGetDataDelay(MlirType channelType) {
50 return cast<ChannelType>(unwrap(channelType)).getDataDelay();
51}
52
53bool circtESITypeIsAnAnyType(MlirType type) {
54 return isa<AnyType>(unwrap(type));
55}
56MlirType circtESIAnyTypeGet(MlirContext ctxt) {
57 return wrap(AnyType::get(unwrap(ctxt)));
58}
59
60bool circtESITypeIsAListType(MlirType type) {
61 return isa<ListType>(unwrap(type));
62}
63
64MlirType circtESIListTypeGet(MlirType inner) {
65 auto cppInner = unwrap(inner);
66 return wrap(ListType::get(cppInner.getContext(), cppInner));
67}
68
69MlirType circtESIListTypeGetElementType(MlirType list) {
70 return wrap(cast<ListType>(unwrap(list)).getElementType());
71}
72
73bool circtESICheckInnerTypeMatch(MlirType to, MlirType from) {
74 return succeeded(checkInnerTypeMatch(unwrap(to), unwrap(from)));
75}
76
77void circtESIAppendMlirFile(MlirModule cMod, MlirStringRef filename) {
78 ModuleOp modOp = unwrap(cMod);
79 auto loadedMod =
80 mlir::parseSourceFile<ModuleOp>(unwrap(filename), modOp.getContext());
81 Block *loadedBlock = loadedMod->getBody();
82 assert(!modOp->getRegions().empty());
83 if (modOp.getBodyRegion().empty()) {
84 modOp.getBodyRegion().push_back(loadedBlock);
85 return;
86 }
87 auto &ops = modOp.getBody()->getOperations();
88 ops.splice(ops.end(), loadedBlock->getOperations());
89}
90MlirOperation circtESILookup(MlirModule mod, MlirStringRef symbol) {
91 return wrap(SymbolTable::lookupSymbolIn(unwrap(mod), unwrap(symbol)));
92}
93
95 MlirStringRef impl_type, CirctESIServiceGeneratorFunc genFunc,
96 void *userData) {
98 unwrap(impl_type), [genFunc, userData](ServiceImplementReqOp req,
99 ServiceDeclOpInterface decl,
100 ServiceImplRecordOp record) {
101 return unwrap(genFunc(wrap(req), wrap(decl.getOperation()),
102 wrap(record.getOperation()), userData));
103 });
104}
105//===----------------------------------------------------------------------===//
106// Channel bundles
107//===----------------------------------------------------------------------===//
108
109bool circtESITypeIsABundleType(MlirType type) {
110 return isa<ChannelBundleType>(unwrap(type));
111}
112MlirType circtESIBundleTypeGet(MlirContext cctxt, size_t numChannels,
113 const CirctESIBundleTypeBundleChannel *channels,
114 bool resettable) {
115 MLIRContext *ctxt = unwrap(cctxt);
116 SmallVector<BundledChannel, 4> channelsVector(llvm::map_range(
117 ArrayRef<CirctESIBundleTypeBundleChannel>(channels, numChannels),
118 [](auto channel) {
119 return BundledChannel{cast<StringAttr>(unwrap(channel.name)),
120 (ChannelDirection)channel.direction,
121 cast<ChannelType>(unwrap(channel.channelType))};
122 }));
123 return wrap(ChannelBundleType::get(
124 ctxt, channelsVector, resettable ? UnitAttr::get(ctxt) : UnitAttr()));
125}
126bool circtESIBundleTypeGetResettable(MlirType bundle) {
127 return cast<ChannelBundleType>(unwrap(bundle)).getResettable() != UnitAttr();
128}
129size_t circtESIBundleTypeGetNumChannels(MlirType bundle) {
130 return cast<ChannelBundleType>(unwrap(bundle)).getChannels().size();
131}
133 size_t idx) {
134 BundledChannel channel =
135 cast<ChannelBundleType>(unwrap(bundle)).getChannels()[idx];
137 wrap(channel.name), (unsigned)channel.direction, wrap(channel.type)};
138}
139
140//===----------------------------------------------------------------------===//
141// Window types
142//===----------------------------------------------------------------------===//
143
144bool circtESITypeIsAWindowType(MlirType type) {
145 return isa<WindowType>(unwrap(type));
146}
147
148MlirType circtESIWindowTypeGet(MlirContext cctxt, MlirAttribute name,
149 MlirType into, size_t numFrames,
150 const MlirType *cFrames) {
151 MLIRContext *ctxt = unwrap(cctxt);
152 SmallVector<WindowFrameType, 4> frames;
153 for (size_t i = 0; i < numFrames; ++i)
154 frames.push_back(cast<WindowFrameType>(unwrap(cFrames[i])));
155 return wrap(WindowType::get(ctxt, cast<StringAttr>(unwrap(name)),
156 unwrap(into), frames));
157}
158
159MlirAttribute circtESIWindowTypeGetName(MlirType window) {
160 return wrap((Attribute)cast<WindowType>(unwrap(window)).getName());
161}
162
163MlirType circtESIWindowTypeGetInto(MlirType window) {
164 return wrap(cast<WindowType>(unwrap(window)).getInto());
165}
166
167size_t circtESIWindowTypeGetNumFrames(MlirType window) {
168 return cast<WindowType>(unwrap(window)).getFrames().size();
169}
170
171MlirType circtESIWindowTypeGetFrame(MlirType window, size_t idx) {
172 return wrap(cast<WindowType>(unwrap(window)).getFrames()[idx]);
173}
174
175MlirType circtESIWindowTypeGetLoweredType(MlirType window) {
176 return wrap(cast<WindowType>(unwrap(window)).getLoweredType());
177}
178
180 return isa<WindowFrameType>(unwrap(type));
181}
182
183MlirType circtESIWindowFrameTypeGet(MlirContext cctxt, MlirAttribute name,
184 size_t numMembers,
185 const MlirType *cMembers) {
186 MLIRContext *ctxt = unwrap(cctxt);
187 SmallVector<WindowFieldType, 4> members;
188 for (size_t i = 0; i < numMembers; ++i)
189 members.push_back(cast<WindowFieldType>(unwrap(cMembers[i])));
190 return wrap(
191 WindowFrameType::get(ctxt, cast<StringAttr>(unwrap(name)), members));
192}
193
194MlirAttribute circtESIWindowFrameTypeGetName(MlirType frame) {
195 return wrap((Attribute)cast<WindowFrameType>(unwrap(frame)).getName());
196}
197
199 return cast<WindowFrameType>(unwrap(frame)).getMembers().size();
200}
201
202MlirType circtESIWindowFrameTypeGetMember(MlirType frame, size_t idx) {
203 return wrap(cast<WindowFrameType>(unwrap(frame)).getMembers()[idx]);
204}
205
207 return isa<WindowFieldType>(unwrap(type));
208}
209
210MlirType circtESIWindowFieldTypeGet(MlirContext cctxt, MlirAttribute fieldName,
211 uint64_t numItems) {
212 return wrap(WindowFieldType::get(
213 unwrap(cctxt), cast<StringAttr>(unwrap(fieldName)), numItems));
214}
215
216MlirAttribute circtESIWindowFieldTypeGetFieldName(MlirType field) {
217 return wrap((Attribute)cast<WindowFieldType>(unwrap(field)).getFieldName());
218}
219
220uint64_t circtESIWindowFieldTypeGetNumItems(MlirType field) {
221 return cast<WindowFieldType>(unwrap(field)).getNumItems();
222}
223
224//===----------------------------------------------------------------------===//
225// AppID
226//===----------------------------------------------------------------------===//
227
228bool circtESIAttributeIsAnAppIDAttr(MlirAttribute attr) {
229 return isa<AppIDAttr>(unwrap(attr));
230}
231
232MlirAttribute circtESIAppIDAttrGet(MlirContext ctxt, MlirStringRef name,
233 uint64_t index) {
234 return wrap(AppIDAttr::get(
235 unwrap(ctxt), StringAttr::get(unwrap(ctxt), unwrap(name)), index));
236}
237MlirAttribute circtESIAppIDAttrGetNoIdx(MlirContext ctxt, MlirStringRef name) {
238 return wrap(AppIDAttr::get(
239 unwrap(ctxt), StringAttr::get(unwrap(ctxt), unwrap(name)), std::nullopt));
240}
241MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr) {
242 return wrap(cast<AppIDAttr>(unwrap(attr)).getName().getValue());
243}
244bool circtESIAppIDAttrGetIndex(MlirAttribute attr, uint64_t *indexOut) {
245 std::optional<uint64_t> index = cast<AppIDAttr>(unwrap(attr)).getIndex();
246 if (!index)
247 return false;
248 *indexOut = index.value();
249 return true;
250}
251
252bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute attr) {
253 return isa<AppIDPathAttr>(unwrap(attr));
254}
255
256MlirAttribute circtESIAppIDAttrPathGet(MlirContext ctxt, MlirAttribute root,
257 intptr_t numElements,
258 MlirAttribute const *cElements) {
259 SmallVector<AppIDAttr, 8> elements;
260 for (intptr_t i = 0; i < numElements; ++i)
261 elements.push_back(cast<AppIDAttr>(unwrap(cElements[i])));
262 return wrap(AppIDPathAttr::get(
263 unwrap(ctxt), cast<FlatSymbolRefAttr>(unwrap(root)), elements));
264}
265MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr) {
266 return wrap(cast<AppIDPathAttr>(unwrap(attr)).getRoot());
267}
268uint64_t circtESIAppIDAttrPathGetNumComponents(MlirAttribute attr) {
269 return cast<AppIDPathAttr>(unwrap(attr)).getPath().size();
270}
271MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr,
272 uint64_t index) {
273 return wrap(cast<AppIDPathAttr>(unwrap(attr)).getPath()[index]);
274}
275
277
278/// Create an index of appids through which to do appid lookups efficiently.
279MLIR_CAPI_EXPORTED CirctESIAppIDIndex
280circtESIAppIDIndexGet(MlirOperation root) {
281 auto *idx = new AppIDIndex(unwrap(root));
282 if (idx->isValid())
283 return wrap(idx);
284 return CirctESIAppIDIndex{nullptr};
285}
286
287/// Free an AppIDIndex.
288MLIR_CAPI_EXPORTED void circtESIAppIDIndexFree(CirctESIAppIDIndex index) {
289 delete unwrap(index);
290}
291
292MLIR_CAPI_EXPORTED MlirAttribute
294 auto mod = cast<hw::HWModuleLike>(unwrap(op));
295 return wrap(unwrap(idx)->getChildAppIDsOf(mod));
296}
297
298MLIR_CAPI_EXPORTED
300 MlirOperation fromMod,
301 MlirAttribute appid,
302 MlirLocation loc) {
303 auto mod = cast<hw::HWModuleLike>(unwrap(fromMod));
304 auto path = cast<AppIDAttr>(unwrap(appid));
305 FailureOr<ArrayAttr> instPath =
306 unwrap(idx)->getAppIDPathAttr(mod, path, unwrap(loc));
307 if (failed(instPath))
308 return MlirAttribute{nullptr};
309 return wrap(*instPath);
310}
assert(baseType &&"element must be base type")
return wrap(CMemoryType::get(unwrap(ctx), baseType, numElements))
MlirType uint64_t numElements
Definition CHIRRTL.cpp:30
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
void circtESIRegisterGlobalServiceGenerator(MlirStringRef impl_type, CirctESIServiceGeneratorFunc genFunc, void *userData)
Definition ESI.cpp:94
MlirType circtESIWindowTypeGetLoweredType(MlirType window)
Definition ESI.cpp:175
MlirType circtESIAnyTypeGet(MlirContext ctxt)
Definition ESI.cpp:56
bool circtESIAppIDAttrGetIndex(MlirAttribute attr, uint64_t *indexOut)
Definition ESI.cpp:244
MlirType circtESIWindowTypeGet(MlirContext cctxt, MlirAttribute name, MlirType into, size_t numFrames, const MlirType *cFrames)
Definition ESI.cpp:148
bool circtESITypeIsAnAnyType(MlirType type)
Definition ESI.cpp:53
MlirType circtESIWindowFrameTypeGet(MlirContext cctxt, MlirAttribute name, size_t numMembers, const MlirType *cMembers)
Definition ESI.cpp:183
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetAppIDPath(CirctESIAppIDIndex idx, MlirOperation fromMod, MlirAttribute appid, MlirLocation loc)
Definition ESI.cpp:299
bool circtESIBundleTypeGetResettable(MlirType bundle)
Definition ESI.cpp:126
bool circtESITypeIsAWindowFrameType(MlirType type)
Definition ESI.cpp:179
uint64_t circtESIWindowFieldTypeGetNumItems(MlirType field)
Definition ESI.cpp:220
void circtESIAppendMlirFile(MlirModule cMod, MlirStringRef filename)
Definition ESI.cpp:77
MlirType circtESIWindowFrameTypeGetMember(MlirType frame, size_t idx)
Definition ESI.cpp:202
MlirType circtESIChannelGetInner(MlirType channelType)
Definition ESI.cpp:43
bool circtESITypeIsABundleType(MlirType type)
Definition ESI.cpp:109
bool circtESICheckInnerTypeMatch(MlirType to, MlirType from)
Definition ESI.cpp:73
MlirAttribute circtESIWindowFrameTypeGetName(MlirType frame)
Definition ESI.cpp:194
uint64_t circtESIAppIDAttrPathGetNumComponents(MlirAttribute attr)
Definition ESI.cpp:268
uint64_t circtESIChannelGetDataDelay(MlirType channelType)
Definition ESI.cpp:49
MlirOperation circtESILookup(MlirModule mod, MlirStringRef symbol)
Definition ESI.cpp:90
MlirType circtESIWindowTypeGetInto(MlirType window)
Definition ESI.cpp:163
MLIR_CAPI_EXPORTED void circtESIAppIDIndexFree(CirctESIAppIDIndex index)
Free an AppIDIndex.
Definition ESI.cpp:288
MlirAttribute circtESIWindowTypeGetName(MlirType window)
Definition ESI.cpp:159
MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr)
Definition ESI.cpp:241
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetChildAppIDsOf(CirctESIAppIDIndex idx, MlirOperation op)
Definition ESI.cpp:293
MlirAttribute circtESIAppIDAttrPathGet(MlirContext ctxt, MlirAttribute root, intptr_t numElements, MlirAttribute const *cElements)
Definition ESI.cpp:256
size_t circtESIBundleTypeGetNumChannels(MlirType bundle)
Definition ESI.cpp:129
MlirType circtESIListTypeGet(MlirType inner)
Definition ESI.cpp:64
MlirType circtESIWindowTypeGetFrame(MlirType window, size_t idx)
Definition ESI.cpp:171
bool circtESIAttributeIsAnAppIDAttr(MlirAttribute attr)
Definition ESI.cpp:228
size_t circtESIWindowTypeGetNumFrames(MlirType window)
Definition ESI.cpp:167
uint32_t circtESIChannelGetSignaling(MlirType channelType)
Definition ESI.cpp:46
MlirType circtESIWindowFieldTypeGet(MlirContext cctxt, MlirAttribute fieldName, uint64_t numItems)
Definition ESI.cpp:210
size_t circtESIWindowFrameTypeGetNumMembers(MlirType frame)
Definition ESI.cpp:198
bool circtESITypeIsAWindowType(MlirType type)
Definition ESI.cpp:144
CirctESIBundleTypeBundleChannel circtESIBundleTypeGetChannel(MlirType bundle, size_t idx)
Definition ESI.cpp:132
MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr, uint64_t index)
Definition ESI.cpp:271
MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr)
Definition ESI.cpp:265
bool circtESITypeIsAListType(MlirType type)
Definition ESI.cpp:60
bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute attr)
Definition ESI.cpp:252
MlirType circtESIListTypeGetElementType(MlirType list)
Definition ESI.cpp:69
bool circtESITypeIsAWindowFieldType(MlirType type)
Definition ESI.cpp:206
bool circtESITypeIsAChannelType(MlirType type)
Definition ESI.cpp:29
MlirType circtESIBundleTypeGet(MlirContext cctxt, size_t numChannels, const CirctESIBundleTypeBundleChannel *channels, bool resettable)
Definition ESI.cpp:112
MLIR_CAPI_EXPORTED CirctESIAppIDIndex circtESIAppIDIndexGet(MlirOperation root)
Create an index of appids through which to do appid lookups efficiently.
Definition ESI.cpp:280
MlirType circtESIChannelTypeGet(MlirType inner, uint32_t signaling, uint64_t dataDelay)
Definition ESI.cpp:33
MlirAttribute circtESIAppIDAttrGet(MlirContext ctxt, MlirStringRef name, uint64_t index)
Definition ESI.cpp:232
MlirAttribute circtESIWindowFieldTypeGetFieldName(MlirType field)
Definition ESI.cpp:216
MlirAttribute circtESIAppIDAttrGetNoIdx(MlirContext ctxt, MlirStringRef name)
Definition ESI.cpp:237
MlirLogicalResult(* CirctESIServiceGeneratorFunc)(MlirOperation serviceImplementReqOp, MlirOperation declOp, MlirOperation recordOp, void *userData)
Definition ESI.h:103
static bool getFieldName(const FieldRef &fieldRef, SmallString< 32 > &string)
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition OM.cpp:111
DEFINE_C_API_PTR_METHODS(SynthLongestPathHistory, llvm::ImmutableListImpl< DebugPoint >) llvm
Definition Synth.cpp:49
An index for resolving AppIDPaths to dynamic instances.
Definition AppID.h:32
void registerGenerator(StringRef implType, ServiceGeneratorFunc gen)
Add a generator to this registry.
static ServiceGeneratorDispatcher & globalDispatcher()
Get the global dispatcher.
LogicalResult checkInnerTypeMatch(Type expected, Type actual)
Definition ESIOps.cpp:391
void registerESIPasses()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition esi.py:1
ChannelDirection direction
Definition ESITypes.h:38