[flang-commits] [clang] [flang] [flang][RISCV][WIP] Add target-abi ModuleFlag. (PR #126188)

Craig Topper via flang-commits flang-commits at lists.llvm.org
Thu Feb 6 22:50:40 PST 2025


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/126188

This is needed to generate proper ABI flags in the ELF header for LTO builds. If these flags aren't set correctly, we can't link with objects that were built with the correct flags.

For non-LTO builds the mcpu/mattr in the TargetMachine will cause the backend to infer an ABI. For LTO builds the mcpu/mattr aren't set.

Still need to add tests and checking for valid ABI names.

>From 6c7034230d4b58bb352548b4b71051fbd058bbbe Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Thu, 6 Feb 2025 21:56:20 -0800
Subject: [PATCH] [flang][RISCV][WIP] Add target-abi ModuleFlag.

This is needed to generate proper ABI flags in the ELF header for LTO builds.
If these flags aren't set correctly, we can't link with objects that
were built with the correct flags.

For non-LTO builds the mcpu/mattr in the TargetMachine will cause
the backend to infer an ABI. For LTO builds the mcpu/mattr aren't set.

Still need testing and validation of the ABI name.
---
 clang/lib/Driver/ToolChains/Flang.cpp        | 4 ++++
 flang/include/flang/Frontend/TargetOptions.h | 3 +++
 flang/lib/Frontend/CompilerInvocation.cpp    | 1 +
 flang/lib/Frontend/FrontendActions.cpp       | 9 ++++++++-
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 1ae865f379110bc..255775b01d5a1f4 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -261,6 +261,10 @@ void Flang::AddPPCTargetArgs(const ArgList &Args,
 void Flang::AddRISCVTargetArgs(const ArgList &Args,
                                ArgStringList &CmdArgs) const {
   const llvm::Triple &Triple = getToolChain().getTriple();
+
+  StringRef ABIName = riscv::getRISCVABI(Args, Triple);
+  CmdArgs.push_back(Args.MakeArgString("-mabi=" + ABIName));
+
   // Handle -mrvv-vector-bits=<bits>
   if (Arg *A = Args.getLastArg(options::OPT_mrvv_vector_bits_EQ)) {
     StringRef Val = A->getValue();
diff --git a/flang/include/flang/Frontend/TargetOptions.h b/flang/include/flang/Frontend/TargetOptions.h
index 01c878067b921dc..4a854644e7ff62c 100644
--- a/flang/include/flang/Frontend/TargetOptions.h
+++ b/flang/include/flang/Frontend/TargetOptions.h
@@ -35,6 +35,9 @@ class TargetOptions {
   /// If given, the name of the target CPU to tune code for.
   std::string cpuToTuneFor;
 
+  /// If given, the name of the target ABI to use.
+  std::string abi;
+
   /// The list of target specific features to enable or disable, as written on
   /// the command line.
   std::vector<std::string> featuresAsWritten;
diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp
index 68b5950d3a51b7b..1f6a777dd5a83e0 100644
--- a/flang/lib/Frontend/CompilerInvocation.cpp
+++ b/flang/lib/Frontend/CompilerInvocation.cpp
@@ -464,6 +464,7 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
 
   if (const llvm::opt::Arg *a =
           args.getLastArg(clang::driver::options::OPT_mabi_EQ)) {
+    opts.abi = a->getValue();
     llvm::StringRef V = a->getValue();
     if (V == "vec-extabi") {
       opts.EnableAIXExtendedAltivecABI = true;
diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp
index b0545a7ac2f99a7..a87d4c2dc08f3b0 100644
--- a/flang/lib/Frontend/FrontendActions.cpp
+++ b/flang/lib/Frontend/FrontendActions.cpp
@@ -920,16 +920,23 @@ void CodeGenAction::generateLLVMIR() {
           static_cast<llvm::PIELevel::Level>(opts.PICLevel));
   }
 
+  const TargetOptions &targetOpts = ci.getInvocation().getTargetOpts();
+  const llvm::Triple triple(targetOpts.triple);
+
   // Set mcmodel level LLVM module flags
   std::optional<llvm::CodeModel::Model> cm = getCodeModel(opts.CodeModel);
   if (cm.has_value()) {
-    const llvm::Triple triple(ci.getInvocation().getTargetOpts().triple);
     llvmModule->setCodeModel(*cm);
     if ((cm == llvm::CodeModel::Medium || cm == llvm::CodeModel::Large) &&
         triple.getArch() == llvm::Triple::x86_64) {
       llvmModule->setLargeDataThreshold(opts.LargeDataThreshold);
     }
   }
+
+  if (triple.isRISCV() && !targetOpts.abi.empty())
+    llvmModule->addModuleFlag(
+        llvm::Module::Error, "target-abi",
+        llvm::MDString::get(llvmModule->getContext(), targetOpts.abi));
 }
 
 static std::unique_ptr<llvm::raw_pwrite_stream>



More information about the flang-commits mailing list