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/IR/Module.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'llvm/lib/IR/Module.cpp') diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp index c7b9f87..c7daaaf 100644 --- a/llvm/lib/IR/Module.cpp +++ b/llvm/lib/IR/Module.cpp @@ -915,3 +915,11 @@ VersionTuple Module::getDarwinTargetVariantSDKVersion() const { void Module::setDarwinTargetVariantSDKVersion(VersionTuple Version) { addSDKVersionMD(Version, *this, "darwin.target_variant.SDK Version"); } + +StringRef Module::getTargetABIFromMD() { + StringRef TargetABI; + if (auto *TargetABIMD = + dyn_cast_or_null(getModuleFlag("target-abi"))) + TargetABI = TargetABIMD->getString(); + return TargetABI; +} -- cgit v1.1