CIRCT 20.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// AppID
142//===----------------------------------------------------------------------===//
143
144bool circtESIAttributeIsAnAppIDAttr(MlirAttribute attr) {
145 return isa<AppIDAttr>(unwrap(attr));
146}
147
148MlirAttribute circtESIAppIDAttrGet(MlirContext ctxt, MlirStringRef name,
149 uint64_t index) {
150 return wrap(AppIDAttr::get(
151 unwrap(ctxt), StringAttr::get(unwrap(ctxt), unwrap(name)), index));
152}
153MlirAttribute circtESIAppIDAttrGetNoIdx(MlirContext ctxt, MlirStringRef name) {
154 return wrap(AppIDAttr::get(
155 unwrap(ctxt), StringAttr::get(unwrap(ctxt), unwrap(name)), std::nullopt));
156}
157MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr) {
158 return wrap(cast<AppIDAttr>(unwrap(attr)).getName().getValue());
159}
160bool circtESIAppIDAttrGetIndex(MlirAttribute attr, uint64_t *indexOut) {
161 std::optional<uint64_t> index = cast<AppIDAttr>(unwrap(attr)).getIndex();
162 if (!index)
163 return false;
164 *indexOut = index.value();
165 return true;
166}
167
168bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute attr) {
169 return isa<AppIDPathAttr>(unwrap(attr));
170}
171
172MlirAttribute circtESIAppIDAttrPathGet(MlirContext ctxt, MlirAttribute root,
173 intptr_t numElements,
174 MlirAttribute const *cElements) {
175 SmallVector<AppIDAttr, 8> elements;
176 for (intptr_t i = 0; i < numElements; ++i)
177 elements.push_back(cast<AppIDAttr>(unwrap(cElements[i])));
178 return wrap(AppIDPathAttr::get(
179 unwrap(ctxt), cast<FlatSymbolRefAttr>(unwrap(root)), elements));
180}
181MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr) {
182 return wrap(cast<AppIDPathAttr>(unwrap(attr)).getRoot());
183}
184uint64_t circtESIAppIDAttrPathGetNumComponents(MlirAttribute attr) {
185 return cast<AppIDPathAttr>(unwrap(attr)).getPath().size();
186}
187MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr,
188 uint64_t index) {
189 return wrap(cast<AppIDPathAttr>(unwrap(attr)).getPath()[index]);
190}
191
193
194/// Create an index of appids through which to do appid lookups efficiently.
195MLIR_CAPI_EXPORTED CirctESIAppIDIndex
196circtESIAppIDIndexGet(MlirOperation root) {
197 auto *idx = new AppIDIndex(unwrap(root));
198 if (idx->isValid())
199 return wrap(idx);
200 return CirctESIAppIDIndex{nullptr};
201}
202
203/// Free an AppIDIndex.
204MLIR_CAPI_EXPORTED void circtESIAppIDIndexFree(CirctESIAppIDIndex index) {
205 delete unwrap(index);
206}
207
208MLIR_CAPI_EXPORTED MlirAttribute
210 auto mod = cast<hw::HWModuleLike>(unwrap(op));
211 return wrap(unwrap(idx)->getChildAppIDsOf(mod));
212}
213
214MLIR_CAPI_EXPORTED
216 MlirOperation fromMod,
217 MlirAttribute appid,
218 MlirLocation loc) {
219 auto mod = cast<hw::HWModuleLike>(unwrap(fromMod));
220 auto path = cast<AppIDAttr>(unwrap(appid));
221 FailureOr<ArrayAttr> instPath =
222 unwrap(idx)->getAppIDPathAttr(mod, path, unwrap(loc));
223 if (failed(instPath))
224 return MlirAttribute{nullptr};
225 return wrap(*instPath);
226}
DEFINE_C_API_PTR_METHODS(CirctFirtoolFirtoolOptions, circt::firtool::FirtoolOptions) CirctFirtoolFirtoolOptions circtFirtoolOptionsCreateDefault()
Definition Firtool.cpp:18
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 circtESIAnyTypeGet(MlirContext ctxt)
Definition ESI.cpp:56
bool circtESIAppIDAttrGetIndex(MlirAttribute attr, uint64_t *indexOut)
Definition ESI.cpp:160
bool circtESITypeIsAnAnyType(MlirType type)
Definition ESI.cpp:53
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetAppIDPath(CirctESIAppIDIndex idx, MlirOperation fromMod, MlirAttribute appid, MlirLocation loc)
Definition ESI.cpp:215
bool circtESIBundleTypeGetResettable(MlirType bundle)
Definition ESI.cpp:126
void circtESIAppendMlirFile(MlirModule cMod, MlirStringRef filename)
Definition ESI.cpp:77
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
uint64_t circtESIAppIDAttrPathGetNumComponents(MlirAttribute attr)
Definition ESI.cpp:184
uint64_t circtESIChannelGetDataDelay(MlirType channelType)
Definition ESI.cpp:49
MlirOperation circtESILookup(MlirModule mod, MlirStringRef symbol)
Definition ESI.cpp:90
MLIR_CAPI_EXPORTED void circtESIAppIDIndexFree(CirctESIAppIDIndex index)
Free an AppIDIndex.
Definition ESI.cpp:204
MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr)
Definition ESI.cpp:157
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetChildAppIDsOf(CirctESIAppIDIndex idx, MlirOperation op)
Definition ESI.cpp:209
MlirAttribute circtESIAppIDAttrPathGet(MlirContext ctxt, MlirAttribute root, intptr_t numElements, MlirAttribute const *cElements)
Definition ESI.cpp:172
size_t circtESIBundleTypeGetNumChannels(MlirType bundle)
Definition ESI.cpp:129
MlirType circtESIListTypeGet(MlirType inner)
Definition ESI.cpp:64
bool circtESIAttributeIsAnAppIDAttr(MlirAttribute attr)
Definition ESI.cpp:144
uint32_t circtESIChannelGetSignaling(MlirType channelType)
Definition ESI.cpp:46
CirctESIBundleTypeBundleChannel circtESIBundleTypeGetChannel(MlirType bundle, size_t idx)
Definition ESI.cpp:132
MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr, uint64_t index)
Definition ESI.cpp:187
MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr)
Definition ESI.cpp:181
bool circtESITypeIsAListType(MlirType type)
Definition ESI.cpp:60
bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute attr)
Definition ESI.cpp:168
MlirType circtESIListTypeGetElementType(MlirType list)
Definition ESI.cpp:69
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:196
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:148
MlirAttribute circtESIAppIDAttrGetNoIdx(MlirContext ctxt, MlirStringRef name)
Definition ESI.cpp:153
MlirLogicalResult(* CirctESIServiceGeneratorFunc)(MlirOperation serviceImplementReqOp, MlirOperation declOp, MlirOperation recordOp, void *userData)
Definition ESI.h:68
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition OM.cpp:113
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:415
void registerESIPasses()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition esi.py:1
ChannelDirection direction
Definition ESITypes.h:38