[llvm-branch-commits] [clang] [Clang] Preserve _BitInt values when clearing padding on big-endian AIX (PR #244578)
Charlie Lin via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Nov 20 04:26:35 PST 2026
https://github.com/clin1234 created https://github.com/llvm/llvm-project/pull/244578
Backport d8217655d8c338927ef37462b561d1a61ac7f64a (#215809) to the release/23.x PaddingClearer in CodeGen. On big-endian targets, the partially occupied byte and the fully occupied bytes can form disjoint intervals. Treating them as one interval can clear value bits.
Add a regression that checks that clearing the padding of a _BitInt(97) preserves its value on 32- and 64-bit AIX and on x86-64 Linux.
Assisted-by: OpenAI Codex
>From 7d97a6d2a5084fbb8a6fb73d2fc22173b17bbe0e Mon Sep 17 00:00:00 2001
From: Charlie Lin <tuug at gmx.us>
Date: Fri, 20 Nov 2026 07:22:02 -0500
Subject: [PATCH] [Clang] Preserve _BitInt values when clearing padding on
big-endian targets
Backport d8217655d8c338927ef37462b561d1a61ac7f64a (#215809) to the
release/23.x PaddingClearer in CodeGen. On big-endian targets, the
partially occupied byte and the fully occupied bytes can form disjoint
intervals. Treating them as one interval can clear value bits.
Add a regression that checks that clearing the padding of a _BitInt(97)
preserves its value on 32- and 64-bit AIX and on x86-64 Linux.
Co-authored-by: Victor Campos <victor.campos at arm.com>
Assisted-by: OpenAI Codex
---
clang/lib/CodeGen/CGBuiltin.cpp | 46 +++++++++++++++++++
.../builtin-clear-padding-bitint-aix.c | 13 ++++++
2 files changed, 59 insertions(+)
create mode 100644 clang/test/CodeGen/builtin-clear-padding-bitint-aix.c
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 37846bbb0b5ec3..1cdf7b11cc2bad 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -2700,6 +2700,11 @@ struct PaddingClearer {
return;
}
+ if (const auto *BITy = D.Ty->getAs<BitIntType>()) {
+ VisitBitInt(BITy, D.StartBitOffset);
+ return;
+ }
+
uint64_t SizeBit = getScalarOccupiedSizeInBits(D.Ty);
OccuppiedIntervals.push_back(
BitInterval{D.StartBitOffset, D.StartBitOffset + SizeBit});
@@ -2800,6 +2805,47 @@ struct PaddingClearer {
BitInterval{StartBitOffset, StartBitOffset + SizeBit});
}
+ /// Compute the occupied bit intervals for a BitInt.
+ ///
+ /// In the case of little endian, the occupied bits are always contiguous so a
+ /// single interval is sufficient. However in big endian, the intervals can be
+ /// disjoint.
+ void VisitBitInt(const BitIntType *Ty, uint64_t StartBitOffset) {
+ const ASTContext &Ctx = CGF.getContext();
+ const uint64_t OccupiedSizeInBits = Ty->getNumBits();
+
+ if (Ctx.getTargetInfo().isLittleEndian()) {
+ OccuppiedIntervals.push_back(
+ {StartBitOffset, StartBitOffset + OccupiedSizeInBits});
+ return;
+ }
+
+ // In big endian mode, the layout of a BitInt in memory has its bytes in
+ // reverse order, and is pictured in this order:
+ // 1. Fully padding bytes.
+ // 2. One partially occupied byte, with padding at the most significant
+ // bits. ("remaining occupied bits")
+ // 3. A sequence of fully occupied bytes up until the end of the storage.
+ const uint64_t StorageSizeInBits = Ctx.getTypeSize(Ty);
+ const uint64_t NumFullyPaddingBytes =
+ (StorageSizeInBits - OccupiedSizeInBits) / CharWidth;
+ const uint64_t NumFullyOccupiedBytes = OccupiedSizeInBits / CharWidth;
+ const uint64_t NumRemainingOccupiedBits = OccupiedSizeInBits % CharWidth;
+
+ // Partially occupied byte
+ if (NumRemainingOccupiedBits > 0)
+ OccuppiedIntervals.push_back(
+ {StartBitOffset + NumFullyPaddingBytes * CharWidth,
+ StartBitOffset + NumFullyPaddingBytes * CharWidth +
+ NumRemainingOccupiedBits});
+
+ // Fully occupied bytes
+ if (NumFullyOccupiedBytes > 0)
+ OccuppiedIntervals.push_back({StartBitOffset + StorageSizeInBits -
+ NumFullyOccupiedBytes * CharWidth,
+ StartBitOffset + StorageSizeInBits});
+ }
+
void MergeOccuppiedIntervals() {
std::sort(OccuppiedIntervals.begin(), OccuppiedIntervals.end(),
[](const BitInterval &lhs, const BitInterval &rhs) {
diff --git a/clang/test/CodeGen/builtin-clear-padding-bitint-aix.c b/clang/test/CodeGen/builtin-clear-padding-bitint-aix.c
new file mode 100644
index 00000000000000..5f17233ceecd0e
--- /dev/null
+++ b/clang/test/CodeGen/builtin-clear-padding-bitint-aix.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -triple powerpc-ibm-aix -O2 -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple powerpc64-ibm-aix -O2 -emit-llvm -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -O2 -emit-llvm -o - %s | FileCheck %s
+
+// CHECK-LABEL: @padding_preserves_37(
+// CHECK: ret i32 1
+int padding_preserves_37(void) {
+ _BitInt(97) value;
+ __builtin_memset(&value, 42, sizeof(value));
+ value = 37;
+ __builtin_clear_padding(&value);
+ return value == 37;
+}
More information about the llvm-branch-commits
mailing list