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