This is a follow-up PR of #169045 and the second part of #179086. In #179086, we added support for defining regions in Python-defined ops, but its usefulness was quite limited because we still couldn’t mark an op as a `Terminator` or `NoTerminator`. In this PR, we port the `DynamicOpTrait` (introduced on the C++ side for `DynamicDialect` in #177735) to Python, so we can dynamically attach traits to Python-defined ops.
88 lines
2.9 KiB
C++
88 lines
2.9 KiB
C++
//===- ExtensibleDialect - C API for MLIR Extensible 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir-c/ExtensibleDialect.h"
|
|
#include "mlir/CAPI/IR.h"
|
|
#include "mlir/CAPI/Support.h"
|
|
#include "mlir/IR/ExtensibleDialect.h"
|
|
#include "mlir/IR/OperationSupport.h"
|
|
|
|
using namespace mlir;
|
|
|
|
DEFINE_C_API_PTR_METHODS(MlirDynamicOpTrait, DynamicOpTrait)
|
|
|
|
bool mlirDynamicOpTraitAttach(MlirDynamicOpTrait dynamicOpTrait,
|
|
MlirStringRef opName, MlirContext context) {
|
|
std::optional<RegisteredOperationName> opNameFound =
|
|
RegisteredOperationName::lookup(unwrap(opName), unwrap(context));
|
|
assert(opNameFound && "operation name must be registered in the context");
|
|
|
|
// The original getImpl() is protected, so we create a small helper struct
|
|
// here.
|
|
struct RegisteredOperationNameWithImpl : RegisteredOperationName {
|
|
Impl *getImpl() { return RegisteredOperationName::getImpl(); }
|
|
};
|
|
OperationName::Impl *impl =
|
|
static_cast<RegisteredOperationNameWithImpl &>(*opNameFound).getImpl();
|
|
|
|
std::unique_ptr<DynamicOpTrait> trait(unwrap(dynamicOpTrait));
|
|
// TODO: we should check whether the `impl` is a DynamicOpDefinition here
|
|
// via llvm-style RTTI.
|
|
return static_cast<DynamicOpDefinition *>(impl)->addTrait(std::move(trait));
|
|
}
|
|
|
|
MlirDynamicOpTrait mlirDynamicOpTraitGetIsTerminator() {
|
|
return wrap(new DynamicOpTraits::IsTerminator());
|
|
}
|
|
|
|
MlirDynamicOpTrait mlirDynamicOpTraitGetNoTerminator() {
|
|
return wrap(new DynamicOpTraits::NoTerminator());
|
|
}
|
|
|
|
void mlirDynamicOpTraitDestroy(MlirDynamicOpTrait dynamicOpTrait) {
|
|
delete unwrap(dynamicOpTrait);
|
|
}
|
|
|
|
namespace mlir {
|
|
|
|
class ExternalDynamicOpTrait : public DynamicOpTrait {
|
|
public:
|
|
ExternalDynamicOpTrait(TypeID typeID, MlirDynamicOpTraitCallbacks callbacks,
|
|
void *userData)
|
|
: typeID(typeID), callbacks(callbacks), userData(userData) {
|
|
if (callbacks.construct)
|
|
callbacks.construct(userData);
|
|
}
|
|
~ExternalDynamicOpTrait() {
|
|
if (callbacks.destruct)
|
|
callbacks.destruct(userData);
|
|
}
|
|
|
|
LogicalResult verifyTrait(Operation *op) const override {
|
|
return unwrap(callbacks.verifyTrait(wrap(op), userData));
|
|
};
|
|
LogicalResult verifyRegionTrait(Operation *op) const override {
|
|
return unwrap(callbacks.verifyRegionTrait(wrap(op), userData));
|
|
};
|
|
|
|
TypeID getTypeID() const override { return typeID; };
|
|
|
|
private:
|
|
TypeID typeID;
|
|
MlirDynamicOpTraitCallbacks callbacks;
|
|
void *userData;
|
|
};
|
|
|
|
} // namespace mlir
|
|
|
|
MlirDynamicOpTrait mlirDynamicOpTraitCreate(
|
|
MlirTypeID typeID, MlirDynamicOpTraitCallbacks callbacks, void *userData) {
|
|
return wrap(
|
|
new mlir::ExternalDynamicOpTrait(unwrap(typeID), callbacks, userData));
|
|
}
|