Skip to content

Commit b150340

Browse files
bognerIanWood1
authored andcommitted
[HLSL] Treat classes and structs as packed by default (llvm#137391)
Fixes llvm#121010.
1 parent 0256344 commit b150340

25 files changed

+361
-367
lines changed

clang/lib/Sema/SemaDecl.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -17567,6 +17567,8 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
1756717567
// parsing of the struct).
1756817568
if (TUK == TagUseKind::Definition &&
1756917569
(!SkipBody || !SkipBody->ShouldSkip)) {
17570+
if (LangOpts.HLSL)
17571+
RD->addAttr(PackedAttr::CreateImplicit(Context));
1757017572
AddAlignmentAttributesForRecord(RD);
1757117573
AddMsStructLayoutForRecord(RD);
1757217574
}
@@ -18257,6 +18259,8 @@ Sema::ActOnTag(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc,
1825718259
// the #pragma tokens are effectively skipped over during the
1825818260
// parsing of the struct).
1825918261
if (TUK == TagUseKind::Definition && (!SkipBody || !SkipBody->ShouldSkip)) {
18262+
if (LangOpts.HLSL)
18263+
RD->addAttr(PackedAttr::CreateImplicit(Context));
1826018264
AddAlignmentAttributesForRecord(RD);
1826118265
AddMsStructLayoutForRecord(RD);
1826218266
}

clang/lib/Sema/SemaTemplate.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,8 @@ DeclResult Sema::CheckClassTemplate(
21162116
// Add alignment attributes if necessary; these attributes are checked when
21172117
// the ASTContext lays out the structure.
21182118
if (TUK == TagUseKind::Definition && (!SkipBody || !SkipBody->ShouldSkip)) {
2119+
if (LangOpts.HLSL)
2120+
NewClass->addAttr(PackedAttr::CreateImplicit(Context));
21192121
AddAlignmentAttributesForRecord(NewClass);
21202122
AddMsStructLayoutForRecord(NewClass);
21212123
}
@@ -8655,6 +8657,8 @@ DeclResult Sema::ActOnClassTemplateSpecialization(
86558657
// Add alignment attributes if necessary; these attributes are checked when
86568658
// the ASTContext lays out the structure.
86578659
if (TUK == TagUseKind::Definition && (!SkipBody || !SkipBody->ShouldSkip)) {
8660+
if (LangOpts.HLSL)
8661+
Specialization->addAttr(PackedAttr::CreateImplicit(Context));
86588662
AddAlignmentAttributesForRecord(Specialization);
86598663
AddMsStructLayoutForRecord(Specialization);
86608664
}

clang/test/AST/HLSL/PackedStruct.hlsl

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -ast-dump %s | FileCheck %s
2+
// Structs are packed by default in HLSL
3+
4+
#include <hlsl/hlsl_basic_types.h>
5+
6+
// CHECK: CXXRecordDecl {{.*}} struct S definition
7+
// CHECK: DefinitionData
8+
// CHECK: PackedAttr {{.*}} Implicit
9+
// CHECK-NEXT: CXXRecordDecl {{.*}} implicit struct S
10+
struct S {
11+
float2 f;
12+
int i;
13+
};

clang/test/CodeGenHLSL/ArrayAssignable.hlsl

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct S {
1010
// CHECK: @c1 = external addrspace(2) global [2 x float], align 4
1111
// CHECK: @c2 = external addrspace(2) global [2 x <4 x i32>], align 16
1212
// CHECK: @c3 = external addrspace(2) global [2 x [2 x i32]], align 4
13-
// CHECK: @c4 = external addrspace(2) global [1 x target("dx.Layout", %S, 8, 0, 4)], align 4
13+
// CHECK: @c4 = external addrspace(2) global [1 x target("dx.Layout", %S, 8, 0, 4)], align 1
1414

1515
cbuffer CBArrays : register(b0) {
1616
float c1[2];
@@ -169,8 +169,8 @@ void arr_assign10() {
169169
}
170170

171171
// CHECK-LABEL: define void {{.*}}arr_assign11
172-
// CHECK: [[C:%.*]] = alloca [1 x %struct.S], align 4
173-
// CHECK: call void @llvm.memcpy.p0.p2.i32(ptr align 4 [[C]], ptr addrspace(2) align 4 @c4, i32 8, i1 false)
172+
// CHECK: [[C:%.*]] = alloca [1 x %struct.S], align 1
173+
// CHECK: call void @llvm.memcpy.p0.p2.i32(ptr align 1 [[C]], ptr addrspace(2) align 1 @c4, i32 8, i1 false)
174174
// CHECK-NEXT: ret void
175175
void arr_assign11() {
176176
S s = {1, 2.0};

clang/test/CodeGenHLSL/ArrayTemporary.hlsl

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ void fn2(Obj O[4]) { }
2424
// CHECK-LABEL: define void {{.*}}call2{{.*}}
2525
// CHECK: [[Arr:%.*]] = alloca [4 x %struct.Obj]
2626
// CHECK: [[Tmp:%.*]] = alloca [4 x %struct.Obj]
27-
// CHECK: call void @llvm.memset.p0.i32(ptr align 4 [[Arr]], i8 0, i32 32, i1 false)
28-
// CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[Tmp]], ptr align 4 [[Arr]], i32 32, i1 false)
29-
// CHECK: call void {{.*}}fn2{{.*}}(ptr noundef byval([4 x %struct.Obj]) align 4 [[Tmp]])
27+
// CHECK: call void @llvm.memset.p0.i32(ptr align 1 [[Arr]], i8 0, i32 32, i1 false)
28+
// CHECK: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[Tmp]], ptr align 1 [[Arr]], i32 32, i1 false)
29+
// CHECK: call void {{.*}}fn2{{.*}}(ptr noundef byval([4 x %struct.Obj]) align 1 [[Tmp]])
3030
void call2() {
3131
Obj Arr[4] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
3232
fn2(Arr);

clang/test/CodeGenHLSL/BasicFeatures/AggregateSplatCast.hlsl

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct S {
5555
// struct splats
5656
// CHECK-LABEL: define void {{.*}}call3
5757
// CHECK: [[A:%.*]] = alloca <1 x i32>, align 4
58-
// CHECK: [[s:%.*]] = alloca %struct.S, align 4
58+
// CHECK: [[s:%.*]] = alloca %struct.S, align 1
5959
// CHECK-NEXT: store <1 x i32> splat (i32 1), ptr [[A]], align 4
6060
// CHECK-NEXT: [[L:%.*]] = load <1 x i32>, ptr [[A]], align 4
6161
// CHECK-NEXT: [[VL:%.*]] = extractelement <1 x i32> [[L]], i32 0
@@ -72,7 +72,7 @@ export void call3() {
7272
// struct splat from vector of length 1
7373
// CHECK-LABEL: define void {{.*}}call5
7474
// CHECK: [[A:%.*]] = alloca <1 x i32>, align 4
75-
// CHECK-NEXT: [[s:%.*]] = alloca %struct.S, align 4
75+
// CHECK-NEXT: [[s:%.*]] = alloca %struct.S, align 1
7676
// CHECK-NEXT: store <1 x i32> splat (i32 1), ptr [[A]], align 4
7777
// CHECK-NEXT: [[L:%.*]] = load <1 x i32>, ptr [[A]], align 4
7878
// CHECK-NEXT: [[VL:%.*]] = extractelement <1 x i32> [[L]], i32 0

clang/test/CodeGenHLSL/BasicFeatures/ArrayElementwiseCast.hlsl

+4-5
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ struct S {
125125

126126
// flatten and truncate from a struct
127127
// CHECK-LABEL: define void {{.*}}call7
128-
// CHECK: [[s:%.*]] = alloca %struct.S, align 4
128+
// CHECK: [[s:%.*]] = alloca %struct.S, align 1
129129
// CHECK-NEXT: [[A:%.*]] = alloca [1 x i32], align 4
130-
// CHECK-NEXT: [[Tmp:%.*]] = alloca %struct.S, align 4
131-
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[s]], ptr align 4 {{.*}}, i32 8, i1 false)
130+
// CHECK-NEXT: [[Tmp:%.*]] = alloca %struct.S, align 1
131+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[s]], ptr align 1 {{.*}}, i32 8, i1 false)
132132
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[A]], ptr align 4 {{.*}}, i32 4, i1 false)
133-
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 4 [[Tmp]], ptr align 4 [[s]], i32 8, i1 false)
133+
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i32(ptr align 1 [[Tmp]], ptr align 1 [[s]], i32 8, i1 false)
134134
// CHECK-NEXT: [[G1:%.*]] = getelementptr inbounds [1 x i32], ptr [[A]], i32 0, i32 0
135135
// CHECK-NEXT: [[G2:%.*]] = getelementptr inbounds %struct.S, ptr [[Tmp]], i32 0, i32 0
136136
// CHECK-NEXT: [[G3:%.*]] = getelementptr inbounds %struct.S, ptr [[Tmp]], i32 0, i32 1
@@ -141,4 +141,3 @@ export void call7() {
141141
int A[1] = {1};
142142
A = (int[1])s;
143143
}
144-

0 commit comments

Comments
 (0)