Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,23 @@ bool ConstantAggregateBuilder::add(mlir::TypedAttr typedAttr, CharUnits offset,
}

// Uncommon case: constant overlaps what we've already created.
cgm.errorNYI("overlapping constants");
return false;
std::optional<size_t> firstElemToReplace = splitAt(offset);
if (!firstElemToReplace)
return false;

CharUnits cSize = getSize(typedAttr);
std::optional<size_t> lastElemToReplace = splitAt(offset + cSize);
if (!lastElemToReplace)
return false;

assert((firstElemToReplace == lastElemToReplace || allowOverwrite) &&
"unexpectedly overwriting field");

Element newElt(typedAttr, offset);
replace(elements, *firstElemToReplace, *lastElemToReplace, {newElt});
size = std::max(size, offset + cSize);
naturalLayout = false;
return true;
}

bool ConstantAggregateBuilder::addBits(llvm::APInt bits, uint64_t offsetInBits,
Expand Down
15 changes: 15 additions & 0 deletions clang/test/CIR/CodeGen/struct-init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s

struct BitfieldStruct {
unsigned int a:4;
unsigned int b:14;
unsigned int c:14;
};

BitfieldStruct overlapping_init = { 3, 2, 1 };

// This is unintuitive. The bitfields are initialized using a struct of constants
// that maps to the bitfields but splits the value into bytes.

// CIR: cir.global external @overlapping_init = #cir.const_record<{#cir.int<35> : !u8i, #cir.int<0> : !u8i, #cir.int<4> : !u8i, #cir.int<0> : !u8i}> : !rec_anon_struct
// LLVM: @overlapping_init = global { i8, i8, i8, i8 } { i8 35, i8 0, i8 4, i8 0 }
// OGCG: @overlapping_init = global { i8, i8, i8, i8 } { i8 35, i8 0, i8 4, i8 0 }

struct S {
int a, b, c;
};
Expand Down
Loading