[flang-commits] [flang] [flang] Control alignment of constant folded reals (PR #149381)
Eugene Epshteyn via flang-commits
flang-commits at lists.llvm.org
Sun Jul 20 14:16:26 PDT 2025
https://github.com/eugeneepshteyn updated https://github.com/llvm/llvm-project/pull/149381
>From a155a355cc1cef2169c37d069ad72b4c180f0633 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Thu, 17 Jul 2025 14:56:11 -0400
Subject: [PATCH 1/3] [flang] Control alignment of constant folded reals
When REAL types are constant folded, the underneath implementation uses
arrays of integers. Ensure that these arrays are properly aligned.
This matters when building flang with clang. In some cases, the
resulting code for flang compiler ended up using SSE2 aligned load
instructions for REAL(16) constant folding on x86_64, and these
instructions require that the values are loaded from the aligned
addresses.
---
flang/include/flang/Evaluate/integer.h | 1 +
flang/include/flang/Evaluate/real.h | 2 +-
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/flang/include/flang/Evaluate/integer.h b/flang/include/flang/Evaluate/integer.h
index fccc2ad774a8f..5953fc81cb111 100644
--- a/flang/include/flang/Evaluate/integer.h
+++ b/flang/include/flang/Evaluate/integer.h
@@ -74,6 +74,7 @@ class Integer {
static_assert(std::is_unsigned_v<BigPart>);
static_assert(CHAR_BIT * sizeof(BigPart) >= 2 * partBits);
static constexpr bool littleEndian{IS_LITTLE_ENDIAN};
+ static constexpr int alignment{ALIGNMENT};
private:
static constexpr int maxPartBits{CHAR_BIT * sizeof(Part)};
diff --git a/flang/include/flang/Evaluate/real.h b/flang/include/flang/Evaluate/real.h
index 03294881850a1..fa74792a0377f 100644
--- a/flang/include/flang/Evaluate/real.h
+++ b/flang/include/flang/Evaluate/real.h
@@ -490,7 +490,7 @@ template <typename WORD, int PREC> class Real {
bool isNegative, int exponent, const Fraction &, Rounding, RoundingBits,
bool multiply = false);
- Word word_{}; // an Integer<>
+ alignas(Word::alignment / 8) Word word_{}; // an Integer<>
};
extern template class Real<Integer<16>, 11>; // IEEE half format
>From 43102f9b97cc055124b7e0e730cecf71fecdc29a Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Thu, 17 Jul 2025 15:43:08 -0400
Subject: [PATCH 2/3] Added comment
---
flang/include/flang/Evaluate/real.h | 3 +++
1 file changed, 3 insertions(+)
diff --git a/flang/include/flang/Evaluate/real.h b/flang/include/flang/Evaluate/real.h
index fa74792a0377f..af17adc8ad8a3 100644
--- a/flang/include/flang/Evaluate/real.h
+++ b/flang/include/flang/Evaluate/real.h
@@ -490,6 +490,9 @@ template <typename WORD, int PREC> class Real {
bool isNegative, int exponent, const Fraction &, Rounding, RoundingBits,
bool multiply = false);
+ // Require alignment, in case code generation code on x86_64 decides that
+ // our Real object is suitable for SSE2 instructions and then gets surprised
+ // by unaligned address.
alignas(Word::alignment / 8) Word word_{}; // an Integer<>
};
>From 26432b55e826ea49030252b43714baf26afa36ac Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Thu, 17 Jul 2025 19:15:09 -0400
Subject: [PATCH 3/3] Comment update
---
flang/include/flang/Evaluate/real.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/flang/include/flang/Evaluate/real.h b/flang/include/flang/Evaluate/real.h
index af17adc8ad8a3..76d25d9fe2670 100644
--- a/flang/include/flang/Evaluate/real.h
+++ b/flang/include/flang/Evaluate/real.h
@@ -490,8 +490,8 @@ template <typename WORD, int PREC> class Real {
bool isNegative, int exponent, const Fraction &, Rounding, RoundingBits,
bool multiply = false);
- // Require alignment, in case code generation code on x86_64 decides that
- // our Real object is suitable for SSE2 instructions and then gets surprised
+ // Require alignment, in case code generation on x86_64 decides that our
+ // Real object is suitable for SSE2 instructions and then gets surprised
// by unaligned address.
alignas(Word::alignment / 8) Word word_{}; // an Integer<>
};
More information about the flang-commits
mailing list