From 58369fce30af484889356f225d89cb0b32009206 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20K=C3=B6ppe?= Date: Tue, 18 May 2021 23:44:25 +0000 Subject: [PATCH] Add a helper function to convert LogicalResult to int for return from main At present, a lot of code contains main function bodies like "return failed(mlir::MlirOptMain(...);". This is unfortunate for two reasons: a) it uses ADL, which is maybe not what the free "failed" function was designed for; and b) it is a bit awkward to read, requring the reader to both understand the boolean nature of the value and the semantics of main's return value. (And it's also not portable, since 1 is not a portable success value.) The replacement code, `return mlir::AsMainReturnCode(mlir::MlirOptMain(...))` is a bit more self-explanatory. The change applies the new function to a few internal uses of MlirOptMain, too. Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D102641 --- .../examples/standalone/standalone-opt/standalone-opt.cpp | 2 +- mlir/include/mlir/Support/MlirOptMain.h | 15 +++++++++++++++ mlir/tools/mlir-opt/mlir-opt.cpp | 6 +++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/mlir/examples/standalone/standalone-opt/standalone-opt.cpp b/mlir/examples/standalone/standalone-opt/standalone-opt.cpp index 0fb211a..97a996a 100644 --- a/mlir/examples/standalone/standalone-opt/standalone-opt.cpp +++ b/mlir/examples/standalone/standalone-opt/standalone-opt.cpp @@ -33,6 +33,6 @@ int main(int argc, char **argv) { // will be *parsed* by the tool, not the one generated // registerAllDialects(registry); - return failed( + return mlir::asMainReturnCode( mlir::MlirOptMain(argc, argv, "Standalone optimizer driver\n", registry)); } diff --git a/mlir/include/mlir/Support/MlirOptMain.h b/mlir/include/mlir/Support/MlirOptMain.h index 8924e47..4ae3535 100644 --- a/mlir/include/mlir/Support/MlirOptMain.h +++ b/mlir/include/mlir/Support/MlirOptMain.h @@ -16,6 +16,7 @@ #include "mlir/Support/LogicalResult.h" #include "llvm/ADT/StringRef.h" +#include #include namespace llvm { @@ -61,6 +62,20 @@ LogicalResult MlirOptMain(int argc, char **argv, llvm::StringRef toolName, 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; +} + } // end namespace mlir #endif // MLIR_SUPPORT_MLIROPTMAIN_H diff --git a/mlir/tools/mlir-opt/mlir-opt.cpp b/mlir/tools/mlir-opt/mlir-opt.cpp index 89b77ab..1ee3b6a 100644 --- a/mlir/tools/mlir-opt/mlir-opt.cpp +++ b/mlir/tools/mlir-opt/mlir-opt.cpp @@ -190,7 +190,7 @@ int main(int argc, char **argv) { #ifdef MLIR_INCLUDE_TESTS test::registerTestDialect(registry); #endif - return failed(MlirOptMain(argc, argv, "MLIR modular optimizer driver\n", - registry, - /*preloadDialectsInContext=*/false)); + return mlir::asMainReturnCode( + mlir::MlirOptMain(argc, argv, "MLIR modular optimizer driver\n", registry, + /*preloadDialectsInContext=*/false)); } -- 2.7.4