8000 [OM] Add initial CAPI for OM dialect Evaluator. by mikeurbach · Pull Request #5248 · llvm/circt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[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
merged 3 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions include/circt-c/Dialect/OM.h
10000
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
10 changes: 10 additions & 0 deletions lib/CAPI/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(LLVM_OPTIONAL_SOURCES
HWArith.cpp
LLHD.cpp
Moore.cpp
OM.cpp
Seq.cpp
SV.cpp
FSM.cpp
Expand Down Expand Up @@ -75,6 +76,15 @@ add_mlir_public_c_api_library(CIRCTCAPIMoore
CIRCTMoore
)

add_mlir_public_c_api_library(CIRCTCAPIOM
OM.cpp

LINK_LIBS PUBLIC
MLIRCAPIIR
CIRCTOM
CIRCTOMEvaluator
)

add_mlir_public_c_api_library(CIRCTCAPISeq
Seq.cpp

Expand Down
151 changes: 151 additions & 0 deletions lib/CAPI/Dialect/OM.cpp
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;
}
14 changes: 14 additions & 0 deletions test/CAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_llvm_executable(circt-capi-ir-test
PARTIAL_SOURCES_INTENDED
ir.c
)
llvm_update_compile_flags(circt-capi-ir-test)
Expand All @@ -13,3 +14,16 @@ target_link_libraries(circt-capi-ir-test
CIRCTCAPIFSM
CIRCTCAPIExportVerilog
)

add_llvm_executable(circt-capi-om-test
PARTIAL_SOURCES_INTENDED
om.c
)
llvm_update_compile_flags(circt-capi-om-test)

target_link_libraries(circt-capi-om-test
PRIVATE

MLIRCAPIIR
CIRCTCAPIOM
)
Loading
0