CIRCT  19.0.0git
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 
9 #include "circt-c/Dialect/ESI.h"
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 
22 using namespace circt;
23 using namespace circt::esi;
24 
25 MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(ESI, esi, circt::esi::ESIDialect)
26 
28 
29 bool circtESITypeIsAChannelType(MlirType type) {
30  return isa<ChannelType>(unwrap(type));
31 }
32 
33 MlirType circtESIChannelTypeGet(MlirType inner, uint32_t signaling) {
34  auto signalEnum = symbolizeChannelSignaling(signaling);
35  if (!signalEnum)
36  return {};
37  auto cppInner = unwrap(inner);
38  return wrap(ChannelType::get(cppInner.getContext(), cppInner, *signalEnum));
39 }
40 
41 MlirType circtESIChannelGetInner(MlirType channelType) {
42  return wrap(cast<ChannelType>(unwrap(channelType)).getInner());
43 }
44 uint32_t circtESIChannelGetSignaling(MlirType channelType) {
45  return (uint32_t)cast<ChannelType>(unwrap(channelType)).getSignaling();
46 }
47 
48 bool circtESITypeIsAnAnyType(MlirType type) {
49  return isa<AnyType>(unwrap(type));
50 }
51 MlirType circtESIAnyTypeGet(MlirContext ctxt) {
52  return wrap(AnyType::get(unwrap(ctxt)));
53 }
54 
55 bool circtESITypeIsAListType(MlirType type) {
56  return isa<ListType>(unwrap(type));
57 }
58 
59 MlirType circtESIListTypeGet(MlirType inner) {
60  auto cppInner = unwrap(inner);
61  return wrap(ListType::get(cppInner.getContext(), cppInner));
62 }
63 
64 MlirType circtESIListTypeGetElementType(MlirType list) {
65  return wrap(cast<ListType>(unwrap(list)).getElementType());
66 }
67 
68 void circtESIAppendMlirFile(MlirModule cMod, MlirStringRef filename) {
69  ModuleOp modOp = unwrap(cMod);
70  auto loadedMod =
71  mlir::parseSourceFile<ModuleOp>(unwrap(filename), modOp.getContext());
72  Block *loadedBlock = loadedMod->getBody();
73  assert(!modOp->getRegions().empty());
74  if (modOp.getBodyRegion().empty()) {
75  modOp.getBodyRegion().push_back(loadedBlock);
76  return;
77  }
78  auto &ops = modOp.getBody()->getOperations();
79  ops.splice(ops.end(), loadedBlock->getOperations());
80 }
81 MlirOperation circtESILookup(MlirModule mod, MlirStringRef symbol) {
82  return wrap(SymbolTable::lookupSymbolIn(unwrap(mod), unwrap(symbol)));
83 }
84 
86  MlirStringRef impl_type, CirctESIServiceGeneratorFunc genFunc,
87  void *userData) {
89  unwrap(impl_type), [genFunc, userData](ServiceImplementReqOp req,
90  ServiceDeclOpInterface decl,
91  ServiceImplRecordOp record) {
92  return unwrap(genFunc(wrap(req), wrap(decl.getOperation()),
93  wrap(record.getOperation()), userData));
94  });
95 }
96 //===----------------------------------------------------------------------===//
97 // Channel bundles
98 //===----------------------------------------------------------------------===//
99 
100 bool circtESITypeIsABundleType(MlirType type) {
101  return isa<ChannelBundleType>(unwrap(type));
102 }
103 MlirType circtESIBundleTypeGet(MlirContext cctxt, size_t numChannels,
104  const CirctESIBundleTypeBundleChannel *channels,
105  bool resettable) {
106  MLIRContext *ctxt = unwrap(cctxt);
107  SmallVector<BundledChannel, 4> channelsVector(llvm::map_range(
108  ArrayRef<CirctESIBundleTypeBundleChannel>(channels, numChannels),
109  [](auto channel) {
110  return BundledChannel{cast<StringAttr>(unwrap(channel.name)),
111  (ChannelDirection)channel.direction,
112  cast<ChannelType>(unwrap(channel.channelType))};
113  }));
115  ctxt, channelsVector, resettable ? UnitAttr::get(ctxt) : UnitAttr()));
116 }
117 bool circtESIBundleTypeGetResettable(MlirType bundle) {
118  return cast<ChannelBundleType>(unwrap(bundle)).getResettable() != UnitAttr();
119 }
120 size_t circtESIBundleTypeGetNumChannels(MlirType bundle) {
121  return cast<ChannelBundleType>(unwrap(bundle)).getChannels().size();
122 }
124  size_t idx) {
125  BundledChannel channel =
126  cast<ChannelBundleType>(unwrap(bundle)).getChannels()[idx];
128  wrap(channel.name), (unsigned)channel.direction, wrap(channel.type)};
129 }
130 
131 //===----------------------------------------------------------------------===//
132 // AppID
133 //===----------------------------------------------------------------------===//
134 
135 bool circtESIAttributeIsAnAppIDAttr(MlirAttribute attr) {
136  return isa<AppIDAttr>(unwrap(attr));
137 }
138 
139 MlirAttribute circtESIAppIDAttrGet(MlirContext ctxt, MlirStringRef name,
140  uint64_t index) {
141  return wrap(AppIDAttr::get(
142  unwrap(ctxt), StringAttr::get(unwrap(ctxt), unwrap(name)), index));
143 }
144 MlirAttribute circtESIAppIDAttrGetNoIdx(MlirContext ctxt, MlirStringRef name) {
145  return wrap(AppIDAttr::get(
146  unwrap(ctxt), StringAttr::get(unwrap(ctxt), unwrap(name)), std::nullopt));
147 }
148 MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr) {
149  return wrap(cast<AppIDAttr>(unwrap(attr)).getName().getValue());
150 }
151 bool circtESIAppIDAttrGetIndex(MlirAttribute attr, uint64_t *indexOut) {
152  std::optional<uint64_t> index = cast<AppIDAttr>(unwrap(attr)).getIndex();
153  if (!index)
154  return false;
155  *indexOut = index.value();
156  return true;
157 }
158 
159 bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute attr) {
160  return isa<AppIDPathAttr>(unwrap(attr));
161 }
162 
163 MlirAttribute circtESIAppIDAttrPathGet(MlirContext ctxt, MlirAttribute root,
164  intptr_t numElements,
165  MlirAttribute const *cElements) {
166  SmallVector<AppIDAttr, 8> elements;
167  for (intptr_t i = 0; i < numElements; ++i)
168  elements.push_back(cast<AppIDAttr>(unwrap(cElements[i])));
169  return wrap(AppIDPathAttr::get(
170  unwrap(ctxt), cast<FlatSymbolRefAttr>(unwrap(root)), elements));
171 }
172 MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr) {
173  return wrap(cast<AppIDPathAttr>(unwrap(attr)).getRoot());
174 }
175 uint64_t circtESIAppIDAttrPathGetNumComponents(MlirAttribute attr) {
176  return cast<AppIDPathAttr>(unwrap(attr)).getPath().size();
177 }
178 MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr,
179  uint64_t index) {
180  return wrap(cast<AppIDPathAttr>(unwrap(attr)).getPath()[index]);
181 }
182 
184 
185 /// Create an index of appids through which to do appid lookups efficiently.
186 MLIR_CAPI_EXPORTED CirctESIAppIDIndex
187 circtESIAppIDIndexGet(MlirOperation root) {
188  auto *idx = new AppIDIndex(unwrap(root));
189  if (idx->isValid())
190  return wrap(idx);
191  return CirctESIAppIDIndex{nullptr};
192 }
193 
194 /// Free an AppIDIndex.
195 MLIR_CAPI_EXPORTED void circtESIAppIDIndexFree(CirctESIAppIDIndex index) {
196  delete unwrap(index);
197 }
198 
199 MLIR_CAPI_EXPORTED MlirAttribute
201  auto mod = cast<hw::HWModuleLike>(unwrap(op));
202  return wrap(unwrap(idx)->getChildAppIDsOf(mod));
203 }
204 
205 MLIR_CAPI_EXPORTED
207  MlirOperation fromMod,
208  MlirAttribute appid,
209  MlirLocation loc) {
210  auto mod = cast<hw::HWModuleLike>(unwrap(fromMod));
211  auto path = cast<AppIDAttr>(unwrap(appid));
212  FailureOr<ArrayAttr> instPath =
213  unwrap(idx)->getAppIDPathAttr(mod, path, unwrap(loc));
214  if (failed(instPath))
215  return MlirAttribute{nullptr};
216  return wrap(*instPath);
217 }
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:85
MlirType circtESIAnyTypeGet(MlirContext ctxt)
Definition: ESI.cpp:51
bool circtESIAppIDAttrGetIndex(MlirAttribute attr, uint64_t *indexOut)
Definition: ESI.cpp:151
bool circtESITypeIsAnAnyType(MlirType type)
Definition: ESI.cpp:48
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetAppIDPath(CirctESIAppIDIndex idx, MlirOperation fromMod, MlirAttribute appid, MlirLocation loc)
Definition: ESI.cpp:206
bool circtESIBundleTypeGetResettable(MlirType bundle)
Definition: ESI.cpp:117
void circtESIAppendMlirFile(MlirModule cMod, MlirStringRef filename)
Definition: ESI.cpp:68
MlirType circtESIChannelGetInner(MlirType channelType)
Definition: ESI.cpp:41
bool circtESITypeIsABundleType(MlirType type)
Definition: ESI.cpp:100
uint64_t circtESIAppIDAttrPathGetNumComponents(MlirAttribute attr)
Definition: ESI.cpp:175
MlirOperation circtESILookup(MlirModule mod, MlirStringRef symbol)
Definition: ESI.cpp:81
MLIR_CAPI_EXPORTED void circtESIAppIDIndexFree(CirctESIAppIDIndex index)
Free an AppIDIndex.
Definition: ESI.cpp:195
MlirType circtESIChannelTypeGet(MlirType inner, uint32_t signaling)
Definition: ESI.cpp:33
MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr)
Definition: ESI.cpp:148
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetChildAppIDsOf(CirctESIAppIDIndex idx, MlirOperation op)
Definition: ESI.cpp:200
MlirAttribute circtESIAppIDAttrPathGet(MlirContext ctxt, MlirAttribute root, intptr_t numElements, MlirAttribute const *cElements)
Definition: ESI.cpp:163
size_t circtESIBundleTypeGetNumChannels(MlirType bundle)
Definition: ESI.cpp:120
MlirType circtESIListTypeGet(MlirType inner)
Definition: ESI.cpp:59
bool circtESIAttributeIsAnAppIDAttr(MlirAttribute attr)
Definition: ESI.cpp:135
uint32_t circtESIChannelGetSignaling(MlirType channelType)
Definition: ESI.cpp:44
CirctESIBundleTypeBundleChannel circtESIBundleTypeGetChannel(MlirType bundle, size_t idx)
Definition: ESI.cpp:123
MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr, uint64_t index)
Definition: ESI.cpp:178
MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr)
Definition: ESI.cpp:172
bool circtESITypeIsAListType(MlirType type)
Definition: ESI.cpp:55
bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute attr)
Definition: ESI.cpp:159
MlirType circtESIListTypeGetElementType(MlirType list)
Definition: ESI.cpp:64
bool circtESITypeIsAChannelType(MlirType type)
Definition: ESI.cpp:29
MlirType circtESIBundleTypeGet(MlirContext cctxt, size_t numChannels, const CirctESIBundleTypeBundleChannel *channels, bool resettable)
Definition: ESI.cpp:103
MLIR_CAPI_EXPORTED CirctESIAppIDIndex circtESIAppIDIndexGet(MlirOperation root)
Create an index of appids through which to do appid lookups efficiently.
Definition: ESI.cpp:187
MlirAttribute circtESIAppIDAttrGet(MlirContext ctxt, MlirStringRef name, uint64_t index)
Definition: ESI.cpp:139
MlirAttribute circtESIAppIDAttrGetNoIdx(MlirContext ctxt, MlirStringRef name)
Definition: ESI.cpp:144
MlirLogicalResult(* CirctESIServiceGeneratorFunc)(MlirOperation serviceImplementReqOp, MlirOperation declOp, MlirOperation recordOp, void *userData)
Definition: ESI.h:64
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition: OM.cpp:96
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.
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:54
void registerESIPasses()
Definition: ESIPasses.cpp:262
StringAttr getName(ArrayAttr names, size_t idx)
Return the name at the specified index of the ArrayAttr or null if it cannot be determined.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: esi.py:1
ChannelDirection direction
Definition: ESITypes.h:38