diff --git a/flang/include/flang/Frontend/FrontendActions.h b/flang/include/flang/Frontend/FrontendActions.h --- a/flang/include/flang/Frontend/FrontendActions.h +++ b/flang/include/flang/Frontend/FrontendActions.h @@ -204,7 +204,7 @@ /// 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 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -143,7 +143,8 @@ } mlirModule = std::make_unique(module.release()); - setUpTargetMachine(); + if (!setUpTargetMachine()) + return false; const llvm::DataLayout &dl = tm->createDataLayout(); setMLIRDataLayout(*mlirModule, dl); return true; @@ -184,7 +185,8 @@ *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::setUpTargetMachine() { +bool CodeGenAction::setUpTargetMachine() { CompilerInstance &ci = this->getInstance(); const TargetOptions &targetOpts = ci.getInvocation().getTargetOpts(); @@ -595,7 +597,11 @@ 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 @@ /*Reloc::Model=*/CGOpts.getRelocationModel(), /*CodeModel::Model=*/std::nullopt, OptLevel)); assert(tm && "Failed to create TargetMachine"); + return true; } static std::unique_ptr @@ -799,7 +806,8 @@ // 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 --- /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"'