Skip to content

Commit

Permalink
Fix tbaa.struct metadata for bitfields using big endian. (llvm#87753)
Browse files Browse the repository at this point in the history
When generating tbaa.struct metadata we treat multiple adjacent
bitfields as a single "field", with one corresponding entry in the
metadata. At the moment this is achieved by adding an entry for the
first bitfield in the run using its StorageSize and skipping the
remaining bitfields. The problem is that "first" is determined by
checking that the Offset of the field in the run is 0, which breaks for
big endian.

PR: llvm#87753
  • Loading branch information
juliannagele authored Apr 5, 2024
1 parent 3c3e0e5 commit f905935
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 8 additions & 1 deletion clang/lib/CodeGen/CodeGenTBAA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "clang/AST/Mangle.h"
#include "clang/AST/RecordLayout.h"
#include "clang/Basic/CodeGenOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/LLVMContext.h"
Expand Down Expand Up @@ -319,7 +320,13 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
// base type.
if ((*i)->isBitField()) {
const CGBitFieldInfo &Info = CGRL.getBitFieldInfo(*i);
if (Info.Offset != 0)
// For big endian targets the first bitfield in the consecutive run is
// at the most-significant end; see CGRecordLowering::setBitFieldInfo
// for more information.
bool IsBE = Context.getTargetInfo().isBigEndian();
bool IsFirst = IsBE ? Info.StorageSize - (Info.Offset + Info.Size) == 0
: Info.Offset == 0;
if (!IsFirst)
continue;
unsigned CurrentBitFieldSize = Info.StorageSize;
uint64_t Size =
Expand Down
10 changes: 3 additions & 7 deletions clang/test/CodeGen/tbaa-struct-bitfield-endianness.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// RUN: %clang_cc1 -triple aarch64_be-apple-darwin -emit-llvm -o - -O1 %s | \
// RUN: FileCheck -check-prefixes=CHECK,CHECK-BE %s
// RUN: FileCheck -check-prefixes=CHECK %s
// RUN: %clang_cc1 -triple aarch64-apple-darwin -emit-llvm -o - -O1 %s | \
// RUN: FileCheck -check-prefixes=CHECK,CHECK-LE %s
// RUN: FileCheck -check-prefixes=CHECK %s
//
// Check that TBAA metadata for structs containing bitfields is
// consistent between big and little endian layouts.
//
// FIXME: The metadata below is invalid for the big endian layout: the
// start offset of 2 is incorrect.

struct NamedBitfields {
int f1 : 8;
Expand All @@ -28,8 +25,7 @@ void copy(NamedBitfields *a1, NamedBitfields *a2) {
*a1 = *a2;
}

// CHECK-BE: [[TBAA_STRUCT2]] = !{i64 2, i64 4, [[META3:![0-9]+]], i64 4, i64 4, [[META6:![0-9]+]], i64 8, i64 8, [[META8:![0-9]+]]}
// CHECK-LE: [[TBAA_STRUCT2]] = !{i64 0, i64 4, [[META3:![0-9]+]], i64 4, i64 4, [[META6:![0-9]+]], i64 8, i64 8, [[META8:![0-9]+]]}
// CHECK: [[TBAA_STRUCT2]] = !{i64 0, i64 4, [[META3:![0-9]+]], i64 4, i64 4, [[META6:![0-9]+]], i64 8, i64 8, [[META8:![0-9]+]]}
// CHECK: [[META3]] = !{[[META4:![0-9]+]], [[META4]], i64 0}
// CHECK: [[META4]] = !{!"omnipotent char", [[META5:![0-9]+]], i64 0}
// CHECK: [[META5]] = !{!"Simple C++ TBAA"}
Expand Down

0 comments on commit f905935

Please sign in to comment.