CIRCT  18.0.0git
OM.cpp
Go to the documentation of this file.
1 //===- OM.cpp - C Interface for the OM 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 // Implements a C Interface for the OM Dialect
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "circt-c/Dialect/OM.h"
17 #include "mlir/CAPI/Registration.h"
18 #include "mlir/CAPI/Wrap.h"
19 #include "mlir/IR/Location.h"
20 #include "llvm/ADT/Hashing.h"
21 #include "llvm/Support/Casting.h"
22 
23 using namespace mlir;
24 using namespace circt::om;
25 
26 //===----------------------------------------------------------------------===//
27 // Dialect API.
28 //===----------------------------------------------------------------------===//
29 
31 
32 //===----------------------------------------------------------------------===//
33 // Type API.
34 //===----------------------------------------------------------------------===//
35 
36 /// Is the Type a ClassType.
37 bool omTypeIsAClassType(MlirType type) { return unwrap(type).isa<ClassType>(); }
38 
39 /// Get the TypeID for a ClassType.
40 MlirTypeID omClassTypeGetTypeID() { return wrap(ClassType::getTypeID()); }
41 
42 /// Get the name for a ClassType.
43 MlirIdentifier omClassTypeGetName(MlirType type) {
44  return wrap(cast<ClassType>(unwrap(type)).getClassName().getAttr());
45 }
46 
47 /// Is the Type a FrozenBasePathType.
48 bool omTypeIsAFrozenBasePathType(MlirType type) {
49  return isa<FrozenBasePathType>(unwrap(type));
50 }
51 
52 /// Get the TypeID for a FrozenBasePathType.
55 }
56 
57 /// Is the Type a FrozenPathType.
58 bool omTypeIsAFrozenPathType(MlirType type) {
59  return isa<FrozenPathType>(unwrap(type));
60 }
61 
62 /// Get the TypeID for a FrozenPathType.
63 MlirTypeID omFrozenPathTypeGetTypeID(void) {
65 }
66 
67 /// Is the Type a StringType.
68 bool omTypeIsAStringType(MlirType type) {
69  return unwrap(type).isa<StringType>();
70 }
71 
72 /// Get a StringType.
73 MlirType omStringTypeGet(MlirContext ctx) {
74  return wrap(StringType::get(unwrap(ctx)));
75 }
76 
77 /// Return a key type of a map.
78 MlirType omMapTypeGetKeyType(MlirType type) {
79  return wrap(unwrap(type).cast<MapType>().getKeyType());
80 }
81 
82 //===----------------------------------------------------------------------===//
83 // Evaluator data structures.
84 //===----------------------------------------------------------------------===//
85 
86 DEFINE_C_API_PTR_METHODS(OMEvaluator, circt::om::Evaluator)
87 
88 /// Define our own wrap and unwrap instead of using the usual macro. This is To
89 /// handle the std::shared_ptr reference counts appropriately. We want to always
90 /// create *new* shared pointers to the EvaluatorValue when we wrap it for C, to
91 /// increment the reference count. We want to use the shared_from_this
92 /// functionality to ensure it is unwrapped into C++ with the correct reference
93 /// count.
94 
95 static inline OMEvaluatorValue wrap(EvaluatorValuePtr object) {
96  return OMEvaluatorValue{
97  static_cast<void *>((new EvaluatorValuePtr(std::move(object)))->get())};
98 }
99 
101  return static_cast<evaluator::EvaluatorValue *>(c.ptr)->shared_from_this();
102 }
103 
104 //===----------------------------------------------------------------------===//
105 // Evaluator API.
106 //===----------------------------------------------------------------------===//
107 
108 /// Construct an Evaluator with an IR module.
109 OMEvaluator omEvaluatorNew(MlirModule mod) {
110  // Just allocate and wrap the Evaluator.
111  return wrap(new Evaluator(unwrap(mod)));
112 }
113 
114 /// Use the Evaluator to Instantiate an Object from its class name and actual
115 /// parameters.
117  MlirAttribute className,
118  intptr_t nActualParams,
119  OMEvaluatorValue *actualParams) {
120  // Unwrap the Evaluator.
121  Evaluator *cppEvaluator = unwrap(evaluator);
122 
123  // Unwrap the className, which the client must supply as a StringAttr.
124  StringAttr cppClassName = unwrap(className).cast<StringAttr>();
125 
126  // Unwrap the actual parameters.
127  SmallVector<std::shared_ptr<evaluator::EvaluatorValue>> cppActualParams;
128  for (unsigned i = 0; i < nActualParams; i++)
129  cppActualParams.push_back(unwrap(actualParams[i]));
130 
131  // Invoke the Evaluator to instantiate the Object.
132  auto result = cppEvaluator->instantiate(cppClassName, cppActualParams);
133 
134  // If instantiation failed, return a null Object. A Diagnostic will be emitted
135  // in this case.
136  if (failed(result))
137  return OMEvaluatorValue();
138 
139  // Wrap and return the Object.
140  return wrap(result.value());
141 }
142 
143 /// Get the Module the Evaluator is built from.
144 MlirModule omEvaluatorGetModule(OMEvaluator evaluator) {
145  // Just unwrap the Evaluator, get the Module, and wrap it.
146  return wrap(unwrap(evaluator)->getModule());
147 }
148 
149 //===----------------------------------------------------------------------===//
150 // Object API.
151 //===----------------------------------------------------------------------===//
152 
153 /// Query if the Object is null.
155  // Just check if the Object shared pointer is null.
156  return !object.ptr;
157 }
158 
159 /// Get the Type from an Object, which will be a ClassType.
161  return wrap(llvm::cast<Object>(unwrap(object).get())->getType());
162 }
163 
164 /// Get the hash for the object.
166  return llvm::hash_value(llvm::cast<Object>(unwrap(object).get()));
167 }
168 
169 /// Check if two objects are same.
171  return llvm::cast<Object>(unwrap(object).get()) ==
172  llvm::cast<Object>(unwrap(other).get());
173 }
174 
175 /// Get an ArrayAttr with the names of the fields in an Object.
177  return wrap(llvm::cast<Object>(unwrap(object).get())->getFieldNames());
178 }
179 
181  return wrap(llvm::cast<evaluator::MapValue>(unwrap(value).get())->getType());
182 }
183 
184 /// Get an ArrayAttr with the keys in a Map.
186  return wrap(llvm::cast<evaluator::MapValue>(unwrap(object).get())->getKeys());
187 }
188 
189 /// Get a field from an Object, which must contain a field of that name.
191  MlirAttribute name) {
192  // Unwrap the Object and get the field of the name, which the client must
193  // supply as a StringAttr.
195  llvm::cast<Object>(unwrap(object).get())
196  ->getField(unwrap(name).cast<StringAttr>());
197 
198  // If getField failed, return a null EvaluatorValue. A Diagnostic will be
199  // emitted in this case.
200  if (failed(result))
201  return OMEvaluatorValue();
202 
203  return OMEvaluatorValue{wrap(result.value())};
204 }
205 
206 //===----------------------------------------------------------------------===//
207 // EvaluatorValue API.
208 //===----------------------------------------------------------------------===//
209 
210 // Get a context from an EvaluatorValue.
211 MlirContext omEvaluatorValueGetContext(OMEvaluatorValue evaluatorValue) {
212  return wrap(unwrap(evaluatorValue)->getContext());
213 }
214 
215 // Get location from an EvaluatorValue.
216 MlirLocation omEvaluatorValueGetLoc(OMEvaluatorValue evaluatorValue) {
217  return wrap(unwrap(evaluatorValue)->getLoc());
218 }
219 
220 // Query if the EvaluatorValue is null.
222  // Check if the pointer is null.
223  return !evaluatorValue.ptr;
224 }
225 
226 /// Query if the EvaluatorValue is an Object.
228  // Check if the Object is non-null.
229  return isa<evaluator::ObjectValue>(unwrap(evaluatorValue).get());
230 }
231 
232 /// Query if the EvaluatorValue is a Primitive.
234  // Check if the Attribute is non-null.
235  return isa<evaluator::AttributeValue>(unwrap(evaluatorValue).get());
236 }
237 
238 /// Get the Primitive from an EvaluatorValue, which must contain a Primitive.
239 MlirAttribute omEvaluatorValueGetPrimitive(OMEvaluatorValue evaluatorValue) {
240  // Assert the Attribute is non-null, and return it.
241  assert(omEvaluatorValueIsAPrimitive(evaluatorValue));
242  return wrap(
243  llvm::cast<evaluator::AttributeValue>(unwrap(evaluatorValue).get())
244  ->getAttr());
245 }
246 
247 /// Get the Primitive from an EvaluatorValue, which must contain a Primitive.
249  // Assert the Attribute is non-null, and return it.
250  return wrap(std::make_shared<evaluator::AttributeValue>(unwrap(primitive)));
251 }
252 
253 /// Query if the EvaluatorValue is a List.
255  return isa<evaluator::ListValue>(unwrap(evaluatorValue).get());
256 }
257 
258 /// Get the List from an EvaluatorValue, which must contain a List.
259 /// TODO: This can be removed.
261  // Assert the List is non-null, and return it.
262  assert(omEvaluatorValueIsAList(evaluatorValue));
263  return evaluatorValue;
264 }
265 
266 /// Get the length of the List.
268  return cast<evaluator::ListValue>(unwrap(evaluatorValue).get())
269  ->getElements()
270  .size();
271 }
272 
273 /// Get an element of the List.
275  intptr_t pos) {
276  return wrap(cast<evaluator::ListValue>(unwrap(evaluatorValue).get())
277  ->getElements()[pos]);
278 }
279 
280 /// Query if the EvaluatorValue is a Tuple.
282  return isa<evaluator::TupleValue>(unwrap(evaluatorValue).get());
283 }
284 
285 /// Get the length of the Tuple.
287  return cast<evaluator::TupleValue>(unwrap(evaluatorValue).get())
288  ->getElements()
289  .size();
290 }
291 
292 /// Get an element of the Tuple.
294  intptr_t pos) {
295  return wrap(cast<evaluator::TupleValue>(unwrap(evaluatorValue).get())
296  ->getElements()[pos]);
297 }
298 
299 /// Get an element of the Map.
301  MlirAttribute attr) {
302  const auto &elements =
303  cast<evaluator::MapValue>(unwrap(evaluatorValue).get())->getElements();
304  const auto &it = elements.find(unwrap(attr));
305  if (it != elements.end())
306  return wrap(it->second);
307  return OMEvaluatorValue{nullptr};
308 }
309 
310 /// Query if the EvaluatorValue is a map.
312  return isa<evaluator::MapValue>(unwrap(evaluatorValue).get());
313 }
314 
316  return isa<evaluator::BasePathValue>(unwrap(evaluatorValue).get());
317 }
318 
320  return wrap(std::make_shared<evaluator::BasePathValue>(unwrap(context)));
321 }
322 
324  return isa<evaluator::PathValue>(unwrap(evaluatorValue).get());
325 }
326 
327 MlirAttribute omEvaluatorPathGetAsString(OMEvaluatorValue evaluatorValue) {
328  const auto *path = cast<evaluator::PathValue>(unwrap(evaluatorValue).get());
329  return wrap((Attribute)path->getAsString());
330 }
331 
332 //===----------------------------------------------------------------------===//
333 // ReferenceAttr API.
334 //===----------------------------------------------------------------------===//
335 
336 bool omAttrIsAReferenceAttr(MlirAttribute attr) {
337  return unwrap(attr).isa<ReferenceAttr>();
338 }
339 
340 MlirAttribute omReferenceAttrGetInnerRef(MlirAttribute referenceAttr) {
341  return wrap(
342  (Attribute)unwrap(referenceAttr).cast<ReferenceAttr>().getInnerRef());
343 }
344 
345 //===----------------------------------------------------------------------===//
346 // IntegerAttr API.
347 //===----------------------------------------------------------------------===//
348 
349 bool omAttrIsAIntegerAttr(MlirAttribute attr) {
350  return unwrap(attr).isa<circt::om::IntegerAttr>();
351 }
352 
353 MlirAttribute omIntegerAttrGetInt(MlirAttribute attr) {
354  return wrap(cast<circt::om::IntegerAttr>(unwrap(attr)).getValue());
355 }
356 
357 MlirAttribute omIntegerAttrGet(MlirAttribute attr) {
358  auto integerAttr = cast<mlir::IntegerAttr>(unwrap(attr));
359  return wrap(
360  circt::om::IntegerAttr::get(integerAttr.getContext(), integerAttr));
361 }
362 
363 //===----------------------------------------------------------------------===//
364 // ListAttr API.
365 //===----------------------------------------------------------------------===//
366 
367 bool omAttrIsAListAttr(MlirAttribute attr) {
368  return unwrap(attr).isa<ListAttr>();
369 }
370 
371 intptr_t omListAttrGetNumElements(MlirAttribute attr) {
372  auto listAttr = llvm::cast<ListAttr>(unwrap(attr));
373  return static_cast<intptr_t>(listAttr.getElements().size());
374 }
375 
376 MlirAttribute omListAttrGetElement(MlirAttribute attr, intptr_t pos) {
377  auto listAttr = llvm::cast<ListAttr>(unwrap(attr));
378  return wrap(listAttr.getElements()[pos]);
379 }
380 
381 //===----------------------------------------------------------------------===//
382 // MapAttr API.
383 //===----------------------------------------------------------------------===//
384 
385 bool omAttrIsAMapAttr(MlirAttribute attr) {
386  return unwrap(attr).isa<MapAttr>();
387 }
388 
389 intptr_t omMapAttrGetNumElements(MlirAttribute attr) {
390  auto mapAttr = llvm::cast<MapAttr>(unwrap(attr));
391  return static_cast<intptr_t>(mapAttr.getElements().size());
392 }
393 
394 MlirIdentifier omMapAttrGetElementKey(MlirAttribute attr, intptr_t pos) {
395  auto mapAttr = llvm::cast<MapAttr>(unwrap(attr));
396  return wrap(mapAttr.getElements().getValue()[pos].getName());
397 }
398 
399 MlirAttribute omMapAttrGetElementValue(MlirAttribute attr, intptr_t pos) {
400  auto mapAttr = llvm::cast<MapAttr>(unwrap(attr));
401  return wrap(mapAttr.getElements().getValue()[pos].getValue());
402 }
lowerAnnotationsNoRefTypePorts FirtoolPreserveValuesMode value
Definition: Firtool.cpp:95
assert(baseType &&"element must be base type")
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(CHIRRTL, chirrtl, circt::chirrtl::CHIRRTLDialect) MlirType chirrtlTypeGetCMemory(MlirContext ctx
static Attribute getAttr(ArrayRef< NamedAttribute > attrs, StringRef name)
Get an attribute by name from a list of named attributes.
Definition: FIRRTLOps.cpp:3429
bool omTypeIsAFrozenBasePathType(MlirType type)
Is the Type a FrozenBasePathType.
Definition: OM.cpp:48
bool omEvaluatorValueIsABasePath(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a BasePath.
Definition: OM.cpp:315
MlirAttribute omMapAttrGetElementValue(MlirAttribute attr, intptr_t pos)
Definition: OM.cpp:399
bool omEvaluatorValueIsNull(OMEvaluatorValue evaluatorValue)
Definition: OM.cpp:221
MlirContext omEvaluatorValueGetContext(OMEvaluatorValue evaluatorValue)
Definition: OM.cpp:211
MlirAttribute omReferenceAttrGetInnerRef(MlirAttribute referenceAttr)
Definition: OM.cpp:340
bool omTypeIsAStringType(MlirType type)
Is the Type a StringType.
Definition: OM.cpp:68
static OMEvaluatorValue wrap(EvaluatorValuePtr object)
Define our own wrap and unwrap instead of using the usual macro.
Definition: OM.cpp:95
MlirAttribute omEvaluatorPathGetAsString(OMEvaluatorValue evaluatorValue)
Get a string representation of a Path.
Definition: OM.cpp:327
bool omEvaluatorValueIsAPath(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Path.
Definition: OM.cpp:323
MlirIdentifier omMapAttrGetElementKey(MlirAttribute attr, intptr_t pos)
Definition: OM.cpp:394
bool omAttrIsAListAttr(MlirAttribute attr)
Definition: OM.cpp:367
OMEvaluatorValue omEvaluatorListGetElement(OMEvaluatorValue evaluatorValue, intptr_t pos)
Get an element of the List.
Definition: OM.cpp:274
OMEvaluatorValue omEvaluatorValueFromPrimitive(MlirAttribute primitive)
Get the Primitive from an EvaluatorValue, which must contain a Primitive.
Definition: OM.cpp:248
OMEvaluatorValue omEvaluatorInstantiate(OMEvaluator evaluator, MlirAttribute className, intptr_t nActualParams, OMEvaluatorValue *actualParams)
Use the Evaluator to Instantiate an Object from its class name and actual parameters.
Definition: OM.cpp:116
MlirType omEvaluatorMapGetType(OMEvaluatorValue value)
Get the Type from a Map, which will be a MapType.
Definition: OM.cpp:180
intptr_t omListAttrGetNumElements(MlirAttribute attr)
Definition: OM.cpp:371
MlirAttribute omEvaluatorObjectGetFieldNames(OMEvaluatorValue object)
Get an ArrayAttr with the names of the fields in an Object.
Definition: OM.cpp:176
bool omAttrIsAIntegerAttr(MlirAttribute attr)
Definition: OM.cpp:349
OMEvaluatorValue omEvaluatorObjectGetField(OMEvaluatorValue object, MlirAttribute name)
Get a field from an Object, which must contain a field of that name.
Definition: OM.cpp:190
MlirLocation omEvaluatorValueGetLoc(OMEvaluatorValue evaluatorValue)
Definition: OM.cpp:216
bool omEvaluatorValueIsAMap(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a map.
Definition: OM.cpp:311
MlirType omMapTypeGetKeyType(MlirType type)
Return a key type of a map.
Definition: OM.cpp:78
MlirTypeID omFrozenBasePathTypeGetTypeID(void)
Get the TypeID for a FrozenBasePathType.
Definition: OM.cpp:53
MlirType omStringTypeGet(MlirContext ctx)
Get a StringType.
Definition: OM.cpp:73
bool omTypeIsAClassType(MlirType type)
Is the Type a ClassType.
Definition: OM.cpp:37
OMEvaluatorValue omEvaluatorTupleGetElement(OMEvaluatorValue evaluatorValue, intptr_t pos)
Get an element of the Tuple.
Definition: OM.cpp:293
MlirIdentifier omClassTypeGetName(MlirType type)
Get the name for a ClassType.
Definition: OM.cpp:43
intptr_t omEvaluatorListGetNumElements(OMEvaluatorValue evaluatorValue)
Get the length of the List.
Definition: OM.cpp:267
MlirAttribute omEvaluatorValueGetPrimitive(OMEvaluatorValue evaluatorValue)
Get the Primitive from an EvaluatorValue, which must contain a Primitive.
Definition: OM.cpp:239
MlirAttribute omEvaluatorMapGetKeys(OMEvaluatorValue object)
Get an ArrayAttr with the keys in a Map.
Definition: OM.cpp:185
MlirAttribute omIntegerAttrGetInt(MlirAttribute attr)
Given an om::IntegerAttr, return the mlir::IntegerAttr.
Definition: OM.cpp:353
bool omEvaluatorValueIsAList(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a List.
Definition: OM.cpp:254
bool omTypeIsAFrozenPathType(MlirType type)
Is the Type a FrozenPathType.
Definition: OM.cpp:58
bool omAttrIsAMapAttr(MlirAttribute attr)
Definition: OM.cpp:385
OMEvaluatorValue omEvaluatorMapGetElement(OMEvaluatorValue evaluatorValue, MlirAttribute attr)
Get an element of the Map.
Definition: OM.cpp:300
bool omEvaluatorValueIsAPrimitive(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Primitive.
Definition: OM.cpp:233
bool omEvaluatorValueIsATuple(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is a Tuple.
Definition: OM.cpp:281
bool omEvaluatorObjectIsNull(OMEvaluatorValue object)
Query if the Object is null.
Definition: OM.cpp:154
MlirModule omEvaluatorGetModule(OMEvaluator evaluator)
Get the Module the Evaluator is built from.
Definition: OM.cpp:144
OMEvaluator omEvaluatorNew(MlirModule mod)
Construct an Evaluator with an IR module.
Definition: OM.cpp:109
OMEvaluatorValue omEvaluatorBasePathGetEmpty(MlirContext context)
Create an empty BasePath.
Definition: OM.cpp:319
MlirAttribute omListAttrGetElement(MlirAttribute attr, intptr_t pos)
Definition: OM.cpp:376
MlirTypeID omFrozenPathTypeGetTypeID(void)
Get the TypeID for a FrozenPathType.
Definition: OM.cpp:63
intptr_t omMapAttrGetNumElements(MlirAttribute attr)
Definition: OM.cpp:389
MlirType omEvaluatorObjectGetType(OMEvaluatorValue object)
Get the Type from an Object, which will be a ClassType.
Definition: OM.cpp:160
intptr_t omEvaluatorTupleGetNumElements(OMEvaluatorValue evaluatorValue)
Get the length of the Tuple.
Definition: OM.cpp:286
bool omAttrIsAReferenceAttr(MlirAttribute attr)
Definition: OM.cpp:336
static EvaluatorValuePtr unwrap(OMEvaluatorValue c)
Definition: OM.cpp:100
MlirAttribute omIntegerAttrGet(MlirAttribute attr)
Get an om::IntegerAttr from mlir::IntegerAttr.
Definition: OM.cpp:357
OMEvaluatorValue omEvaluatorValueGetList(OMEvaluatorValue evaluatorValue)
Get the List from an EvaluatorValue, which must contain a List.
Definition: OM.cpp:260
bool omEvaluatorValueIsAObject(OMEvaluatorValue evaluatorValue)
Query if the EvaluatorValue is an Object.
Definition: OM.cpp:227
MlirTypeID omClassTypeGetTypeID()
Get the TypeID for a ClassType.
Definition: OM.cpp:40
unsigned omEvaluatorObjectGetHash(OMEvaluatorValue object)
Get the hash for the object.
Definition: OM.cpp:165
bool omEvaluatorObjectIsEq(OMEvaluatorValue object, OMEvaluatorValue other)
Check if two objects are same.
Definition: OM.cpp:170
struct OMEvaluatorValue OMEvaluatorValue
Definition: OM.h:90
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:53
StringAttr getTypeID(Type t)
Definition: ESIPasses.cpp:26
llvm::hash_code hash_value(const BundledChannel channel)
Definition: ESITypes.h:48
std::shared_ptr< EvaluatorValue > EvaluatorValuePtr
A value of an object in memory.
Definition: Evaluator.h:38
evaluator::EvaluatorValuePtr EvaluatorValuePtr
Definition: Evaluator.h:400
Definition: om.py:1
A value type for use in C APIs that just wraps a pointer to an Object.
Definition: OM.h:83
void * ptr
Definition: OM.h:84
A value type for use in C APIs that just wraps a pointer to an Evaluator.
Definition: OM.h:72
An Evaluator, which is constructed with an IR module and can instantiate Objects.
Definition: Evaluator.h:408
FailureOr< evaluator::EvaluatorValuePtr > instantiate(StringAttr className, ArrayRef< EvaluatorValuePtr > actualParams)
Instantiate an Object with its class name and actual parameters.
Definition: Evaluator.cpp:279
Base class for evaluator runtime values.
Definition: Evaluator.h:48