[flang-commits] [flang] 02fb5b7 - [flang][driver] Define the default frontend driver triple

Andrzej Warzynski via flang-commits flang-commits at lists.llvm.org
Wed May 4 05:02:20 PDT 2022


Author: Andrzej Warzynski
Date: 2022-05-04T12:01:53Z
New Revision: 02fb5b771d7ff130eff6c3ee5ac1c7cbdfb5dc63

URL: https://github.com/llvm/llvm-project/commit/02fb5b771d7ff130eff6c3ee5ac1c7cbdfb5dc63
DIFF: https://github.com/llvm/llvm-project/commit/02fb5b771d7ff130eff6c3ee5ac1c7cbdfb5dc63.diff

LOG: [flang][driver] Define the default frontend driver triple

*SUMMARY*
Currently, the frontend driver assumes that a target triple is either:
  * provided by the frontend itself (e.g. when lowering and generating
    code),
  * specified through the `-triple/-target` command line flags.

If `-triple/-target` is not used, the frontend will simply use the host
triple.

This is going to be insufficient when e.g. consuming an LLVM IR file
that has no triple specified (reading LLVM files is WIP, see D124667).
We shouldn't require the triple to be specified via the command line in
such situation. Instead, the frontend driver should contain a good
default, e.g.  the host triple.

This patch updates Flang's `CompilerInvocation` to do just that, i.e.
defines its default target triple. Similarly to Clang:
 * the default `CompilerInvocation` triple is set as the host triple,
 * the value specified with `-triple` takes precedence over the frontend
   driver default and the current module triple,
 * the frontend driver default takes precedence over the module triple.

*TESTS*
This change requires 2 unit tests to be updated. That's because relevant
frontend actions are updated to assume that there's always a valid
triple available in the current `CompilerInvocation`. This update is
required because the unit tests bypass the regular `CompilerInvocation`
set-up (in particular, they don't call
`CompilerInvocation::CreateFromArgs`). I've also taken the liberty to
disable the pre-precossor formatting in the affected unit tests as well
(it is not required).

No new tests are added. As `flang-new -fc1` does not support consuming
LLVM IR files just yet, it is not possible to compile an LLVM IR file
without a triple. More specifically, atm all LLVM IR files are generated
and stored internally and the driver makes sure that these contain a
valid target triple. This is about to change in D124667 (which adds
support for reading LLVM IR/BC files) and that's where tests for
exercising the default frontend driver triple will be added.

*WHAT DOES CLANG DO?*
For reference, the default target triple for Clang's
`CompilerInvocation` is set through option marshalling infra [1] in
Options.td. Please check the definition of the `-triple` flag:
```
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">;
```
Ideally, we should re-use the marshalling infra in Flang.

[1] https://clang.llvm.org/docs/InternalsManual.html#option-marshalling-infrastructure

Differential Revision: https://reviews.llvm.org/D124664

Added: 
    

Modified: 
    flang/lib/Frontend/CompilerInvocation.cpp
    flang/lib/Frontend/FrontendActions.cpp
    flang/unittests/Frontend/FrontendActionTest.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index d56566df9660d..9ea4be7df1bfc 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -19,11 +19,13 @@
 #include "clang/Driver/Options.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSwitch.h"
+#include "llvm/ADT/Triple.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Option/OptTable.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Process.h"
 #include "llvm/Support/raw_ostream.h"
@@ -93,7 +95,9 @@ bool Fortran::frontend::ParseDiagnosticArgs(clang::DiagnosticOptions &opts,
 /// \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);
+  if (const llvm::opt::Arg *a =
+          args.getLastArg(clang::driver::options::OPT_triple))
+    opts.triple = a->getValue();
 }
 
 // Tweak the frontend configuration based on the frontend action
@@ -559,6 +563,15 @@ bool CompilerInvocation::CreateFromArgs(CompilerInvocation &res,
 
   bool success = true;
 
+  // Set the default triple for this CompilerInvocation. This might be
+  // overridden by users with `-triple` (see the call to `ParseTargetArgs`
+  // below).
+  // NOTE: Like in Clang, it would be nice to use option marshalling
+  // for this so that the entire logic for setting-up the triple is in one
+  // place.
+  res.targetOpts().triple =
+      llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
+
   // Parse the arguments
   const llvm::opt::OptTable &opts = clang::driver::getDriverOptTable();
   const unsigned includedFlagsBitmask = clang::driver::options::FC1Option;

diff  --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index fd93bcaed7a83..d37d0ac037236 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -30,6 +30,7 @@
 #include "mlir/IR/Dialect.h"
 #include "mlir/Pass/PassManager.h"
 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
+#include "clang/Basic/DiagnosticFrontend.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
@@ -481,9 +482,15 @@ void EmitLLVMBitcodeAction::ExecuteAction() {
   if (!llvmModule)
     GenerateLLVMIR();
 
-  // Create and configure `Target`
+  // Set the triple based on the CompilerInvocation set-up
+  const std::string &theTriple = ci.invocation().targetOpts().triple;
+  if (llvmModule->getTargetTriple() != theTriple) {
+    ci.diagnostics().Report(clang::diag::warn_fe_override_module) << theTriple;
+    llvmModule->setTargetTriple(theTriple);
+  }
+
+  // Create `Target`
   std::string error;
-  std::string theTriple = llvmModule->getTargetTriple();
   const llvm::Target *theTarget =
       llvm::TargetRegistry::lookupTarget(theTriple, error);
   assert(theTarget && "Failed to create Target");
@@ -546,13 +553,17 @@ void BackendAction::ExecuteAction() {
   if (!llvmModule)
     GenerateLLVMIR();
 
+  // Set the triple based on the CompilerInvocation set-up
+  const std::string &theTriple = ci.invocation().targetOpts().triple;
+  if (llvmModule->getTargetTriple() != theTriple) {
+    ci.diagnostics().Report(clang::diag::warn_fe_override_module) << theTriple;
+    llvmModule->setTargetTriple(theTriple);
+  }
+
   // Create `Target`
   std::string error;
-  const std::string &theTriple = llvmModule->getTargetTriple();
   const llvm::Target *theTarget =
       llvm::TargetRegistry::lookupTarget(theTriple, error);
-  // TODO: Make this a diagnostic once `flang-new` can consume LLVM IR files
-  // (in which users could use unsupported triples)
   assert(theTarget && "Failed to create Target");
 
   // Create `TargetMachine`

diff  --git a/flang/unittests/Frontend/FrontendActionTest.cpp b/flang/unittests/Frontend/FrontendActionTest.cpp
index b88f1ab8710f0..69c79855504fa 100644
--- a/flang/unittests/Frontend/FrontendActionTest.cpp
+++ b/flang/unittests/Frontend/FrontendActionTest.cpp
@@ -10,7 +10,9 @@
 #include "flang/Frontend/CompilerInvocation.h"
 #include "flang/Frontend/FrontendOptions.h"
 #include "flang/FrontendTool/Utils.h"
+#include "llvm/ADT/Triple.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
 #include "llvm/Support/TargetSelect.h"
 #include "llvm/Support/raw_ostream.h"
 
@@ -170,7 +172,10 @@ TEST_F(FrontendActionTest, EmitLLVM) {
 
   // Set-up the action kind.
   compInst_.invocation().frontendOpts().programAction = EmitLLVM;
-  compInst_.invocation().preprocessorOpts().noReformat = true;
+
+  // Set-up default target triple.
+  compInst_.invocation().targetOpts().triple =
+      llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
 
   // Set-up the output stream. We are using output buffer wrapped as an output
   // stream, as opposed to an actual file (or a file descriptor).
@@ -197,7 +202,10 @@ TEST_F(FrontendActionTest, EmitAsm) {
 
   // Set-up the action kind.
   compInst_.invocation().frontendOpts().programAction = EmitAssembly;
-  compInst_.invocation().preprocessorOpts().noReformat = true;
+
+  // Set-up default target triple.
+  compInst_.invocation().targetOpts().triple =
+      llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
 
   // Initialise LLVM backend
   llvm::InitializeAllTargets();


        


More information about the flang-commits mailing list