Skip to content

Commit 6c70342

Browse files
committed
[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.
1 parent c3b7894 commit 6c70342

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ void Flang::AddPPCTargetArgs(const ArgList &Args,
261261
void Flang::AddRISCVTargetArgs(const ArgList &Args,
262262
ArgStringList &CmdArgs) const {
263263
const llvm::Triple &Triple = getToolChain().getTriple();
264+
265+
StringRef ABIName = riscv::getRISCVABI(Args, Triple);
266+
CmdArgs.push_back(Args.MakeArgString("-mabi=" + ABIName));
267+
264268
// Handle -mrvv-vector-bits=<bits>
265269
if (Arg *A = Args.getLastArg(options::OPT_mrvv_vector_bits_EQ)) {
266270
StringRef Val = A->getValue();

flang/include/flang/Frontend/TargetOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class TargetOptions {
3535
/// If given, the name of the target CPU to tune code for.
3636
std::string cpuToTuneFor;
3737

38+
/// If given, the name of the target ABI to use.
39+
std::string abi;
40+
3841
/// The list of target specific features to enable or disable, as written on
3942
/// the command line.
4043
std::vector<std::string> featuresAsWritten;

flang/lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ static void parseTargetArgs(TargetOptions &opts, llvm::opt::ArgList &args) {
464464

465465
if (const llvm::opt::Arg *a =
466466
args.getLastArg(clang::driver::options::OPT_mabi_EQ)) {
467+
opts.abi = a->getValue();
467468
llvm::StringRef V = a->getValue();
468469
if (V == "vec-extabi") {
469470
opts.EnableAIXExtendedAltivecABI = true;

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,16 +920,23 @@ void CodeGenAction::generateLLVMIR() {
920920
static_cast<llvm::PIELevel::Level>(opts.PICLevel));
921921
}
922922

923+
const TargetOptions &targetOpts = ci.getInvocation().getTargetOpts();
924+
const llvm::Triple triple(targetOpts.triple);
925+
923926
// Set mcmodel level LLVM module flags
924927
std::optional<llvm::CodeModel::Model> cm = getCodeModel(opts.CodeModel);
925928
if (cm.has_value()) {
926-
const llvm::Triple triple(ci.getInvocation().getTargetOpts().triple);
927929
llvmModule->setCodeModel(*cm);
928930
if ((cm == llvm::CodeModel::Medium || cm == llvm::CodeModel::Large) &&
929931
triple.getArch() == llvm::Triple::x86_64) {
930932
llvmModule->setLargeDataThreshold(opts.LargeDataThreshold);
931933
}
932934
}
935+
936+
if (triple.isRISCV() && !targetOpts.abi.empty())
937+
llvmModule->addModuleFlag(
938+
llvm::Module::Error, "target-abi",
939+
llvm::MDString::get(llvmModule->getContext(), targetOpts.abi));
933940
}
934941

935942
static std::unique_ptr<llvm::raw_pwrite_stream>

0 commit comments

Comments
 (0)