Skip to content

Commit b5fb765

Browse files
authored
[CIR][CUDA] Fix CUDA CIR mangling bug (#1396)
Includes function name while mangling C functions to avoid link errors. [This is the same way OG handles it](https://github.com/llvm/clangir/blob/c301b4a0d3d2d79b26c9c809c11b8a1137c0a9ec/clang/lib/CodeGen/CodeGenModule.cpp#L1896).
1 parent af9a39d commit b5fb765

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2428,7 +2428,7 @@ static std::string getMangledNameImpl(CIRGenModule &CGM, GlobalDecl GD,
24282428
assert(0 && "NYI");
24292429
} else if (FD && FD->hasAttr<CUDAGlobalAttr>() &&
24302430
GD.getKernelReferenceKind() == KernelReferenceKind::Stub) {
2431-
Out << "__device_stub__";
2431+
Out << "__device_stub__" << II->getName();
24322432
} else {
24332433
Out << II->getName();
24342434
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "../Inputs/cuda.h"
2+
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \
4+
// RUN: -x cuda -emit-cir -target-sdk-version=12.3 \
5+
// RUN: %s -o %t.cir
6+
// RUN: FileCheck --check-prefix=CIR-HOST --input-file=%t.cir %s
7+
8+
// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -fclangir \
9+
// RUN: -fcuda-is-device -emit-cir -target-sdk-version=12.3 \
10+
// RUN: %s -o %t.cir
11+
// RUN: FileCheck --check-prefix=CIR-DEVICE --input-file=%t.cir %s
12+
13+
namespace ns {
14+
__global__ void cpp_global_function_1(int a, int* b, float c) {}
15+
// CIR-HOST: cir.func @_ZN2ns36__device_stub__cpp_global_function_1EiPif
16+
// CIR-DEVICE: cir.func @_ZN2ns21cpp_global_function_1EiPif
17+
18+
__global__ void cpp_global_function_2(int a, int* b, float c) {}
19+
20+
// CIR-HOST: cir.func @_ZN2ns36__device_stub__cpp_global_function_2EiPif
21+
// CIR-DEVICE: cir.func @_ZN2ns21cpp_global_function_2EiPif
22+
23+
__host__ void cpp_host_function_1(int a, int* b, float c) {}
24+
25+
// CIR-HOST: cir.func @_ZN2ns19cpp_host_function_1EiPif
26+
27+
__host__ void cpp_host_function_2(int a, int* b, float c) {}
28+
29+
// CIR-HOST: cir.func @_ZN2ns19cpp_host_function_2EiPif
30+
31+
__device__ void cpp_device_function_1(int a, int* b, float c) {}
32+
33+
// CIR-DEVICE: cir.func @_ZN2ns21cpp_device_function_1EiPif
34+
35+
__device__ void cpp_device_function_2(int a, int* b, float c) {}
36+
37+
// CIR-DEVICE: cir.func @_ZN2ns21cpp_device_function_2EiPif
38+
}
39+
40+
__global__ void cpp_global_function_1(int a, int* b, float c) {}
41+
42+
// CIR-HOST: cir.func @_Z36__device_stub__cpp_global_function_1iPif
43+
// CIR-DEVICE: cir.func @_Z21cpp_global_function_1iPif
44+
45+
__global__ void cpp_global_function_2(int a, int* b, float c) {}
46+
47+
// CIR-HOST: cir.func @_Z36__device_stub__cpp_global_function_2iPif
48+
// CIR-DEVICE: cir.func @_Z21cpp_global_function_2iPif
49+
50+
__host__ void cpp_host_function_1(int a, int* b, float c) {}
51+
52+
// CIR-HOST: cir.func @_Z19cpp_host_function_1iPif
53+
54+
__host__ void cpp_host_function_2(int a, int* b, float c) {}
55+
56+
// CIR-HOST: cir.func @_Z19cpp_host_function_2iPif
57+
58+
__device__ void cpp_device_function_1(int a, int* b, float c) {}
59+
60+
// CIR-DEVICE: cir.func @_Z21cpp_device_function_1iPif
61+
62+
__device__ void cpp_device_function_2(int a, int* b, float c) {}
63+
64+
// CIR-DEVICE: cir.func @_Z21cpp_device_function_2iPif
65+
66+
extern "C" {
67+
__global__ void c_global_function_1(int a, int* b, float c) {}
68+
69+
// CIR-HOST: cir.func @__device_stub__c_global_function_1
70+
// CIR-DEVICE: cir.func @c_global_function_1
71+
72+
__global__ void c_global_function_2(int a, int* b, float c) {}
73+
74+
// CIR-HOST: cir.func @__device_stub__c_global_function_2
75+
// CIR-DEVICE: cir.func @c_global_function_2
76+
77+
__host__ void c_host_function_1(int a, int* b, float c) {}
78+
79+
// CIR-HOST: cir.func @c_host_function_1
80+
81+
__host__ void c_host_function_2(int a, int* b, float c) {}
82+
83+
// CIR-HOST: cir.func @c_host_function_2
84+
85+
__device__ void c_device_function_1(int a, int* b, float c) {}
86+
87+
// CIR-DEVICE: cir.func @c_device_function_1
88+
89+
__device__ void c_device_function_2(int a, int* b, float c) {}
90+
91+
// CIR-DEVICE: cir.func @c_device_function_2
92+
}

0 commit comments

Comments
 (0)