diff options
author | Andrzej Warzynski <andrzej.warzynski@arm.com> | 2022-06-06 17:57:33 +0000 |
---|---|---|
committer | Andrzej Warzynski <andrzej.warzynski@arm.com> | 2022-06-10 10:36:25 +0000 |
commit | 3e782ba21be4b88c761d3b0854df130d7ca08a56 (patch) | |
tree | f3430775a084e09c5cf669a3f066964c08eea7b5 /flang/lib | |
parent | ceb21fa4e49ddc8478371b41250f206082c5c67e (diff) | |
download | llvm-3e782ba21be4b88c761d3b0854df130d7ca08a56.zip llvm-3e782ba21be4b88c761d3b0854df130d7ca08a56.tar.gz llvm-3e782ba21be4b88c761d3b0854df130d7ca08a56.tar.bz2 |
[flang][driver] Fix support for `-x`
Until now, `-x` wasn't really taken into account in Flang's compiler and
frontend drivers. `flang-new` and `flang-new -fc1` only recently gained
powers to consume inputs other than Fortran files and that's probably
why this hasn't been noticed yet.
This patch makes sure that `-x` is supported correctly and consistently
with Clang. To this end, verification is added when reading LLVM IR
files (i.e. IR modules are verified with `llvm::verifyModule`). This
way, LLVM IR parsing errors are correctly reported to Flang users. This
also aids testing.
With the new functionality, we can verify that `-x ir` breaks
compilation for e.g. Fortran files and vice-versa. Tests are updated
accordingly.
Differential Revision: https://reviews.llvm.org/D127207
Diffstat (limited to 'flang/lib')
-rw-r--r-- | flang/lib/Frontend/CompilerInvocation.cpp | 5 | ||||
-rw-r--r-- | flang/lib/Frontend/FrontendActions.cpp | 22 |
2 files changed, 21 insertions, 6 deletions
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp index 9ea4810..cc376f3 100644 --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -263,7 +263,10 @@ static bool parseFrontendArgs(FrontendOptions &opts, llvm::opt::ArgList &args, llvm::StringRef xValue = a->getValue(); // Principal languages. dashX = llvm::StringSwitch<InputKind>(xValue) - .Case("f90", Language::Fortran) + // Flang does not differentiate between pre-processed and not + // pre-processed inputs. + .Case("f95", Language::Fortran) + .Case("f95-cpp-input", Language::Fortran) .Default(Language::Unknown); // Some special cases cannot be combined with suffixes. diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp index 334849c..57b6203 100644 --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -41,6 +41,7 @@ #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/Bitcode/BitcodeWriterPass.h" #include "llvm/IR/LegacyPassManager.h" +#include "llvm/IR/Verifier.h" #include "llvm/IRReader/IRReader.h" #include "llvm/MC/TargetRegistry.h" #include "llvm/Passes/PassBuilder.h" @@ -78,25 +79,36 @@ bool PrescanAndSemaDebugAction::beginSourceFileAction() { bool CodeGenAction::beginSourceFileAction() { llvmCtx = std::make_unique<llvm::LLVMContext>(); + CompilerInstance &ci = this->getInstance(); // If the input is an LLVM file, just parse it and return. if (this->getCurrentInput().getKind().getLanguage() == Language::LLVM_IR) { llvm::SMDiagnostic err; llvmModule = llvm::parseIRFile(getCurrentInput().getFile(), err, *llvmCtx); + if (!llvmModule || llvm::verifyModule(*llvmModule, &llvm::errs())) { + err.print("flang-new", llvm::errs()); + unsigned diagID = ci.getDiagnostics().getCustomDiagID( + clang::DiagnosticsEngine::Error, "Could not parse IR"); + ci.getDiagnostics().Report(diagID); + return false; + } - return (nullptr != llvmModule); + return true; } // Otherwise, generate an MLIR module from the input Fortran source - assert(getCurrentInput().getKind().getLanguage() == Language::Fortran && - "Invalid input type - expecting a Fortran file"); + if (getCurrentInput().getKind().getLanguage() != Language::Fortran) { + unsigned diagID = ci.getDiagnostics().getCustomDiagID( + clang::DiagnosticsEngine::Error, + "Invalid input type - expecting a Fortran file"); + ci.getDiagnostics().Report(diagID); + return false; + } bool res = runPrescan() && runParse() && runSemanticChecks() && generateRtTypeTables(); if (!res) return res; - CompilerInstance &ci = this->getInstance(); - // Load the MLIR dialects required by Flang mlir::DialectRegistry registry; mlirCtx = std::make_unique<mlir::MLIRContext>(registry); |