-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
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.
@llvm/pr-subscribers-flang-fir-hlfir @llvm/pr-subscribers-clang Author: Craig Topper (topperc) ChangesThis 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:
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>
|
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") { |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this 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.
✅ With the latest revision this PR passed the C/C++ code formatter. |
LGTM. Thanks @topperc ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable. Thanks.
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
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
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