
This patch adds support for: * `-S` in Flang's compiler and frontend drivers, and implements: * `-emit-obj` in Flang's frontend driver and `-c` in Flang's compiler driver (this is consistent with Clang). (these options were already available before, but only as placeholders). The semantics of these options in Clang and Flang are identical. The `EmitObjAction` frontend action is renamed as `BackendAction`. This new name more accurately reflects the fact that this action will primarily run the code-gen/backend pipeline in LLVM. It also makes more sense as an action implementing both `-emit-obj` and `-S` (originally, it was just `-emit-obj`). `tripleName` from FirContext.cpp is deleted and, when a target triple is required, `mlir::LLVM::LLVMDialect::getTargetTripleAttrName()` is used instead. In practice, this means that `fir.triple` is replaced with `llvm.target_triple`. The former was effectively ignored. The latter is used when lowering from the LLVM dialect in MLIR to LLVM IR (i.e. it's embedded in the generated LLVM IR module). The driver can then re-use it when configuring the backend. With this change, the LLVM IR files generated by e.g. `tco` will from now on contain the correct target triple. The code-gen.f90 test is replaced with code-gen-x86.f90 and code-gen-aarch64.f90. With 2 seperate files we can verify that `--target` is correctly taken into account. LIT configuration is updated to enable e.g.: ``` ! REQUIRES: aarch64-registered-target ``` Differential Revision: https://reviews.llvm.org/D120568
153 lines
5.1 KiB
C++
153 lines
5.1 KiB
C++
//===--- ExecuteCompilerInvocation.cpp ------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file holds ExecuteCompilerInvocation(). It is split into its own file to
|
|
// minimize the impact of pulling in essentially everything else in Flang.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "flang/Frontend/CompilerInstance.h"
|
|
#include "flang/Frontend/FrontendActions.h"
|
|
#include "flang/Frontend/FrontendPluginRegistry.h"
|
|
#include "clang/Driver/Options.h"
|
|
#include "llvm/Option/OptTable.h"
|
|
#include "llvm/Option/Option.h"
|
|
#include "llvm/Support/BuryPointer.h"
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
namespace Fortran::frontend {
|
|
|
|
static std::unique_ptr<FrontendAction> CreateFrontendBaseAction(
|
|
CompilerInstance &ci) {
|
|
|
|
ActionKind ak = ci.frontendOpts().programAction;
|
|
switch (ak) {
|
|
case InputOutputTest:
|
|
return std::make_unique<InputOutputTestAction>();
|
|
case PrintPreprocessedInput:
|
|
return std::make_unique<PrintPreprocessedAction>();
|
|
case ParseSyntaxOnly:
|
|
return std::make_unique<ParseSyntaxOnlyAction>();
|
|
case EmitMLIR:
|
|
return std::make_unique<EmitMLIRAction>();
|
|
case EmitLLVM:
|
|
return std::make_unique<EmitLLVMAction>();
|
|
case EmitObj:
|
|
return std::make_unique<BackendAction>(
|
|
BackendAction::BackendActionTy::Backend_EmitObj);
|
|
case EmitAssembly:
|
|
return std::make_unique<BackendAction>(
|
|
BackendAction::BackendActionTy::Backend_EmitAssembly);
|
|
case DebugUnparse:
|
|
return std::make_unique<DebugUnparseAction>();
|
|
case DebugUnparseNoSema:
|
|
return std::make_unique<DebugUnparseNoSemaAction>();
|
|
case DebugUnparseWithSymbols:
|
|
return std::make_unique<DebugUnparseWithSymbolsAction>();
|
|
case DebugDumpSymbols:
|
|
return std::make_unique<DebugDumpSymbolsAction>();
|
|
case DebugDumpParseTree:
|
|
return std::make_unique<DebugDumpParseTreeAction>();
|
|
case DebugDumpPFT:
|
|
return std::make_unique<DebugDumpPFTAction>();
|
|
case DebugDumpParseTreeNoSema:
|
|
return std::make_unique<DebugDumpParseTreeNoSemaAction>();
|
|
case DebugDumpAll:
|
|
return std::make_unique<DebugDumpAllAction>();
|
|
case DebugDumpProvenance:
|
|
return std::make_unique<DebugDumpProvenanceAction>();
|
|
case DebugDumpParsingLog:
|
|
return std::make_unique<DebugDumpParsingLogAction>();
|
|
case DebugMeasureParseTree:
|
|
return std::make_unique<DebugMeasureParseTreeAction>();
|
|
case DebugPreFIRTree:
|
|
return std::make_unique<DebugPreFIRTreeAction>();
|
|
case GetDefinition:
|
|
return std::make_unique<GetDefinitionAction>();
|
|
case GetSymbolsSources:
|
|
return std::make_unique<GetSymbolsSourcesAction>();
|
|
case InitOnly:
|
|
return std::make_unique<InitOnlyAction>();
|
|
case PluginAction: {
|
|
for (const FrontendPluginRegistry::entry &plugin :
|
|
FrontendPluginRegistry::entries()) {
|
|
if (plugin.getName() == ci.frontendOpts().ActionName) {
|
|
std::unique_ptr<PluginParseTreeAction> p(plugin.instantiate());
|
|
return std::move(p);
|
|
}
|
|
}
|
|
unsigned diagID = ci.diagnostics().getCustomDiagID(
|
|
clang::DiagnosticsEngine::Error, "unable to find plugin '%0'");
|
|
ci.diagnostics().Report(diagID) << ci.frontendOpts().ActionName;
|
|
return nullptr;
|
|
}
|
|
default:
|
|
break;
|
|
// TODO:
|
|
// case ParserSyntaxOnly:
|
|
// case EmitLLVM:
|
|
// case EmitLLVMOnly:
|
|
// case EmitCodeGenOnly:
|
|
// (...)
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
std::unique_ptr<FrontendAction> CreateFrontendAction(CompilerInstance &ci) {
|
|
// Create the underlying action.
|
|
std::unique_ptr<FrontendAction> act = CreateFrontendBaseAction(ci);
|
|
if (!act)
|
|
return nullptr;
|
|
|
|
return act;
|
|
}
|
|
|
|
bool ExecuteCompilerInvocation(CompilerInstance *flang) {
|
|
// Honor -help.
|
|
if (flang->frontendOpts().showHelp) {
|
|
clang::driver::getDriverOptTable().printHelp(llvm::outs(),
|
|
"flang-new -fc1 [options] file...", "LLVM 'Flang' Compiler",
|
|
/*Include=*/clang::driver::options::FC1Option,
|
|
/*Exclude=*/llvm::opt::DriverFlag::HelpHidden,
|
|
/*ShowAllAliases=*/false);
|
|
return true;
|
|
}
|
|
|
|
// Honor -version.
|
|
if (flang->frontendOpts().showVersion) {
|
|
llvm::cl::PrintVersionMessage();
|
|
return true;
|
|
}
|
|
|
|
// Load any requested plugins.
|
|
for (const std::string &Path : flang->frontendOpts().plugins) {
|
|
std::string Error;
|
|
if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(
|
|
Path.c_str(), &Error)) {
|
|
unsigned diagID = flang->diagnostics().getCustomDiagID(
|
|
clang::DiagnosticsEngine::Error, "unable to load plugin '%0': '%1'");
|
|
flang->diagnostics().Report(diagID) << Path << Error;
|
|
}
|
|
}
|
|
|
|
// If there were errors in processing arguments, don't do anything else.
|
|
if (flang->diagnostics().hasErrorOccurred()) {
|
|
return false;
|
|
}
|
|
|
|
// Create and execute the frontend action.
|
|
std::unique_ptr<FrontendAction> act(CreateFrontendAction(*flang));
|
|
if (!act)
|
|
return false;
|
|
|
|
bool success = flang->ExecuteAction(*act);
|
|
return success;
|
|
}
|
|
|
|
} // namespace Fortran::frontend
|