From 733ad3fdebf782be5afffdb8310a0ce15675086c Mon Sep 17 00:00:00 2001 From: Kito Cheng Date: Fri, 7 Mar 2025 14:09:26 +0800 Subject: [LTO] Override TargetABI from module flags if present when creating TargetMachine (#126497) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …argetMachine RISC-V's data layout is determined by the ABI, not just the target triple. However, the TargetMachine is created using the data layout from the target triple, which is not always correct. This patch uses the target ABI from the module and passes it to the TargetMachine, ensuring that the data layout is set correctly according to the ABI. The same problem will happen with other targets like MIPS, but unfortunately, MIPS didn't emit the target-abi into the module flags, so this patch only fixes the issue for RISC-V. NOTE: MIPS with -mabi=n32 can trigger the same issue. Another possible solution is add new parameter to the TargetMachine constructor, but that would require changes in all the targets. --- llvm/lib/LTO/LTOBackend.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'llvm/lib/LTO/LTOBackend.cpp') diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index b38252a..139c39a 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -221,8 +221,13 @@ createTargetMachine(const Config &Conf, const Target *TheTarget, Module &M) { else CodeModel = M.getCodeModel(); + TargetOptions TargetOpts = Conf.Options; + if (TargetOpts.MCOptions.ABIName.empty()) { + TargetOpts.MCOptions.ABIName = M.getTargetABIFromMD(); + } + std::unique_ptr TM(TheTarget->createTargetMachine( - TheTriple.str(), Conf.CPU, Features.getString(), Conf.Options, RelocModel, + TheTriple.str(), Conf.CPU, Features.getString(), TargetOpts, RelocModel, CodeModel, Conf.CGOptLevel)); assert(TM && "Failed to create target machine"); -- cgit v1.1