Skip to content

Commit 2535bf5

Browse files
koparasylanza
authored andcommitted
[CIR][HIP] Compile HIP device code (#1322)
Depends on #1319
1 parent 6b167f8 commit 2535bf5

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1651,7 +1651,8 @@ static void getTrivialDefaultFunctionAttributes(
16511651
// AFAIK, neither of them support exceptions in device code.
16521652
if (langOpts.SYCLIsDevice)
16531653
llvm_unreachable("NYI");
1654-
if (langOpts.OpenCL || (langOpts.CUDA && langOpts.CUDAIsDevice)) {
1654+
if (langOpts.OpenCL ||
1655+
((langOpts.CUDA || langOpts.HIP) && langOpts.CUDAIsDevice)) {
16551656
auto noThrow = cir::NoThrowAttr::get(CGM.getBuilder().getContext());
16561657
funcAttrs.set(noThrow.getMnemonic(), noThrow);
16571658
}

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3112,7 +3112,7 @@ void CIRGenModule::emitDeferred(unsigned recursionLimit) {
31123112
// Emit CUDA/HIP static device variables referenced by host code only. Note we
31133113
// should not clear CUDADeviceVarODRUsedByHost since it is still needed for
31143114
// further handling.
3115-
if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
3115+
if ((getLangOpts().CUDA || getLangOpts().HIP) && getLangOpts().CUDAIsDevice &&
31163116
!getASTContext().CUDADeviceVarODRUsedByHost.empty()) {
31173117
llvm_unreachable("NYI");
31183118
}

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,11 @@ mlir::Type CIRGenTypes::convertType(QualType T) {
348348

349349
// For the device-side compilation, CUDA device builtin surface/texture types
350350
// may be represented in different types.
351-
if (astContext.getLangOpts().CUDAIsDevice) {
351+
// NOTE: CUDAIsDevice is true when building also HIP code.
352+
// 1. There is no SurfaceType on HIP,
353+
// 2. There is Texture memory on HIP but accessing the memory goes through
354+
// calls to the runtime. e.g. for a 2D: `tex2D<float>(tex, x, y);`
355+
if (astContext.getLangOpts().CUDA && astContext.getLangOpts().CUDAIsDevice) {
352356
if (Ty->isCUDADeviceBuiltinSurfaceType() ||
353357
Ty->isCUDADeviceBuiltinTextureType())
354358
llvm_unreachable("NYI");
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "../Inputs/cuda.h"
2+
3+
// RUN: %clang_cc1 -triple=amdgcn-amd-amdhsa -x hip -fcuda-is-device \
4+
// RUN: -fclangir -emit-cir -o - %s | FileCheck %s
5+
6+
// This shouldn't emit.
7+
__host__ void host_fn(int *a, int *b, int *c) {}
8+
9+
// CHECK-NOT: cir.func @_Z7host_fnPiS_S_
10+
11+
// This should emit as a normal C++ function.
12+
__device__ void device_fn(int* a, double b, float c) {}
13+
14+
// CIR: cir.func @_Z9device_fnPidf

0 commit comments

Comments
 (0)