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 unwrap(type).isa<ChannelType>();
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(unwrap(channelType).cast<ChannelType>().getInner());
43 }
44 uint32_t circtESIChannelGetSignaling(MlirType channelType) {
45  return (uint32_t)unwrap(channelType).cast<ChannelType>().getSignaling();
46 }
47 
48 bool circtESITypeIsAnAnyType(MlirType type) {
49  return unwrap(type).isa<AnyType>();
50 }
51 MlirType circtESIAnyTypeGet(MlirContext ctxt) {
52  return wrap(AnyType::get(unwrap(ctxt)));
53 }
54 
55 bool circtESITypeIsAListType(MlirType type) {
56  return unwrap(type).isa<ListType>();
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(unwrap(list).cast<ListType>().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),
90  [genFunc, userData](ServiceImplementReqOp req,
91  ServiceDeclOpInterface decl, ServiceImplRecordOp) {
92  return unwrap(genFunc(wrap(req), wrap(decl.getOperation()), userData));
93  });
94 }
95 //===----------------------------------------------------------------------===//
96 // Channel bundles
97 //===----------------------------------------------------------------------===//
98 
99 bool circtESITypeIsABundleType(MlirType type) {
100  return isa<ChannelBundleType>(unwrap(type));
101 }
102 MlirType circtESIBundleTypeGet(MlirContext cctxt, size_t numChannels,
103  const CirctESIBundleTypeBundleChannel *channels,
104  bool resettable) {
105  MLIRContext *ctxt = unwrap(cctxt);
106  SmallVector<BundledChannel, 4> channelsVector(llvm::map_range(
107  ArrayRef<CirctESIBundleTypeBundleChannel>(channels, numChannels),
108  [](auto channel) {
109  return BundledChannel{cast<StringAttr>(unwrap(channel.name)),
110  (ChannelDirection)channel.direction,
111  cast<ChannelType>(unwrap(channel.channelType))};
112  }));
114  ctxt, channelsVector, resettable ? UnitAttr::get(ctxt) : UnitAttr()));
115 }
116 bool circtESIBundleTypeGetResettable(MlirType bundle) {
117  return cast<ChannelBundleType>(unwrap(bundle)).getResettable() != UnitAttr();
118 }
119 size_t circtESIBundleTypeGetNumChannels(MlirType bundle) {
120  return cast<ChannelBundleType>(unwrap(bundle)).getChannels().size();
121 }
123  size_t idx) {
124  BundledChannel channel =
125  cast<ChannelBundleType>(unwrap(bundle)).getChannels()[idx];
127  wrap(channel.name), (unsigned)channel.direction, wrap(channel.type)};
128 }
129 
130 //===----------------------------------------------------------------------===//
131 // AppID
132 //===----------------------------------------------------------------------===//
133 
134 bool circtESIAttributeIsAnAppIDAttr(MlirAttribute attr) {
135  return unwrap(attr).isa<AppIDAttr>();
136 }
137 
138 MlirAttribute circtESIAppIDAttrGet(MlirContext ctxt, MlirStringRef name,
139  uint64_t index) {
140  return wrap(AppIDAttr::get(
141  unwrap(ctxt), StringAttr::get(unwrap(ctxt), unwrap(name)), index));
142 }
143 MlirAttribute circtESIAppIDAttrGetNoIdx(MlirContext ctxt, MlirStringRef name) {
144  return wrap(AppIDAttr::get(
145  unwrap(ctxt), StringAttr::get(unwrap(ctxt), unwrap(name)), std::nullopt));
146 }
147 MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr) {
148  return wrap(unwrap(attr).cast<AppIDAttr>().getName().getValue());
149 }
150 bool circtESIAppIDAttrGetIndex(MlirAttribute attr, uint64_t *indexOut) {
151  std::optional<uint64_t> index = unwrap(attr).cast<AppIDAttr>().getIndex();
152  if (!index)
153  return false;
154  *indexOut = index.value();
155  return true;
156 }
157 
158 bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute attr) {
159  return isa<AppIDPathAttr>(unwrap(attr));
160 }
161 
162 MlirAttribute circtESIAppIDAttrPathGet(MlirContext ctxt, MlirAttribute root,
163  intptr_t numElements,
164  MlirAttribute const *cElements) {
165  SmallVector<AppIDAttr, 8> elements;
166  for (intptr_t i = 0; i < numElements; ++i)
167  elements.push_back(cast<AppIDAttr>(unwrap(cElements[i])));
168  return wrap(AppIDPathAttr::get(
169  unwrap(ctxt), cast<FlatSymbolRefAttr>(unwrap(root)), elements));
170 }
171 MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr) {
172  return wrap(cast<AppIDPathAttr>(unwrap(attr)).getRoot());
173 }
174 uint64_t circtESIAppIDAttrPathGetNumComponents(MlirAttribute attr) {
175  return cast<AppIDPathAttr>(unwrap(attr)).getPath().size();
176 }
177 MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr,
178  uint64_t index) {
179  return wrap(cast<AppIDPathAttr>(unwrap(attr)).getPath()[index]);
180 }
181 
183 
184 /// Create an index of appids through which to do appid lookups efficiently.
185 MLIR_CAPI_EXPORTED CirctESIAppIDIndex
186 circtESIAppIDIndexGet(MlirOperation root) {
187  auto *idx = new AppIDIndex(unwrap(root));
188  if (idx->isValid())
189  return wrap(idx);
190  return CirctESIAppIDIndex{nullptr};
191 }
192 
193 /// Free an AppIDIndex.
194 MLIR_CAPI_EXPORTED void circtESIAppIDIndexFree(CirctESIAppIDIndex index) {
195  delete unwrap(index);
196 }
197 
198 MLIR_CAPI_EXPORTED MlirAttribute
200  auto mod = cast<hw::HWModuleLike>(unwrap(op));
201  return wrap(unwrap(idx)->getChildAppIDsOf(mod));
202 }
203 
204 MLIR_CAPI_EXPORTED
206  MlirOperation fromMod,
207  MlirAttribute appid,
208  MlirLocation loc) {
209  auto mod = cast<hw::HWModuleLike>(unwrap(fromMod));
210  auto path = cast<AppIDAttr>(unwrap(appid));
211  FailureOr<ArrayAttr> instPath =
212  unwrap(idx)->getAppIDPathAttr(mod, path, unwrap(loc));
213  if (failed(instPath))
214  return MlirAttribute{nullptr};
215  return wrap(*instPath);
216 }
DEFINE_C_API_PTR_METHODS(CirctFirtoolFirtoolOptions, circt::firtool::FirtoolOptions) CirctFirtoolFirtoolOptions circtFirtoolOptionsCreateDefault()
Definition: Firtool.cpp:20
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:150
bool circtESITypeIsAnAnyType(MlirType type)
Definition: ESI.cpp:48
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetAppIDPath(CirctESIAppIDIndex idx, MlirOperation fromMod, MlirAttribute appid, MlirLocation loc)
Definition: ESI.cpp:205
bool circtESIBundleTypeGetResettable(MlirType bundle)
Definition: ESI.cpp:116
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:99
uint64_t circtESIAppIDAttrPathGetNumComponents(MlirAttribute attr)
Definition: ESI.cpp:174
MlirOperation circtESILookup(MlirModule mod, MlirStringRef symbol)
Definition: ESI.cpp:81
MLIR_CAPI_EXPORTED void circtESIAppIDIndexFree(CirctESIAppIDIndex index)
Free an AppIDIndex.
Definition: ESI.cpp:194
MlirType circtESIChannelTypeGet(MlirType inner, uint32_t signaling)
Definition: ESI.cpp:33
MlirStringRef circtESIAppIDAttrGetName(MlirAttribute attr)
Definition: ESI.cpp:147
MLIR_CAPI_EXPORTED MlirAttribute circtESIAppIDIndexGetChildAppIDsOf(CirctESIAppIDIndex idx, MlirOperation op)
Definition: ESI.cpp:199
MlirAttribute circtESIAppIDAttrPathGet(MlirContext ctxt, MlirAttribute root, intptr_t numElements, MlirAttribute const *cElements)
Definition: ESI.cpp:162
size_t circtESIBundleTypeGetNumChannels(MlirType bundle)
Definition: ESI.cpp:119
MlirType circtESIListTypeGet(MlirType inner)
Definition: ESI.cpp:59
bool circtESIAttributeIsAnAppIDAttr(MlirAttribute attr)
Definition: ESI.cpp:134
uint32_t circtESIChannelGetSignaling(MlirType channelType)
Definition: ESI.cpp:44
CirctESIBundleTypeBundleChannel circtESIBundleTypeGetChannel(MlirType bundle, size_t idx)
Definition: ESI.cpp:122
MlirAttribute circtESIAppIDAttrPathGetComponent(MlirAttribute attr, uint64_t index)
Definition: ESI.cpp:177
MlirAttribute circtESIAppIDAttrPathGetRoot(MlirAttribute attr)
Definition: ESI.cpp:171
bool circtESITypeIsAListType(MlirType type)
Definition: ESI.cpp:55
bool circtESIAttributeIsAnAppIDPathAttr(MlirAttribute attr)
Definition: ESI.cpp:158
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:102
MLIR_CAPI_EXPORTED CirctESIAppIDIndex circtESIAppIDIndexGet(MlirOperation root)
Create an index of appids through which to do appid lookups efficiently.
Definition: ESI.cpp:186
MlirAttribute circtESIAppIDAttrGet(MlirContext ctxt, MlirStringRef name, uint64_t index)
Definition: ESI.cpp:138
MlirAttribute circtESIAppIDAttrGetNoIdx(MlirContext ctxt, MlirStringRef name)
Definition: ESI.cpp:143
MlirLogicalResult(* CirctESIServiceGeneratorFunc)(MlirOperation serviceImplementReqOp, MlirOperation declOp, 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:264
StringAttr getName(ArrayAttr names, size_t idx)
Return the name at the specified index of the ArrayAttr or null if it cannot be determined.
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21
Definition: esi.py:1
ChannelDirection direction
Definition: ESITypes.h:38