[clang] [CIR] Add bitfield offset calculation for big-endian targets (PR #145067)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 20 09:29:49 PDT 2025


https://github.com/Andres-Salamanca created https://github.com/llvm/llvm-project/pull/145067

This PR updates the bitfield offset calculation to correctly handle big-endian architectures.

>From b67d32a0d888096d748f855ce2335b230aedaf0f Mon Sep 17 00:00:00 2001
From: Andres Salamanca <andrealebarbaritos at gmail.com>
Date: Fri, 20 Jun 2025 11:23:50 -0500
Subject: [PATCH] Add bitfield offset calculation for big-endian targets

---
 clang/include/clang/CIR/MissingFeatures.h       |  1 -
 .../CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp   |  3 ++-
 clang/test/CIR/CodeGen/bitfields_be.c           | 17 +++++++++++++++++
 3 files changed, 19 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CIR/CodeGen/bitfields_be.c

diff --git a/clang/include/clang/CIR/MissingFeatures.h b/clang/include/clang/CIR/MissingFeatures.h
index e0b2959f374f8..d8e45d02cd2a0 100644
--- a/clang/include/clang/CIR/MissingFeatures.h
+++ b/clang/include/clang/CIR/MissingFeatures.h
@@ -150,7 +150,6 @@ struct MissingFeatures {
   static bool cxxabiAppleARM64CXXABI() { return false; }
   static bool cxxabiStructorImplicitParam() { return false; }
   static bool isDiscreteBitFieldABI() { return false; }
-  static bool isBigEndian() { return false; }
 
   // Address class
   static bool addressOffset() { return false; }
diff --git a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
index 8dbf1b36a93b2..d2fb4b711b0c2 100644
--- a/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp
@@ -228,7 +228,8 @@ void CIRRecordLowering::setBitFieldInfo(const FieldDecl *fd,
   // out as packed bits within an integer-sized unit, we can imagine the bits
   // counting from the most-significant-bit instead of the
   // least-significant-bit.
-  assert(!cir::MissingFeatures::isBigEndian());
+  if (dataLayout.isBigEndian())
+    info.offset = info.storageSize - (info.offset + info.size);
 
   info.volatileStorageSize = 0;
   info.volatileOffset = 0;
diff --git a/clang/test/CIR/CodeGen/bitfields_be.c b/clang/test/CIR/CodeGen/bitfields_be.c
new file mode 100644
index 0000000000000..149e9c9ac33ff
--- /dev/null
+++ b/clang/test/CIR/CodeGen/bitfields_be.c
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1  -triple aarch64_be-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
+// RUN: %clang_cc1  -triple aarch64_be-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
+// RUN: %clang_cc1  -triple aarch64_be-unknown-linux-gnu -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
+
+typedef struct {
+    int a : 4;
+    int b : 11;
+    int c : 17;
+} S;
+S s;
+
+// CIR:  !rec_S = !cir.record<struct "S" {!u32i}>
+// LLVM: %struct.S = type { i32 }
+// OGCG: %struct.S = type { i32 }



More information about the cfe-commits mailing list