Skip to content

Commit 5629249

Browse files
authored
[CUDA] Emit used function list in deterministic order. (#102661)
Fixes #101560
1 parent c4724f6 commit 5629249

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "llvm/ADT/MapVector.h"
3535
#include "llvm/ADT/PointerIntPair.h"
3636
#include "llvm/ADT/PointerUnion.h"
37+
#include "llvm/ADT/SetVector.h"
3738
#include "llvm/ADT/SmallVector.h"
3839
#include "llvm/ADT/StringMap.h"
3940
#include "llvm/ADT/StringRef.h"
@@ -1194,8 +1195,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
11941195
llvm::DenseSet<const VarDecl *> CUDADeviceVarODRUsedByHost;
11951196

11961197
/// Keep track of CUDA/HIP external kernels or device variables ODR-used by
1197-
/// host code.
1198-
llvm::DenseSet<const ValueDecl *> CUDAExternalDeviceDeclODRUsedByHost;
1198+
/// host code. SetVector is used to maintain the order.
1199+
llvm::SetVector<const ValueDecl *> CUDAExternalDeviceDeclODRUsedByHost;
11991200

12001201
/// Keep track of CUDA/HIP implicit host device functions used on device side
12011202
/// in device compilation.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fcuda-is-device -x hip %s \
2+
// RUN: -fgpu-rdc -std=c++11 -emit-llvm -o - -target-cpu gfx906 | FileCheck %s
3+
4+
#include "Inputs/cuda.h"
5+
6+
// CHECK-LABEL: @__clang_gpu_used_external = internal {{.*}}global
7+
// References to the kernels must be in order of appearance.
8+
// CHECK-SAME: [ptr @_Z6kernelILi3EEvPi, ptr @_Z6kernelILi1EEvPi, ptr @_Z6kernelILi2EEvPi, ptr @_Z6kernelILi0EEvPi]
9+
10+
template <int N>
11+
__global__ void kernel(int* out) { *out = N; }
12+
13+
void host(int n) {
14+
void * k;
15+
switch (n) {
16+
case 3: k = (void*)&kernel<3>; break;
17+
case 1: k = (void*)&kernel<1>; break;
18+
case 2: k = (void*)&kernel<2>; break;
19+
case 0: k = (void*)&kernel<0>; break;
20+
}
21+
}

0 commit comments

Comments
 (0)