Skip to content

Commit db307ce

Browse files
authored
[CIR][HIP] Use CUDA attributes for HIP global functions (#1333)
1 parent 090718c commit db307ce

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -2357,8 +2357,9 @@ cir::FuncOp CIRGenModule::GetAddrOfFunction(clang::GlobalDecl GD, mlir::Type Ty,
23572357
// As __global__ functions (kernels) always reside on device,
23582358
// when we access them from host, we must refer to the kernel handle.
23592359
// For CUDA, it's just the device stub. For HIP, it's something different.
2360-
if (langOpts.CUDA && !langOpts.CUDAIsDevice && langOpts.HIP &&
2361-
cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>()) {
2360+
if ((langOpts.CUDA || langOpts.HIP) && !langOpts.CUDAIsDevice &&
2361+
cast<FunctionDecl>(GD.getDecl())->hasAttr<CUDAGlobalAttr>() &&
2362+
cast<FunctionDecl>(GD.getDecl())->isThisDeclarationADefinition()) {
23622363
llvm_unreachable("NYI");
23632364
}
23642365

clang/test/CIR/CodeGen/HIP/simple-device.cpp

-14
This file was deleted.

clang/test/CIR/CodeGen/HIP/simple.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
#include "../Inputs/cuda.h"
22

3-
// RUN: %clang_cc1 -triple=amdgcn-amd-amdhsa -x hip -fclangir \
4-
// RUN: -emit-cir %s -o %t.cir
5-
// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s
3+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir \
4+
// RUN: -x hip -emit-cir %s -o %t.cir
5+
// RUN: FileCheck --check-prefix=CIR-HOST --input-file=%t.cir %s
66

7+
// RUN: %clang_cc1 -triple=amdgcn-amd-amdhsa -x hip \
8+
// RUN: -fcuda-is-device -emit-cir %s -o %t.cir
9+
// RUN: FileCheck --check-prefix=CIR-DEVICE --input-file=%t.cir %s
10+
11+
// Attribute for global_fn
12+
// CIR-HOST: [[Kernel:#[a-zA-Z_0-9]+]] = {{.*}}#cir.cuda_kernel_name<_Z9global_fnv>{{.*}}
713

8-
// This should emit as a normal C++ function.
914
__host__ void host_fn(int *a, int *b, int *c) {}
15+
// CIR-HOST: cir.func @_Z7host_fnPiS_S_
16+
// CIR-DEVICE-NOT: cir.func @_Z7host_fnPiS_S_
1017

11-
// CIR: cir.func @_Z7host_fnPiS_S_
18+
__device__ void device_fn(int *a, double b, float c) {}
19+
// CIR-HOST-NOT: cir.func @_Z9device_fnPidf
20+
// CIR-DEVICE: cir.func @_Z9device_fnPidf
1221

13-
// This shouldn't emit.
14-
__device__ void device_fn(int* a, double b, float c) {}
22+
#ifdef __AMDGPU__
23+
__global__ void global_fn() {}
24+
#else
25+
__global__ void global_fn();
26+
#endif
27+
// CIR-HOST: @_Z24__device_stub__global_fnv(){{.*}}extra([[Kernel]])
28+
// CIR-DEVICE: @_Z9global_fnv
1529

16-
// CHECK-NOT: cir.func @_Z9device_fnPidf
30+
// Make sure `global_fn` indeed gets emitted
31+
__host__ void x() { auto v = global_fn; }

0 commit comments

Comments
 (0)