Skip to content

[flang][RISCV] Add target-abi ModuleFlag. #126188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2025
Merged

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Feb 7, 2025

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.

I've only added lp64, lp64f, and lp64d ABIs. ilp32* requires riscv32 which is not yet supported in flang. lp64e requires a different DataLayout string and would need additional plumbing.

Fixes #115679

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.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' flang:driver flang Flang issues not falling into any other category labels Feb 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 7, 2025

@llvm/pr-subscribers-flang-fir-hlfir
@llvm/pr-subscribers-flang-driver
@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Craig Topper (topperc)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/126188.diff

4 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Flang.cpp (+4)
  • (modified) flang/include/flang/Frontend/TargetOptions.h (+3)
  • (modified) flang/lib/Frontend/CompilerInvocation.cpp (+1)
  • (modified) flang/lib/Frontend/FrontendActions.cpp (+8-1)
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>

@topperc topperc requested a review from rofirrim February 7, 2025 06:51
@topperc topperc marked this pull request as draft February 7, 2025 06:52
@topperc
Copy link
Collaborator Author

topperc commented Feb 7, 2025

CC @ppenzin

@@ -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") {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Existing bug but why are these not checking the target is powerpc...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should check the target here. I will fix it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrtc27 I look at it further. The guard is not necessary. In AddPPCTargetArgs, the option is added only if it is PPC and isOSAIX() is true. So if it is not PPC, args in parseTargetArgs will not contain -mabi=vec-extabi.

@jeanPerier jeanPerier requested a review from kkwli February 7, 2025 10:10
Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know much the target-abi LLVM module flag, but this looks reasonable.

@topperc topperc marked this pull request as ready for review February 12, 2025 20:24
@topperc topperc changed the title [flang][RISCV][WIP] Add target-abi ModuleFlag. [flang][RISCV] Add target-abi ModuleFlag. Feb 12, 2025
Copy link

github-actions bot commented Feb 12, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@rofirrim
Copy link
Collaborator

LGTM. Thanks @topperc !

Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable. Thanks.

@topperc topperc merged commit 8da8ff8 into llvm:main Feb 13, 2025
8 checks passed
@topperc topperc deleted the pr/flang-abi branch February 13, 2025 16:08
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
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.

I've only added lp64, lp64f, and lp64d ABIs. ilp32* requires riscv32
which is not yet supported in flang. lp64e requires a different
DataLayout string and would need additional plumbing.

Fixes llvm#115679
sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
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.

I've only added lp64, lp64f, and lp64d ABIs. ilp32* requires riscv32
which is not yet supported in flang. lp64e requires a different
DataLayout string and would need additional plumbing.

Fixes llvm#115679
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category flang:driver flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang]When flang-new compiles for the RISC-V platform, the object files have an incorrect ABI.
7 participants