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