[Mlir-commits] [mlir] 83d0616 - [mlir][ods] Add documentation on how to use sharded op definitions (NFC) (#89664)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Apr 24 14:59:23 PDT 2024
Author: Jeff Niu
Date: 2024-04-24T14:59:14-07:00
New Revision: 83d06162cca4f35a68e9c64032ba348ac3e27647
URL: https://github.com/llvm/llvm-project/commit/83d06162cca4f35a68e9c64032ba348ac3e27647
DIFF: https://github.com/llvm/llvm-project/commit/83d06162cca4f35a68e9c64032ba348ac3e27647.diff
LOG: [mlir][ods] Add documentation on how to use sharded op definitions (NFC) (#89664)
This adds explanations and instructions on how to set up a dialect for
sharded op definitions to the MLIR documentation.
Added:
Modified:
mlir/docs/DefiningDialects/Operations.md
Removed:
################################################################################
diff --git a/mlir/docs/DefiningDialects/Operations.md b/mlir/docs/DefiningDialects/Operations.md
index 729393d5362673..79a0cc55f13840 100644
--- a/mlir/docs/DefiningDialects/Operations.md
+++ b/mlir/docs/DefiningDialects/Operations.md
@@ -1114,6 +1114,100 @@ void process(AddOp op, ArrayRef<Value> newOperands) {
}
```
+#### Sharded Operation Definitions
+
+Large dialects with many operations may struggle with C++ compile time of
+generated op definitions, due to large compilation units. `mlir-tblgen`
+provides the ability to shard op definitions by splitting them up evenly
+by passing `-op-shard-count` to `-gen-op-defs` and `-gen-op-decls`. The tool
+will generate a single include file for the definitions broken up by
+`GET_OP_DEFS_${N}` where `${N}` is the shard number. A shard can be compiled in
+a single compilation unit by adding a file like this to your dialect library:
+
+```c++
+#include "mlir/IR/Operation.h"
+// Add any other required includes.
+
+// Utilities shared by generated op definitions: custom directive parsers,
+// printers, etc.
+#include "OpUtils.h"
+
+#define GET_OP_DEFS_0
+#include "MyDialectOps.cpp.inc"
+```
+
+Note: this requires restructing shared utility functions within the dialect
+library so they can be shared by multiple compilation units. I.e. instead of
+defining `static` methods in the same source file, you should declare them in a
+shared header and define them in their own source file.
+
+The op registration hooks are also sharded, because the template instantiation
+can take a very long time to compile. Operations should be registered in your
+dialect like:
+
+```c++
+void MyDialect::initialize() {
+ registerMyDialectOperations(this);
+}
+```
+
+CMake and Bazel functions are included to make sharding dialects easier.
+Assuming you have organized your operation utility functions into their own
+header, define a file that looks like the one above, but without the `#define`:
+
+```c++
+// MyDialectOps.cpp
+#include "mlir/IR/Operation.h"
+
+#include "OpUtils.h"
+
+#include "MyDialectOps.cpp.inc"
+```
+
+In CMake, remove the manual `mlir_tablegen` invocations and replace them with:
+
+```cmake
+set(LLVM_TARGET_DEFINITIONS MyDialectOps.td)
+add_sharded_ops(MyDialectOps 8) # shard the op definitions by 8
+
+add_mlir_library(MyDialect
+ MyDialect.cpp
+ MyDialectOpDefs.cpp
+ ${SHARDED_SRCS}
+
+ DEPENDS
+ MLIRTestOpsShardGen
+)
+```
+
+This will automatically duplicate the `MyDialectOps.cpp` source file and add the
+`#define` up the number of shards indicated.
+
+It is recommended that any out-of-line op member functions (like verifiers) be
+defined in a separate source file. In this example, it is called
+`MyDialectOpDefs.cpp`.
+
+In Bazel, remove the `-gen-op-defs` and `-gen-op-decls` invocations, and add
+
+```bazel
+gentbl_sharded_ops(
+ name = "MyDialectOpSrcs",
+ hdr_out = "MyDialectOps.h.inc",
+ shard_count = 8,
+ sharder = "//mlir:mlir-src-sharder",
+ src_file = "MyDialectOps.cpp",
+ src_out = "MyDialectOps.cpp.inc",
+ tblgen = "//mlir:mlir-tblgen",
+ td_file = "MyDialectOps.td",
+ deps = [":MyDialectOpsTdFiles"],
+)
+
+cc_library(
+ name = "MyDialect",
+ srcs = glob(["MyDialect/*.cpp"]) + [":MyDialectOpSrcs"]
+)
+```
+
## Constraints
Constraint is a core concept in table-driven operation definition: operation
More information about the Mlir-commits
mailing list