[flang-commits] [flang] f67b481 - [Flang] Exit gracefully with a useful message when we fail to lookup a target
via flang-commits
flang-commits at lists.llvm.org
Mon Mar 20 12:08:57 PDT 2023
Author: Nadeem, Usman
Date: 2023-03-20T12:08:34-07:00
New Revision: f67b481098cc30567d0f50a2b21f8f57b92052bd
URL: https://github.com/llvm/llvm-project/commit/f67b481098cc30567d0f50a2b21f8f57b92052bd
DIFF: https://github.com/llvm/llvm-project/commit/f67b481098cc30567d0f50a2b21f8f57b92052bd.diff
LOG: [Flang] Exit gracefully with a useful message when we fail to lookup a target
Without this patch we were asserting with a generic message `Failed to
create Target`, but we already have a detailed error message stored in
the variable `error` after calling `lookupTarget()` but this error was not
getting used/printed.
With this patch we will emit a message with more details instead of a
stack dump with a generic message.
Differential Revision: https://reviews.llvm.org/D146333
Change-Id: I7ddee917cf921a2133ca3e6b35791b2142f770a2
Added:
flang/test/Driver/target-machine-error.f90
Modified:
flang/include/flang/Frontend/FrontendActions.h
flang/lib/Frontend/FrontendActions.cpp
Removed:
################################################################################
diff --git a/flang/include/flang/Frontend/FrontendActions.h b/flang/include/flang/Frontend/FrontendActions.h
index 7a5042b814a14..2b96125e41639 100644
--- a/flang/include/flang/Frontend/FrontendActions.h
+++ b/flang/include/flang/Frontend/FrontendActions.h
@@ -204,7 +204,7 @@ class CodeGenAction : public FrontendAction {
/// Runs prescan, parsing, sema and lowers to MLIR.
bool beginSourceFileAction() override;
/// Sets up LLVM's TargetMachine.
- void setUpTargetMachine();
+ bool setUpTargetMachine();
/// Runs the optimization (aka middle-end) pipeline on the LLVM module
/// associated with this action.
void runOptimizationPipeline(llvm::raw_pwrite_stream &os);
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index 64cabd87e6a41..b723fe89387cd 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -143,7 +143,8 @@ bool CodeGenAction::beginSourceFileAction() {
}
mlirModule = std::make_unique<mlir::ModuleOp>(module.release());
- setUpTargetMachine();
+ if (!setUpTargetMachine())
+ return false;
const llvm::DataLayout &dl = tm->createDataLayout();
setMLIRDataLayout(*mlirModule, dl);
return true;
@@ -184,7 +185,8 @@ bool CodeGenAction::beginSourceFileAction() {
*mlirModule, ci.getInvocation().getLangOpts().OpenMPIsDevice);
}
- setUpTargetMachine();
+ if (!setUpTargetMachine())
+ return false;
const llvm::DataLayout &dl = tm->createDataLayout();
setMLIRDataLayout(*mlirModule, dl);
@@ -585,7 +587,7 @@ void CodeGenAction::generateLLVMIR() {
}
-void CodeGenAction::setUpTargetMachine() {
+bool CodeGenAction::setUpTargetMachine() {
CompilerInstance &ci = this->getInstance();
const TargetOptions &targetOpts = ci.getInvocation().getTargetOpts();
@@ -595,7 +597,11 @@ void CodeGenAction::setUpTargetMachine() {
std::string error;
const llvm::Target *theTarget =
llvm::TargetRegistry::lookupTarget(theTriple, error);
- assert(theTarget && "Failed to create Target");
+ if (!theTarget) {
+ ci.getDiagnostics().Report(clang::diag::err_fe_unable_to_create_target)
+ << error;
+ return false;
+ }
// Create `TargetMachine`
const auto &CGOpts = ci.getInvocation().getCodeGenOpts();
@@ -611,6 +617,7 @@ void CodeGenAction::setUpTargetMachine() {
/*Reloc::Model=*/CGOpts.getRelocationModel(),
/*CodeModel::Model=*/std::nullopt, OptLevel));
assert(tm && "Failed to create TargetMachine");
+ return true;
}
static std::unique_ptr<llvm::raw_pwrite_stream>
@@ -799,7 +806,8 @@ void CodeGenAction::executeAction() {
// Set the triple based on the targetmachine (this comes compiler invocation
// and the command-line target option if specified, or the default if not
// given on the command-line).
- setUpTargetMachine();
+ if (!setUpTargetMachine())
+ return;
const std::string &theTriple = tm->getTargetTriple().str();
if (llvmModule->getTargetTriple() != theTriple) {
diff --git a/flang/test/Driver/target-machine-error.f90 b/flang/test/Driver/target-machine-error.f90
new file mode 100644
index 0000000000000..8f6522de1f617
--- /dev/null
+++ b/flang/test/Driver/target-machine-error.f90
@@ -0,0 +1,6 @@
+! RUN: not %flang --target=some-invalid-triple -S %s -o \
+! RUN: /dev/null 2>&1 | FileCheck %s
+! RUN: not %flang_fc1 -triple some-invalid-triple -S %s -o \
+! RUN: /dev/null 2>&1 | FileCheck %s
+
+! CHECK: error: unable to create target: 'No available targets are compatible with triple "some-invalid-triple"'
More information about the flang-commits
mailing list