[Mlir-commits] [mlir] 1b21e9c - Add a static assertions for custom Op<> to not defined data members (NFC)

Mehdi Amini llvmlistbot at llvm.org
Wed Jun 9 14:45:15 PDT 2021


Author: Mehdi Amini
Date: 2021-06-09T21:45:06Z
New Revision: 1b21e9c1fa990a303fa5a543c17a5f470a32e112

URL: https://github.com/llvm/llvm-project/commit/1b21e9c1fa990a303fa5a543c17a5f470a32e112
DIFF: https://github.com/llvm/llvm-project/commit/1b21e9c1fa990a303fa5a543c17a5f470a32e112.diff

LOG: Add a static assertions for custom Op<> to not defined data members (NFC)

A common mistake for newcomers to MLIR is to try to store extra member
on the Op class. However these are intended to be thing wrapper around
an Operation*, all the storage is meant to be encoded in attribute on
the underlying Operation. This can be confusing to debug, so better
catch it at build time.

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D103869

Added: 
    

Modified: 
    mlir/include/mlir/IR/OpDefinition.h

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/OpDefinition.h b/mlir/include/mlir/IR/OpDefinition.h
index dadf028d281f..8fdf29cbd492 100644
--- a/mlir/include/mlir/IR/OpDefinition.h
+++ b/mlir/include/mlir/IR/OpDefinition.h
@@ -1779,7 +1779,17 @@ class Op : public OpState, public Traits<ConcreteType>... {
   static AbstractOperation::VerifyInvariantsFn getVerifyInvariantsFn() {
     return &verifyInvariants;
   }
+
+  static constexpr bool hasNoDataMembers() {
+    // Checking that the derived class does not define any member by comparing
+    // its size to an ad-hoc EmptyOp.
+    class EmptyOp : public Op<EmptyOp, Traits...> {};
+    return sizeof(ConcreteType) == sizeof(EmptyOp);
+  }
+
   static LogicalResult verifyInvariants(Operation *op) {
+    static_assert(hasNoDataMembers(),
+                  "Op class shouldn't define new data members");
     return failure(
         failed(op_definition_impl::verifyTraits<VerifiableTraitsTupleT>(op)) ||
         failed(cast<ConcreteType>(op).verify()));


        


More information about the Mlir-commits mailing list