[Mlir-commits] [mlir] 58369fc - Add a helper function to convert LogicalResult to int for return from main
Mehdi Amini
llvmlistbot at llvm.org
Tue May 18 17:12:51 PDT 2021
Author: Thomas Köppe
Date: 2021-05-19T00:12:39Z
New Revision: 58369fce30af484889356f225d89cb0b32009206
URL: https://github.com/llvm/llvm-project/commit/58369fce30af484889356f225d89cb0b32009206
DIFF: https://github.com/llvm/llvm-project/commit/58369fce30af484889356f225d89cb0b32009206.diff
LOG: 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
Added:
Modified:
mlir/examples/standalone/standalone-opt/standalone-opt.cpp
mlir/include/mlir/Support/MlirOptMain.h
mlir/tools/mlir-opt/mlir-opt.cpp
Removed:
################################################################################
diff --git a/mlir/examples/standalone/standalone-opt/standalone-opt.cpp b/mlir/examples/standalone/standalone-opt/standalone-opt.cpp
index 0fb211add2cbe..97a996aa94502 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 8924e479b950c..4ae3535d4c408 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 <cstdlib>
#include <memory>
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 89b77ab53ca72..1ee3b6a0f2ace 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));
}
More information about the Mlir-commits
mailing list