Skip to content

Commit f4d31cd

Browse files
[CIR] Add bitfield offset calculation for big-endian targets (#145067)
This PR updates the bitfield offset calculation to correctly handle big-endian architectures.
1 parent e6f98ff commit f4d31cd

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

clang/include/clang/CIR/MissingFeatures.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ struct MissingFeatures {
150150
static bool cxxabiAppleARM64CXXABI() { return false; }
151151
static bool cxxabiStructorImplicitParam() { return false; }
152152
static bool isDiscreteBitFieldABI() { return false; }
153-
static bool isBigEndian() { return false; }
154153

155154
// Address class
156155
static bool addressOffset() { return false; }

clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ void CIRRecordLowering::setBitFieldInfo(const FieldDecl *fd,
228228
// out as packed bits within an integer-sized unit, we can imagine the bits
229229
// counting from the most-significant-bit instead of the
230230
// least-significant-bit.
231-
assert(!cir::MissingFeatures::isBigEndian());
231+
if (dataLayout.isBigEndian())
232+
info.offset = info.storageSize - (info.offset + info.size);
232233

233234
info.volatileStorageSize = 0;
234235
info.volatileOffset = 0;

clang/test/CIR/CodeGen/bitfields_be.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -triple aarch64_be-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2+
// RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
3+
// RUN: %clang_cc1 -triple aarch64_be-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4+
// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM
5+
// RUN: %clang_cc1 -triple aarch64_be-unknown-linux-gnu -emit-llvm %s -o %t.ll
6+
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
7+
8+
typedef struct {
9+
int a : 4;
10+
int b : 11;
11+
int c : 17;
12+
} S;
13+
S s;
14+
15+
// CIR: !rec_S = !cir.record<struct "S" {!u32i}>
16+
// LLVM: %struct.S = type { i32 }
17+
// OGCG: %struct.S = type { i32 }

0 commit comments

Comments
 (0)