diff --git a/mlir/docs/Tools/MLIRLSP.md b/mlir/docs/Tools/MLIRLSP.md --- a/mlir/docs/Tools/MLIRLSP.md +++ b/mlir/docs/Tools/MLIRLSP.md @@ -36,7 +36,7 @@ mlir::DialectRegistry registry; registerMyDialects(registry); registerMyPasses(); - return mlir::failed(mlir::MlirLspServerMain(argc, argv, registry)); + return mlir::asMainReturnCode(mlir::MlirLspServerMain(argc, argv, registry)); } ``` diff --git a/mlir/docs/Tools/mlir-reduce.md b/mlir/docs/Tools/mlir-reduce.md --- a/mlir/docs/Tools/mlir-reduce.md +++ b/mlir/docs/Tools/mlir-reduce.md @@ -110,7 +110,7 @@ registerMyDialects(registry); // Register the DialectReductionPatternInterface if any. MLIRContext context(registry); - return failed(mlirReduceMain(argc, argv, context)); + return asMainReturnCode(mlirReduceMain(argc, argv, context)); } ``` diff --git a/mlir/examples/standalone/standalone-translate/standalone-translate.cpp b/mlir/examples/standalone/standalone-translate/standalone-translate.cpp --- a/mlir/examples/standalone/standalone-translate/standalone-translate.cpp +++ b/mlir/examples/standalone/standalone-translate/standalone-translate.cpp @@ -22,6 +22,6 @@ // TODO: Register standalone translations here. - return failed( + return mlir::asMainReturnCode( mlir::mlirTranslateMain(argc, argv, "MLIR Translation Testing Tool")); } diff --git a/mlir/include/mlir/Support/LogicalResult.h b/mlir/include/mlir/Support/LogicalResult.h --- a/mlir/include/mlir/Support/LogicalResult.h +++ b/mlir/include/mlir/Support/LogicalResult.h @@ -12,6 +12,8 @@ #include "mlir/Support/LLVM.h" #include "llvm/ADT/Optional.h" +#include + namespace mlir { /// This class represents an efficient way to signal success or failure. It @@ -71,6 +73,21 @@ /// to a failure value. inline bool failed(LogicalResult result) { return result.failed(); } +/// Utility function that converts LogicalResult to an `int` value that can be +/// returned directly from `main`. +/// +/// Example: +/// ```c++ +/// int main(int argc, char **argv) { +/// // ... +/// return asMainReturnCode(MlirOptMain(argc, argv, /* ... */)); +/// } +/// ``` +/// +inline int asMainReturnCode(LogicalResult result) { + return result.succeeded() ? EXIT_SUCCESS : EXIT_FAILURE; +} + /// This class provides support for representing a failure result, or a valid /// value of type `T`. This allows for integrating with LogicalResult, while /// also providing a value on the success path. diff --git a/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h b/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h --- a/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h +++ b/mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h @@ -79,20 +79,6 @@ DialectRegistry ®istry, bool preloadDialectsInContext = false); -/// Helper wrapper to return the result of MlirOptMain directly from main. -/// -/// Example: -/// -/// int main(int argc, char **argv) { -/// // ... -/// return mlir::asMainReturnCode(mlir::MlirOptMain( -/// argc, argv, /* ... */); -/// } -/// -inline int asMainReturnCode(LogicalResult r) { - return r.succeeded() ? EXIT_SUCCESS : EXIT_FAILURE; -} - } // namespace mlir #endif // MLIR_TOOLS_MLIROPT_MLIROPTMAIN_H diff --git a/mlir/tools/mlir-lsp-server/mlir-lsp-server.cpp b/mlir/tools/mlir-lsp-server/mlir-lsp-server.cpp --- a/mlir/tools/mlir-lsp-server/mlir-lsp-server.cpp +++ b/mlir/tools/mlir-lsp-server/mlir-lsp-server.cpp @@ -16,5 +16,5 @@ int main(int argc, char **argv) { DialectRegistry registry; registerAllDialects(registry); - return failed(MlirLspServerMain(argc, argv, registry)); + return asMainReturnCode(MlirLspServerMain(argc, argv, registry)); } diff --git a/mlir/tools/mlir-pdll-lsp-server/mlir-pdll-lsp-server.cpp b/mlir/tools/mlir-pdll-lsp-server/mlir-pdll-lsp-server.cpp --- a/mlir/tools/mlir-pdll-lsp-server/mlir-pdll-lsp-server.cpp +++ b/mlir/tools/mlir-pdll-lsp-server/mlir-pdll-lsp-server.cpp @@ -12,5 +12,5 @@ using namespace mlir; int main(int argc, char **argv) { - return failed(MlirPdllLspServerMain(argc, argv)); + return asMainReturnCode(MlirPdllLspServerMain(argc, argv)); } diff --git a/mlir/tools/mlir-reduce/mlir-reduce.cpp b/mlir/tools/mlir-reduce/mlir-reduce.cpp --- a/mlir/tools/mlir-reduce/mlir-reduce.cpp +++ b/mlir/tools/mlir-reduce/mlir-reduce.cpp @@ -37,5 +37,5 @@ #endif MLIRContext context(registry); - return failed(mlirReduceMain(argc, argv, context)); + return asMainReturnCode(mlirReduceMain(argc, argv, context)); } diff --git a/mlir/tools/mlir-translate/mlir-translate.cpp b/mlir/tools/mlir-translate/mlir-translate.cpp --- a/mlir/tools/mlir-translate/mlir-translate.cpp +++ b/mlir/tools/mlir-translate/mlir-translate.cpp @@ -31,5 +31,6 @@ int main(int argc, char **argv) { registerAllTranslations(); registerTestTranslations(); - return failed(mlirTranslateMain(argc, argv, "MLIR Translation Testing Tool")); + return mlir::asMainReturnCode( + mlirTranslateMain(argc, argv, "MLIR Translation Testing Tool")); }