Skip to content

Commit f9ebba1

Browse files
Andres-Salamancakrishna2803
authored andcommitted
[CIR] Fix outdated bitfield iteration logic in accumulateFields (llvm#151741)
This PR fixes the outdated logic for accumulating bitfields in `accumulateFields`. The old approach remained after the algorithm was updated. A non-bitfield member would act as a barrier, causing `accumulateBitFields` to receive an incomplete range of fields. As a result, it failed to accumulate them properly when clipping was necessary. For reference, in ClangIR we already handle this correctly: [https://github.com/llvm/clangir/blob/b647f4b97b1f936fd7700ec0fd0d896a12fe581b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp#L711-L714](https://github.com/llvm/clangir/blob/b647f4b97b1f936fd7700ec0fd0d896a12fe581b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp#L711-L714)
1 parent bddbf1a commit f9ebba1

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

clang/lib/CIR/CodeGen/CIRGenRecordLayoutBuilder.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,7 @@ void CIRRecordLowering::accumulateFields() {
501501
fieldEnd = recordDecl->field_end();
502502
field != fieldEnd;) {
503503
if (field->isBitField()) {
504-
RecordDecl::field_iterator start = field;
505-
// Iterate to gather the list of bitfields.
506-
for (++field; field != fieldEnd && field->isBitField(); ++field)
507-
;
508-
field = accumulateBitFields(start, field);
504+
field = accumulateBitFields(field, fieldEnd);
509505
assert((field == fieldEnd || !field->isBitField()) &&
510506
"Failed to accumulate all the bitfields");
511507
} else if (!field->isZeroSize(astContext)) {

clang/test/CIR/CodeGen/bitfields.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,23 @@ typedef struct {
7171
// LLVM-DAG: %struct.U = type <{ i8, i8, i8, i8, i64 }>
7272
// OGCG-DAG: %struct.U = type <{ i8, i8, i8, i8, i64 }>
7373

74+
typedef struct{
75+
int a : 24;
76+
char b;
77+
int c: 30;
78+
} Clip;
79+
80+
// CIR-DAG: !rec_Clip = !cir.record<struct "Clip" {!cir.array<!u8i x 3>, !s8i, !u32i}>
81+
// LLVM-DAG: %struct.Clip = type { [3 x i8], i8, i32 }
82+
// OGCG-DAG: %struct.Clip = type { [3 x i8], i8, i32 }
83+
7484
void def() {
7585
A a;
7686
D d;
7787
S s;
7888
T t;
7989
U u;
90+
Clip c;
8091
}
8192

8293
int load_field(S* s) {

0 commit comments

Comments
 (0)