Skip to content

Commit db60244

Browse files
authored
[PCH, CUDA] Take CUDA attributes into account (llvm#125127)
During deserialization of CUDA AST we must consider CUDA target attributes to distinguish overloads from redeclarations. Fixes llvm#106394
1 parent 9fbd5fb commit db60244

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clang/lib/AST/ASTContext.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7224,6 +7224,16 @@ static bool isSameQualifier(const NestedNameSpecifier *X,
72247224
return !PX && !PY;
72257225
}
72267226

7227+
static bool hasSameCudaAttrs(const FunctionDecl *A, const FunctionDecl *B) {
7228+
if (!A->getASTContext().getLangOpts().CUDA)
7229+
return true; // Target attributes are overloadable in CUDA compilation only.
7230+
if (A->hasAttr<CUDADeviceAttr>() != B->hasAttr<CUDADeviceAttr>())
7231+
return false;
7232+
if (A->hasAttr<CUDADeviceAttr>() && B->hasAttr<CUDADeviceAttr>())
7233+
return A->hasAttr<CUDAHostAttr>() == B->hasAttr<CUDAHostAttr>();
7234+
return true; // unattributed and __host__ functions are the same.
7235+
}
7236+
72277237
/// Determine whether the attributes we can overload on are identical for A and
72287238
/// B. Will ignore any overloadable attrs represented in the type of A and B.
72297239
static bool hasSameOverloadableAttrs(const FunctionDecl *A,
@@ -7254,7 +7264,7 @@ static bool hasSameOverloadableAttrs(const FunctionDecl *A,
72547264
if (Cand1ID != Cand2ID)
72557265
return false;
72567266
}
7257-
return true;
7267+
return hasSameCudaAttrs(A, B);
72587268
}
72597269

72607270
bool ASTContext::isSameEntity(const NamedDecl *X, const NamedDecl *Y) const {

clang/test/PCH/cuda-kernel-call.cu

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// RUN: %clang_cc1 -emit-pch -o %t %s
22
// RUN: %clang_cc1 -include-pch %t -fsyntax-only %s
3+
// RUN: %clang_cc1 -emit-pch -fcuda-is-device -o %t-device %s
4+
// RUN: %clang_cc1 -fcuda-is-device -include-pch %t-device -fsyntax-only %s
35

46
#ifndef HEADER
57
#define HEADER
@@ -14,12 +16,21 @@ void kcall(void (*kp)()) {
1416
__global__ void kern() {
1517
}
1618

19+
// Make sure that target overloaded functions remain
20+
// available as overloads after PCH deserialization.
21+
__host__ int overloaded_func();
22+
__device__ int overloaded_func();
23+
1724
#else
1825
// Using the header.
1926

2027
void test() {
2128
kcall(kern);
2229
kern<<<1, 1>>>();
30+
overloaded_func();
2331
}
2432

33+
__device__ void test () {
34+
overloaded_func();
35+
}
2536
#endif

0 commit comments

Comments
 (0)