Skip to content

Commit fc438d3

Browse files
authored
[HLSL] avoid unnamed bit fields when dealing with InitLists in HLSL (#159212)
In HLSL Init List code avoid initializing unnamed bitfields and avoid initializing using unnamed bit fields. Add tests. Closes #157922
1 parent d79036c commit fc438d3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3323,7 +3323,8 @@ static void BuildFlattenedTypeList(QualType BaseTy,
33233323

33243324
llvm::SmallVector<QualType, 16> FieldTypes;
33253325
for (const auto *FD : RD->fields())
3326-
FieldTypes.push_back(FD->getType());
3326+
if (!FD->isUnnamedBitField())
3327+
FieldTypes.push_back(FD->getType());
33273328
// Reverse the newly added sub-range.
33283329
std::reverse(FieldTypes.begin(), FieldTypes.end());
33293330
llvm::append_range(WorkList, FieldTypes);
@@ -4158,6 +4159,8 @@ class InitListTransformer {
41584159
while (!RecordDecls.empty()) {
41594160
CXXRecordDecl *RD = RecordDecls.pop_back_val();
41604161
for (auto *FD : RD->fields()) {
4162+
if (FD->isUnnamedBitField())
4163+
continue;
41614164
DeclAccessPair Found = DeclAccessPair::make(FD, FD->getAccess());
41624165
DeclarationNameInfo NameInfo(FD->getDeclName(), E->getBeginLoc());
41634166
ExprResult Res = S.BuildFieldReferenceExpr(
@@ -4207,7 +4210,8 @@ class InitListTransformer {
42074210
while (!RecordDecls.empty()) {
42084211
CXXRecordDecl *RD = RecordDecls.pop_back_val();
42094212
for (auto *FD : RD->fields())
4210-
Inits.push_back(generateInitListsImpl(FD->getType()));
4213+
if (!FD->isUnnamedBitField())
4214+
Inits.push_back(generateInitListsImpl(FD->getType()));
42114215
}
42124216
}
42134217
auto *NewInit = new (Ctx) InitListExpr(Ctx, Inits.front()->getBeginLoc(),

clang/test/CodeGenHLSL/BasicFeatures/InitLists.hlsl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ struct SlicyBits {
4545
int W : 8;
4646
};
4747

48+
struct Unnamed {
49+
int A;
50+
int : 8;
51+
};
52+
4853
// Case 1: Extraneous braces get ignored in literal instantiation.
4954
// CHECK-LABEL: define hidden void @_Z5case1v(
5055
// CHECK-SAME: ptr dead_on_unwind noalias writable sret([[STRUCT_TWOFLOATS:%.*]]) align 1 [[AGG_RESULT:%.*]]) #[[ATTR0:[0-9]+]] {
@@ -959,3 +964,24 @@ int case17Helper(int x) {
959964
void case17() {
960965
int2 X = {case17Helper(0), case17Helper(1)};
961966
}
967+
968+
// InitList with Struct with unnamed bitfield on LHS
969+
// CHECK-LABEL: case18
970+
// CHECK: [[U:%.*]] = alloca %struct.Unnamed, align 1
971+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[U]], ptr align 1 {{.*}}, i32 5, i1 false)
972+
void case18() {
973+
Unnamed U = {1};
974+
}
975+
976+
// InitList with Struct with unnamed bitfield on RHS
977+
// CHECK-LABEL: case19
978+
// CHECK: [[TI:%.*]] = alloca %struct.TwoInts, align 1
979+
// CHECK-NEXT: [[Z:%.*]] = getelementptr inbounds nuw %struct.TwoInts, ptr [[TI]], i32 0, i32 0
980+
// CHECK-NEXT: [[A:%.*]] = getelementptr inbounds nuw %struct.Unnamed, ptr %U, i32 0, i32 0
981+
// CHECK-NEXT: [[L:%.*]] = load i32, ptr [[A]], align 1
982+
// CHECK-NEXT: store i32 [[L]], ptr [[Z]], align 1
983+
// CHECK-NEXT: [[W:%.*]] = getelementptr inbounds nuw %struct.TwoInts, ptr [[TI]], i32 0, i32 1
984+
// CHECK-NEXT: store i32 1, ptr [[W]], align 1
985+
void case19(Unnamed U) {
986+
TwoInts TI = {U, 1};
987+
}

0 commit comments

Comments
 (0)