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