[mlir][ods] ODS ops get an `extraClassDefinition`
authorMogball <jeffniu22@gmail.com>
Thu, 6 Jan 2022 01:42:12 +0000 (01:42 +0000)
committerMogball <jeffniu22@gmail.com>
Thu, 6 Jan 2022 01:43:26 +0000 (01:43 +0000)
commitb0774e5f500b5bb68451ee3f0590035d0f6e4e54
tree1a00bb6b4d4a29f8dc9caea2de2492fd95b075e6
parent0f5b718030e7112773ff4e88dd026204ba5b2890
[mlir][ods] ODS ops get an `extraClassDefinition`

Extra definitions are placed in the generated source file for each op class. The substitution `$cppClass` is replaced by the op's C++ class name.

This is useful when declaring but not defining methods in TableGen base classes:

```
class BaseOp<string mnemonic>
    : Op<MyDialect, mnemonic, [DeclareOpInterfaceMethods<SomeInterface>] {
  let extraClassDeclaration = [{
    // ZOp is declared at at the bottom of the file and is incomplete here
    ZOp getParent();
  }];
  let extraClassDefinition = [{
    int $cppClass::someInterfaceMethod() {
      return someUtilityFunction(*this);
    }
    ZOp $cppClass::getParent() {
      return dyn_cast<ZOp>(this->getParentOp());
    }
  }];
}
```

Certain things may prevent defining these functions inline, in the declaration. In this example, `ZOp` in the same dialect is incomplete at the function declaration because ops classes are declared in alphabetical order. Alternatively, functions may be too big to be desired as inlined, or they may require dependencies that create cyclic includes, or they may be calling a templated utility function that one may not want to expose in a header. If the functions are not inlined, then inheriting from the base class N times means that each function will need to be defined N times. With `extraClassDefinitions`, they only need to be defined once.

Reviewed By: rriddle

Differential Revision: https://reviews.llvm.org/D115783
mlir/docs/OpDefinitions.md
mlir/include/mlir/IR/OpBase.td
mlir/include/mlir/TableGen/Class.h
mlir/include/mlir/TableGen/Operator.h
mlir/lib/TableGen/Class.cpp
mlir/lib/TableGen/Operator.cpp
mlir/test/lib/Dialect/Test/TestOps.td
mlir/tools/mlir-tblgen/OpClass.cpp
mlir/tools/mlir-tblgen/OpClass.h
mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp