[llvm] [LLVMABI] Fix build for GCC < 8 (PR #190974)

Mészáros Gergely via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 8 06:48:40 PDT 2026


https://github.com/Maetveis updated https://github.com/llvm/llvm-project/pull/190974

>From 19dc768818b2394bd59983c7c68ea6e39d4d4573 Mon Sep 17 00:00:00 2001
From: "Meszaros, Gergely" <gergely.meszaros at intel.com>
Date: Wed, 8 Apr 2026 13:34:58 +0000
Subject: [PATCH 1/2] [LLVMABI] Fix build for GCC < 8

`llvm::abi::ArgInfo::get*` functions return `llvm::abi::ArgInfo` by value,
so we need the type to be copyable. It is not though, because of the
non trivially copyable member `MaybeAlign` (`std::optional<Align>`)
in a union, which deletes the implicit copy/move constructors and
assignment operators.
For modern GCC versions and for clang this does not present as a problem
due to named return value optimization (NRVO), but this is not a bug
in old GCC versions, because NRVO is not guaranteed.
Similary `std::optional`
could be trivially copyable, but that is also not guaranteed by the standard.

Instead of depending on these details of the C++ implementation,
simply move the `MaybeAlign` member out of the union, and make it a separate member of `abi::ArgInfo`.
We lose the expressiveness from the type system that `IndirectAttrInfo` always has a defined
alignment, but we can get that back in practice by placing asserts in the code that uses it.
Therefore `llvm::abi::ArgInfo` can still maintain this invariant.

Fixes: #190972
---
 llvm/include/llvm/ABI/FunctionInfo.h | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/llvm/include/llvm/ABI/FunctionInfo.h b/llvm/include/llvm/ABI/FunctionInfo.h
index 40ad6008f32a1..52e621c815c86 100644
--- a/llvm/include/llvm/ABI/FunctionInfo.h
+++ b/llvm/include/llvm/ABI/FunctionInfo.h
@@ -45,14 +45,19 @@ class ArgInfo {
 
 private:
   const Type *CoercionType = nullptr;
+  // Alignment is optional for direct arguments, but required for indirect
+  // arguments. This invariant is enforced by the methods of this class.
+  //
+  // The field is not part of DirectAttrInfo/IndirectAttrInfo because it would
+  // make the union non-trivial, disabling implicit copy/move constructors and
+  // assignment operators for the entire class.
+  MaybeAlign Alignment;
 
   struct DirectAttrInfo {
     unsigned Offset;
-    MaybeAlign Alignment;
   };
 
   struct IndirectAttrInfo {
-    Align Alignment;
     unsigned AddrSpace;
   };
 
@@ -84,8 +89,8 @@ class ArgInfo {
                            MaybeAlign Align = std::nullopt) {
     ArgInfo AI(Direct);
     AI.CoercionType = T;
+    AI.Alignment = Align;
     AI.DirectAttr.Offset = Offset;
-    AI.DirectAttr.Alignment = Align;
     return AI;
   }
 
@@ -95,8 +100,8 @@ class ArgInfo {
 
     ArgInfo AI(Extend);
     AI.CoercionType = T;
+    AI.Alignment = std::nullopt;
     AI.DirectAttr.Offset = 0;
-    AI.DirectAttr.Alignment = std::nullopt;
 
     const IntegerType *IntTy = cast<IntegerType>(T);
     if (IntTy->isSigned())
@@ -112,7 +117,7 @@ class ArgInfo {
   static ArgInfo getIndirect(Align Align, bool ByVal, unsigned AddrSpace = 0,
                              bool Realign = false) {
     ArgInfo AI(Indirect);
-    AI.IndirectAttr.Alignment = Align;
+    AI.Alignment = Align;
     AI.IndirectAttr.AddrSpace = AddrSpace;
     AI.IndirectByVal = ByVal;
     AI.IndirectRealign = Realign;
@@ -148,12 +153,13 @@ class ArgInfo {
 
   MaybeAlign getDirectAlign() const {
     assert((isDirect() || isExtend()) && "Not a direct or extend kind");
-    return DirectAttr.Alignment;
+    return Alignment;
   }
 
   Align getIndirectAlign() const {
     assert(isIndirect() && "Invalid Kind!");
-    return IndirectAttr.Alignment;
+    assert(Alignment && "Indirect arguments must have an alignment");
+    return *Alignment;
   }
 
   unsigned getIndirectAddrSpace() const {

>From 873f1184c6e18f0c7ac194be1f11ca81a6f0ee7f Mon Sep 17 00:00:00 2001
From: "Meszaros, Gergely" <gergely.meszaros at intel.com>
Date: Wed, 8 Apr 2026 13:48:27 +0000
Subject: [PATCH 2/2] Be more explicit .has_value() instead of bool conversion

---
 llvm/include/llvm/ABI/FunctionInfo.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/ABI/FunctionInfo.h b/llvm/include/llvm/ABI/FunctionInfo.h
index 52e621c815c86..7f7b6a44ba6ad 100644
--- a/llvm/include/llvm/ABI/FunctionInfo.h
+++ b/llvm/include/llvm/ABI/FunctionInfo.h
@@ -158,7 +158,8 @@ class ArgInfo {
 
   Align getIndirectAlign() const {
     assert(isIndirect() && "Invalid Kind!");
-    assert(Alignment && "Indirect arguments must have an alignment");
+    assert(Alignment.has_value() &&
+           "Indirect arguments must have an alignment");
     return *Alignment;
   }
 



More information about the llvm-commits mailing list