[clang] 2e9439e - [flang][driver] Add support for `--target`/`--triple`
Andrzej Warzynski via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 25 01:45:46 PST 2022
Author: Andrzej Warzynski
Date: 2022-02-25T09:38:10Z
New Revision: 2e9439e4891c3d427c0fed477b54f3da2ff8ec28
URL: https://github.com/llvm/llvm-project/commit/2e9439e4891c3d427c0fed477b54f3da2ff8ec28
DIFF: https://github.com/llvm/llvm-project/commit/2e9439e4891c3d427c0fed477b54f3da2ff8ec28.diff
LOG: [flang][driver] Add support for `--target`/`--triple`
This patch adds support for:
* `--target` in the compiler driver (`flang-new`)
* `--triple` in the frontend driver (`flang-new -fc1`)
The semantics of these flags are inherited from `clangDriver`, i.e.
consistent with `clang --target` and `clang -cc1 --triple`,
respectively.
A new structure is defined, `TargetOptions`, that will hold various
Frontend options related to the target. Currently, this is mostly a
placeholder that contains the target triple. In the future, it will be
used for storing e.g. the CPU to tune for or the target features to
enable.
Additionally, the following target/triple related options are enabled
[*]: `-print-effective-triple`, `-print-target-triple`. Definitions in
Options.td are updated accordingly and, to facilated testing,
`-emit-llvm` is added to the list of options available in `flang-new`
(previously it was only enabled in `flang-new -fc1`).
[*] These options were actually available before (like all other options
defined in `clangDriver`), but not included in `flang-new --help`.
Before this change, `flang-new` would just use `native` for defining the
target, so these options were of little value.
Differential Revision: https://reviews.llvm.org/D120246
Added:
flang/include/flang/Frontend/TargetOptions.h
flang/test/Driver/print-effective-triple.f90
flang/test/Driver/print-target-triple.f90
flang/test/Driver/target.f90
Modified:
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Flang.cpp
flang/include/flang/Frontend/CompilerInvocation.h
flang/lib/Frontend/CompilerInvocation.cpp
flang/lib/Frontend/FrontendActions.cpp
flang/test/Driver/driver-help-hidden.f90
flang/test/Driver/driver-help.f90
Removed:
################################################################################
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 8dd16ca990a14..60e90f3f2237a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1078,7 +1078,7 @@ def d_Flag : Flag<["-"], "d">, Group<d_Group>;
def d_Joined : Joined<["-"], "d">, Group<d_Group>;
def emit_ast : Flag<["-"], "emit-ast">,
HelpText<"Emit Clang AST files for source inputs">;
-def emit_llvm : Flag<["-"], "emit-llvm">, Flags<[CC1Option, FC1Option]>, Group<Action_Group>,
+def emit_llvm : Flag<["-"], "emit-llvm">, Flags<[CC1Option, FC1Option, FlangOption]>, Group<Action_Group>,
HelpText<"Use the LLVM representation for assembler and object files">;
def emit_interface_stubs : Flag<["-"], "emit-interface-stubs">, Flags<[CC1Option]>, Group<Action_Group>,
HelpText<"Generate Interface Stub Files.">;
@@ -3894,9 +3894,9 @@ def print_multi_lib : Flag<["-", "--"], "print-multi-lib">;
def print_multi_os_directory : Flag<["-", "--"], "print-multi-os-directory">,
Flags<[Unsupported]>;
def print_target_triple : Flag<["-", "--"], "print-target-triple">,
- HelpText<"Print the normalized target triple">;
+ HelpText<"Print the normalized target triple">, Flags<[FlangOption]>;
def print_effective_triple : Flag<["-", "--"], "print-effective-triple">,
- HelpText<"Print the effective target triple">;
+ HelpText<"Print the effective target triple">, Flags<[FlangOption]>;
def print_multiarch : Flag<["-", "--"], "print-multiarch">,
HelpText<"Print the multiarch target triple">;
def print_prog_name_EQ : Joined<["-", "--"], "print-prog-name=">,
@@ -4014,7 +4014,7 @@ def no_system_header_prefix : Joined<["--"], "no-system-header-prefix=">,
"system header.">;
def : Separate<["--"], "no-system-header-prefix">, Alias<no_system_header_prefix>;
def s : Flag<["-"], "s">, Group<Link_Group>;
-def target : Joined<["--"], "target=">, Flags<[NoXarchOption, CoreOption]>,
+def target : Joined<["--"], "target=">, Flags<[NoXarchOption, CoreOption, FlangOption]>,
HelpText<"Generate code for the given target">;
def darwin_target_variant : Separate<["-"], "darwin-target-variant">,
Flags<[NoXarchOption, CoreOption]>,
@@ -4803,7 +4803,7 @@ def emit_fir : Flag<["-"], "emit-fir">, Alias<emit_mlir>;
let Flags = [CC1Option, NoDriverOption] in {
//===----------------------------------------------------------------------===//
-// Target Options
+// Target Options (cc1 + cc1as)
//===----------------------------------------------------------------------===//
let Flags = [CC1Option, CC1AsOption, NoDriverOption] in {
@@ -4817,10 +4817,6 @@ def tune_cpu : Separate<["-"], "tune-cpu">,
def target_feature : Separate<["-"], "target-feature">,
HelpText<"Target specific attributes">,
MarshallingInfoStringVector<TargetOpts<"FeaturesAsWritten">>;
-def triple : Separate<["-"], "triple">,
- HelpText<"Specify target triple (e.g. i686-apple-darwin9)">,
- MarshallingInfoString<TargetOpts<"Triple">, "llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple())">,
- AlwaysEmit, Normalizer<"normalizeTriple">;
def target_abi : Separate<["-"], "target-abi">,
HelpText<"Target a particular ABI type">,
MarshallingInfoString<TargetOpts<"ABI">>;
@@ -4834,7 +4830,23 @@ def darwin_target_variant_sdk_version_EQ : Joined<["-"],
"darwin-target-variant-sdk-version=">,
HelpText<"The version of darwin target variant SDK used for compilation">;
-}
+} // let Flags = [CC1Option, CC1AsOption, NoDriverOption]
+
+//===----------------------------------------------------------------------===//
+// Target Options (cc1 + cc1as + fc1)
+//===----------------------------------------------------------------------===//
+let Flags = [CC1Option, CC1AsOption, FC1Option, NoDriverOption] in {
+
+def triple : Separate<["-"], "triple">,
+ HelpText<"Specify target triple (e.g. i686-apple-darwin9)">,
+ MarshallingInfoString<TargetOpts<"Triple">, "llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple())">,
+ AlwaysEmit, Normalizer<"normalizeTriple">;
+
+} // let Flags = [CC1Option, CC1ASOption, FC1Option, NoDriverOption]
+
+//===----------------------------------------------------------------------===//
+// Target Options (other)
+//===----------------------------------------------------------------------===//
def target_linker_version : Separate<["-"], "target-linker-version">,
HelpText<"Target linker version">,
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index c169e3d457938..c77aa68257a67 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -54,21 +54,17 @@ void Flang::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output, const InputInfoList &Inputs,
const ArgList &Args, const char *LinkingOutput) const {
const auto &TC = getToolChain();
- // TODO: Once code-generation is available, this will need to be commented
- // out.
- // const llvm::Triple &Triple = TC.getEffectiveTriple();
- // const std::string &TripleStr = Triple.getTriple();
+ const llvm::Triple &Triple = TC.getEffectiveTriple();
+ const std::string &TripleStr = Triple.getTriple();
ArgStringList CmdArgs;
// Invoke ourselves in -fc1 mode.
CmdArgs.push_back("-fc1");
- // TODO: Once code-generation is available, this will need to be commented
- // out.
// Add the "effective" target triple.
- // CmdArgs.push_back("-triple");
- // CmdArgs.push_back(Args.MakeArgString(TripleStr));
+ CmdArgs.push_back("-triple");
+ CmdArgs.push_back(Args.MakeArgString(TripleStr));
if (isa<PreprocessJobAction>(JA)) {
CmdArgs.push_back("-E");
diff --git a/flang/include/flang/Frontend/CompilerInvocation.h b/flang/include/flang/Frontend/CompilerInvocation.h
index 0b59da690c874..5b335cd0a4fd1 100644
--- a/flang/include/flang/Frontend/CompilerInvocation.h
+++ b/flang/include/flang/Frontend/CompilerInvocation.h
@@ -10,6 +10,7 @@
#include "flang/Frontend/FrontendOptions.h"
#include "flang/Frontend/PreprocessorOptions.h"
+#include "flang/Frontend/TargetOptions.h"
#include "flang/Parser/parsing.h"
#include "flang/Semantics/semantics.h"
#include "clang/Basic/Diagnostic.h"
@@ -61,6 +62,9 @@ class CompilerInvocation : public CompilerInvocationBase {
// of options.
Fortran::parser::Options parserOpts_;
+ /// Options controlling the target.
+ Fortran::frontend::TargetOptions targetOpts_;
+
// Semantics context
std::unique_ptr<Fortran::semantics::SemanticsContext> semanticsContext_;
@@ -117,6 +121,9 @@ class CompilerInvocation : public CompilerInvocationBase {
Fortran::parser::Options &fortranOpts() { return parserOpts_; }
const Fortran::parser::Options &fortranOpts() const { return parserOpts_; }
+ TargetOptions &targetOpts() { return targetOpts_; }
+ const TargetOptions &TargetOpts() const { return targetOpts_; }
+
Fortran::semantics::SemanticsContext &semanticsContext() {
return *semanticsContext_;
}
diff --git a/flang/include/flang/Frontend/TargetOptions.h b/flang/include/flang/Frontend/TargetOptions.h
new file mode 100644
index 0000000000000..bb44714e5d56e
--- /dev/null
+++ b/flang/include/flang/Frontend/TargetOptions.h
@@ -0,0 +1,35 @@
+//===--- TargetOptions.h ----------------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Defines the flang::TargetOptions class.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_FLANG_TARGETOPTIONS_H
+#define LLVM_FLANG_TARGETOPTIONS_H
+
+namespace Fortran::frontend {
+
+/// Options for controlling the target. Currently this is just a placeholder.
+/// In the future, we will use this to specify various target options that
+/// will affect the generated code e.g.:
+/// * CPU to tune the code for
+/// * available CPU/hardware extensions
+/// * target specific features to enable/disable
+/// * options for accelerators (e.g. GPUs)
+/// * (...)
+class TargetOptions {
+public:
+ /// The name of the target triple to compile for.
+ std::string triple;
+};
+
+} // end namespace Fortran::frontend
+
+#endif
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 7507b0091e13c..32888bde3ab39 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -9,6 +9,7 @@
#include "flang/Frontend/CompilerInvocation.h"
#include "flang/Common/Fortran-features.h"
#include "flang/Frontend/PreprocessorOptions.h"
+#include "flang/Frontend/TargetOptions.h"
#include "flang/Semantics/semantics.h"
#include "flang/Version.inc"
#include "clang/Basic/AllDiagnostics.h"
@@ -88,6 +89,15 @@ bool Fortran::frontend::ParseDiagnosticArgs(clang::DiagnosticOptions &opts,
return true;
}
+/// Parses all target input arguments and populates the target
+/// options accordingly.
+///
+/// \param [in] opts The target options instance to update
+/// \param [in] args The list of input arguments (from the compiler invocation)
+static void ParseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
+ opts.triple = args.getLastArgValue(clang::driver::options::OPT_triple);
+}
+
// Tweak the frontend configuration based on the frontend action
static void setUpFrontendBasedOnAction(FrontendOptions &opts) {
assert(opts.programAction != Fortran::frontend::InvalidAction &&
@@ -563,6 +573,7 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
}
success &= ParseFrontendArgs(res.frontendOpts(), args, diags);
+ ParseTargetArgs(res.targetOpts(), args);
parsePreprocessorArgs(res.preprocessorOpts(), args);
success &= parseSemaArgs(res, args, diags);
success &= parseDialectArgs(res, args, diags);
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index d787add5dcfe2..68ac24653d993 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -81,7 +81,7 @@ bool CodeGenAction::BeginSourceFileAction() {
llvm::ArrayRef<fir::KindTy>{fir::fromDefaultKinds(defKinds)});
lower::LoweringBridge lb = Fortran::lower::LoweringBridge::create(*mlirCtx,
defKinds, ci.invocation().semanticsContext().intrinsics(),
- ci.parsing().allCooked(), /*triple=*/"native", kindMap);
+ ci.parsing().allCooked(), ci.invocation().targetOpts().triple, kindMap);
// Create a parse tree and lower it to FIR
Fortran::parser::Program &parseTree{*ci.parsing().parseTree()};
diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 92e6af5786ed5..5aed6f25ae52f 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -21,6 +21,7 @@
! CHECK-NEXT: -cpp Enable predefined and command line preprocessor macros
! CHECK-NEXT: -c Only run preprocess, compile, and assemble steps
! CHECK-NEXT: -D <macro>=<value> Define <macro> to <value> (or 1 if <value> omitted)
+! CHECK-NEXT: -emit-llvm Use the LLVM representation for assembler and object files
! CHECK-NEXT: -E Only run the preprocessor
! CHECK-NEXT: -falternative-parameter-statement
! CHECK-NEXT: Enable the old style PARAMETER statement
@@ -50,8 +51,11 @@
! CHECK-NEXT: -nocpp Disable predefined and command line preprocessor macros
! CHECK-NEXT: -o <file> Write output to <file>
! CHECK-NEXT: -pedantic Warn on language extensions
+! CHECK-NEXT: -print-effective-triple Print the effective target triple
+! CHECK-NEXT: -print-target-triple Print the normalized target triple
! CHECK-NEXT: -P Disable linemarker output in -E mode
! CHECK-NEXT: -std=<value> Language standard to compile for
+! CHECK-NEXT: --target=<value> Generate code for the given target
! CHECK-NEXT: -U <macro> Undefine macro <macro>
! CHECK-NEXT: --version Print version information
! CHECK-NEXT: -W<warning> Enable the specified warning
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index 73d4697591919..66950597b9e8f 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -21,6 +21,7 @@
! HELP-NEXT: -cpp Enable predefined and command line preprocessor macros
! HELP-NEXT: -c Only run preprocess, compile, and assemble steps
! HELP-NEXT: -D <macro>=<value> Define <macro> to <value> (or 1 if <value> omitted)
+! HELP-NEXT: -emit-llvm Use the LLVM representation for assembler and object files
! HELP-NEXT: -E Only run the preprocessor
! HELP-NEXT: -falternative-parameter-statement
! HELP-NEXT: Enable the old style PARAMETER statement
@@ -50,8 +51,11 @@
! HELP-NEXT: -nocpp Disable predefined and command line preprocessor macros
! HELP-NEXT: -o <file> Write output to <file>
! HELP-NEXT: -pedantic Warn on language extensions
+! HELP-NEXT: -print-effective-triple Print the effective target triple
+! HELP-NEXT: -print-target-triple Print the normalized target triple
! HELP-NEXT: -P Disable linemarker output in -E mode
! HELP-NEXT: -std=<value> Language standard to compile for
+! HELP-NEXT: --target=<value> Generate code for the given target
! HELP-NEXT: -U <macro> Undefine macro <macro>
! HELP-NEXT: --version Print version information
! HELP-NEXT: -W<warning> Enable the specified warning
@@ -124,6 +128,7 @@
! HELP-FC1-NEXT: -P Disable linemarker output in -E mode
! HELP-FC1-NEXT: -std=<value> Language standard to compile for
! HELP-FC1-NEXT: -test-io Run the InputOuputTest action. Use for development and testing only.
+! HELP-FC1-NEXT: -triple <value> Specify target triple (e.g. i686-apple-darwin9)
! HELP-FC1-NEXT: -U <macro> Undefine macro <macro>
! HELP-FC1-NEXT: --version Print version information
! HELP-FC1-NEXT: -W<warning> Enable the specified warning
diff --git a/flang/test/Driver/print-effective-triple.f90 b/flang/test/Driver/print-effective-triple.f90
new file mode 100644
index 0000000000000..5f53d5f1aee54
--- /dev/null
+++ b/flang/test/Driver/print-effective-triple.f90
@@ -0,0 +1,11 @@
+! Test that -print-target-triple prints correct triple.
+
+!-----------------------------------------
+! RUN LINE
+!-----------------------------------------
+! RUN: %flang -print-effective-triple 2>&1 --target=thumb-linux-gnu | FileCheck %s
+
+!-----------------
+! EXPECTED OUTPUT
+!-----------------
+! CHECK: armv4t-unknown-linux-gnu
diff --git a/flang/test/Driver/print-target-triple.f90 b/flang/test/Driver/print-target-triple.f90
new file mode 100644
index 0000000000000..1a73af6ec633e
--- /dev/null
+++ b/flang/test/Driver/print-target-triple.f90
@@ -0,0 +1,16 @@
+! Test that -print-target-triple prints correct triple
+
+!-----------------------------------------
+! RUN LINES
+!-----------------------------------------
+! RUN: %flang -print-target-triple 2>&1 --target=aarch64-linux-gnu \
+! RUN: | FileCheck --check-prefix=AARCH64 %s
+
+! RUN: %flang -print-target-triple 2>&1 --target=x86_64-linux-gnu \
+! RUN: | FileCheck --check-prefix=X86_64 %s
+
+!-----------------
+! EXPECTED OUTPUT
+!-----------------
+! X86_64: x86_64-unknown-linux-gnu
+! AARCH64: aarch64-unknown-linux-gnu
diff --git a/flang/test/Driver/target.f90 b/flang/test/Driver/target.f90
new file mode 100644
index 0000000000000..bdcf139e9f4b8
--- /dev/null
+++ b/flang/test/Driver/target.f90
@@ -0,0 +1,13 @@
+! Test that --target indeed sets the target
+
+!-----------------------------------------
+! RUN LINES
+!-----------------------------------------
+! RUN: %flang --target=unknown-unknown-unknown -emit-llvm -c %s \
+! RUN: -o %t.o -### 2>&1 | FileCheck %s
+
+!-----------------
+! EXPECTED OUTPUT
+!-----------------
+! CHECK: Target: unknown-unknown-unknown
+! CHECK: "-triple" "unknown-unknown-unknown"
More information about the cfe-commits
mailing list