[llvm] [llvm] Move data layout string computation to TargetParser (PR #157612)
Reid Kleckner via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 11 09:42:34 PDT 2025
https://github.com/rnk updated https://github.com/llvm/llvm-project/pull/157612
>From 0b07de24a73dbf675327f2979c5c682b682b7503 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Mon, 8 Sep 2025 22:09:25 +0000
Subject: [PATCH 1/7] [IR] Make DataLayout compute canonical data layouts
Clang and other frontends generally need the LLVM data layout string in
order to generate LLVM IR modules for LLVM. MLIR clients often need it
as well, since MLIR users often lower to LLVM IR.
Before this change, the LLVM datalayout string was computed in the
LLVM${TGT}CodeGen library in the relevant TargetMachine subclass.
However, none of the logic for computing the data layout string requires
any details of code generation. Clients who want to avoid duplicating
this information were forced to link in LLVMCodeGen and all registered
targets, leading to bloated binaries.
By moving this information to the DataLayout class in the IR file, we
can delete the duplicate datalayout strings in Clang, and retain the
ability to generate IR for unregistered targets.
---
llvm/include/llvm/IR/DataLayout.h | 6 +
llvm/lib/IR/DataLayout.cpp | 621 ++++++++++++++++++
.../Target/AArch64/AArch64TargetMachine.cpp | 30 +-
.../lib/Target/AMDGPU/AMDGPUTargetMachine.cpp | 21 +-
llvm/lib/Target/ARC/ARCTargetMachine.cpp | 9 +-
llvm/lib/Target/ARM/ARMTargetMachine.cpp | 69 +-
llvm/lib/Target/ARM/ARMTargetMachine.h | 3 +-
llvm/lib/Target/AVR/AVRTargetMachine.cpp | 5 +-
llvm/lib/Target/BPF/BPFTargetMachine.cpp | 12 +-
llvm/lib/Target/CSKY/CSKYTargetMachine.cpp | 19 +-
.../Target/DirectX/DirectXTargetMachine.cpp | 8 +-
.../Target/Hexagon/HexagonTargetMachine.cpp | 12 +-
llvm/lib/Target/Lanai/LanaiTargetMachine.cpp | 15 +-
.../LoongArch/LoongArchTargetMachine.cpp | 9 +-
llvm/lib/Target/M68k/M68kTargetMachine.cpp | 33 +-
.../lib/Target/MSP430/MSP430TargetMachine.cpp | 9 +-
.../Target/Mips/AsmParser/MipsAsmParser.cpp | 4 +-
.../Target/Mips/MCTargetDesc/MipsABIInfo.cpp | 11 +-
.../Target/Mips/MCTargetDesc/MipsABIInfo.h | 3 +-
.../Mips/MCTargetDesc/MipsAsmBackend.cpp | 2 +-
.../Mips/MCTargetDesc/MipsMCAsmInfo.cpp | 2 +-
llvm/lib/Target/Mips/MipsTargetMachine.cpp | 42 +-
llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp | 29 +-
llvm/lib/Target/PowerPC/PPCTargetMachine.cpp | 58 +-
llvm/lib/Target/RISCV/RISCVTargetMachine.cpp | 40 +-
llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp | 25 +-
llvm/lib/Target/Sparc/SparcTargetMachine.cpp | 35 +-
.../Target/SystemZ/SystemZTargetMachine.cpp | 43 +-
llvm/lib/Target/VE/VETargetMachine.cpp | 36 +-
.../WebAssembly/WebAssemblyTargetMachine.cpp | 16 +-
llvm/lib/Target/X86/X86TargetMachine.cpp | 50 +-
.../lib/Target/Xtensa/XtensaTargetMachine.cpp | 9 +-
32 files changed, 699 insertions(+), 587 deletions(-)
diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h
index 2acae246c0b1e..9439ddcbb8cb2 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -194,6 +194,12 @@ class DataLayout {
/// description on failure.
LLVM_ABI static Expected<DataLayout> parse(StringRef LayoutString);
+ /// Given an LLVM target triple, compute the well-known datalayout string.
+ /// TODO: Create a static factor helper version of this method and refactor
+ /// the TargetMachine callers to use it.
+ LLVM_ABI static std::string computeStringForTriple(const Triple &T,
+ StringRef ABIName = "");
+
/// Layout endianness...
bool isLittleEndian() const { return !BigEndian; }
bool isBigEndian() const { return BigEndian; }
diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp
index ed629d4e5ea22..aef1d989e2889 100644
--- a/llvm/lib/IR/DataLayout.cpp
+++ b/llvm/lib/IR/DataLayout.cpp
@@ -26,12 +26,14 @@
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemAlloc.h"
#include "llvm/Support/TypeSize.h"
#include "llvm/TargetParser/Triple.h"
+#include "llvm/TargetParser/ARMTargetParser.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
@@ -172,6 +174,625 @@ struct LessPointerAddrSpace {
};
} // namespace
+static std::string computeARMDataLayout(const Triple &TT, StringRef ABIName) {
+ auto ABI = ARM::computeTargetABI(TT, ABIName);
+ std::string Ret;
+
+ if (TT.isLittleEndian())
+ // Little endian.
+ Ret += "e";
+ else
+ // Big endian.
+ Ret += "E";
+
+ Ret += DataLayout::getManglingComponent(TT);
+
+ // Pointers are 32 bits and aligned to 32 bits.
+ Ret += "-p:32:32";
+
+ // Function pointers are aligned to 8 bits (because the LSB stores the
+ // ARM/Thumb state).
+ Ret += "-Fi8";
+
+ // ABIs other than APCS have 64 bit integers with natural alignment.
+ if (ABI != ARM::ARM_ABI_APCS)
+ Ret += "-i64:64";
+
+ // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
+ // bits, others to 64 bits. We always try to align to 64 bits.
+ if (ABI == ARM::ARM_ABI_APCS)
+ Ret += "-f64:32:64";
+
+ // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
+ // to 64. We always ty to give them natural alignment.
+ if (ABI == ARM::ARM_ABI_APCS)
+ Ret += "-v64:32:64-v128:32:128";
+ else if (ABI != ARM::ARM_ABI_AAPCS16)
+ Ret += "-v128:64:128";
+
+ // Try to align aggregates to 32 bits (the default is 64 bits, which has no
+ // particular hardware support on 32-bit ARM).
+ Ret += "-a:0:32";
+
+ // Integer registers are 32 bits.
+ Ret += "-n32";
+
+ // The stack is 64 bit aligned on AAPCS and 32 bit aligned everywhere else.
+ if (ABI == ARM::ARM_ABI_AAPCS16)
+ Ret += "-S128";
+ else if (ABI == ARM::ARM_ABI_AAPCS)
+ Ret += "-S64";
+ else
+ Ret += "-S32";
+
+ return Ret;
+}
+
+// Helper function to build a DataLayout string
+static std::string computeAArch64DataLayout(const Triple &TT) {
+ if (TT.isOSBinFormatMachO()) {
+ if (TT.getArch() == Triple::aarch64_32)
+ return "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-"
+ "n32:64-S128-Fn32";
+ return "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-"
+ "Fn32";
+ }
+ if (TT.isOSBinFormatCOFF())
+ return "e-m:w-p270:32:32-p271:32:32-p272:64:64-p:64:64-i32:32-i64:64-i128:"
+ "128-n32:64-S128-Fn32";
+ std::string Endian = TT.isLittleEndian() ? "e" : "E";
+ std::string Ptr32 = TT.getEnvironment() == Triple::GNUILP32 ? "-p:32:32" : "";
+ return Endian + "-m:e" + Ptr32 +
+ "-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-"
+ "n32:64-S128-Fn32";
+}
+
+// DataLayout: little or big endian
+static std::string computeBPFDataLayout(const Triple &TT) {
+ if (TT.getArch() == Triple::bpfeb)
+ return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
+ else
+ return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
+}
+
+static std::string computeCSKYDataLayout(const Triple &TT) {
+ std::string Ret;
+
+ // Only support little endian for now.
+ // TODO: Add support for big endian.
+ Ret += "e";
+
+ // CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
+ // It's a 4-byte aligned stack with ELF mangling only.
+ Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
+ "-v128:32:32-a:0:32-Fi32-n32";
+
+ return Ret;
+}
+
+static std::string computeLoongArchDataLayout(const Triple &TT) {
+ if (TT.isArch64Bit())
+ return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
+ assert(TT.isArch32Bit() && "only LA32 and LA64 are currently supported");
+ return "e-m:e-p:32:32-i64:64-n32-S128";
+}
+
+static std::string computeM68kDataLayout(const Triple &TT) {
+ std::string Ret = "";
+ // M68k is Big Endian
+ Ret += "E";
+
+ // FIXME how to wire it with the used object format?
+ Ret += "-m:e";
+
+ // M68k pointers are always 32 bit wide even for 16-bit CPUs.
+ // The ABI only specifies 16-bit alignment.
+ // On at least the 68020+ with a 32-bit bus, there is a performance benefit
+ // to having 32-bit alignment.
+ Ret += "-p:32:16:32";
+
+ // Bytes do not require special alignment, words are word aligned and
+ // long words are word aligned at minimum.
+ Ret += "-i8:8:8-i16:16:16-i32:16:32";
+
+ // FIXME no floats at the moment
+
+ // The registers can hold 8, 16, 32 bits
+ Ret += "-n8:16:32";
+
+ Ret += "-a:0:16-S16";
+
+ return Ret;
+}
+
+namespace {
+enum class MipsABI {
+ Unknown,
+ O32,
+ N32,
+ N64
+};
+}
+
+// FIXME: This duplicates MipsABIInfo::computeTargetABI, but duplicating this is
+// preferable to violating layering rules. Ideally that information should live
+// in LLVM TargetParser, but for now we just duplicate some ABI name string
+// logic for simplicity.
+static MipsABI getMipsABI(const Triple &TT, StringRef ABIName) {
+ if (ABIName.starts_with("o32"))
+ return MipsABI::O32;
+ if (ABIName.starts_with("n32"))
+ return MipsABI::N32;
+ if (ABIName.starts_with("n64"))
+ return MipsABI::N64;
+ if (TT.isABIN32())
+ return MipsABI::N32;
+ assert(ABIName.empty() && "Unknown ABI option for MIPS");
+
+ if (TT.isMIPS64())
+ return MipsABI::N64;
+ return MipsABI::O32;
+}
+
+static std::string computeMipsDataLayout(const Triple &TT, StringRef ABIName) {
+ std::string Ret;
+ MipsABI ABI = getMipsABI(TT, ABIName);
+
+ // There are both little and big endian mips.
+ if (TT.isLittleEndian())
+ Ret += "e";
+ else
+ Ret += "E";
+
+ if (ABI == MipsABI::O32)
+ Ret += "-m:m";
+ else
+ Ret += "-m:e";
+
+ // Pointers are 32 bit on some ABIs.
+ if (ABI != MipsABI::N64)
+ Ret += "-p:32:32";
+
+ // 8 and 16 bit integers only need to have natural alignment, but try to
+ // align them to 32 bits. 64 bit integers have natural alignment.
+ Ret += "-i8:8:32-i16:16:32-i64:64";
+
+ // 32 bit registers are always available and the stack is at least 64 bit
+ // aligned. On N64 64 bit registers are also available and the stack is
+ // 128 bit aligned.
+ if (ABI == MipsABI::N64 || ABI == MipsABI::N32)
+ Ret += "-i128:128-n32:64-S128";
+ else
+ Ret += "-n32-S64";
+
+ return Ret;
+}
+
+static std::string computePowerDataLayout(const Triple &T) {
+ bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
+ std::string Ret;
+
+ // Most PPC* platforms are big endian, PPC(64)LE is little endian.
+ if (T.isLittleEndian())
+ Ret = "e";
+ else
+ Ret = "E";
+
+ Ret += DataLayout::getManglingComponent(T);
+
+ // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
+ // pointers.
+ if (!is64Bit || T.getOS() == Triple::Lv2)
+ Ret += "-p:32:32";
+
+ // If the target ABI uses function descriptors, then the alignment of function
+ // pointers depends on the alignment used to emit the descriptor. Otherwise,
+ // function pointers are aligned to 32 bits because the instructions must be.
+ if ((T.getArch() == Triple::ppc64 && !T.isPPC64ELFv2ABI())) {
+ Ret += "-Fi64";
+ } else if (T.isOSAIX()) {
+ Ret += is64Bit ? "-Fi64" : "-Fi32";
+ } else {
+ Ret += "-Fn32";
+ }
+
+ // Note, the alignment values for f64 and i64 on ppc64 in Darwin
+ // documentation are wrong; these are correct (i.e. "what gcc does").
+ Ret += "-i64:64";
+
+ // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
+ if (is64Bit)
+ Ret += "-i128:128-n32:64";
+ else
+ Ret += "-n32";
+
+ // Specify the vector alignment explicitly. For v256i1 and v512i1, the
+ // calculated alignment would be 256*alignment(i1) and 512*alignment(i1),
+ // which is 256 and 512 bytes - way over aligned.
+ if (is64Bit && (T.isOSAIX() || T.isOSLinux()))
+ Ret += "-S128-v256:256:256-v512:512:512";
+
+ return Ret;
+}
+
+static std::string computeAMDDataLayout(const Triple &TT) {
+ if (TT.getArch() == Triple::r600) {
+ // 32-bit pointers.
+ return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128"
+ "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1";
+ }
+
+ // 32-bit private, local, and region pointers. 64-bit global, constant and
+ // flat. 160-bit non-integral fat buffer pointers that include a 128-bit
+ // buffer descriptor and a 32-bit offset, which are indexed by 32-bit values
+ // (address space 7), and 128-bit non-integral buffer resourcees (address
+ // space 8) which cannot be non-trivilally accessed by LLVM memory operations
+ // like getelementptr.
+ return "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32"
+ "-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-i64:64-"
+ "v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-"
+ "v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9";
+}
+
+static std::string computeRISCVDataLayout(const Triple &TT, StringRef ABIName) {
+ std::string Ret;
+
+ if (TT.isLittleEndian())
+ Ret += "e";
+ else
+ Ret += "E";
+
+ Ret += "-m:e";
+
+ // Pointer and integer sizes.
+ if (TT.isArch64Bit()) {
+ Ret += "-p:64:64-i64:64-i128:128";
+ Ret += "-n32:64";
+ } else {
+ assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported");
+ Ret += "-p:32:32-i64:64";
+ Ret += "-n32";
+ }
+
+ // Stack alignment based on ABI.
+ StringRef ABI = ABIName;
+ if (ABI == "ilp32e")
+ Ret += "-S32";
+ else if (ABI == "lp64e")
+ Ret += "-S64";
+ else
+ Ret += "-S128";
+
+ return Ret;
+}
+
+static std::string computeSparcDataLayout(const Triple &T) {
+ const bool is64Bit = T.isSPARC64();
+
+ // Sparc is typically big endian, but some are little.
+ std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E";
+ Ret += "-m:e";
+
+ // Some ABIs have 32bit pointers.
+ if (!is64Bit)
+ Ret += "-p:32:32";
+
+ // Alignments for 64 bit integers.
+ Ret += "-i64:64";
+
+ // Alignments for 128 bit integers.
+ // This is not specified in the ABI document but is the de facto standard.
+ Ret += "-i128:128";
+
+ // On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
+ // On SparcV9 registers can hold 64 or 32 bits, on others only 32.
+ if (is64Bit)
+ Ret += "-n32:64";
+ else
+ Ret += "-f128:64-n32";
+
+ if (is64Bit)
+ Ret += "-S128";
+ else
+ Ret += "-S64";
+
+ return Ret;
+}
+
+static std::string computeSystemZDataLayout(const Triple &TT) {
+ std::string Ret;
+
+ // Big endian.
+ Ret += "E";
+
+ // Data mangling.
+ Ret += DataLayout::getManglingComponent(TT);
+
+ // Special features for z/OS.
+ if (TT.isOSzOS()) {
+ if (TT.isArch64Bit()) {
+ // Custom address space for ptr32.
+ Ret += "-p1:32:32";
+ }
+ }
+
+ // Make sure that global data has at least 16 bits of alignment by
+ // default, so that we can refer to it using LARL. We don't have any
+ // special requirements for stack variables though.
+ Ret += "-i1:8:16-i8:8:16";
+
+ // 64-bit integers are naturally aligned.
+ Ret += "-i64:64";
+
+ // 128-bit floats are aligned only to 64 bits.
+ Ret += "-f128:64";
+
+ // The DataLayout string always holds a vector alignment of 64 bits, see
+ // comment in clang/lib/Basic/Targets/SystemZ.h.
+ Ret += "-v128:64";
+
+ // We prefer 16 bits of aligned for all globals; see above.
+ Ret += "-a:8:16";
+
+ // Integer registers are 32 or 64 bits.
+ Ret += "-n32:64";
+
+ return Ret;
+}
+
+static std::string computeX86DataLayout(const Triple &TT) {
+ // X86 is little endian
+ std::string Ret = "e";
+
+ Ret += DataLayout::getManglingComponent(TT);
+ // X86 and x32 have 32 bit pointers.
+ if (!TT.isArch64Bit() || TT.isX32())
+ Ret += "-p:32:32";
+
+ // Address spaces for 32 bit signed, 32 bit unsigned, and 64 bit pointers.
+ Ret += "-p270:32:32-p271:32:32-p272:64:64";
+
+ // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
+ // 128 bit integers are not specified in the 32-bit ABIs but are used
+ // internally for lowering f128, so we match the alignment to that.
+ if (TT.isArch64Bit() || TT.isOSWindows())
+ Ret += "-i64:64-i128:128";
+ else if (TT.isOSIAMCU())
+ Ret += "-i64:32-f64:32";
+ else
+ Ret += "-i128:128-f64:32:64";
+
+ // Some ABIs align long double to 128 bits, others to 32.
+ if (TT.isOSIAMCU())
+ ; // No f80
+ else if (TT.isArch64Bit() || TT.isOSDarwin() || TT.isWindowsMSVCEnvironment())
+ Ret += "-f80:128";
+ else
+ Ret += "-f80:32";
+
+ if (TT.isOSIAMCU())
+ Ret += "-f128:32";
+
+ // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
+ if (TT.isArch64Bit())
+ Ret += "-n8:16:32:64";
+ else
+ Ret += "-n8:16:32";
+
+ // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
+ if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
+ Ret += "-a:0:32-S32";
+ else
+ Ret += "-S128";
+
+ return Ret;
+}
+
+
+static cl::opt<bool> NVPTXUseShortPointers(
+ "nvptx-short-ptr",
+ cl::desc(
+ "Use 32-bit pointers for accessing const/local/shared address spaces."),
+ cl::init(false), cl::Hidden);
+
+static std::string computeNVPTXDataLayout(const Triple &T) {
+ std::string Ret = "e";
+
+ // Tensor Memory (addrspace:6) is always 32-bits.
+ // Distributed Shared Memory (addrspace:7) follows shared memory
+ // (addrspace:3).
+ if (!T.isArch64Bit())
+ Ret += "-p:32:32-p6:32:32-p7:32:32";
+ else if (NVPTXUseShortPointers)
+ Ret += "-p3:32:32-p4:32:32-p5:32:32-p6:32:32-p7:32:32";
+ else
+ Ret += "-p6:32:32";
+
+ Ret += "-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64";
+
+ return Ret;
+}
+
+static std::string computeSPIRVDataLayout(const Triple &TT) {
+ const auto Arch = TT.getArch();
+ // TODO: this probably needs to be revisited:
+ // Logical SPIR-V has no pointer size, so any fixed pointer size would be
+ // wrong. The choice to default to 32 or 64 is just motivated by another
+ // memory model used for graphics: PhysicalStorageBuffer64. But it shouldn't
+ // mean anything.
+ if (Arch == Triple::spirv32)
+ return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-"
+ "v256:256-v512:512-v1024:1024-n8:16:32:64-G1";
+ if (Arch == Triple::spirv)
+ return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
+ "v512:512-v1024:1024-n8:16:32:64-G10";
+ if (TT.getVendor() == Triple::VendorType::AMD &&
+ TT.getOS() == Triple::OSType::AMDHSA)
+ return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
+ "v512:512-v1024:1024-n32:64-S32-G1-P4-A0";
+ return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
+ "v512:512-v1024:1024-n8:16:32:64-G1";
+}
+
+static std::string computeLanaiDataLayout() {
+ // Data layout (keep in sync with clang/lib/Basic/Targets.cpp)
+ return "E" // Big endian
+ "-m:e" // ELF name manging
+ "-p:32:32" // 32-bit pointers, 32 bit aligned
+ "-i64:64" // 64 bit integers, 64 bit aligned
+ "-a:0:32" // 32 bit alignment of objects of aggregate type
+ "-n32" // 32 bit native integer width
+ "-S64"; // 64 bit natural stack alignment
+}
+
+static std::string computeWebAssemblyDataLayout(const Triple &TT) {
+ return TT.isArch64Bit()
+ ? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
+ "i128:128-f128:64-n32:64-S128-ni:1:10:20"
+ : "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
+ "i128:128-n32:64-S128-ni:1:10:20")
+ : (TT.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
+ "i128:128-f128:64-n32:64-S128-ni:1:10:20"
+ : "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
+ "i128:128-n32:64-S128-ni:1:10:20");
+}
+
+static std::string computeVEDataLayout(const Triple &T) {
+ // Aurora VE is little endian
+ std::string Ret = "e";
+
+ // Use ELF mangling
+ Ret += "-m:e";
+
+ // Alignments for 64 bit integers.
+ Ret += "-i64:64";
+
+ // VE supports 32 bit and 64 bits integer on registers
+ Ret += "-n32:64";
+
+ // Stack alignment is 128 bits
+ Ret += "-S128";
+
+ // Vector alignments are 64 bits
+ // Need to define all of them. Otherwise, each alignment becomes
+ // the size of each data by default.
+ Ret += "-v64:64:64"; // for v2f32
+ Ret += "-v128:64:64";
+ Ret += "-v256:64:64";
+ Ret += "-v512:64:64";
+ Ret += "-v1024:64:64";
+ Ret += "-v2048:64:64";
+ Ret += "-v4096:64:64";
+ Ret += "-v8192:64:64";
+ Ret += "-v16384:64:64"; // for v256f64
+
+ return Ret;
+}
+
+// static
+std::string DataLayout::computeStringForTriple(const Triple &T,
+ StringRef ABIName) {
+ switch (T.getArch()) {
+ case Triple::arm:
+ case Triple::armeb:
+ case Triple::thumb:
+ case Triple::thumbeb:
+ return computeARMDataLayout(T, ABIName);
+ case Triple::aarch64:
+ case Triple::aarch64_be:
+ case Triple::aarch64_32:
+ return computeAArch64DataLayout(T);
+ case Triple::arc:
+ return "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
+ "f32:32:32-i64:32-f64:32-a:0:32-n32";
+ case Triple::avr:
+ return "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8:16-a:8";
+ case Triple::bpfel:
+ case Triple::bpfeb:
+ return computeBPFDataLayout(T);
+ case Triple::csky:
+ return computeCSKYDataLayout(T);
+ case Triple::dxil:
+ return "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-"
+ "f32:32-f64:64-n8:16:32:64";
+ case Triple::hexagon:
+ return "e-m:e-p:32:32:32-a:0-n16:32-"
+ "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
+ "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048";
+ case Triple::loongarch32:
+ case Triple::loongarch64:
+ return computeLoongArchDataLayout(T);
+ case Triple::m68k:
+ return computeM68kDataLayout(T);
+ case Triple::mips:
+ case Triple::mipsel:
+ case Triple::mips64:
+ case Triple::mips64el:
+ return computeMipsDataLayout(T, ABIName);
+ case Triple::msp430:
+ return "e-m:e-p:16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S16";
+ case Triple::ppc:
+ case Triple::ppcle:
+ case Triple::ppc64:
+ case Triple::ppc64le:
+ return computePowerDataLayout(T);
+ case Triple::r600:
+ case Triple::amdgcn:
+ return computeAMDDataLayout(T);
+ case Triple::riscv32:
+ case Triple::riscv64:
+ case Triple::riscv32be:
+ case Triple::riscv64be:
+ return computeRISCVDataLayout(T, ABIName);
+ case Triple::sparc:
+ case Triple::sparcv9:
+ case Triple::sparcel:
+ return computeSparcDataLayout(T);
+ case Triple::systemz:
+ return computeSystemZDataLayout(T);
+ case Triple::tce:
+ case Triple::tcele:
+ case Triple::x86:
+ case Triple::x86_64:
+ return computeX86DataLayout(T);
+ case Triple::xcore:
+ case Triple::xtensa:
+ return "e-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32";
+ case Triple::nvptx:
+ case Triple::nvptx64:
+ return computeNVPTXDataLayout(T);
+ case Triple::spir:
+ case Triple::spir64:
+ case Triple::spirv:
+ case Triple::spirv32:
+ case Triple::spirv64:
+ return computeSPIRVDataLayout(T);
+ case Triple::lanai:
+ return computeLanaiDataLayout();
+ case Triple::wasm32:
+ case Triple::wasm64:
+ return computeWebAssemblyDataLayout(T);
+ case Triple::ve:
+ return computeVEDataLayout(T);
+
+ case Triple::amdil:
+ case Triple::amdil64:
+ case Triple::hsail:
+ case Triple::hsail64:
+ case Triple::kalimba:
+ case Triple::shave:
+ case Triple::renderscript32:
+ case Triple::renderscript64:
+ // These are all virtual ISAs with no LLVM backend, and therefore no fixed
+ // LLVM data layout.
+ return "";
+
+ case Triple::UnknownArch:
+ return "";
+ }
+ return "";
+}
+
const char *DataLayout::getManglingComponent(const Triple &T) {
if (T.isOSBinFormatGOFF())
return "-m:l";
diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
index 4650b2d0c8151..e8f48d3f65941 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
@@ -295,27 +295,6 @@ static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
return std::make_unique<AArch64_ELFTargetObjectFile>();
}
-// Helper function to build a DataLayout string
-static std::string computeDataLayout(const Triple &TT,
- const MCTargetOptions &Options,
- bool LittleEndian) {
- if (TT.isOSBinFormatMachO()) {
- if (TT.getArch() == Triple::aarch64_32)
- return "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-"
- "n32:64-S128-Fn32";
- return "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-"
- "Fn32";
- }
- if (TT.isOSBinFormatCOFF())
- return "e-m:w-p270:32:32-p271:32:32-p272:64:64-p:64:64-i32:32-i64:64-i128:"
- "128-n32:64-S128-Fn32";
- std::string Endian = LittleEndian ? "e" : "E";
- std::string Ptr32 = TT.getEnvironment() == Triple::GNUILP32 ? "-p:32:32" : "";
- return Endian + "-m:e" + Ptr32 +
- "-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-"
- "n32:64-S128-Fn32";
-}
-
static StringRef computeDefaultCPU(const Triple &TT, StringRef CPU) {
if (CPU.empty() && TT.isArm64e())
return "apple-a12";
@@ -368,11 +347,10 @@ AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT,
bool LittleEndian)
- : CodeGenTargetMachineImpl(
- T, computeDataLayout(TT, Options.MCOptions, LittleEndian), TT,
- computeDefaultCPU(TT, CPU), FS, Options,
- getEffectiveRelocModel(TT, RM),
- getEffectiveAArch64CodeModel(TT, CM, JIT), OL),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ computeDefaultCPU(TT, CPU), FS, Options,
+ getEffectiveRelocModel(TT, RM),
+ getEffectiveAArch64CodeModel(TT, CM, JIT), OL),
TLOF(createTLOF(getTargetTriple())), isLittle(LittleEndian),
UseNewSMEABILowering(EnableNewSMEABILowering) {
initAsmInfo();
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
index 9afe7590fe4ef..afe8bbf88e8fb 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
@@ -720,25 +720,6 @@ static MachineSchedRegistry GCNILPSchedRegistry(
"Run GCN iterative scheduler for ILP scheduling (experimental)",
createIterativeILPMachineScheduler);
-static StringRef computeDataLayout(const Triple &TT) {
- if (TT.getArch() == Triple::r600) {
- // 32-bit pointers.
- return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128"
- "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1";
- }
-
- // 32-bit private, local, and region pointers. 64-bit global, constant and
- // flat. 160-bit non-integral fat buffer pointers that include a 128-bit
- // buffer descriptor and a 32-bit offset, which are indexed by 32-bit values
- // (address space 7), and 128-bit non-integral buffer resourcees (address
- // space 8) which cannot be non-trivilally accessed by LLVM memory operations
- // like getelementptr.
- return "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32"
- "-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-i64:64-"
- "v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-"
- "v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9";
-}
-
LLVM_READNONE
static StringRef getGPUOrDefault(const Triple &TT, StringRef GPU) {
if (!GPU.empty())
@@ -764,7 +745,7 @@ AMDGPUTargetMachine::AMDGPUTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OptLevel)
: CodeGenTargetMachineImpl(
- T, computeDataLayout(TT), TT, getGPUOrDefault(TT, CPU), FS, Options,
+ T, DataLayout::computeStringForTriple(TT), TT, getGPUOrDefault(TT, CPU), FS, Options,
getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OptLevel),
TLOF(createTLOF(getTargetTriple())) {
diff --git a/llvm/lib/Target/ARC/ARCTargetMachine.cpp b/llvm/lib/Target/ARC/ARCTargetMachine.cpp
index 370336394ba7f..7a555f76d57f0 100644
--- a/llvm/lib/Target/ARC/ARCTargetMachine.cpp
+++ b/llvm/lib/Target/ARC/ARCTargetMachine.cpp
@@ -33,12 +33,9 @@ ARCTargetMachine::ARCTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(
- T,
- "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
- "f32:32:32-i64:32-f64:32-a:0:32-n32",
- TT, CPU, FS, Options, getRelocModel(RM),
- getEffectiveCodeModel(CM, CodeModel::Small), OL),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, getRelocModel(RM),
+ getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
initAsmInfo();
diff --git a/llvm/lib/Target/ARM/ARMTargetMachine.cpp b/llvm/lib/Target/ARM/ARMTargetMachine.cpp
index fedf9e2cf34b1..f98e0d648e46d 100644
--- a/llvm/lib/Target/ARM/ARMTargetMachine.cpp
+++ b/llvm/lib/Target/ARM/ARMTargetMachine.cpp
@@ -121,62 +121,6 @@ static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
return std::make_unique<ARMElfTargetObjectFile>();
}
-static std::string computeDataLayout(const Triple &TT,
- const TargetOptions &Options,
- bool isLittle) {
- auto ABI = ARM::computeTargetABI(TT, Options.MCOptions.ABIName);
- std::string Ret;
-
- if (isLittle)
- // Little endian.
- Ret += "e";
- else
- // Big endian.
- Ret += "E";
-
- Ret += DataLayout::getManglingComponent(TT);
-
- // Pointers are 32 bits and aligned to 32 bits.
- Ret += "-p:32:32";
-
- // Function pointers are aligned to 8 bits (because the LSB stores the
- // ARM/Thumb state).
- Ret += "-Fi8";
-
- // ABIs other than APCS have 64 bit integers with natural alignment.
- if (ABI != ARM::ARM_ABI_APCS)
- Ret += "-i64:64";
-
- // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
- // bits, others to 64 bits. We always try to align to 64 bits.
- if (ABI == ARM::ARM_ABI_APCS)
- Ret += "-f64:32:64";
-
- // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
- // to 64. We always ty to give them natural alignment.
- if (ABI == ARM::ARM_ABI_APCS)
- Ret += "-v64:32:64-v128:32:128";
- else if (ABI != ARM::ARM_ABI_AAPCS16)
- Ret += "-v128:64:128";
-
- // Try to align aggregates to 32 bits (the default is 64 bits, which has no
- // particular hardware support on 32-bit ARM).
- Ret += "-a:0:32";
-
- // Integer registers are 32 bits.
- Ret += "-n32";
-
- // The stack is 64 bit aligned on AAPCS and 32 bit aligned everywhere else.
- if (ABI == ARM::ARM_ABI_AAPCS16)
- Ret += "-S128";
- else if (ABI == ARM::ARM_ABI_AAPCS)
- Ret += "-S64";
- else
- Ret += "-S32";
-
- return Ret;
-}
-
static Reloc::Model getEffectiveRelocModel(const Triple &TT,
std::optional<Reloc::Model> RM) {
if (!RM)
@@ -201,9 +145,12 @@ ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
const TargetOptions &Options,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
- CodeGenOptLevel OL, bool isLittle)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT, Options, isLittle), TT,
- CPU, FS, Options, getEffectiveRelocModel(TT, RM),
+ CodeGenOptLevel OL)
+ : CodeGenTargetMachineImpl(T,
+ DataLayout::computeStringForTriple(
+ TT, Options.MCOptions.ABIName),
+ TT, CPU, FS, Options,
+ getEffectiveRelocModel(TT, RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TargetABI(ARM::computeTargetABI(TT, Options.MCOptions.ABIName)),
TLOF(createTLOF(getTargetTriple())), isLittle(isLittle) {
@@ -334,7 +281,7 @@ ARMLETargetMachine::ARMLETargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
+ : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {}
ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
@@ -342,7 +289,7 @@ ARMBETargetMachine::ARMBETargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
+ : ARMBaseTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL) {}
namespace {
diff --git a/llvm/lib/Target/ARM/ARMTargetMachine.h b/llvm/lib/Target/ARM/ARMTargetMachine.h
index 1d73af1da6d02..c417c4c8bae65 100644
--- a/llvm/lib/Target/ARM/ARMTargetMachine.h
+++ b/llvm/lib/Target/ARM/ARMTargetMachine.h
@@ -42,8 +42,7 @@ class ARMBaseTargetMachine : public CodeGenTargetMachineImpl {
ARMBaseTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
StringRef FS, const TargetOptions &Options,
std::optional<Reloc::Model> RM,
- std::optional<CodeModel::Model> CM, CodeGenOptLevel OL,
- bool isLittle);
+ std::optional<CodeModel::Model> CM, CodeGenOptLevel OL);
~ARMBaseTargetMachine() override;
const ARMSubtarget *getSubtargetImpl(const Function &F) const override;
diff --git a/llvm/lib/Target/AVR/AVRTargetMachine.cpp b/llvm/lib/Target/AVR/AVRTargetMachine.cpp
index fbd148478c894..2367478d7c117 100644
--- a/llvm/lib/Target/AVR/AVRTargetMachine.cpp
+++ b/llvm/lib/Target/AVR/AVRTargetMachine.cpp
@@ -28,9 +28,6 @@
namespace llvm {
-static const char *AVRDataLayout =
- "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8:16-a:8";
-
/// Processes a CPU name.
static StringRef getCPU(StringRef CPU) {
if (CPU.empty() || CPU == "generic") {
@@ -50,7 +47,7 @@ AVRTargetMachine::AVRTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, AVRDataLayout, TT, getCPU(CPU), FS, Options,
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT, getCPU(CPU), FS, Options,
getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
SubTarget(TT, std::string(getCPU(CPU)), std::string(FS), *this) {
diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.cpp b/llvm/lib/Target/BPF/BPFTargetMachine.cpp
index 527a480354571..611a5f47280fd 100644
--- a/llvm/lib/Target/BPF/BPFTargetMachine.cpp
+++ b/llvm/lib/Target/BPF/BPFTargetMachine.cpp
@@ -59,14 +59,6 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeBPFTarget() {
initializeBPFMIPreEmitCheckingPass(PR);
}
-// DataLayout: little or big endian
-static std::string computeDataLayout(const Triple &TT) {
- if (TT.getArch() == Triple::bpfeb)
- return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
- else
- return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
-}
-
static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
return RM.value_or(Reloc::PIC_);
}
@@ -77,8 +69,8 @@ BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT), TT, CPU, FS, Options,
- getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
diff --git a/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp b/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
index ae6ef89fdcd07..03ccce9bb2c8e 100644
--- a/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
+++ b/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
@@ -33,29 +33,14 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYTarget() {
initializeCSKYDAGToDAGISelLegacyPass(*Registry);
}
-static std::string computeDataLayout(const Triple &TT) {
- std::string Ret;
-
- // Only support little endian for now.
- // TODO: Add support for big endian.
- Ret += "e";
-
- // CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
- // It's a 4-byte aligned stack with ELF mangling only.
- Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
- "-v128:32:32-a:0:32-Fi32-n32";
-
- return Ret;
-}
-
CSKYTargetMachine::CSKYTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT), TT, CPU, FS, Options,
- RM.value_or(Reloc::Static),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, RM.value_or(Reloc::Static),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<CSKYELFTargetObjectFile>()) {
initAsmInfo();
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index f5d5a73c926e9..d281eeb640a1b 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -134,11 +134,9 @@ DirectXTargetMachine::DirectXTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(
- T,
- "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-"
- "f32:32-f64:64-n8:16:32:64",
- TT, CPU, FS, Options, Reloc::Static, CodeModel::Small, OL),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, Reloc::Static,
+ CodeModel::Small, OL),
TLOF(std::make_unique<DXILTargetObjectFile>()),
Subtarget(std::make_unique<DirectXSubtarget>(TT, CPU, FS, *this)) {
initAsmInfo();
diff --git a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp
index 66508fd767793..a0cafd0187ac1 100644
--- a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp
@@ -231,14 +231,10 @@ HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
// Specify the vector alignment explicitly. For v512x1, the calculated
// alignment would be 512*alignment(i1), which is 512 bytes, instead of
// the required minimum of 64 bytes.
- : CodeGenTargetMachineImpl(
- T,
- "e-m:e-p:32:32:32-a:0-n16:32-"
- "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
- "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048",
- TT, CPU, FS, Options, getEffectiveRelocModel(RM),
- getEffectiveCodeModel(CM, CodeModel::Small),
- (HexagonNoOpt ? CodeGenOptLevel::None : OL)),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, getEffectiveRelocModel(RM),
+ getEffectiveCodeModel(CM, CodeModel::Small),
+ (HexagonNoOpt ? CodeGenOptLevel::None : OL)),
TLOF(std::make_unique<HexagonTargetObjectFile>()),
Subtarget(Triple(TT), CPU, FS, *this) {
initAsmInfo();
diff --git a/llvm/lib/Target/Lanai/LanaiTargetMachine.cpp b/llvm/lib/Target/Lanai/LanaiTargetMachine.cpp
index 3d6ba9ecc55e2..f76ce255bdb65 100644
--- a/llvm/lib/Target/Lanai/LanaiTargetMachine.cpp
+++ b/llvm/lib/Target/Lanai/LanaiTargetMachine.cpp
@@ -37,17 +37,6 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLanaiTarget() {
initializeLanaiMemAluCombinerPass(PR);
}
-static std::string computeDataLayout() {
- // Data layout (keep in sync with clang/lib/Basic/Targets.cpp)
- return "E" // Big endian
- "-m:e" // ELF name manging
- "-p:32:32" // 32-bit pointers, 32 bit aligned
- "-i64:64" // 64 bit integers, 64 bit aligned
- "-a:0:32" // 32 bit alignment of objects of aggregate type
- "-n32" // 32 bit native integer width
- "-S64"; // 64 bit natural stack alignment
-}
-
static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
return RM.value_or(Reloc::PIC_);
}
@@ -58,8 +47,8 @@ LanaiTargetMachine::LanaiTargetMachine(
std::optional<CodeModel::Model> CodeModel, CodeGenOptLevel OptLevel,
bool JIT)
: CodeGenTargetMachineImpl(
- T, computeDataLayout(), TT, Cpu, FeatureString, Options,
- getEffectiveRelocModel(RM),
+ T, DataLayout::computeStringForTriple(TT), TT, Cpu, FeatureString,
+ Options, getEffectiveRelocModel(RM),
getEffectiveCodeModel(CodeModel, CodeModel::Medium), OptLevel),
Subtarget(TT, Cpu, FeatureString, *this, Options, getCodeModel(),
OptLevel),
diff --git a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
index c36db9c75dd3a..c0e7c49e8221a 100644
--- a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
@@ -57,13 +57,6 @@ static cl::opt<bool>
cl::desc("Enable the loop data prefetch pass"),
cl::init(false));
-static std::string computeDataLayout(const Triple &TT) {
- if (TT.isArch64Bit())
- return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
- assert(TT.isArch32Bit() && "only LA32 and LA64 are currently supported");
- return "e-m:e-p:32:32-i64:64-n32-S128";
-}
-
static Reloc::Model getEffectiveRelocModel(const Triple &TT,
std::optional<Reloc::Model> RM) {
return RM.value_or(Reloc::Static);
@@ -93,7 +86,7 @@ LoongArchTargetMachine::LoongArchTargetMachine(
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
const TargetOptions &Options, std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT), TT, CPU, FS, Options,
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT, CPU, FS, Options,
getEffectiveRelocModel(TT, RM),
getEffectiveLoongArchCodeModel(TT, CM), OL),
TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {
diff --git a/llvm/lib/Target/M68k/M68kTargetMachine.cpp b/llvm/lib/Target/M68k/M68kTargetMachine.cpp
index ce15ee635e21b..53daa7c4d4950 100644
--- a/llvm/lib/Target/M68k/M68kTargetMachine.cpp
+++ b/llvm/lib/Target/M68k/M68kTargetMachine.cpp
@@ -46,35 +46,6 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeM68kTarget() {
namespace {
-std::string computeDataLayout(const Triple &TT, StringRef CPU,
- const TargetOptions &Options) {
- std::string Ret = "";
- // M68k is Big Endian
- Ret += "E";
-
- // FIXME how to wire it with the used object format?
- Ret += "-m:e";
-
- // M68k pointers are always 32 bit wide even for 16-bit CPUs.
- // The ABI only specifies 16-bit alignment.
- // On at least the 68020+ with a 32-bit bus, there is a performance benefit
- // to having 32-bit alignment.
- Ret += "-p:32:16:32";
-
- // Bytes do not require special alignment, words are word aligned and
- // long words are word aligned at minimum.
- Ret += "-i8:8:8-i16:16:16-i32:16:32";
-
- // FIXME no floats at the moment
-
- // The registers can hold 8, 16, 32 bits
- Ret += "-n8:16:32";
-
- Ret += "-a:0:16-S16";
-
- return Ret;
-}
-
Reloc::Model getEffectiveRelocModel(const Triple &TT,
std::optional<Reloc::Model> RM) {
// If not defined we default to static
@@ -101,8 +72,8 @@ M68kTargetMachine::M68kTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT, CPU, Options), TT, CPU,
- FS, Options, getEffectiveRelocModel(TT, RM),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, getEffectiveRelocModel(TT, RM),
::getEffectiveCodeModel(CM, JIT), OL),
TLOF(std::make_unique<M68kELFTargetObjectFile>()),
Subtarget(TT, CPU, FS, *this) {
diff --git a/llvm/lib/Target/MSP430/MSP430TargetMachine.cpp b/llvm/lib/Target/MSP430/MSP430TargetMachine.cpp
index e6024f4a62185..fbffbb2e6f875 100644
--- a/llvm/lib/Target/MSP430/MSP430TargetMachine.cpp
+++ b/llvm/lib/Target/MSP430/MSP430TargetMachine.cpp
@@ -34,19 +34,14 @@ static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
return RM.value_or(Reloc::Static);
}
-static std::string computeDataLayout(const Triple &TT, StringRef CPU,
- const TargetOptions &Options) {
- return "e-m:e-p:16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S16";
-}
-
MSP430TargetMachine::MSP430TargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT, CPU, Options), TT, CPU,
- FS, Options, getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
diff --git a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
index 7b2ee832ae7db..8a5cb517c94c5 100644
--- a/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
+++ b/llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
@@ -524,8 +524,8 @@ class MipsAsmParser : public MCTargetAsmParser {
MipsAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
const MCInstrInfo &MII, const MCTargetOptions &Options)
: MCTargetAsmParser(Options, sti, MII),
- ABI(MipsABIInfo::computeTargetABI(sti.getTargetTriple(), sti.getCPU(),
- Options)) {
+ ABI(MipsABIInfo::computeTargetABI(sti.getTargetTriple(),
+ Options.getABIName())) {
MCAsmParserExtension::Initialize(parser);
parser.addAliasForDirective(".asciiz", ".asciz");
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsABIInfo.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsABIInfo.cpp
index 1be29cf3c94b9..d7809e27e23f3 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsABIInfo.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsABIInfo.cpp
@@ -57,17 +57,16 @@ unsigned MipsABIInfo::GetCalleeAllocdArgSizeInBytes(CallingConv::ID CC) const {
llvm_unreachable("Unhandled ABI");
}
-MipsABIInfo MipsABIInfo::computeTargetABI(const Triple &TT, StringRef CPU,
- const MCTargetOptions &Options) {
- if (Options.getABIName().starts_with("o32"))
+MipsABIInfo MipsABIInfo::computeTargetABI(const Triple &TT, StringRef ABIName) {
+ if (ABIName.starts_with("o32"))
return MipsABIInfo::O32();
- if (Options.getABIName().starts_with("n32"))
+ if (ABIName.starts_with("n32"))
return MipsABIInfo::N32();
- if (Options.getABIName().starts_with("n64"))
+ if (ABIName.starts_with("n64"))
return MipsABIInfo::N64();
if (TT.isABIN32())
return MipsABIInfo::N32();
- assert(Options.getABIName().empty() && "Unknown ABI option for MIPS");
+ assert(ABIName.empty() && "Unknown ABI option for MIPS");
if (TT.isMIPS64())
return MipsABIInfo::N64();
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsABIInfo.h b/llvm/lib/Target/Mips/MCTargetDesc/MipsABIInfo.h
index 44b023c7c3ef6..d8003d2fcc164 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsABIInfo.h
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsABIInfo.h
@@ -33,8 +33,7 @@ class MipsABIInfo {
static MipsABIInfo O32() { return MipsABIInfo(ABI::O32); }
static MipsABIInfo N32() { return MipsABIInfo(ABI::N32); }
static MipsABIInfo N64() { return MipsABIInfo(ABI::N64); }
- static MipsABIInfo computeTargetABI(const Triple &TT, StringRef CPU,
- const MCTargetOptions &Options);
+ static MipsABIInfo computeTargetABI(const Triple &TT, StringRef ABIName);
bool IsKnown() const { return ThisABI != ABI::Unknown; }
bool IsO32() const { return ThisABI == ABI::O32; }
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
index 33aab71044b09..74e7baf1db293 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsAsmBackend.cpp
@@ -619,7 +619,7 @@ MCAsmBackend *llvm::createMipsAsmBackend(const Target &T,
return new WindowsMipsAsmBackend(T, MRI, STI);
MipsABIInfo ABI = MipsABIInfo::computeTargetABI(STI.getTargetTriple(),
- STI.getCPU(), Options);
+ Options.getABIName());
return new MipsAsmBackend(T, MRI, STI.getTargetTriple(), STI.getCPU(),
ABI.IsN32());
}
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCAsmInfo.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCAsmInfo.cpp
index 8b28ee62b878c..7f3fd0445ceb7 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCAsmInfo.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCAsmInfo.cpp
@@ -24,7 +24,7 @@ MipsELFMCAsmInfo::MipsELFMCAsmInfo(const Triple &TheTriple,
const MCTargetOptions &Options) {
IsLittleEndian = TheTriple.isLittleEndian();
- MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TheTriple, "", Options);
+ MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TheTriple, Options.getABIName());
if (TheTriple.isMIPS64() && !ABI.IsN32())
CodePointerSize = CalleeSaveStackSlotSize = 8;
diff --git a/llvm/lib/Target/Mips/MipsTargetMachine.cpp b/llvm/lib/Target/Mips/MipsTargetMachine.cpp
index 8c519fa379dd8..7d4b609dc0a15 100644
--- a/llvm/lib/Target/Mips/MipsTargetMachine.cpp
+++ b/llvm/lib/Target/Mips/MipsTargetMachine.cpp
@@ -77,42 +77,6 @@ static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
return std::make_unique<MipsTargetObjectFile>();
}
-static std::string computeDataLayout(const Triple &TT, StringRef CPU,
- const TargetOptions &Options,
- bool isLittle) {
- std::string Ret;
- MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions);
-
- // There are both little and big endian mips.
- if (isLittle)
- Ret += "e";
- else
- Ret += "E";
-
- if (ABI.IsO32())
- Ret += "-m:m";
- else
- Ret += "-m:e";
-
- // Pointers are 32 bit on some ABIs.
- if (!ABI.IsN64())
- Ret += "-p:32:32";
-
- // 8 and 16 bit integers only need to have natural alignment, but try to
- // align them to 32 bits. 64 bit integers have natural alignment.
- Ret += "-i8:8:32-i16:16:32-i64:64";
-
- // 32 bit registers are always available and the stack is at least 64 bit
- // aligned. On N64 64 bit registers are also available and the stack is
- // 128 bit aligned.
- if (ABI.IsN64() || ABI.IsN32())
- Ret += "-i128:128-n32:64-S128";
- else
- Ret += "-n32-S64";
-
- return Ret;
-}
-
static Reloc::Model getEffectiveRelocModel(bool JIT,
std::optional<Reloc::Model> RM) {
if (!RM || JIT)
@@ -132,12 +96,14 @@ MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT,
bool isLittle)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT, CPU, Options, isLittle),
+ : CodeGenTargetMachineImpl(T,
+ DataLayout::computeStringForTriple(
+ TT, Options.MCOptions.getABIName()),
TT, CPU, FS, Options,
getEffectiveRelocModel(JIT, RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
isLittle(isLittle), TLOF(createTLOF(getTargetTriple())),
- ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)),
+ ABI(MipsABIInfo::computeTargetABI(TT, Options.MCOptions.getABIName())),
Subtarget(nullptr),
DefaultSubtarget(TT, CPU, FS, isLittle, *this, std::nullopt),
NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
diff --git a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
index 833f014a4c870..f4f2eb1cf1a83 100644
--- a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
@@ -59,12 +59,6 @@ static cl::opt<bool> DisableRequireStructuredCFG(
"unexpected regressions happen."),
cl::init(false), cl::Hidden);
-static cl::opt<bool> UseShortPointersOpt(
- "nvptx-short-ptr",
- cl::desc(
- "Use 32-bit pointers for accessing const/local/shared address spaces."),
- cl::init(false), cl::Hidden);
-
// byval arguments in NVPTX are special. We're only allowed to read from them
// using a special instruction, and if we ever need to write to them or take an
// address, we must make a local copy and use it, instead.
@@ -118,24 +112,6 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeNVPTXTarget() {
initializeNVPTXPrologEpilogPassPass(PR);
}
-static std::string computeDataLayout(bool is64Bit, bool UseShortPointers) {
- std::string Ret = "e";
-
- // Tensor Memory (addrspace:6) is always 32-bits.
- // Distributed Shared Memory (addrspace:7) follows shared memory
- // (addrspace:3).
- if (!is64Bit)
- Ret += "-p:32:32-p6:32:32-p7:32:32";
- else if (UseShortPointers)
- Ret += "-p3:32:32-p4:32:32-p5:32:32-p6:32:32-p7:32:32";
- else
- Ret += "-p6:32:32";
-
- Ret += "-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64";
-
- return Ret;
-}
-
NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
@@ -144,9 +120,8 @@ NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, const Triple &TT,
CodeGenOptLevel OL, bool is64bit)
// The pic relocation model is used regardless of what the client has
// specified, as it is the only relocation model currently supported.
- : CodeGenTargetMachineImpl(T,
- computeDataLayout(is64bit, UseShortPointersOpt),
- TT, CPU, FS, Options, Reloc::PIC_,
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, Reloc::PIC_,
getEffectiveCodeModel(CM, CodeModel::Small), OL),
is64bit(is64bit), TLOF(std::make_unique<NVPTXTargetObjectFile>()),
Subtarget(TT, std::string(CPU), std::string(FS), *this),
diff --git a/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp b/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
index ae92d5eab20cd..dd84905e16deb 100644
--- a/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
+++ b/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
@@ -149,58 +149,6 @@ LLVMInitializePowerPCTarget() {
initializePPCAIXAsmPrinterPass(PR);
}
-static bool isLittleEndianTriple(const Triple &T) {
- return T.getArch() == Triple::ppc64le || T.getArch() == Triple::ppcle;
-}
-
-/// Return the datalayout string of a subtarget.
-static std::string getDataLayoutString(const Triple &T) {
- bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
- std::string Ret;
-
- // Most PPC* platforms are big endian, PPC(64)LE is little endian.
- if (isLittleEndianTriple(T))
- Ret = "e";
- else
- Ret = "E";
-
- Ret += DataLayout::getManglingComponent(T);
-
- // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
- // pointers.
- if (!is64Bit || T.getOS() == Triple::Lv2)
- Ret += "-p:32:32";
-
- // If the target ABI uses function descriptors, then the alignment of function
- // pointers depends on the alignment used to emit the descriptor. Otherwise,
- // function pointers are aligned to 32 bits because the instructions must be.
- if ((T.getArch() == Triple::ppc64 && !T.isPPC64ELFv2ABI())) {
- Ret += "-Fi64";
- } else if (T.isOSAIX()) {
- Ret += is64Bit ? "-Fi64" : "-Fi32";
- } else {
- Ret += "-Fn32";
- }
-
- // Note, the alignment values for f64 and i64 on ppc64 in Darwin
- // documentation are wrong; these are correct (i.e. "what gcc does").
- Ret += "-i64:64";
-
- // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
- if (is64Bit)
- Ret += "-i128:128-n32:64";
- else
- Ret += "-n32";
-
- // Specify the vector alignment explicitly. For v256i1 and v512i1, the
- // calculated alignment would be 256*alignment(i1) and 512*alignment(i1),
- // which is 256 and 512 bytes - way over aligned.
- if (is64Bit && (T.isOSAIX() || T.isOSLinux()))
- Ret += "-S128-v256:256:256-v512:512:512";
-
- return Ret;
-}
-
static std::string computeFSAdditions(StringRef FS, CodeGenOptLevel OL,
const Triple &TT) {
std::string FullFS = std::string(FS);
@@ -348,13 +296,13 @@ PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, getDataLayoutString(TT), TT, CPU,
- computeFSAdditions(FS, OL, TT), Options,
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, computeFSAdditions(FS, OL, TT), Options,
getEffectiveRelocModel(TT, RM),
getEffectivePPCCodeModel(TT, CM, JIT), OL),
TLOF(createTLOF(getTargetTriple())),
TargetABI(computeTargetABI(TT, Options)),
- Endianness(isLittleEndianTriple(TT) ? Endian::LITTLE : Endian::BIG) {
+ Endianness(TT.isLittleEndian() ? Endian::LITTLE : Endian::BIG) {
initAsmInfo();
}
diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index 460bb33f2553a..36a1bab0de6f1 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -141,39 +141,6 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeRISCVTarget() {
initializeRISCVAsmPrinterPass(*PR);
}
-static std::string computeDataLayout(const Triple &TT,
- const TargetOptions &Opts) {
- std::string Ret;
-
- if (TT.isLittleEndian())
- Ret += "e";
- else
- Ret += "E";
-
- Ret += "-m:e";
-
- // Pointer and integer sizes.
- if (TT.isArch64Bit()) {
- Ret += "-p:64:64-i64:64-i128:128";
- Ret += "-n32:64";
- } else {
- assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported");
- Ret += "-p:32:32-i64:64";
- Ret += "-n32";
- }
-
- // Stack alignment based on ABI.
- StringRef ABI = Opts.MCOptions.getABIName();
- if (ABI == "ilp32e")
- Ret += "-S32";
- else if (ABI == "lp64e")
- Ret += "-S64";
- else
- Ret += "-S128";
-
- return Ret;
-}
-
static Reloc::Model getEffectiveRelocModel(const Triple &TT,
std::optional<Reloc::Model> RM) {
return RM.value_or(Reloc::Static);
@@ -185,8 +152,11 @@ RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT, Options), TT, CPU, FS,
- Options, getEffectiveRelocModel(TT, RM),
+ : CodeGenTargetMachineImpl(T,
+ DataLayout::computeStringForTriple(
+ TT, Options.MCOptions.getABIName()),
+ TT, CPU, FS, Options,
+ getEffectiveRelocModel(TT, RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<RISCVELFTargetObjectFile>()) {
initAsmInfo();
diff --git a/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp b/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp
index e0bfb77f4b530..3f8262a82c007 100644
--- a/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp
@@ -60,27 +60,6 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeSPIRVTarget() {
initializeSPIRVStripConvergentIntrinsicsPass(PR);
}
-static std::string computeDataLayout(const Triple &TT) {
- const auto Arch = TT.getArch();
- // TODO: this probably needs to be revisited:
- // Logical SPIR-V has no pointer size, so any fixed pointer size would be
- // wrong. The choice to default to 32 or 64 is just motivated by another
- // memory model used for graphics: PhysicalStorageBuffer64. But it shouldn't
- // mean anything.
- if (Arch == Triple::spirv32)
- return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-"
- "v256:256-v512:512-v1024:1024-n8:16:32:64-G1";
- if (Arch == Triple::spirv)
- return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
- "v512:512-v1024:1024-n8:16:32:64-G10";
- if (TT.getVendor() == Triple::VendorType::AMD &&
- TT.getOS() == Triple::OSType::AMDHSA)
- return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
- "v512:512-v1024:1024-n32:64-S32-G1-P4-A0";
- return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
- "v512:512-v1024:1024-n8:16:32:64-G1";
-}
-
static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
if (!RM)
return Reloc::PIC_;
@@ -96,8 +75,8 @@ SPIRVTargetMachine::SPIRVTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT), TT, CPU, FS, Options,
- getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<SPIRVTargetObjectFile>()),
Subtarget(TT, CPU.str(), FS.str(), *this) {
diff --git a/llvm/lib/Target/Sparc/SparcTargetMachine.cpp b/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
index 754c8f63ca4ec..a023a41c41aeb 100644
--- a/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
+++ b/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
@@ -38,39 +38,6 @@ static cl::opt<bool>
BranchRelaxation("sparc-enable-branch-relax", cl::Hidden, cl::init(true),
cl::desc("Relax out of range conditional branches"));
-static std::string computeDataLayout(const Triple &T) {
- const bool is64Bit = T.isSPARC64();
-
- // Sparc is typically big endian, but some are little.
- std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E";
- Ret += "-m:e";
-
- // Some ABIs have 32bit pointers.
- if (!is64Bit)
- Ret += "-p:32:32";
-
- // Alignments for 64 bit integers.
- Ret += "-i64:64";
-
- // Alignments for 128 bit integers.
- // This is not specified in the ABI document but is the de facto standard.
- Ret += "-i128:128";
-
- // On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
- // On SparcV9 registers can hold 64 or 32 bits, on others only 32.
- if (is64Bit)
- Ret += "-n32:64";
- else
- Ret += "-f128:64-n32";
-
- if (is64Bit)
- Ret += "-S128";
- else
- Ret += "-S64";
-
- return Ret;
-}
-
static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
return RM.value_or(Reloc::Static);
}
@@ -111,7 +78,7 @@ SparcTargetMachine::SparcTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
: CodeGenTargetMachineImpl(
- T, computeDataLayout(TT), TT, CPU, FS, Options,
+ T, DataLayout::computeStringForTriple(TT), TT, CPU, FS, Options,
getEffectiveRelocModel(RM),
getEffectiveSparcCodeModel(CM, getEffectiveRelocModel(RM),
TT.isSPARC64(), JIT),
diff --git a/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp b/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
index ece8928accd0c..84579d0b50786 100644
--- a/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
@@ -54,47 +54,6 @@ LLVMInitializeSystemZTarget() {
initializeSystemZCopyPhysRegsPass(PR);
}
-static std::string computeDataLayout(const Triple &TT) {
- std::string Ret;
-
- // Big endian.
- Ret += "E";
-
- // Data mangling.
- Ret += DataLayout::getManglingComponent(TT);
-
- // Special features for z/OS.
- if (TT.isOSzOS()) {
- if (TT.isArch64Bit()) {
- // Custom address space for ptr32.
- Ret += "-p1:32:32";
- }
- }
-
- // Make sure that global data has at least 16 bits of alignment by
- // default, so that we can refer to it using LARL. We don't have any
- // special requirements for stack variables though.
- Ret += "-i1:8:16-i8:8:16";
-
- // 64-bit integers are naturally aligned.
- Ret += "-i64:64";
-
- // 128-bit floats are aligned only to 64 bits.
- Ret += "-f128:64";
-
- // The DataLayout string always holds a vector alignment of 64 bits, see
- // comment in clang/lib/Basic/Targets/SystemZ.h.
- Ret += "-v128:64";
-
- // We prefer 16 bits of aligned for all globals; see above.
- Ret += "-a:8:16";
-
- // Integer registers are 32 or 64 bits.
- Ret += "-n32:64";
-
- return Ret;
-}
-
static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
if (TT.isOSzOS())
return std::make_unique<TargetLoweringObjectFileGOFF>();
@@ -163,7 +122,7 @@ SystemZTargetMachine::SystemZTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
: CodeGenTargetMachineImpl(
- T, computeDataLayout(TT), TT, CPU, FS, Options,
+ T, DataLayout::computeStringForTriple(TT), TT, CPU, FS, Options,
getEffectiveRelocModel(RM),
getEffectiveSystemZCodeModel(CM, getEffectiveRelocModel(RM), JIT),
OL),
diff --git a/llvm/lib/Target/VE/VETargetMachine.cpp b/llvm/lib/Target/VE/VETargetMachine.cpp
index 14b8e330d87a4..d76e06881d0e0 100644
--- a/llvm/lib/Target/VE/VETargetMachine.cpp
+++ b/llvm/lib/Target/VE/VETargetMachine.cpp
@@ -35,38 +35,6 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeVETarget() {
initializeVEDAGToDAGISelLegacyPass(PR);
}
-static std::string computeDataLayout(const Triple &T) {
- // Aurora VE is little endian
- std::string Ret = "e";
-
- // Use ELF mangling
- Ret += "-m:e";
-
- // Alignments for 64 bit integers.
- Ret += "-i64:64";
-
- // VE supports 32 bit and 64 bits integer on registers
- Ret += "-n32:64";
-
- // Stack alignment is 128 bits
- Ret += "-S128";
-
- // Vector alignments are 64 bits
- // Need to define all of them. Otherwise, each alignment becomes
- // the size of each data by default.
- Ret += "-v64:64:64"; // for v2f32
- Ret += "-v128:64:64";
- Ret += "-v256:64:64";
- Ret += "-v512:64:64";
- Ret += "-v1024:64:64";
- Ret += "-v2048:64:64";
- Ret += "-v4096:64:64";
- Ret += "-v8192:64:64";
- Ret += "-v16384:64:64"; // for v256f64
-
- return Ret;
-}
-
static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM) {
return RM.value_or(Reloc::Static);
}
@@ -91,8 +59,8 @@ VETargetMachine::VETargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT), TT, CPU, FS, Options,
- getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(createTLOF()),
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
index 6827ee6527947..874d414a9ee73 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -196,19 +196,9 @@ WebAssemblyTargetMachine::WebAssemblyTargetMachine(
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
const TargetOptions &Options, std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(
- T,
- TT.isArch64Bit()
- ? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
- "i128:128-f128:64-n32:64-S128-ni:1:10:20"
- : "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
- "i128:128-n32:64-S128-ni:1:10:20")
- : (TT.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
- "i128:128-f128:64-n32:64-S128-ni:1:10:20"
- : "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
- "i128:128-n32:64-S128-ni:1:10:20"),
- TT, CPU, FS, Options, getEffectiveRelocModel(RM, TT),
- getEffectiveCodeModel(CM, CodeModel::Large), OL),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options, getEffectiveRelocModel(RM, TT),
+ getEffectiveCodeModel(CM, CodeModel::Large), OL),
TLOF(new WebAssemblyTargetObjectFile()),
UsesMultivalueABI(Options.MCOptions.getABIName() == "experimental-mv") {
// WebAssembly type-checks instructions, but a noreturn function with a return
diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index 6d9c6cdedd9e5..268144fd23c6d 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -125,53 +125,6 @@ static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
return std::make_unique<X86ELFTargetObjectFile>();
}
-static std::string computeDataLayout(const Triple &TT) {
- // X86 is little endian
- std::string Ret = "e";
-
- Ret += DataLayout::getManglingComponent(TT);
- // X86 and x32 have 32 bit pointers.
- if (!TT.isArch64Bit() || TT.isX32())
- Ret += "-p:32:32";
-
- // Address spaces for 32 bit signed, 32 bit unsigned, and 64 bit pointers.
- Ret += "-p270:32:32-p271:32:32-p272:64:64";
-
- // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
- // 128 bit integers are not specified in the 32-bit ABIs but are used
- // internally for lowering f128, so we match the alignment to that.
- if (TT.isArch64Bit() || TT.isOSWindows())
- Ret += "-i64:64-i128:128";
- else if (TT.isOSIAMCU())
- Ret += "-i64:32-f64:32";
- else
- Ret += "-i128:128-f64:32:64";
-
- // Some ABIs align long double to 128 bits, others to 32.
- if (TT.isOSIAMCU())
- ; // No f80
- else if (TT.isArch64Bit() || TT.isOSDarwin() || TT.isWindowsMSVCEnvironment())
- Ret += "-f80:128";
- else
- Ret += "-f80:32";
-
- if (TT.isOSIAMCU())
- Ret += "-f128:32";
-
- // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
- if (TT.isArch64Bit())
- Ret += "-n8:16:32:64";
- else
- Ret += "-n8:16:32";
-
- // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
- if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
- Ret += "-a:0:32-S32";
- else
- Ret += "-S128";
-
- return Ret;
-}
static Reloc::Model getEffectiveRelocModel(const Triple &TT, bool JIT,
std::optional<Reloc::Model> RM) {
@@ -236,7 +189,8 @@ X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT), TT, CPU, FS, Options,
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ CPU, FS, Options,
getEffectiveRelocModel(TT, JIT, RM),
getEffectiveX86CodeModel(TT, CM, JIT), OL),
TLOF(createTLOF(getTargetTriple())), IsJIT(JIT) {
diff --git a/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp b/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
index c9f1ca8b46dab..1622427eda35f 100644
--- a/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
+++ b/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
@@ -32,13 +32,6 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeXtensaTarget() {
initializeXtensaAsmPrinterPass(PR);
}
-static std::string computeDataLayout(const Triple &TT, StringRef CPU,
- const TargetOptions &Options,
- bool IsLittle) {
- std::string Ret = "e-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32";
- return Ret;
-}
-
static Reloc::Model getEffectiveRelocModel(bool JIT,
std::optional<Reloc::Model> RM) {
if (!RM || JIT)
@@ -53,7 +46,7 @@ XtensaTargetMachine::XtensaTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT,
bool IsLittle)
- : CodeGenTargetMachineImpl(T, computeDataLayout(TT, CPU, Options, IsLittle),
+ : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT),
TT, CPU, FS, Options,
getEffectiveRelocModel(JIT, RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
>From 6b58c6fb560788a823f142e497690cb62ad253c5 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Tue, 9 Sep 2025 02:47:12 +0000
Subject: [PATCH 2/7] fix buglet
---
llvm/lib/Target/ARM/ARMTargetMachine.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Target/ARM/ARMTargetMachine.cpp b/llvm/lib/Target/ARM/ARMTargetMachine.cpp
index f98e0d648e46d..7f27d5a2dd005 100644
--- a/llvm/lib/Target/ARM/ARMTargetMachine.cpp
+++ b/llvm/lib/Target/ARM/ARMTargetMachine.cpp
@@ -153,7 +153,7 @@ ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
getEffectiveRelocModel(TT, RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TargetABI(ARM::computeTargetABI(TT, Options.MCOptions.ABIName)),
- TLOF(createTLOF(getTargetTriple())), isLittle(isLittle) {
+ TLOF(createTLOF(getTargetTriple())), isLittle(TT.isLittleEndian()) {
// Default to triple-appropriate float ABI
if (Options.FloatABIType == FloatABI::Default) {
>From 24f49dc7d02154720853bf46601e1cb413bd3b7a Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Mon, 8 Sep 2025 21:11:40 -0700
Subject: [PATCH 3/7] Move the data layout computation to TargetParser so clang
can use it
---
llvm/benchmarks/RuntimeLibcalls.cpp | 5 +-
llvm/include/llvm/IR/DataLayout.h | 8 -
llvm/include/llvm/TargetParser/Triple.h | 4 +
llvm/lib/IR/DataLayout.cpp | 633 ------------------
.../Target/AArch64/AArch64TargetMachine.cpp | 2 +-
.../lib/Target/AMDGPU/AMDGPUTargetMachine.cpp | 2 +-
llvm/lib/Target/ARC/ARCTargetMachine.cpp | 4 +-
llvm/lib/Target/ARM/ARMTargetMachine.cpp | 10 +-
llvm/lib/Target/AVR/AVRTargetMachine.cpp | 4 +-
llvm/lib/Target/BPF/BPFTargetMachine.cpp | 4 +-
llvm/lib/Target/CSKY/CSKYTargetMachine.cpp | 4 +-
.../Target/DirectX/DirectXTargetMachine.cpp | 5 +-
.../Target/Hexagon/HexagonTargetMachine.cpp | 4 +-
llvm/lib/Target/Lanai/LanaiTargetMachine.cpp | 4 +-
.../LoongArch/LoongArchTargetMachine.cpp | 2 +-
llvm/lib/Target/M68k/M68kTargetMachine.cpp | 4 +-
.../lib/Target/MSP430/MSP430TargetMachine.cpp | 4 +-
llvm/lib/Target/Mips/MipsTargetMachine.cpp | 10 +-
llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp | 13 +-
llvm/lib/Target/PowerPC/PPCTargetMachine.cpp | 4 +-
llvm/lib/Target/RISCV/RISCVTargetMachine.cpp | 10 +-
llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp | 4 +-
llvm/lib/Target/Sparc/SparcTargetMachine.cpp | 2 +-
.../Target/SystemZ/SystemZTargetMachine.cpp | 2 +-
llvm/lib/Target/VE/VETargetMachine.cpp | 4 +-
.../WebAssembly/WebAssemblyTargetMachine.cpp | 4 +-
llvm/lib/Target/X86/X86TargetMachine.cpp | 3 +-
.../lib/Target/Xtensa/XtensaTargetMachine.cpp | 3 +-
llvm/lib/TargetParser/CMakeLists.txt | 1 +
llvm/lib/TargetParser/TargetDataLayout.cpp | 631 +++++++++++++++++
llvm/unittests/IR/DataLayoutTest.cpp | 7 -
llvm/unittests/TargetParser/TripleTest.cpp | 9 +
32 files changed, 701 insertions(+), 709 deletions(-)
create mode 100644 llvm/lib/TargetParser/TargetDataLayout.cpp
diff --git a/llvm/benchmarks/RuntimeLibcalls.cpp b/llvm/benchmarks/RuntimeLibcalls.cpp
index 9ac77bb74a3df..707bdca7ceab7 100644
--- a/llvm/benchmarks/RuntimeLibcalls.cpp
+++ b/llvm/benchmarks/RuntimeLibcalls.cpp
@@ -54,10 +54,7 @@ static std::vector<std::string> readSymbolsFromFile(StringRef InputFile) {
// Hackily figure out if there's a prefix on the symbol names - llvm-nm
// appears to not have a flag to skip this.
llvm::Triple HostTriple(LLVM_HOST_TRIPLE);
- std::string DummyDatalayout = "e";
- DummyDatalayout += DataLayout::getManglingComponent(HostTriple);
-
- DataLayout DL(DummyDatalayout);
+ DataLayout DL(HostTriple.computeDataLayout());
char GlobalPrefix = DL.getGlobalPrefix();
std::vector<std::string> Lines;
diff --git a/llvm/include/llvm/IR/DataLayout.h b/llvm/include/llvm/IR/DataLayout.h
index 9439ddcbb8cb2..5653ee7b6837d 100644
--- a/llvm/include/llvm/IR/DataLayout.h
+++ b/llvm/include/llvm/IR/DataLayout.h
@@ -194,12 +194,6 @@ class DataLayout {
/// description on failure.
LLVM_ABI static Expected<DataLayout> parse(StringRef LayoutString);
- /// Given an LLVM target triple, compute the well-known datalayout string.
- /// TODO: Create a static factor helper version of this method and refactor
- /// the TargetMachine callers to use it.
- LLVM_ABI static std::string computeStringForTriple(const Triple &T,
- StringRef ABIName = "");
-
/// Layout endianness...
bool isLittleEndian() const { return !BigEndian; }
bool isBigEndian() const { return BigEndian; }
@@ -309,8 +303,6 @@ class DataLayout {
llvm_unreachable("invalid mangling mode");
}
- LLVM_ABI static const char *getManglingComponent(const Triple &T);
-
/// Returns true if the specified type fits in a native integer type
/// supported by the CPU.
///
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index 8e12c6852075d..5a462ceafb639 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -1327,6 +1327,10 @@ class Triple {
const VersionTuple &Version);
LLVM_ABI ExceptionHandling getDefaultExceptionHandling() const;
+
+ /// Compute the LLVM IR data layout string based on the triple. Some targets
+ /// customize the layout based on the ABIName string.
+ LLVM_ABI std::string computeDataLayout(StringRef ABIName = "") const;
};
} // End llvm namespace
diff --git a/llvm/lib/IR/DataLayout.cpp b/llvm/lib/IR/DataLayout.cpp
index aef1d989e2889..77f9b997a2ebf 100644
--- a/llvm/lib/IR/DataLayout.cpp
+++ b/llvm/lib/IR/DataLayout.cpp
@@ -26,14 +26,12 @@
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/Casting.h"
-#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemAlloc.h"
#include "llvm/Support/TypeSize.h"
#include "llvm/TargetParser/Triple.h"
-#include "llvm/TargetParser/ARMTargetParser.h"
#include <algorithm>
#include <cassert>
#include <cstdint>
@@ -174,637 +172,6 @@ struct LessPointerAddrSpace {
};
} // namespace
-static std::string computeARMDataLayout(const Triple &TT, StringRef ABIName) {
- auto ABI = ARM::computeTargetABI(TT, ABIName);
- std::string Ret;
-
- if (TT.isLittleEndian())
- // Little endian.
- Ret += "e";
- else
- // Big endian.
- Ret += "E";
-
- Ret += DataLayout::getManglingComponent(TT);
-
- // Pointers are 32 bits and aligned to 32 bits.
- Ret += "-p:32:32";
-
- // Function pointers are aligned to 8 bits (because the LSB stores the
- // ARM/Thumb state).
- Ret += "-Fi8";
-
- // ABIs other than APCS have 64 bit integers with natural alignment.
- if (ABI != ARM::ARM_ABI_APCS)
- Ret += "-i64:64";
-
- // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
- // bits, others to 64 bits. We always try to align to 64 bits.
- if (ABI == ARM::ARM_ABI_APCS)
- Ret += "-f64:32:64";
-
- // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
- // to 64. We always ty to give them natural alignment.
- if (ABI == ARM::ARM_ABI_APCS)
- Ret += "-v64:32:64-v128:32:128";
- else if (ABI != ARM::ARM_ABI_AAPCS16)
- Ret += "-v128:64:128";
-
- // Try to align aggregates to 32 bits (the default is 64 bits, which has no
- // particular hardware support on 32-bit ARM).
- Ret += "-a:0:32";
-
- // Integer registers are 32 bits.
- Ret += "-n32";
-
- // The stack is 64 bit aligned on AAPCS and 32 bit aligned everywhere else.
- if (ABI == ARM::ARM_ABI_AAPCS16)
- Ret += "-S128";
- else if (ABI == ARM::ARM_ABI_AAPCS)
- Ret += "-S64";
- else
- Ret += "-S32";
-
- return Ret;
-}
-
-// Helper function to build a DataLayout string
-static std::string computeAArch64DataLayout(const Triple &TT) {
- if (TT.isOSBinFormatMachO()) {
- if (TT.getArch() == Triple::aarch64_32)
- return "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-"
- "n32:64-S128-Fn32";
- return "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-"
- "Fn32";
- }
- if (TT.isOSBinFormatCOFF())
- return "e-m:w-p270:32:32-p271:32:32-p272:64:64-p:64:64-i32:32-i64:64-i128:"
- "128-n32:64-S128-Fn32";
- std::string Endian = TT.isLittleEndian() ? "e" : "E";
- std::string Ptr32 = TT.getEnvironment() == Triple::GNUILP32 ? "-p:32:32" : "";
- return Endian + "-m:e" + Ptr32 +
- "-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-"
- "n32:64-S128-Fn32";
-}
-
-// DataLayout: little or big endian
-static std::string computeBPFDataLayout(const Triple &TT) {
- if (TT.getArch() == Triple::bpfeb)
- return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
- else
- return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
-}
-
-static std::string computeCSKYDataLayout(const Triple &TT) {
- std::string Ret;
-
- // Only support little endian for now.
- // TODO: Add support for big endian.
- Ret += "e";
-
- // CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
- // It's a 4-byte aligned stack with ELF mangling only.
- Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
- "-v128:32:32-a:0:32-Fi32-n32";
-
- return Ret;
-}
-
-static std::string computeLoongArchDataLayout(const Triple &TT) {
- if (TT.isArch64Bit())
- return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
- assert(TT.isArch32Bit() && "only LA32 and LA64 are currently supported");
- return "e-m:e-p:32:32-i64:64-n32-S128";
-}
-
-static std::string computeM68kDataLayout(const Triple &TT) {
- std::string Ret = "";
- // M68k is Big Endian
- Ret += "E";
-
- // FIXME how to wire it with the used object format?
- Ret += "-m:e";
-
- // M68k pointers are always 32 bit wide even for 16-bit CPUs.
- // The ABI only specifies 16-bit alignment.
- // On at least the 68020+ with a 32-bit bus, there is a performance benefit
- // to having 32-bit alignment.
- Ret += "-p:32:16:32";
-
- // Bytes do not require special alignment, words are word aligned and
- // long words are word aligned at minimum.
- Ret += "-i8:8:8-i16:16:16-i32:16:32";
-
- // FIXME no floats at the moment
-
- // The registers can hold 8, 16, 32 bits
- Ret += "-n8:16:32";
-
- Ret += "-a:0:16-S16";
-
- return Ret;
-}
-
-namespace {
-enum class MipsABI {
- Unknown,
- O32,
- N32,
- N64
-};
-}
-
-// FIXME: This duplicates MipsABIInfo::computeTargetABI, but duplicating this is
-// preferable to violating layering rules. Ideally that information should live
-// in LLVM TargetParser, but for now we just duplicate some ABI name string
-// logic for simplicity.
-static MipsABI getMipsABI(const Triple &TT, StringRef ABIName) {
- if (ABIName.starts_with("o32"))
- return MipsABI::O32;
- if (ABIName.starts_with("n32"))
- return MipsABI::N32;
- if (ABIName.starts_with("n64"))
- return MipsABI::N64;
- if (TT.isABIN32())
- return MipsABI::N32;
- assert(ABIName.empty() && "Unknown ABI option for MIPS");
-
- if (TT.isMIPS64())
- return MipsABI::N64;
- return MipsABI::O32;
-}
-
-static std::string computeMipsDataLayout(const Triple &TT, StringRef ABIName) {
- std::string Ret;
- MipsABI ABI = getMipsABI(TT, ABIName);
-
- // There are both little and big endian mips.
- if (TT.isLittleEndian())
- Ret += "e";
- else
- Ret += "E";
-
- if (ABI == MipsABI::O32)
- Ret += "-m:m";
- else
- Ret += "-m:e";
-
- // Pointers are 32 bit on some ABIs.
- if (ABI != MipsABI::N64)
- Ret += "-p:32:32";
-
- // 8 and 16 bit integers only need to have natural alignment, but try to
- // align them to 32 bits. 64 bit integers have natural alignment.
- Ret += "-i8:8:32-i16:16:32-i64:64";
-
- // 32 bit registers are always available and the stack is at least 64 bit
- // aligned. On N64 64 bit registers are also available and the stack is
- // 128 bit aligned.
- if (ABI == MipsABI::N64 || ABI == MipsABI::N32)
- Ret += "-i128:128-n32:64-S128";
- else
- Ret += "-n32-S64";
-
- return Ret;
-}
-
-static std::string computePowerDataLayout(const Triple &T) {
- bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
- std::string Ret;
-
- // Most PPC* platforms are big endian, PPC(64)LE is little endian.
- if (T.isLittleEndian())
- Ret = "e";
- else
- Ret = "E";
-
- Ret += DataLayout::getManglingComponent(T);
-
- // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
- // pointers.
- if (!is64Bit || T.getOS() == Triple::Lv2)
- Ret += "-p:32:32";
-
- // If the target ABI uses function descriptors, then the alignment of function
- // pointers depends on the alignment used to emit the descriptor. Otherwise,
- // function pointers are aligned to 32 bits because the instructions must be.
- if ((T.getArch() == Triple::ppc64 && !T.isPPC64ELFv2ABI())) {
- Ret += "-Fi64";
- } else if (T.isOSAIX()) {
- Ret += is64Bit ? "-Fi64" : "-Fi32";
- } else {
- Ret += "-Fn32";
- }
-
- // Note, the alignment values for f64 and i64 on ppc64 in Darwin
- // documentation are wrong; these are correct (i.e. "what gcc does").
- Ret += "-i64:64";
-
- // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
- if (is64Bit)
- Ret += "-i128:128-n32:64";
- else
- Ret += "-n32";
-
- // Specify the vector alignment explicitly. For v256i1 and v512i1, the
- // calculated alignment would be 256*alignment(i1) and 512*alignment(i1),
- // which is 256 and 512 bytes - way over aligned.
- if (is64Bit && (T.isOSAIX() || T.isOSLinux()))
- Ret += "-S128-v256:256:256-v512:512:512";
-
- return Ret;
-}
-
-static std::string computeAMDDataLayout(const Triple &TT) {
- if (TT.getArch() == Triple::r600) {
- // 32-bit pointers.
- return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128"
- "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1";
- }
-
- // 32-bit private, local, and region pointers. 64-bit global, constant and
- // flat. 160-bit non-integral fat buffer pointers that include a 128-bit
- // buffer descriptor and a 32-bit offset, which are indexed by 32-bit values
- // (address space 7), and 128-bit non-integral buffer resourcees (address
- // space 8) which cannot be non-trivilally accessed by LLVM memory operations
- // like getelementptr.
- return "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32"
- "-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-i64:64-"
- "v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-"
- "v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9";
-}
-
-static std::string computeRISCVDataLayout(const Triple &TT, StringRef ABIName) {
- std::string Ret;
-
- if (TT.isLittleEndian())
- Ret += "e";
- else
- Ret += "E";
-
- Ret += "-m:e";
-
- // Pointer and integer sizes.
- if (TT.isArch64Bit()) {
- Ret += "-p:64:64-i64:64-i128:128";
- Ret += "-n32:64";
- } else {
- assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported");
- Ret += "-p:32:32-i64:64";
- Ret += "-n32";
- }
-
- // Stack alignment based on ABI.
- StringRef ABI = ABIName;
- if (ABI == "ilp32e")
- Ret += "-S32";
- else if (ABI == "lp64e")
- Ret += "-S64";
- else
- Ret += "-S128";
-
- return Ret;
-}
-
-static std::string computeSparcDataLayout(const Triple &T) {
- const bool is64Bit = T.isSPARC64();
-
- // Sparc is typically big endian, but some are little.
- std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E";
- Ret += "-m:e";
-
- // Some ABIs have 32bit pointers.
- if (!is64Bit)
- Ret += "-p:32:32";
-
- // Alignments for 64 bit integers.
- Ret += "-i64:64";
-
- // Alignments for 128 bit integers.
- // This is not specified in the ABI document but is the de facto standard.
- Ret += "-i128:128";
-
- // On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
- // On SparcV9 registers can hold 64 or 32 bits, on others only 32.
- if (is64Bit)
- Ret += "-n32:64";
- else
- Ret += "-f128:64-n32";
-
- if (is64Bit)
- Ret += "-S128";
- else
- Ret += "-S64";
-
- return Ret;
-}
-
-static std::string computeSystemZDataLayout(const Triple &TT) {
- std::string Ret;
-
- // Big endian.
- Ret += "E";
-
- // Data mangling.
- Ret += DataLayout::getManglingComponent(TT);
-
- // Special features for z/OS.
- if (TT.isOSzOS()) {
- if (TT.isArch64Bit()) {
- // Custom address space for ptr32.
- Ret += "-p1:32:32";
- }
- }
-
- // Make sure that global data has at least 16 bits of alignment by
- // default, so that we can refer to it using LARL. We don't have any
- // special requirements for stack variables though.
- Ret += "-i1:8:16-i8:8:16";
-
- // 64-bit integers are naturally aligned.
- Ret += "-i64:64";
-
- // 128-bit floats are aligned only to 64 bits.
- Ret += "-f128:64";
-
- // The DataLayout string always holds a vector alignment of 64 bits, see
- // comment in clang/lib/Basic/Targets/SystemZ.h.
- Ret += "-v128:64";
-
- // We prefer 16 bits of aligned for all globals; see above.
- Ret += "-a:8:16";
-
- // Integer registers are 32 or 64 bits.
- Ret += "-n32:64";
-
- return Ret;
-}
-
-static std::string computeX86DataLayout(const Triple &TT) {
- // X86 is little endian
- std::string Ret = "e";
-
- Ret += DataLayout::getManglingComponent(TT);
- // X86 and x32 have 32 bit pointers.
- if (!TT.isArch64Bit() || TT.isX32())
- Ret += "-p:32:32";
-
- // Address spaces for 32 bit signed, 32 bit unsigned, and 64 bit pointers.
- Ret += "-p270:32:32-p271:32:32-p272:64:64";
-
- // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
- // 128 bit integers are not specified in the 32-bit ABIs but are used
- // internally for lowering f128, so we match the alignment to that.
- if (TT.isArch64Bit() || TT.isOSWindows())
- Ret += "-i64:64-i128:128";
- else if (TT.isOSIAMCU())
- Ret += "-i64:32-f64:32";
- else
- Ret += "-i128:128-f64:32:64";
-
- // Some ABIs align long double to 128 bits, others to 32.
- if (TT.isOSIAMCU())
- ; // No f80
- else if (TT.isArch64Bit() || TT.isOSDarwin() || TT.isWindowsMSVCEnvironment())
- Ret += "-f80:128";
- else
- Ret += "-f80:32";
-
- if (TT.isOSIAMCU())
- Ret += "-f128:32";
-
- // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
- if (TT.isArch64Bit())
- Ret += "-n8:16:32:64";
- else
- Ret += "-n8:16:32";
-
- // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
- if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
- Ret += "-a:0:32-S32";
- else
- Ret += "-S128";
-
- return Ret;
-}
-
-
-static cl::opt<bool> NVPTXUseShortPointers(
- "nvptx-short-ptr",
- cl::desc(
- "Use 32-bit pointers for accessing const/local/shared address spaces."),
- cl::init(false), cl::Hidden);
-
-static std::string computeNVPTXDataLayout(const Triple &T) {
- std::string Ret = "e";
-
- // Tensor Memory (addrspace:6) is always 32-bits.
- // Distributed Shared Memory (addrspace:7) follows shared memory
- // (addrspace:3).
- if (!T.isArch64Bit())
- Ret += "-p:32:32-p6:32:32-p7:32:32";
- else if (NVPTXUseShortPointers)
- Ret += "-p3:32:32-p4:32:32-p5:32:32-p6:32:32-p7:32:32";
- else
- Ret += "-p6:32:32";
-
- Ret += "-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64";
-
- return Ret;
-}
-
-static std::string computeSPIRVDataLayout(const Triple &TT) {
- const auto Arch = TT.getArch();
- // TODO: this probably needs to be revisited:
- // Logical SPIR-V has no pointer size, so any fixed pointer size would be
- // wrong. The choice to default to 32 or 64 is just motivated by another
- // memory model used for graphics: PhysicalStorageBuffer64. But it shouldn't
- // mean anything.
- if (Arch == Triple::spirv32)
- return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-"
- "v256:256-v512:512-v1024:1024-n8:16:32:64-G1";
- if (Arch == Triple::spirv)
- return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
- "v512:512-v1024:1024-n8:16:32:64-G10";
- if (TT.getVendor() == Triple::VendorType::AMD &&
- TT.getOS() == Triple::OSType::AMDHSA)
- return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
- "v512:512-v1024:1024-n32:64-S32-G1-P4-A0";
- return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
- "v512:512-v1024:1024-n8:16:32:64-G1";
-}
-
-static std::string computeLanaiDataLayout() {
- // Data layout (keep in sync with clang/lib/Basic/Targets.cpp)
- return "E" // Big endian
- "-m:e" // ELF name manging
- "-p:32:32" // 32-bit pointers, 32 bit aligned
- "-i64:64" // 64 bit integers, 64 bit aligned
- "-a:0:32" // 32 bit alignment of objects of aggregate type
- "-n32" // 32 bit native integer width
- "-S64"; // 64 bit natural stack alignment
-}
-
-static std::string computeWebAssemblyDataLayout(const Triple &TT) {
- return TT.isArch64Bit()
- ? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
- "i128:128-f128:64-n32:64-S128-ni:1:10:20"
- : "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
- "i128:128-n32:64-S128-ni:1:10:20")
- : (TT.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
- "i128:128-f128:64-n32:64-S128-ni:1:10:20"
- : "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
- "i128:128-n32:64-S128-ni:1:10:20");
-}
-
-static std::string computeVEDataLayout(const Triple &T) {
- // Aurora VE is little endian
- std::string Ret = "e";
-
- // Use ELF mangling
- Ret += "-m:e";
-
- // Alignments for 64 bit integers.
- Ret += "-i64:64";
-
- // VE supports 32 bit and 64 bits integer on registers
- Ret += "-n32:64";
-
- // Stack alignment is 128 bits
- Ret += "-S128";
-
- // Vector alignments are 64 bits
- // Need to define all of them. Otherwise, each alignment becomes
- // the size of each data by default.
- Ret += "-v64:64:64"; // for v2f32
- Ret += "-v128:64:64";
- Ret += "-v256:64:64";
- Ret += "-v512:64:64";
- Ret += "-v1024:64:64";
- Ret += "-v2048:64:64";
- Ret += "-v4096:64:64";
- Ret += "-v8192:64:64";
- Ret += "-v16384:64:64"; // for v256f64
-
- return Ret;
-}
-
-// static
-std::string DataLayout::computeStringForTriple(const Triple &T,
- StringRef ABIName) {
- switch (T.getArch()) {
- case Triple::arm:
- case Triple::armeb:
- case Triple::thumb:
- case Triple::thumbeb:
- return computeARMDataLayout(T, ABIName);
- case Triple::aarch64:
- case Triple::aarch64_be:
- case Triple::aarch64_32:
- return computeAArch64DataLayout(T);
- case Triple::arc:
- return "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
- "f32:32:32-i64:32-f64:32-a:0:32-n32";
- case Triple::avr:
- return "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8:16-a:8";
- case Triple::bpfel:
- case Triple::bpfeb:
- return computeBPFDataLayout(T);
- case Triple::csky:
- return computeCSKYDataLayout(T);
- case Triple::dxil:
- return "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-"
- "f32:32-f64:64-n8:16:32:64";
- case Triple::hexagon:
- return "e-m:e-p:32:32:32-a:0-n16:32-"
- "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
- "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048";
- case Triple::loongarch32:
- case Triple::loongarch64:
- return computeLoongArchDataLayout(T);
- case Triple::m68k:
- return computeM68kDataLayout(T);
- case Triple::mips:
- case Triple::mipsel:
- case Triple::mips64:
- case Triple::mips64el:
- return computeMipsDataLayout(T, ABIName);
- case Triple::msp430:
- return "e-m:e-p:16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S16";
- case Triple::ppc:
- case Triple::ppcle:
- case Triple::ppc64:
- case Triple::ppc64le:
- return computePowerDataLayout(T);
- case Triple::r600:
- case Triple::amdgcn:
- return computeAMDDataLayout(T);
- case Triple::riscv32:
- case Triple::riscv64:
- case Triple::riscv32be:
- case Triple::riscv64be:
- return computeRISCVDataLayout(T, ABIName);
- case Triple::sparc:
- case Triple::sparcv9:
- case Triple::sparcel:
- return computeSparcDataLayout(T);
- case Triple::systemz:
- return computeSystemZDataLayout(T);
- case Triple::tce:
- case Triple::tcele:
- case Triple::x86:
- case Triple::x86_64:
- return computeX86DataLayout(T);
- case Triple::xcore:
- case Triple::xtensa:
- return "e-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32";
- case Triple::nvptx:
- case Triple::nvptx64:
- return computeNVPTXDataLayout(T);
- case Triple::spir:
- case Triple::spir64:
- case Triple::spirv:
- case Triple::spirv32:
- case Triple::spirv64:
- return computeSPIRVDataLayout(T);
- case Triple::lanai:
- return computeLanaiDataLayout();
- case Triple::wasm32:
- case Triple::wasm64:
- return computeWebAssemblyDataLayout(T);
- case Triple::ve:
- return computeVEDataLayout(T);
-
- case Triple::amdil:
- case Triple::amdil64:
- case Triple::hsail:
- case Triple::hsail64:
- case Triple::kalimba:
- case Triple::shave:
- case Triple::renderscript32:
- case Triple::renderscript64:
- // These are all virtual ISAs with no LLVM backend, and therefore no fixed
- // LLVM data layout.
- return "";
-
- case Triple::UnknownArch:
- return "";
- }
- return "";
-}
-
-const char *DataLayout::getManglingComponent(const Triple &T) {
- if (T.isOSBinFormatGOFF())
- return "-m:l";
- if (T.isOSBinFormatMachO())
- return "-m:o";
- if ((T.isOSWindows() || T.isUEFI()) && T.isOSBinFormatCOFF())
- return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
- if (T.isOSBinFormatXCOFF())
- return "-m:a";
- return "-m:e";
-}
-
// Default primitive type specifications.
// NOTE: These arrays must be sorted by type bit width.
constexpr DataLayout::PrimitiveSpec DefaultIntSpecs[] = {
diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
index e8f48d3f65941..dde1d88403bfe 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
@@ -347,7 +347,7 @@ AArch64TargetMachine::AArch64TargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT,
bool LittleEndian)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT,
computeDefaultCPU(TT, CPU), FS, Options,
getEffectiveRelocModel(TT, RM),
getEffectiveAArch64CodeModel(TT, CM, JIT), OL),
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
index afe8bbf88e8fb..92a587b5771b6 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
@@ -745,7 +745,7 @@ AMDGPUTargetMachine::AMDGPUTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OptLevel)
: CodeGenTargetMachineImpl(
- T, DataLayout::computeStringForTriple(TT), TT, getGPUOrDefault(TT, CPU), FS, Options,
+ T, TT.computeDataLayout(), TT, getGPUOrDefault(TT, CPU), FS, Options,
getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OptLevel),
TLOF(createTLOF(getTargetTriple())) {
diff --git a/llvm/lib/Target/ARC/ARCTargetMachine.cpp b/llvm/lib/Target/ARC/ARCTargetMachine.cpp
index 7a555f76d57f0..8e1944062a2c3 100644
--- a/llvm/lib/Target/ARC/ARCTargetMachine.cpp
+++ b/llvm/lib/Target/ARC/ARCTargetMachine.cpp
@@ -33,8 +33,8 @@ ARCTargetMachine::ARCTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, getRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ getRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
diff --git a/llvm/lib/Target/ARM/ARMTargetMachine.cpp b/llvm/lib/Target/ARM/ARMTargetMachine.cpp
index 7f27d5a2dd005..346776e0c4b25 100644
--- a/llvm/lib/Target/ARM/ARMTargetMachine.cpp
+++ b/llvm/lib/Target/ARM/ARMTargetMachine.cpp
@@ -146,12 +146,10 @@ ARMBaseTargetMachine::ARMBaseTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL)
- : CodeGenTargetMachineImpl(T,
- DataLayout::computeStringForTriple(
- TT, Options.MCOptions.ABIName),
- TT, CPU, FS, Options,
- getEffectiveRelocModel(TT, RM),
- getEffectiveCodeModel(CM, CodeModel::Small), OL),
+ : CodeGenTargetMachineImpl(
+ T, TT.computeDataLayout(Options.MCOptions.ABIName), TT, CPU, FS,
+ Options, getEffectiveRelocModel(TT, RM),
+ getEffectiveCodeModel(CM, CodeModel::Small), OL),
TargetABI(ARM::computeTargetABI(TT, Options.MCOptions.ABIName)),
TLOF(createTLOF(getTargetTriple())), isLittle(TT.isLittleEndian()) {
diff --git a/llvm/lib/Target/AVR/AVRTargetMachine.cpp b/llvm/lib/Target/AVR/AVRTargetMachine.cpp
index 2367478d7c117..f001d7974669a 100644
--- a/llvm/lib/Target/AVR/AVRTargetMachine.cpp
+++ b/llvm/lib/Target/AVR/AVRTargetMachine.cpp
@@ -47,8 +47,8 @@ AVRTargetMachine::AVRTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT, getCPU(CPU), FS, Options,
- getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, getCPU(CPU), FS,
+ Options, getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
SubTarget(TT, std::string(getCPU(CPU)), std::string(FS), *this) {
this->TLOF = std::make_unique<AVRTargetObjectFile>();
diff --git a/llvm/lib/Target/BPF/BPFTargetMachine.cpp b/llvm/lib/Target/BPF/BPFTargetMachine.cpp
index 611a5f47280fd..10b758647c735 100644
--- a/llvm/lib/Target/BPF/BPFTargetMachine.cpp
+++ b/llvm/lib/Target/BPF/BPFTargetMachine.cpp
@@ -69,8 +69,8 @@ BPFTargetMachine::BPFTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
diff --git a/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp b/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
index 03ccce9bb2c8e..d0058b9af14be 100644
--- a/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
+++ b/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
@@ -39,8 +39,8 @@ CSKYTargetMachine::CSKYTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, RM.value_or(Reloc::Static),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ RM.value_or(Reloc::Static),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<CSKYELFTargetObjectFile>()) {
initAsmInfo();
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index d281eeb640a1b..bcf84403b2c0d 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -134,9 +134,8 @@ DirectXTargetMachine::DirectXTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, Reloc::Static,
- CodeModel::Small, OL),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ Reloc::Static, CodeModel::Small, OL),
TLOF(std::make_unique<DXILTargetObjectFile>()),
Subtarget(std::make_unique<DirectXSubtarget>(TT, CPU, FS, *this)) {
initAsmInfo();
diff --git a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp
index a0cafd0187ac1..0afa04ab57e81 100644
--- a/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonTargetMachine.cpp
@@ -231,8 +231,8 @@ HexagonTargetMachine::HexagonTargetMachine(const Target &T, const Triple &TT,
// Specify the vector alignment explicitly. For v512x1, the calculated
// alignment would be 512*alignment(i1), which is 512 bytes, instead of
// the required minimum of 64 bytes.
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small),
(HexagonNoOpt ? CodeGenOptLevel::None : OL)),
TLOF(std::make_unique<HexagonTargetObjectFile>()),
diff --git a/llvm/lib/Target/Lanai/LanaiTargetMachine.cpp b/llvm/lib/Target/Lanai/LanaiTargetMachine.cpp
index f76ce255bdb65..df56f9ae39fe2 100644
--- a/llvm/lib/Target/Lanai/LanaiTargetMachine.cpp
+++ b/llvm/lib/Target/Lanai/LanaiTargetMachine.cpp
@@ -47,8 +47,8 @@ LanaiTargetMachine::LanaiTargetMachine(
std::optional<CodeModel::Model> CodeModel, CodeGenOptLevel OptLevel,
bool JIT)
: CodeGenTargetMachineImpl(
- T, DataLayout::computeStringForTriple(TT), TT, Cpu, FeatureString,
- Options, getEffectiveRelocModel(RM),
+ T, TT.computeDataLayout(), TT, Cpu, FeatureString, Options,
+ getEffectiveRelocModel(RM),
getEffectiveCodeModel(CodeModel, CodeModel::Medium), OptLevel),
Subtarget(TT, Cpu, FeatureString, *this, Options, getCodeModel(),
OptLevel),
diff --git a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
index c0e7c49e8221a..d0a8ababe8e58 100644
--- a/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchTargetMachine.cpp
@@ -86,7 +86,7 @@ LoongArchTargetMachine::LoongArchTargetMachine(
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
const TargetOptions &Options, std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT, CPU, FS, Options,
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
getEffectiveRelocModel(TT, RM),
getEffectiveLoongArchCodeModel(TT, CM), OL),
TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {
diff --git a/llvm/lib/Target/M68k/M68kTargetMachine.cpp b/llvm/lib/Target/M68k/M68kTargetMachine.cpp
index 53daa7c4d4950..847c27bac2cba 100644
--- a/llvm/lib/Target/M68k/M68kTargetMachine.cpp
+++ b/llvm/lib/Target/M68k/M68kTargetMachine.cpp
@@ -72,8 +72,8 @@ M68kTargetMachine::M68kTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, getEffectiveRelocModel(TT, RM),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ getEffectiveRelocModel(TT, RM),
::getEffectiveCodeModel(CM, JIT), OL),
TLOF(std::make_unique<M68kELFTargetObjectFile>()),
Subtarget(TT, CPU, FS, *this) {
diff --git a/llvm/lib/Target/MSP430/MSP430TargetMachine.cpp b/llvm/lib/Target/MSP430/MSP430TargetMachine.cpp
index fbffbb2e6f875..988bcae120f9f 100644
--- a/llvm/lib/Target/MSP430/MSP430TargetMachine.cpp
+++ b/llvm/lib/Target/MSP430/MSP430TargetMachine.cpp
@@ -40,8 +40,8 @@ MSP430TargetMachine::MSP430TargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<TargetLoweringObjectFileELF>()),
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
diff --git a/llvm/lib/Target/Mips/MipsTargetMachine.cpp b/llvm/lib/Target/Mips/MipsTargetMachine.cpp
index 7d4b609dc0a15..03bedc5b15c4f 100644
--- a/llvm/lib/Target/Mips/MipsTargetMachine.cpp
+++ b/llvm/lib/Target/Mips/MipsTargetMachine.cpp
@@ -96,12 +96,10 @@ MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT,
bool isLittle)
- : CodeGenTargetMachineImpl(T,
- DataLayout::computeStringForTriple(
- TT, Options.MCOptions.getABIName()),
- TT, CPU, FS, Options,
- getEffectiveRelocModel(JIT, RM),
- getEffectiveCodeModel(CM, CodeModel::Small), OL),
+ : CodeGenTargetMachineImpl(
+ T, TT.computeDataLayout(Options.MCOptions.getABIName()), TT, CPU, FS,
+ Options, getEffectiveRelocModel(JIT, RM),
+ getEffectiveCodeModel(CM, CodeModel::Small), OL),
isLittle(isLittle), TLOF(createTLOF(getTargetTriple())),
ABI(MipsABIInfo::computeTargetABI(TT, Options.MCOptions.getABIName())),
Subtarget(nullptr),
diff --git a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
index f4f2eb1cf1a83..4343388089bd9 100644
--- a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
@@ -112,6 +112,12 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeNVPTXTarget() {
initializeNVPTXPrologEpilogPassPass(PR);
}
+static cl::opt<bool> NVPTXUseShortPointers(
+ "nvptx-short-ptr",
+ cl::desc(
+ "Use 32-bit pointers for accessing const/local/shared address spaces."),
+ cl::init(false), cl::Hidden);
+
NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
@@ -120,9 +126,10 @@ NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, const Triple &TT,
CodeGenOptLevel OL, bool is64bit)
// The pic relocation model is used regardless of what the client has
// specified, as it is the only relocation model currently supported.
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, Reloc::PIC_,
- getEffectiveCodeModel(CM, CodeModel::Small), OL),
+ : CodeGenTargetMachineImpl(
+ T, TT.computeDataLayout(NVPTXUseShortPointers ? "" : "shortptr"), TT,
+ CPU, FS, Options, Reloc::PIC_,
+ getEffectiveCodeModel(CM, CodeModel::Small), OL),
is64bit(is64bit), TLOF(std::make_unique<NVPTXTargetObjectFile>()),
Subtarget(TT, std::string(CPU), std::string(FS), *this),
StrPool(StrAlloc) {
diff --git a/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp b/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
index dd84905e16deb..000d29610678f 100644
--- a/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
+++ b/llvm/lib/Target/PowerPC/PPCTargetMachine.cpp
@@ -296,8 +296,8 @@ PPCTargetMachine::PPCTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, computeFSAdditions(FS, OL, TT), Options,
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU,
+ computeFSAdditions(FS, OL, TT), Options,
getEffectiveRelocModel(TT, RM),
getEffectivePPCCodeModel(TT, CM, JIT), OL),
TLOF(createTLOF(getTargetTriple())),
diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index 36a1bab0de6f1..a1ec24f1fe719 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -152,12 +152,10 @@ RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T,
- DataLayout::computeStringForTriple(
- TT, Options.MCOptions.getABIName()),
- TT, CPU, FS, Options,
- getEffectiveRelocModel(TT, RM),
- getEffectiveCodeModel(CM, CodeModel::Small), OL),
+ : CodeGenTargetMachineImpl(
+ T, TT.computeDataLayout(Options.MCOptions.getABIName()), TT, CPU, FS,
+ Options, getEffectiveRelocModel(TT, RM),
+ getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<RISCVELFTargetObjectFile>()) {
initAsmInfo();
diff --git a/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp b/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp
index 3f8262a82c007..ec8c26d919af1 100644
--- a/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVTargetMachine.cpp
@@ -75,8 +75,8 @@ SPIRVTargetMachine::SPIRVTargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<SPIRVTargetObjectFile>()),
Subtarget(TT, CPU.str(), FS.str(), *this) {
diff --git a/llvm/lib/Target/Sparc/SparcTargetMachine.cpp b/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
index a023a41c41aeb..27ab57c11cf71 100644
--- a/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
+++ b/llvm/lib/Target/Sparc/SparcTargetMachine.cpp
@@ -78,7 +78,7 @@ SparcTargetMachine::SparcTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
: CodeGenTargetMachineImpl(
- T, DataLayout::computeStringForTriple(TT), TT, CPU, FS, Options,
+ T, TT.computeDataLayout(), TT, CPU, FS, Options,
getEffectiveRelocModel(RM),
getEffectiveSparcCodeModel(CM, getEffectiveRelocModel(RM),
TT.isSPARC64(), JIT),
diff --git a/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp b/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
index 84579d0b50786..3d0c04b574933 100644
--- a/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
+++ b/llvm/lib/Target/SystemZ/SystemZTargetMachine.cpp
@@ -122,7 +122,7 @@ SystemZTargetMachine::SystemZTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
: CodeGenTargetMachineImpl(
- T, DataLayout::computeStringForTriple(TT), TT, CPU, FS, Options,
+ T, TT.computeDataLayout(), TT, CPU, FS, Options,
getEffectiveRelocModel(RM),
getEffectiveSystemZCodeModel(CM, getEffectiveRelocModel(RM), JIT),
OL),
diff --git a/llvm/lib/Target/VE/VETargetMachine.cpp b/llvm/lib/Target/VE/VETargetMachine.cpp
index d76e06881d0e0..dc9ca48cc221b 100644
--- a/llvm/lib/Target/VE/VETargetMachine.cpp
+++ b/llvm/lib/Target/VE/VETargetMachine.cpp
@@ -59,8 +59,8 @@ VETargetMachine::VETargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, getEffectiveRelocModel(RM),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ getEffectiveRelocModel(RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(createTLOF()),
Subtarget(TT, std::string(CPU), std::string(FS), *this) {
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
index 874d414a9ee73..a9c638cde1259 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -196,8 +196,8 @@ WebAssemblyTargetMachine::WebAssemblyTargetMachine(
const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
const TargetOptions &Options, std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM, CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options, getEffectiveRelocModel(RM, TT),
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
+ getEffectiveRelocModel(RM, TT),
getEffectiveCodeModel(CM, CodeModel::Large), OL),
TLOF(new WebAssemblyTargetObjectFile()),
UsesMultivalueABI(Options.MCOptions.getABIName() == "experimental-mv") {
diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index 268144fd23c6d..c1dd680cd0522 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -189,8 +189,7 @@ X86TargetMachine::X86TargetMachine(const Target &T, const Triple &TT,
std::optional<Reloc::Model> RM,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT), TT,
- CPU, FS, Options,
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
getEffectiveRelocModel(TT, JIT, RM),
getEffectiveX86CodeModel(TT, CM, JIT), OL),
TLOF(createTLOF(getTargetTriple())), IsJIT(JIT) {
diff --git a/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp b/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
index 1622427eda35f..72cb61b5e864e 100644
--- a/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
+++ b/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
@@ -46,8 +46,7 @@ XtensaTargetMachine::XtensaTargetMachine(const Target &T, const Triple &TT,
std::optional<CodeModel::Model> CM,
CodeGenOptLevel OL, bool JIT,
bool IsLittle)
- : CodeGenTargetMachineImpl(T, DataLayout::computeStringForTriple(TT),
- TT, CPU, FS, Options,
+ : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,
getEffectiveRelocModel(JIT, RM),
getEffectiveCodeModel(CM, CodeModel::Small), OL),
TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {
diff --git a/llvm/lib/TargetParser/CMakeLists.txt b/llvm/lib/TargetParser/CMakeLists.txt
index 5eecfbf80b2f7..e1a30199e1ade 100644
--- a/llvm/lib/TargetParser/CMakeLists.txt
+++ b/llvm/lib/TargetParser/CMakeLists.txt
@@ -26,6 +26,7 @@ add_llvm_component_library(LLVMTargetParser
SubtargetFeature.cpp
TargetParser.cpp
Triple.cpp
+ TargetDataLayout.cpp
X86TargetParser.cpp
XtensaTargetParser.cpp
diff --git a/llvm/lib/TargetParser/TargetDataLayout.cpp b/llvm/lib/TargetParser/TargetDataLayout.cpp
new file mode 100644
index 0000000000000..b0c9d5c85e4c6
--- /dev/null
+++ b/llvm/lib/TargetParser/TargetDataLayout.cpp
@@ -0,0 +1,631 @@
+//===--- TargetDataLayout.cpp - Map Triple to LLVM data layout string -----===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/TargetParser/ARMTargetParser.h"
+#include "llvm/TargetParser/Triple.h"
+#include <cstring>
+using namespace llvm;
+
+static const char *getManglingComponent(const Triple &T) {
+ if (T.isOSBinFormatGOFF())
+ return "-m:l";
+ if (T.isOSBinFormatMachO())
+ return "-m:o";
+ if ((T.isOSWindows() || T.isUEFI()) && T.isOSBinFormatCOFF())
+ return T.getArch() == Triple::x86 ? "-m:x" : "-m:w";
+ if (T.isOSBinFormatXCOFF())
+ return "-m:a";
+ return "-m:e";
+}
+
+static std::string computeARMDataLayout(const Triple &TT, StringRef ABIName) {
+ auto ABI = ARM::computeTargetABI(TT, ABIName);
+ std::string Ret;
+
+ if (TT.isLittleEndian())
+ // Little endian.
+ Ret += "e";
+ else
+ // Big endian.
+ Ret += "E";
+
+ Ret += getManglingComponent(TT);
+
+ // Pointers are 32 bits and aligned to 32 bits.
+ Ret += "-p:32:32";
+
+ // Function pointers are aligned to 8 bits (because the LSB stores the
+ // ARM/Thumb state).
+ Ret += "-Fi8";
+
+ // ABIs other than APCS have 64 bit integers with natural alignment.
+ if (ABI != ARM::ARM_ABI_APCS)
+ Ret += "-i64:64";
+
+ // We have 64 bits floats. The APCS ABI requires them to be aligned to 32
+ // bits, others to 64 bits. We always try to align to 64 bits.
+ if (ABI == ARM::ARM_ABI_APCS)
+ Ret += "-f64:32:64";
+
+ // We have 128 and 64 bit vectors. The APCS ABI aligns them to 32 bits, others
+ // to 64. We always ty to give them natural alignment.
+ if (ABI == ARM::ARM_ABI_APCS)
+ Ret += "-v64:32:64-v128:32:128";
+ else if (ABI != ARM::ARM_ABI_AAPCS16)
+ Ret += "-v128:64:128";
+
+ // Try to align aggregates to 32 bits (the default is 64 bits, which has no
+ // particular hardware support on 32-bit ARM).
+ Ret += "-a:0:32";
+
+ // Integer registers are 32 bits.
+ Ret += "-n32";
+
+ // The stack is 64 bit aligned on AAPCS and 32 bit aligned everywhere else.
+ if (ABI == ARM::ARM_ABI_AAPCS16)
+ Ret += "-S128";
+ else if (ABI == ARM::ARM_ABI_AAPCS)
+ Ret += "-S64";
+ else
+ Ret += "-S32";
+
+ return Ret;
+}
+
+// Helper function to build a DataLayout string
+static std::string computeAArch64DataLayout(const Triple &TT) {
+ if (TT.isOSBinFormatMachO()) {
+ if (TT.getArch() == Triple::aarch64_32)
+ return "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-"
+ "n32:64-S128-Fn32";
+ return "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-"
+ "Fn32";
+ }
+ if (TT.isOSBinFormatCOFF())
+ return "e-m:w-p270:32:32-p271:32:32-p272:64:64-p:64:64-i32:32-i64:64-i128:"
+ "128-n32:64-S128-Fn32";
+ std::string Endian = TT.isLittleEndian() ? "e" : "E";
+ std::string Ptr32 = TT.getEnvironment() == Triple::GNUILP32 ? "-p:32:32" : "";
+ return Endian + "-m:e" + Ptr32 +
+ "-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-"
+ "n32:64-S128-Fn32";
+}
+
+// DataLayout: little or big endian
+static std::string computeBPFDataLayout(const Triple &TT) {
+ if (TT.getArch() == Triple::bpfeb)
+ return "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
+ else
+ return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
+}
+
+static std::string computeCSKYDataLayout(const Triple &TT) {
+ std::string Ret;
+
+ // Only support little endian for now.
+ // TODO: Add support for big endian.
+ Ret += "e";
+
+ // CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
+ // It's a 4-byte aligned stack with ELF mangling only.
+ Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
+ "-v128:32:32-a:0:32-Fi32-n32";
+
+ return Ret;
+}
+
+static std::string computeLoongArchDataLayout(const Triple &TT) {
+ if (TT.isArch64Bit())
+ return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
+ assert(TT.isArch32Bit() && "only LA32 and LA64 are currently supported");
+ return "e-m:e-p:32:32-i64:64-n32-S128";
+}
+
+static std::string computeM68kDataLayout(const Triple &TT) {
+ std::string Ret = "";
+ // M68k is Big Endian
+ Ret += "E";
+
+ // FIXME how to wire it with the used object format?
+ Ret += "-m:e";
+
+ // M68k pointers are always 32 bit wide even for 16-bit CPUs.
+ // The ABI only specifies 16-bit alignment.
+ // On at least the 68020+ with a 32-bit bus, there is a performance benefit
+ // to having 32-bit alignment.
+ Ret += "-p:32:16:32";
+
+ // Bytes do not require special alignment, words are word aligned and
+ // long words are word aligned at minimum.
+ Ret += "-i8:8:8-i16:16:16-i32:16:32";
+
+ // FIXME no floats at the moment
+
+ // The registers can hold 8, 16, 32 bits
+ Ret += "-n8:16:32";
+
+ Ret += "-a:0:16-S16";
+
+ return Ret;
+}
+
+namespace {
+enum class MipsABI { Unknown, O32, N32, N64 };
+}
+
+// FIXME: This duplicates MipsABIInfo::computeTargetABI, but duplicating this is
+// preferable to violating layering rules. Ideally that information should live
+// in LLVM TargetParser, but for now we just duplicate some ABI name string
+// logic for simplicity.
+static MipsABI getMipsABI(const Triple &TT, StringRef ABIName) {
+ if (ABIName.starts_with("o32"))
+ return MipsABI::O32;
+ if (ABIName.starts_with("n32"))
+ return MipsABI::N32;
+ if (ABIName.starts_with("n64"))
+ return MipsABI::N64;
+ if (TT.isABIN32())
+ return MipsABI::N32;
+ assert(ABIName.empty() && "Unknown ABI option for MIPS");
+
+ if (TT.isMIPS64())
+ return MipsABI::N64;
+ return MipsABI::O32;
+}
+
+static std::string computeMipsDataLayout(const Triple &TT, StringRef ABIName) {
+ std::string Ret;
+ MipsABI ABI = getMipsABI(TT, ABIName);
+
+ // There are both little and big endian mips.
+ if (TT.isLittleEndian())
+ Ret += "e";
+ else
+ Ret += "E";
+
+ if (ABI == MipsABI::O32)
+ Ret += "-m:m";
+ else
+ Ret += "-m:e";
+
+ // Pointers are 32 bit on some ABIs.
+ if (ABI != MipsABI::N64)
+ Ret += "-p:32:32";
+
+ // 8 and 16 bit integers only need to have natural alignment, but try to
+ // align them to 32 bits. 64 bit integers have natural alignment.
+ Ret += "-i8:8:32-i16:16:32-i64:64";
+
+ // 32 bit registers are always available and the stack is at least 64 bit
+ // aligned. On N64 64 bit registers are also available and the stack is
+ // 128 bit aligned.
+ if (ABI == MipsABI::N64 || ABI == MipsABI::N32)
+ Ret += "-i128:128-n32:64-S128";
+ else
+ Ret += "-n32-S64";
+
+ return Ret;
+}
+
+static std::string computePowerDataLayout(const Triple &T) {
+ bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
+ std::string Ret;
+
+ // Most PPC* platforms are big endian, PPC(64)LE is little endian.
+ if (T.isLittleEndian())
+ Ret = "e";
+ else
+ Ret = "E";
+
+ Ret += getManglingComponent(T);
+
+ // PPC32 has 32 bit pointers. The PS3 (OS Lv2) is a PPC64 machine with 32 bit
+ // pointers.
+ if (!is64Bit || T.getOS() == Triple::Lv2)
+ Ret += "-p:32:32";
+
+ // If the target ABI uses function descriptors, then the alignment of function
+ // pointers depends on the alignment used to emit the descriptor. Otherwise,
+ // function pointers are aligned to 32 bits because the instructions must be.
+ if ((T.getArch() == Triple::ppc64 && !T.isPPC64ELFv2ABI())) {
+ Ret += "-Fi64";
+ } else if (T.isOSAIX()) {
+ Ret += is64Bit ? "-Fi64" : "-Fi32";
+ } else {
+ Ret += "-Fn32";
+ }
+
+ // Note, the alignment values for f64 and i64 on ppc64 in Darwin
+ // documentation are wrong; these are correct (i.e. "what gcc does").
+ Ret += "-i64:64";
+
+ // PPC64 has 32 and 64 bit registers, PPC32 has only 32 bit ones.
+ if (is64Bit)
+ Ret += "-i128:128-n32:64";
+ else
+ Ret += "-n32";
+
+ // Specify the vector alignment explicitly. For v256i1 and v512i1, the
+ // calculated alignment would be 256*alignment(i1) and 512*alignment(i1),
+ // which is 256 and 512 bytes - way over aligned.
+ if (is64Bit && (T.isOSAIX() || T.isOSLinux()))
+ Ret += "-S128-v256:256:256-v512:512:512";
+
+ return Ret;
+}
+
+static std::string computeAMDDataLayout(const Triple &TT) {
+ if (TT.getArch() == Triple::r600) {
+ // 32-bit pointers.
+ return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128"
+ "-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1";
+ }
+
+ // 32-bit private, local, and region pointers. 64-bit global, constant and
+ // flat. 160-bit non-integral fat buffer pointers that include a 128-bit
+ // buffer descriptor and a 32-bit offset, which are indexed by 32-bit values
+ // (address space 7), and 128-bit non-integral buffer resourcees (address
+ // space 8) which cannot be non-trivilally accessed by LLVM memory operations
+ // like getelementptr.
+ return "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32"
+ "-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-i64:64-"
+ "v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-"
+ "v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9";
+}
+
+static std::string computeRISCVDataLayout(const Triple &TT, StringRef ABIName) {
+ std::string Ret;
+
+ if (TT.isLittleEndian())
+ Ret += "e";
+ else
+ Ret += "E";
+
+ Ret += "-m:e";
+
+ // Pointer and integer sizes.
+ if (TT.isArch64Bit()) {
+ Ret += "-p:64:64-i64:64-i128:128";
+ Ret += "-n32:64";
+ } else {
+ assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported");
+ Ret += "-p:32:32-i64:64";
+ Ret += "-n32";
+ }
+
+ // Stack alignment based on ABI.
+ StringRef ABI = ABIName;
+ if (ABI == "ilp32e")
+ Ret += "-S32";
+ else if (ABI == "lp64e")
+ Ret += "-S64";
+ else
+ Ret += "-S128";
+
+ return Ret;
+}
+
+static std::string computeSparcDataLayout(const Triple &T) {
+ const bool is64Bit = T.isSPARC64();
+
+ // Sparc is typically big endian, but some are little.
+ std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E";
+ Ret += "-m:e";
+
+ // Some ABIs have 32bit pointers.
+ if (!is64Bit)
+ Ret += "-p:32:32";
+
+ // Alignments for 64 bit integers.
+ Ret += "-i64:64";
+
+ // Alignments for 128 bit integers.
+ // This is not specified in the ABI document but is the de facto standard.
+ Ret += "-i128:128";
+
+ // On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
+ // On SparcV9 registers can hold 64 or 32 bits, on others only 32.
+ if (is64Bit)
+ Ret += "-n32:64";
+ else
+ Ret += "-f128:64-n32";
+
+ if (is64Bit)
+ Ret += "-S128";
+ else
+ Ret += "-S64";
+
+ return Ret;
+}
+
+static std::string computeSystemZDataLayout(const Triple &TT) {
+ std::string Ret;
+
+ // Big endian.
+ Ret += "E";
+
+ // Data mangling.
+ Ret += getManglingComponent(TT);
+
+ // Special features for z/OS.
+ if (TT.isOSzOS()) {
+ if (TT.isArch64Bit()) {
+ // Custom address space for ptr32.
+ Ret += "-p1:32:32";
+ }
+ }
+
+ // Make sure that global data has at least 16 bits of alignment by
+ // default, so that we can refer to it using LARL. We don't have any
+ // special requirements for stack variables though.
+ Ret += "-i1:8:16-i8:8:16";
+
+ // 64-bit integers are naturally aligned.
+ Ret += "-i64:64";
+
+ // 128-bit floats are aligned only to 64 bits.
+ Ret += "-f128:64";
+
+ // The DataLayout string always holds a vector alignment of 64 bits, see
+ // comment in clang/lib/Basic/Targets/SystemZ.h.
+ Ret += "-v128:64";
+
+ // We prefer 16 bits of aligned for all globals; see above.
+ Ret += "-a:8:16";
+
+ // Integer registers are 32 or 64 bits.
+ Ret += "-n32:64";
+
+ return Ret;
+}
+
+static std::string computeX86DataLayout(const Triple &TT) {
+ // X86 is little endian
+ std::string Ret = "e";
+
+ Ret += getManglingComponent(TT);
+ // X86 and x32 have 32 bit pointers.
+ if (!TT.isArch64Bit() || TT.isX32())
+ Ret += "-p:32:32";
+
+ // Address spaces for 32 bit signed, 32 bit unsigned, and 64 bit pointers.
+ Ret += "-p270:32:32-p271:32:32-p272:64:64";
+
+ // Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
+ // 128 bit integers are not specified in the 32-bit ABIs but are used
+ // internally for lowering f128, so we match the alignment to that.
+ if (TT.isArch64Bit() || TT.isOSWindows())
+ Ret += "-i64:64-i128:128";
+ else if (TT.isOSIAMCU())
+ Ret += "-i64:32-f64:32";
+ else
+ Ret += "-i128:128-f64:32:64";
+
+ // Some ABIs align long double to 128 bits, others to 32.
+ if (TT.isOSIAMCU())
+ ; // No f80
+ else if (TT.isArch64Bit() || TT.isOSDarwin() || TT.isWindowsMSVCEnvironment())
+ Ret += "-f80:128";
+ else
+ Ret += "-f80:32";
+
+ if (TT.isOSIAMCU())
+ Ret += "-f128:32";
+
+ // The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
+ if (TT.isArch64Bit())
+ Ret += "-n8:16:32:64";
+ else
+ Ret += "-n8:16:32";
+
+ // The stack is aligned to 32 bits on some ABIs and 128 bits on others.
+ if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
+ Ret += "-a:0:32-S32";
+ else
+ Ret += "-S128";
+
+ return Ret;
+}
+
+static std::string computeNVPTXDataLayout(const Triple &T, StringRef ABIName) {
+ std::string Ret = "e";
+
+ // Tensor Memory (addrspace:6) is always 32-bits.
+ // Distributed Shared Memory (addrspace:7) follows shared memory
+ // (addrspace:3).
+ if (!T.isArch64Bit())
+ Ret += "-p:32:32-p6:32:32-p7:32:32";
+ else if (ABIName == "shortptr")
+ Ret += "-p3:32:32-p4:32:32-p5:32:32-p6:32:32-p7:32:32";
+ else
+ Ret += "-p6:32:32";
+
+ Ret += "-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64";
+
+ return Ret;
+}
+
+static std::string computeSPIRVDataLayout(const Triple &TT) {
+ const auto Arch = TT.getArch();
+ // TODO: this probably needs to be revisited:
+ // Logical SPIR-V has no pointer size, so any fixed pointer size would be
+ // wrong. The choice to default to 32 or 64 is just motivated by another
+ // memory model used for graphics: PhysicalStorageBuffer64. But it shouldn't
+ // mean anything.
+ if (Arch == Triple::spirv32)
+ return "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-"
+ "v256:256-v512:512-v1024:1024-n8:16:32:64-G1";
+ if (Arch == Triple::spirv)
+ return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
+ "v512:512-v1024:1024-n8:16:32:64-G10";
+ if (TT.getVendor() == Triple::VendorType::AMD &&
+ TT.getOS() == Triple::OSType::AMDHSA)
+ return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
+ "v512:512-v1024:1024-n32:64-S32-G1-P4-A0";
+ return "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-"
+ "v512:512-v1024:1024-n8:16:32:64-G1";
+}
+
+static std::string computeLanaiDataLayout() {
+ // Data layout (keep in sync with clang/lib/Basic/Targets.cpp)
+ return "E" // Big endian
+ "-m:e" // ELF name manging
+ "-p:32:32" // 32-bit pointers, 32 bit aligned
+ "-i64:64" // 64 bit integers, 64 bit aligned
+ "-a:0:32" // 32 bit alignment of objects of aggregate type
+ "-n32" // 32 bit native integer width
+ "-S64"; // 64 bit natural stack alignment
+}
+
+static std::string computeWebAssemblyDataLayout(const Triple &TT) {
+ return TT.isArch64Bit()
+ ? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
+ "i128:128-f128:64-n32:64-S128-ni:1:10:20"
+ : "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
+ "i128:128-n32:64-S128-ni:1:10:20")
+ : (TT.isOSEmscripten() ? "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
+ "i128:128-f128:64-n32:64-S128-ni:1:10:20"
+ : "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-"
+ "i128:128-n32:64-S128-ni:1:10:20");
+}
+
+static std::string computeVEDataLayout(const Triple &T) {
+ // Aurora VE is little endian
+ std::string Ret = "e";
+
+ // Use ELF mangling
+ Ret += "-m:e";
+
+ // Alignments for 64 bit integers.
+ Ret += "-i64:64";
+
+ // VE supports 32 bit and 64 bits integer on registers
+ Ret += "-n32:64";
+
+ // Stack alignment is 128 bits
+ Ret += "-S128";
+
+ // Vector alignments are 64 bits
+ // Need to define all of them. Otherwise, each alignment becomes
+ // the size of each data by default.
+ Ret += "-v64:64:64"; // for v2f32
+ Ret += "-v128:64:64";
+ Ret += "-v256:64:64";
+ Ret += "-v512:64:64";
+ Ret += "-v1024:64:64";
+ Ret += "-v2048:64:64";
+ Ret += "-v4096:64:64";
+ Ret += "-v8192:64:64";
+ Ret += "-v16384:64:64"; // for v256f64
+
+ return Ret;
+}
+
+std::string Triple::computeDataLayout(StringRef ABIName) const {
+ switch (getArch()) {
+ case Triple::arm:
+ case Triple::armeb:
+ case Triple::thumb:
+ case Triple::thumbeb:
+ return computeARMDataLayout(*this, ABIName);
+ case Triple::aarch64:
+ case Triple::aarch64_be:
+ case Triple::aarch64_32:
+ return computeAArch64DataLayout(*this);
+ case Triple::arc:
+ return "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-"
+ "f32:32:32-i64:32-f64:32-a:0:32-n32";
+ case Triple::avr:
+ return "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8:16-a:8";
+ case Triple::bpfel:
+ case Triple::bpfeb:
+ return computeBPFDataLayout(*this);
+ case Triple::csky:
+ return computeCSKYDataLayout(*this);
+ case Triple::dxil:
+ return "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-"
+ "f32:32-f64:64-n8:16:32:64";
+ case Triple::hexagon:
+ return "e-m:e-p:32:32:32-a:0-n16:32-"
+ "i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-"
+ "v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048";
+ case Triple::loongarch32:
+ case Triple::loongarch64:
+ return computeLoongArchDataLayout(*this);
+ case Triple::m68k:
+ return computeM68kDataLayout(*this);
+ case Triple::mips:
+ case Triple::mipsel:
+ case Triple::mips64:
+ case Triple::mips64el:
+ return computeMipsDataLayout(*this, ABIName);
+ case Triple::msp430:
+ return "e-m:e-p:16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S16";
+ case Triple::ppc:
+ case Triple::ppcle:
+ case Triple::ppc64:
+ case Triple::ppc64le:
+ return computePowerDataLayout(*this);
+ case Triple::r600:
+ case Triple::amdgcn:
+ return computeAMDDataLayout(*this);
+ case Triple::riscv32:
+ case Triple::riscv64:
+ case Triple::riscv32be:
+ case Triple::riscv64be:
+ return computeRISCVDataLayout(*this, ABIName);
+ case Triple::sparc:
+ case Triple::sparcv9:
+ case Triple::sparcel:
+ return computeSparcDataLayout(*this);
+ case Triple::systemz:
+ return computeSystemZDataLayout(*this);
+ case Triple::tce:
+ case Triple::tcele:
+ case Triple::x86:
+ case Triple::x86_64:
+ return computeX86DataLayout(*this);
+ case Triple::xcore:
+ case Triple::xtensa:
+ return "e-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32";
+ case Triple::nvptx:
+ case Triple::nvptx64:
+ return computeNVPTXDataLayout(*this, ABIName);
+ case Triple::spir:
+ case Triple::spir64:
+ case Triple::spirv:
+ case Triple::spirv32:
+ case Triple::spirv64:
+ return computeSPIRVDataLayout(*this);
+ case Triple::lanai:
+ return computeLanaiDataLayout();
+ case Triple::wasm32:
+ case Triple::wasm64:
+ return computeWebAssemblyDataLayout(*this);
+ case Triple::ve:
+ return computeVEDataLayout(*this);
+
+ case Triple::amdil:
+ case Triple::amdil64:
+ case Triple::hsail:
+ case Triple::hsail64:
+ case Triple::kalimba:
+ case Triple::shave:
+ case Triple::renderscript32:
+ case Triple::renderscript64:
+ // These are all virtual ISAs with no LLVM backend, and therefore no fixed
+ // LLVM data layout.
+ return "";
+
+ case Triple::UnknownArch:
+ return "";
+ }
+ return "";
+}
diff --git a/llvm/unittests/IR/DataLayoutTest.cpp b/llvm/unittests/IR/DataLayoutTest.cpp
index afa72a53ab2c0..e0c0f35847f07 100644
--- a/llvm/unittests/IR/DataLayoutTest.cpp
+++ b/llvm/unittests/IR/DataLayoutTest.cpp
@@ -677,11 +677,4 @@ TEST(DataLayoutTest, VectorAlign) {
EXPECT_EQ(Align(4 * 8), DL->getPrefTypeAlign(V8F32Ty));
}
-TEST(DataLayoutTest, UEFI) {
- Triple TT = Triple("x86_64-unknown-uefi");
-
- // Test UEFI X86_64 Mangling Component.
- EXPECT_STREQ(DataLayout::getManglingComponent(TT), "-m:w");
-}
-
} // anonymous namespace
diff --git a/llvm/unittests/TargetParser/TripleTest.cpp b/llvm/unittests/TargetParser/TripleTest.cpp
index d2ca30583fe82..567a1038416a9 100644
--- a/llvm/unittests/TargetParser/TripleTest.cpp
+++ b/llvm/unittests/TargetParser/TripleTest.cpp
@@ -10,6 +10,7 @@
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/VersionTuple.h"
+#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace llvm;
@@ -3284,4 +3285,12 @@ TEST(TripleTest, isCompatibleWith) {
EXPECT_TRUE(DoTest(C.B, C.A, C.Result));
}
}
+
+TEST(DataLayoutTest, UEFI) {
+ Triple TT = Triple("x86_64-unknown-uefi");
+
+ // Test UEFI X86_64 Mangling Component.
+ EXPECT_THAT(TT.computeDataLayout(), testing::HasSubstr("-m:w-"));
+}
+
} // end anonymous namespace
>From 215efb27df2770e8fb28747dfbe2cc31fdcee66d Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Mon, 8 Sep 2025 21:33:14 -0700
Subject: [PATCH 4/7] Fix NVPTX logic bug
---
llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
index 4343388089bd9..a6837a482608c 100644
--- a/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp
@@ -59,6 +59,12 @@ static cl::opt<bool> DisableRequireStructuredCFG(
"unexpected regressions happen."),
cl::init(false), cl::Hidden);
+static cl::opt<bool> UseShortPointersOpt(
+ "nvptx-short-ptr",
+ cl::desc(
+ "Use 32-bit pointers for accessing const/local/shared address spaces."),
+ cl::init(false), cl::Hidden);
+
// byval arguments in NVPTX are special. We're only allowed to read from them
// using a special instruction, and if we ever need to write to them or take an
// address, we must make a local copy and use it, instead.
@@ -112,12 +118,6 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeNVPTXTarget() {
initializeNVPTXPrologEpilogPassPass(PR);
}
-static cl::opt<bool> NVPTXUseShortPointers(
- "nvptx-short-ptr",
- cl::desc(
- "Use 32-bit pointers for accessing const/local/shared address spaces."),
- cl::init(false), cl::Hidden);
-
NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
@@ -127,7 +127,7 @@ NVPTXTargetMachine::NVPTXTargetMachine(const Target &T, const Triple &TT,
// The pic relocation model is used regardless of what the client has
// specified, as it is the only relocation model currently supported.
: CodeGenTargetMachineImpl(
- T, TT.computeDataLayout(NVPTXUseShortPointers ? "" : "shortptr"), TT,
+ T, TT.computeDataLayout(UseShortPointersOpt ? "shortptr" : ""), TT,
CPU, FS, Options, Reloc::PIC_,
getEffectiveCodeModel(CM, CodeModel::Small), OL),
is64bit(is64bit), TLOF(std::make_unique<NVPTXTargetObjectFile>()),
>From b77e7455583e555a673355be88ef2f6259b1736c Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Mon, 8 Sep 2025 21:34:50 -0700
Subject: [PATCH 5/7] Fix formatting issues
---
llvm/lib/Target/Mips/MCTargetDesc/MipsMCAsmInfo.cpp | 3 ++-
llvm/lib/Target/X86/X86TargetMachine.cpp | 1 -
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCAsmInfo.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCAsmInfo.cpp
index 7f3fd0445ceb7..e1c9954c19cc0 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsMCAsmInfo.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsMCAsmInfo.cpp
@@ -24,7 +24,8 @@ MipsELFMCAsmInfo::MipsELFMCAsmInfo(const Triple &TheTriple,
const MCTargetOptions &Options) {
IsLittleEndian = TheTriple.isLittleEndian();
- MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TheTriple, Options.getABIName());
+ MipsABIInfo ABI =
+ MipsABIInfo::computeTargetABI(TheTriple, Options.getABIName());
if (TheTriple.isMIPS64() && !ABI.IsN32())
CodePointerSize = CalleeSaveStackSlotSize = 8;
diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index c1dd680cd0522..babbe95cc7808 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -125,7 +125,6 @@ static std::unique_ptr<TargetLoweringObjectFile> createTLOF(const Triple &TT) {
return std::make_unique<X86ELFTargetObjectFile>();
}
-
static Reloc::Model getEffectiveRelocModel(const Triple &TT, bool JIT,
std::optional<Reloc::Model> RM) {
bool is64Bit = TT.getArch() == Triple::x86_64;
>From d45ce60d4111cd22634c7e99c7a9b36ac7ef7a5c Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Tue, 9 Sep 2025 06:59:11 -0700
Subject: [PATCH 6/7] Apply suggestions from code review
Co-authored-by: Matt Arsenault <arsenm2 at gmail.com>
Co-authored-by: Sergei Barannikov <barannikov88 at gmail.com>
---
llvm/lib/TargetParser/TargetDataLayout.cpp | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/TargetParser/TargetDataLayout.cpp b/llvm/lib/TargetParser/TargetDataLayout.cpp
index b0c9d5c85e4c6..67eacf81bff3e 100644
--- a/llvm/lib/TargetParser/TargetDataLayout.cpp
+++ b/llvm/lib/TargetParser/TargetDataLayout.cpp
@@ -13,7 +13,7 @@
#include <cstring>
using namespace llvm;
-static const char *getManglingComponent(const Triple &T) {
+static StringRef getManglingComponent(const Triple &T) {
if (T.isOSBinFormatGOFF())
return "-m:l";
if (T.isOSBinFormatMachO())
@@ -122,9 +122,9 @@ static std::string computeCSKYDataLayout(const Triple &TT) {
}
static std::string computeLoongArchDataLayout(const Triple &TT) {
- if (TT.isArch64Bit())
+ if (TT.isLoongArch64())
return "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128";
- assert(TT.isArch32Bit() && "only LA32 and LA64 are currently supported");
+ assert(TT.isLoongArch32() && "only LA32 and LA64 are currently supported");
return "e-m:e-p:32:32-i64:64-n32-S128";
}
@@ -215,7 +215,7 @@ static std::string computeMipsDataLayout(const Triple &TT, StringRef ABIName) {
}
static std::string computePowerDataLayout(const Triple &T) {
- bool is64Bit = T.getArch() == Triple::ppc64 || T.getArch() == Triple::ppc64le;
+ bool is64Bit = T.isPPC64();
std::string Ret;
// Most PPC* platforms are big endian, PPC(64)LE is little endian.
@@ -291,11 +291,11 @@ static std::string computeRISCVDataLayout(const Triple &TT, StringRef ABIName) {
Ret += "-m:e";
// Pointer and integer sizes.
- if (TT.isArch64Bit()) {
+ if (TT.isRISCV64()) {
Ret += "-p:64:64-i64:64-i128:128";
Ret += "-n32:64";
} else {
- assert(TT.isArch32Bit() && "only RV32 and RV64 are currently supported");
+ assert(TT.isRISCV32() && "only RV32 and RV64 are currently supported");
Ret += "-p:32:32-i64:64";
Ret += "-n32";
}
@@ -313,7 +313,7 @@ static std::string computeRISCVDataLayout(const Triple &TT, StringRef ABIName) {
}
static std::string computeSparcDataLayout(const Triple &T) {
- const bool is64Bit = T.isSPARC64();
+ const bool Is64Bit = T.isSPARC64();
// Sparc is typically big endian, but some are little.
std::string Ret = T.getArch() == Triple::sparcel ? "e" : "E";
@@ -627,5 +627,5 @@ std::string Triple::computeDataLayout(StringRef ABIName) const {
case Triple::UnknownArch:
return "";
}
- return "";
+ llvm_unreachable("Invalid arch");
}
>From 25ab0423920b422e3c4e9e595f82f967101f0dd0 Mon Sep 17 00:00:00 2001
From: Reid Kleckner <rnk at google.com>
Date: Tue, 9 Sep 2025 07:17:24 -0700
Subject: [PATCH 7/7] Remove all uses of isArch64Bit and simplify CSKY
implementation
---
llvm/lib/TargetParser/TargetDataLayout.cpp | 41 ++++++++++------------
1 file changed, 18 insertions(+), 23 deletions(-)
diff --git a/llvm/lib/TargetParser/TargetDataLayout.cpp b/llvm/lib/TargetParser/TargetDataLayout.cpp
index 67eacf81bff3e..93a22e9216b8f 100644
--- a/llvm/lib/TargetParser/TargetDataLayout.cpp
+++ b/llvm/lib/TargetParser/TargetDataLayout.cpp
@@ -107,18 +107,12 @@ static std::string computeBPFDataLayout(const Triple &TT) {
}
static std::string computeCSKYDataLayout(const Triple &TT) {
- std::string Ret;
-
- // Only support little endian for now.
- // TODO: Add support for big endian.
- Ret += "e";
-
// CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
// It's a 4-byte aligned stack with ELF mangling only.
- Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
+ // Only support little endian for now.
+ // TODO: Add support for big endian.
+ return "e-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
"-v128:32:32-a:0:32-Fi32-n32";
-
- return Ret;
}
static std::string computeLoongArchDataLayout(const Triple &TT) {
@@ -320,7 +314,7 @@ static std::string computeSparcDataLayout(const Triple &T) {
Ret += "-m:e";
// Some ABIs have 32bit pointers.
- if (!is64Bit)
+ if (!Is64Bit)
Ret += "-p:32:32";
// Alignments for 64 bit integers.
@@ -332,12 +326,12 @@ static std::string computeSparcDataLayout(const Triple &T) {
// On SparcV9 128 floats are aligned to 128 bits, on others only to 64.
// On SparcV9 registers can hold 64 or 32 bits, on others only 32.
- if (is64Bit)
+ if (Is64Bit)
Ret += "-n32:64";
else
Ret += "-f128:64-n32";
- if (is64Bit)
+ if (Is64Bit)
Ret += "-S128";
else
Ret += "-S64";
@@ -356,10 +350,8 @@ static std::string computeSystemZDataLayout(const Triple &TT) {
// Special features for z/OS.
if (TT.isOSzOS()) {
- if (TT.isArch64Bit()) {
- // Custom address space for ptr32.
- Ret += "-p1:32:32";
- }
+ // Custom address space for ptr32.
+ Ret += "-p1:32:32";
}
// Make sure that global data has at least 16 bits of alignment by
@@ -387,12 +379,14 @@ static std::string computeSystemZDataLayout(const Triple &TT) {
}
static std::string computeX86DataLayout(const Triple &TT) {
+ bool Is64Bit = TT.getArch() == Triple::x86_64;
+
// X86 is little endian
std::string Ret = "e";
Ret += getManglingComponent(TT);
// X86 and x32 have 32 bit pointers.
- if (!TT.isArch64Bit() || TT.isX32())
+ if (!Is64Bit || TT.isX32())
Ret += "-p:32:32";
// Address spaces for 32 bit signed, 32 bit unsigned, and 64 bit pointers.
@@ -401,7 +395,7 @@ static std::string computeX86DataLayout(const Triple &TT) {
// Some ABIs align 64 bit integers and doubles to 64 bits, others to 32.
// 128 bit integers are not specified in the 32-bit ABIs but are used
// internally for lowering f128, so we match the alignment to that.
- if (TT.isArch64Bit() || TT.isOSWindows())
+ if (Is64Bit || TT.isOSWindows())
Ret += "-i64:64-i128:128";
else if (TT.isOSIAMCU())
Ret += "-i64:32-f64:32";
@@ -411,7 +405,7 @@ static std::string computeX86DataLayout(const Triple &TT) {
// Some ABIs align long double to 128 bits, others to 32.
if (TT.isOSIAMCU())
; // No f80
- else if (TT.isArch64Bit() || TT.isOSDarwin() || TT.isWindowsMSVCEnvironment())
+ else if (Is64Bit || TT.isOSDarwin() || TT.isWindowsMSVCEnvironment())
Ret += "-f80:128";
else
Ret += "-f80:32";
@@ -420,13 +414,13 @@ static std::string computeX86DataLayout(const Triple &TT) {
Ret += "-f128:32";
// The registers can hold 8, 16, 32 or, in x86-64, 64 bits.
- if (TT.isArch64Bit())
+ if (Is64Bit)
Ret += "-n8:16:32:64";
else
Ret += "-n8:16:32";
// The stack is aligned to 32 bits on some ABIs and 128 bits on others.
- if ((!TT.isArch64Bit() && TT.isOSWindows()) || TT.isOSIAMCU())
+ if ((!Is64Bit && TT.isOSWindows()) || TT.isOSIAMCU())
Ret += "-a:0:32-S32";
else
Ret += "-S128";
@@ -435,12 +429,13 @@ static std::string computeX86DataLayout(const Triple &TT) {
}
static std::string computeNVPTXDataLayout(const Triple &T, StringRef ABIName) {
+ bool Is64Bit = T.getArch() == Triple::nvptx64;
std::string Ret = "e";
// Tensor Memory (addrspace:6) is always 32-bits.
// Distributed Shared Memory (addrspace:7) follows shared memory
// (addrspace:3).
- if (!T.isArch64Bit())
+ if (!Is64Bit)
Ret += "-p:32:32-p6:32:32-p7:32:32";
else if (ABIName == "shortptr")
Ret += "-p3:32:32-p4:32:32-p5:32:32-p6:32:32-p7:32:32";
@@ -485,7 +480,7 @@ static std::string computeLanaiDataLayout() {
}
static std::string computeWebAssemblyDataLayout(const Triple &TT) {
- return TT.isArch64Bit()
+ return TT.getArch() == Triple::wasm64
? (TT.isOSEmscripten() ? "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
"i128:128-f128:64-n32:64-S128-ni:1:10:20"
: "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-"
More information about the llvm-commits
mailing list