[mlir-opt] Enable defining which operations are defined at link time.
Previously mlir-opt had initializeMLIRContext function that added certain ops to the OperationSet of the context. But for different tests we'd want to register different ops. Make initializeMLIRContext an extern function so that the context initialization/set of ops to register can be determined at link time. This allows out-of-tree operations to easily expand the custom parsing/printing while still using mlir-opt.
PiperOrigin-RevId: 209078315
diff --git a/tools/mlir-opt/mlir-opt.cpp b/tools/mlir-opt/mlir-opt.cpp
index 6eb31d6..29fc9c4 100644
--- a/tools/mlir-opt/mlir-opt.cpp
+++ b/tools/mlir-opt/mlir-opt.cpp
@@ -85,9 +85,10 @@
return result;
}
-static void initializeMLIRContext(MLIRContext &ctx) {
- TFControlFlow::registerOperations(ctx);
-}
+// The function to initialize the MLIRContext for different ops is defined in
+// another compilation unit to allow different tests to link in different
+// context initializations (e.g., op registrations).
+extern void initializeMLIRContext(MLIRContext *ctx);
/// Parses the memory buffer and, if successfully parsed, prints the parsed
/// output. Optionally, convert ML functions into CFG functions.
@@ -99,7 +100,7 @@
// Parse the input file.
MLIRContext context;
- initializeMLIRContext(context);
+ initializeMLIRContext(&context);
std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context));
if (!module)
return OptFailure;
@@ -219,7 +220,7 @@
// Parse the input file.
MLIRContext context;
- initializeMLIRContext(context);
+ initializeMLIRContext(&context);
// TODO: refactor into initializeMLIRContext so the normal parser pass
// gets to use this.