Skip to content

Commit a61adcc

Browse files
committed
[CUDA][HIP] Fix record layout on Windows
On windows, record layout should be consistent with host side, otherwise host code is no able to access fields of the record correctly. Fixes: #51031 Fixes: SWDEV-446010
1 parent 5fd9bab commit a61adcc

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

clang/lib/AST/RecordLayoutBuilder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,6 +2458,11 @@ static bool mustSkipTailPadding(TargetCXXABI ABI, const CXXRecordDecl *RD) {
24582458
}
24592459

24602460
static bool isMsLayout(const ASTContext &Context) {
2461+
// Check if it's CUDA device compilation; ensure layout consistency with host.
2462+
if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice &&
2463+
Context.getAuxTargetInfo())
2464+
return Context.getAuxTargetInfo()->getCXXABI().isMicrosoft();
2465+
24612466
return Context.getTargetInfo().getCXXABI().isMicrosoft();
24622467
}
24632468

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -fdump-record-layouts \
2+
// RUN: -emit-llvm -o %t -xhip %s 2>&1 | FileCheck %s --check-prefix=AST
3+
// RUN: cat %t | FileCheck %s
4+
// RUN: %clang_cc1 -fcuda-is-device -triple amdgcn-amd-amdhsa -target-cpu gfx1100 \
5+
// RUN: -emit-llvm -fdump-record-layouts -aux-triple x86_64-pc-windows-msvc \
6+
// RUN: -o %t -xhip %s | FileCheck %s --check-prefix=AST
7+
// RUN: cat %t | FileCheck %s
8+
9+
#include "Inputs/cuda.h"
10+
11+
// AST: *** Dumping AST Record Layout
12+
// AST-LABEL: 0 | struct C
13+
// AST-NEXT: 0 | struct A (base) (empty)
14+
// AST-NEXT: 1 | struct B (base) (empty)
15+
// AST-NEXT: 4 | int i
16+
// AST-NEXT: | [sizeof=8, align=4,
17+
// AST-NEXT: | nvsize=8, nvalign=4]
18+
19+
// CHECK: %struct.C = type { [4 x i8], i32 }
20+
21+
struct A {};
22+
struct B {};
23+
struct C : A, B {
24+
int i;
25+
};
26+
27+
__device__ C c;
28+
__global__ void test_C(C c)
29+
{}
30+
31+
// AST: *** Dumping AST Record Layout
32+
// AST-LABEL: 0 | struct I
33+
// AST-NEXT: 0 | (I vftable pointer)
34+
// AST-NEXT: 8 | int i
35+
// AST-NEXT: | [sizeof=16, align=8,
36+
// AST-NEXT: | nvsize=16, nvalign=8]
37+
38+
// AST: *** Dumping AST Record Layout
39+
// AST-LABEL: 0 | struct J
40+
// AST-NEXT: 0 | struct I (primary base)
41+
// AST-NEXT: 0 | (I vftable pointer)
42+
// AST-NEXT: 8 | int i
43+
// AST-NEXT: 16 | int j
44+
// AST-NEXT: | [sizeof=24, align=8,
45+
// AST-NEXT: | nvsize=24, nvalign=8]
46+
47+
// CHECK: %struct.J = type { %struct.I, i32 }
48+
// CHECK: %struct.I = type { ptr, i32 }
49+
50+
struct I {
51+
virtual void f() = 0;
52+
int i;
53+
};
54+
struct J : I {
55+
void f() override {}
56+
int j;
57+
};
58+
59+
__global__ void test_J(J j)
60+
{}
61+
62+
void test(C c, J j) {
63+
test_C<<<1, 1>>>(c);
64+
test_J<<<1, 1>>>(j);
65+
}

0 commit comments

Comments
 (0)