-
Notifications
You must be signed in to change notification settings - Fork 347
[OM] Add initial CAPI for OM dialect Evaluator. #5248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+393
−2
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
//===-- circt-c/Dialect/OM.h - C API for OM dialect -----------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This header declares the C interface for registering and accessing the | ||
// OM dialect. A dialect should be registered with a context to make it | ||
// available to users of the context. These users must load the dialect | ||
// before using any of its attributes, operations or types. Parser and pass | ||
// manager can load registered dialects automatically. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CIRCT_C_DIALECT_OM_H | ||
#define CIRCT_C_DIALECT_OM_H | ||
|
||
#include "mlir-c/IR.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Dialect API. | ||
//===----------------------------------------------------------------------===// | ||
|
||
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(OM, om); | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Evaluator data structures. | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// A value type for use in C APIs that just wraps a pointer to an Evaluator. | ||
/// This is in line with the usual MLIR DEFINE_C_API_STRUCT. | ||
struct OMEvaluator { | ||
void *ptr; | ||
}; | ||
|
||
// clang-tidy doesn't respect extern "C". | ||
// see https://github.com/llvm/llvm-project/issues/35272. | ||
// NOLINTNEXTLINE(modernize-use-using) | ||
typedef struct OMEvaluator OMEvaluator; | ||
|
||
/// A value type for use in C APIs that just wraps a pointer to an Object. | ||
/// This is in line with the usual MLIR DEFINE_C_API_STRUCT. | ||
struct OMObject { | ||
void *ptr; | ||
}; | ||
|
||
// clang-tidy doesn't respect extern "C". | ||
// see https://github.com/llvm/llvm-project/issues/35272. | ||
// NOLINTNEXTLINE(modernize-use-using) | ||
typedef struct OMObject OMObject; | ||
|
||
/// A value type for use in C APIs that represents an ObjectValue. | ||
/// Because ObjectValue is a std::variant, which doesn't work well with C APIs, | ||
/// we use a struct with both fields, one of which will always be null. | ||
struct OMObjectValue { | ||
MlirAttribute primitive; | ||
OMObject object; | ||
}; | ||
|
||
// clang-tidy doesn't respect extern "C". | ||
// see https://github.com/llvm/llvm-project/issues/35272. | ||
// NOLINTNEXTLINE(modernize-use-using) | ||
typedef struct OMObjectValue OMObjectValue; | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Evaluator API. | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Construct an Evaluator with an IR module. | ||
MLIR_CAPI_EXPORTED OMEvaluator omEvaluatorNew(MlirModule mod); | ||
|
||
/// Use the Evaluator to Instantiate an Object from its class name and actual | ||
/// parameters. | ||
MLIR_CAPI_EXPORTED OMObject omEvaluatorInstantiate( | ||
OMEvaluator evaluator, MlirAttribute className, intptr_t nActualParams, | ||
MlirAttribute const *actualParams); | ||
|
||
/// Get the Module the Evaluator is built from. | ||
MLIR_CAPI_EXPORTED MlirModule omEvaluatorGetModule(OMEvaluator evaluator); | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Object API. | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Query if the Object is null. | ||
MLIR_CAPI_EXPORTED bool omEvaluatorObjectIsNull(OMObject object); | ||
|
||
/// Get a field from an Object, which must contain a field of that name. | ||
MLIR_CAPI_EXPORTED OMObjectValue omEvaluatorObjectGetField(OMObject object, | ||
MlirAttribute name); | ||
|
||
//===----------------------------------------------------------------------===// | ||
// ObjectValue API. | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Query if the ObjectValue is null. | ||
MLIR_CAPI_EXPORTED bool omEvaluatorObjectValueIsNull(OMObjectValue objectValue); | ||
|
||
/// Query if the ObjectValue is an Object. | ||
MLIR_CAPI_EXPORTED bool | ||
omEvaluatorObjectValueIsAObject(OMObjectValue objectValue); | ||
|
||
/// Get the Object from an ObjectValue, which must contain an Object. | ||
MLIR_CAPI_EXPORTED OMObject | ||
omEvaluatorObjectValueGetObject(OMObjectValue objectValue); | ||
|
||
/// Query if the ObjectValue is a Primitive. | ||
MLIR_CAPI_EXPORTED bool | ||
omEvaluatorObjectValueIsAPrimitive(OMObjectValue objectValue); | ||
|
||
/// Get the Primitive from an ObjectValue, which must contain a Primitive. | ||
MLIR_CAPI_EXPORTED MlirAttribute | ||
omEvaluatorObjectValueGetPrimitive(OMObjectValue objectValue); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // CIRCT_C_DIALECT_OM_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
//===- OM.cpp - C Interface for the OM Dialect ----------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Implements a C Interface for the OM Dialect | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "circt-c/Dialect/OM.h" | ||
#include "circt/Dialect/OM/Evaluator/Evaluator.h" | ||
#include "circt/Dialect/OM/OMDialect.h" | ||
#include "mlir/CAPI/Registration.h" | ||
#include "mlir/CAPI/Wrap.h" | ||
|
||
using namespace mlir; | ||
using namespace circt::om; | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Dialect API. | ||
//===----------------------------------------------------------------------===// | ||
|
||
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(OM, om, OMDialect) | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Evaluator data structures. | ||
//===----------------------------------------------------------------------===// | ||
|
||
DEFINE_C_API_PTR_METHODS(OMEvaluator, circt::om::Evaluator) | ||
DEFINE_C_API_PTR_METHODS(OMObject, std::shared_ptr<circt::om::Object>) | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Evaluator API. | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Construct an Evaluator with an IR module. | ||
OMEvaluator omEvaluatorNew(MlirModule mod) { | ||
// Just allocate and wrap the Evaluator. | ||
return wrap(new Evaluator(unwrap(mod))); | ||
} | ||
|
||
/// Use the Evaluator to Instantiate an Object from its class name and actual | ||
/// parameters. | ||
OMObject omEvaluatorInstantiate(OMEvaluator evaluator, MlirAttribute className, | ||
intptr_t nActualParams, | ||
MlirAttribute const *actualParams) { | ||
// Unwrap the Evaluator. | ||
Evaluator *cppEvaluator = unwrap(evaluator); | ||
|
||
// Unwrap the className, which the client must supply as a StringAttr. | ||
StringAttr cppClassName = unwrap(className).cast<StringAttr>(); | ||
|
||
// Unwrap the actual parameters, which the client must supply as Attributes. | ||
SmallVector<Attribute> actualParamsTmp; | ||
SmallVector<ObjectValue> cppActualParams( | ||
unwrapList(nActualParams, actualParams, actualParamsTmp)); | ||
|
||
// Invoke the Evaluator to instantiate the Object. | ||
FailureOr<std::shared_ptr<Object>> result = | ||
cppEvaluator->instantiate(cppClassName, cppActualParams); | ||
|
||
// If instantiation failed, return a null Object. A Diagnostic will be emitted | ||
// in this case. | ||
if (failed(result)) | ||
return OMObject(); | ||
|
||
// Wrap and return a *new* shared pointer to the Object, to ensure the | ||
// reference count is kept up to date. | ||
return wrap(new std::shared_ptr<Object>(result.value())); | ||
} | ||
|
||
/// Get the Module the Evaluator is built from. | ||
MlirModule omEvaluatorGetModule(OMEvaluator evaluator) { | ||
// Just unwrap the Evaluator, get the Module, and wrap it. | ||
return wrap(unwrap(evaluator)->getModule()); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Object API. | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Query if the Object is null. | ||
bool omEvaluatorObjectIsNull(OMObject object) { | ||
// Just check if the Object shared pointer is null. | ||
return !object.ptr; | ||
} | ||
|
||
/// Get a field from an Object, which must contain a field of that name. | ||
OMObjectValue omEvaluatorObjectGetField(OMObject object, MlirAttribute name) { | ||
// Unwrap the Object and get the field of the name, which the client must | ||
// supply as a StringAttr. | ||
FailureOr<ObjectValue> result = | ||
(*unwrap(object))->getField(unwrap(name).cast<StringAttr>()); | ||
|
||
// If getField failed, return a null ObjectValue. A Diagnostic will be emitted | ||
// in this case. | ||
if (failed(result)) | ||
return OMObjectValue(); | ||
|
||
// If the field is an Object, return an ObjectValue with the Object set. | ||
if (auto *object = std::get_if<std::shared_ptr<Object>>(&result.value())) | ||
return OMObjectValue{MlirAttribute(), wrap(object)}; | ||
|
||
// If the field is an Attribute, return an ObjectValue with the Primitive set. | ||
if (auto *primitive = std::get_if<Attribute>(&result.value())) | ||
return OMObjectValue{wrap(*primitive), OMObject()}; | ||
|
||
// This case should never be hit, but return a null ObjectValue that is | ||
// neither an Object nor a Primitive. | ||
return OMObjectValue(); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// ObjectValue API. | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Query if the ObjectValue is null. | ||
bool omEvaluatorObjectValueIsNull(OMObjectValue objectValue) { | ||
// Check if both Object and Attribute are null. | ||
return !omEvaluatorObjectValueIsAObject(objectValue) && | ||
!omEvaluatorObjectValueIsAPrimitive(objectValue); | ||
} | ||
|
||
/// Query if the ObjectValue is an Object. | ||
bool omEvaluatorObjectValueIsAObject(OMObjectValue objectValue) { | ||
// Check if the Object is non-null. | ||
return !omEvaluatorObjectIsNull(objectValue.object); | ||
} | ||
|
||
/// Get the Object from an ObjectValue, which must contain an Object. | ||
OMObject omEvaluatorObjectValueGetObject(OMObjectValue objectValue) { | ||
// Assert the Object is non-null, and return it. | ||
assert(omEvaluatorObjectValueIsAObject(objectValue)); | ||
return objectValue.object; | ||
} | ||
|
||
/// Query if the ObjectValue is a Primitive. | ||
bool omEvaluatorObjectValueIsAPrimitive(OMObjectValue objectValue) { | ||
// Check if the Attribute is non-null. | ||
return !mlirAttributeIsNull(objectValue.primitive); | ||
} | ||
|
||
/// Get the Primitive from an ObjectValue, which must contain a Primitive. | ||
MlirAttribute omEvaluatorObjectValueGetPrimitive(OMObjectValue objectValue) { | ||
// Assert the Attribute is non-null, and return it. | ||
assert(omEvaluatorObjectValueIsAPrimitive(objectValue)); | ||
return objectValue.primitive; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.