From a67b0501cbbab83a4a0795365ef9c38f0a6473f6 Mon Sep 17 00:00:00 2001 From: Roger Ferrer Ibanez Date: Tue, 26 Mar 2019 08:01:18 +0000 Subject: [PATCH] [RISCV] Pass -target-abi to -cc1as The RISC-V assembler needs the target ABI because it defines a flag of the ELF file, as described in [1]. Make clang (the driver) to pass the target ABI to -cc1as in exactly the same way it does for -cc1. Currently -cc1as knows about -target-abi but is not handling it. Handle it and pass it to the MC layer via MCTargetOptions. [1] https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md#file-header Differential Revision: https://reviews.llvm.org/D59298 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@356981 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Driver/ToolChains/Clang.cpp | 14 ++++++++++++++ lib/Driver/ToolChains/Clang.h | 2 ++ test/Driver/riscv-abi.c | 8 ++++++++ tools/driver/cc1as_main.cpp | 13 +++++++++---- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/lib/Driver/ToolChains/Clang.cpp b/lib/Driver/ToolChains/Clang.cpp index 20f55dff5a..bbc2958697 100644 --- a/lib/Driver/ToolChains/Clang.cpp +++ b/lib/Driver/ToolChains/Clang.cpp @@ -5931,6 +5931,15 @@ void ClangAs::AddX86TargetArgs(const ArgList &Args, } } +void ClangAs::AddRISCVTargetArgs(const ArgList &Args, + ArgStringList &CmdArgs) const { + const llvm::Triple &Triple = getToolChain().getTriple(); + StringRef ABIName = riscv::getRISCVABI(Args, Triple); + + CmdArgs.push_back("-target-abi"); + CmdArgs.push_back(ABIName.data()); +} + void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const ArgList &Args, @@ -6100,6 +6109,11 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-arm-add-build-attributes"); } break; + + case llvm::Triple::riscv32: + case llvm::Triple::riscv64: + AddRISCVTargetArgs(Args, CmdArgs); + break; } // Consume all the warning flags. Usually this would be handled more diff --git a/lib/Driver/ToolChains/Clang.h b/lib/Driver/ToolChains/Clang.h index 81d8a35144..fc4f5ecdd2 100644 --- a/lib/Driver/ToolChains/Clang.h +++ b/lib/Driver/ToolChains/Clang.h @@ -119,6 +119,8 @@ public: llvm::opt::ArgStringList &CmdArgs) const; void AddX86TargetArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const; + void AddRISCVTargetArgs(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CmdArgs) const; bool hasGoodDiagnostics() const override { return true; } bool hasIntegratedAssembler() const override { return false; } bool hasIntegratedCPP() const override { return false; } diff --git a/test/Driver/riscv-abi.c b/test/Driver/riscv-abi.c index 8b79c8462b..6a97ff671d 100644 --- a/test/Driver/riscv-abi.c +++ b/test/Driver/riscv-abi.c @@ -2,6 +2,10 @@ // RUN: | FileCheck -check-prefix=CHECK-ILP32 %s // RUN: %clang -target riscv32-unknown-elf %s -### -o %t.o -mabi=ilp32 2>&1 \ // RUN: | FileCheck -check-prefix=CHECK-ILP32 %s +// RUN: %clang -target riscv32-unknown-elf -x assembler %s -### -o %t.o 2>&1 \ +// RUN: | FileCheck -check-prefix=CHECK-ILP32 %s +// RUN: %clang -target riscv32-unknown-elf -x assembler %s -### -o %t.o \ +// RUN: -mabi=ilp32 2>&1 | FileCheck -check-prefix=CHECK-ILP32 %s // CHECK-ILP32: "-target-abi" "ilp32" @@ -26,6 +30,10 @@ // RUN: | FileCheck -check-prefix=CHECK-LP64 %s // RUN: %clang -target riscv64-unknown-elf %s -### -o %t.o -mabi=lp64 2>&1 \ // RUN: | FileCheck -check-prefix=CHECK-LP64 %s +// RUN: %clang -target riscv64-unknown-elf -x assembler %s -### -o %t.o 2>&1 \ +// RUN: | FileCheck -check-prefix=CHECK-LP64 %s +// RUN: %clang -target riscv64-unknown-elf -x assembler %s -### -o %t.o \ +// RUN: -mabi=lp64 2>&1 | FileCheck -check-prefix=CHECK-LP64 %s // CHECK-LP64: "-target-abi" "lp64" diff --git a/tools/driver/cc1as_main.cpp b/tools/driver/cc1as_main.cpp index a234ba3fc1..c0334609be 100644 --- a/tools/driver/cc1as_main.cpp +++ b/tools/driver/cc1as_main.cpp @@ -137,6 +137,10 @@ struct AssemblerInvocation { /// The name of the relocation model to use. std::string RelocationModel; + /// The ABI targeted by the backend. Specified using -target-abi. Empty + /// otherwise. + std::string TargetABI; + /// @} public: @@ -282,6 +286,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack); Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings); Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic"); + Opts.TargetABI = Args.getLastArgValue(OPT_target_abi); Opts.IncrementalLinkerCompatible = Args.hasArg(OPT_mincremental_linker_compatible); Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym); @@ -426,6 +431,9 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, raw_pwrite_stream *Out = FDOS.get(); std::unique_ptr BOS; + MCTargetOptions MCOptions; + MCOptions.ABIName = Opts.TargetABI; + // FIXME: There is a bit of code duplication with addPassesToEmitFile. if (Opts.OutputType == AssemblerInvocation::FT_Asm) { MCInstPrinter *IP = TheTarget->createMCInstPrinter( @@ -434,7 +442,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, std::unique_ptr CE; if (Opts.ShowEncoding) CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); - MCTargetOptions MCOptions; std::unique_ptr MAB( TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); @@ -455,7 +462,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, std::unique_ptr CE( TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); - MCTargetOptions MCOptions; std::unique_ptr MAB( TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); std::unique_ptr OW = @@ -489,9 +495,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); // FIXME: init MCTargetOptions from sanitizer flags here. - MCTargetOptions Options; std::unique_ptr TAP( - TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options)); + TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions)); if (!TAP) Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; -- 2.40.0