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
This commit is contained in:
Thomas Köppe 2021-05-18 23:44:25 +00:00 committed by Mehdi Amini
parent 861d69a525
commit 58369fce30
3 changed files with 19 additions and 4 deletions

View File

@ -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));
}

View File

@ -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 &registry,
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

View File

@ -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));
}