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
This commit is contained in:
Mehdi Amini 2021-06-08 18:28:39 +00:00
parent b65f30d6fb
commit 1b21e9c1fa
1 changed files with 10 additions and 0 deletions

View File

@ -1779,7 +1779,17 @@ private:
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()));