[mlir][ODS] Allow to specify custom namespace for NativeOpTrait
This will allow to use `NativeOpTrait` and Operations declared outside of `mlir` namespace. Reviewed By: ftynse Differential Revision: https://reviews.llvm.org/D96128
This commit is contained in:
parent
0fc1738eb7
commit
035abe30c9
|
@ -1718,8 +1718,9 @@ class OpTrait;
|
|||
// NativeOpTrait corresponds to the MLIR C++ OpTrait mechanism. The
|
||||
// purpose to wrap around C++ symbol string with this class is to make
|
||||
// traits specified for ops in TableGen less alien and more integrated.
|
||||
class NativeOpTrait<string prop> : OpTrait {
|
||||
string trait = "::mlir::OpTrait::" # prop;
|
||||
class NativeOpTrait<string name> : OpTrait {
|
||||
string trait = name;
|
||||
string cppNamespace = "::mlir::OpTrait";
|
||||
}
|
||||
|
||||
// ParamNativeOpTrait corresponds to the template-parameterized traits in the
|
||||
|
@ -1852,6 +1853,7 @@ class CArg<string ty, string value = ""> {
|
|||
class OpInterfaceTrait<string name, code verifyBody = [{}]>
|
||||
: NativeOpTrait<""> {
|
||||
let trait = name # "::Trait";
|
||||
let cppNamespace = "";
|
||||
|
||||
// Specify the body of the verification function. `$_op` will be replaced with
|
||||
// the operation being verified.
|
||||
|
|
|
@ -63,7 +63,7 @@ protected:
|
|||
class NativeOpTrait : public OpTrait {
|
||||
public:
|
||||
// Returns the trait corresponding to a C++ trait class.
|
||||
StringRef getTrait() const;
|
||||
std::string getTrait() const;
|
||||
|
||||
static bool classof(const OpTrait *t) { return t->getKind() == Kind::Native; }
|
||||
};
|
||||
|
|
|
@ -35,8 +35,11 @@ OpTrait OpTrait::create(const llvm::Init *init) {
|
|||
|
||||
OpTrait::OpTrait(Kind kind, const llvm::Record *def) : def(def), kind(kind) {}
|
||||
|
||||
llvm::StringRef NativeOpTrait::getTrait() const {
|
||||
return def->getValueAsString("trait");
|
||||
std::string NativeOpTrait::getTrait() const {
|
||||
llvm::StringRef trait = def->getValueAsString("trait");
|
||||
llvm::StringRef cppNamespace = def->getValueAsString("cppNamespace");
|
||||
return cppNamespace.empty() ? trait.str()
|
||||
: (cppNamespace + "::" + trait).str();
|
||||
}
|
||||
|
||||
llvm::StringRef InternalOpTrait::getTrait() const {
|
||||
|
|
|
@ -248,6 +248,19 @@ def NS_JOp : NS_Op<"op_with_InferTypeOpInterface_interface", [DeclareOpInterface
|
|||
// CHECK: static void build(::mlir::OpBuilder &, ::mlir::OperationState &odsState, ::mlir::TypeRange resultTypes, ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> attributes = {});
|
||||
// CHECK: static void build(::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &odsState, ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> attributes = {});
|
||||
|
||||
// Check native OpTrait usage
|
||||
// ---
|
||||
|
||||
def NS_TestTrait : NativeOpTrait<"TestTrait"> {
|
||||
let cppNamespace = "SomeNamespace";
|
||||
}
|
||||
|
||||
def NS_KWithTraitOp : NS_Op<"KWithTrait", [NS_TestTrait]>;
|
||||
|
||||
// CHECK-LABEL: NS::KWithTraitOp declarations
|
||||
// CHECK: class KWithTraitOp : public ::mlir::Op<KWithTraitOp
|
||||
// CHECK-SAME: SomeNamespace::TestTrait
|
||||
|
||||
// Test that type defs have the proper namespaces when used as a constraint.
|
||||
// ---
|
||||
|
||||
|
|
Loading…
Reference in a new issue