Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][HIP] Compile HIP device code #1322

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,8 @@ static void getTrivialDefaultFunctionAttributes(
// AFAIK, neither of them support exceptions in device code.
if (langOpts.SYCLIsDevice)
llvm_unreachable("NYI");
if (langOpts.OpenCL || (langOpts.CUDA && langOpts.CUDAIsDevice)) {
if (langOpts.OpenCL ||
((langOpts.CUDA || langOpts.HIP) && langOpts.CUDAIsDevice)) {
auto noThrow = cir::NoThrowAttr::get(CGM.getBuilder().getContext());
funcAttrs.set(noThrow.getMnemonic(), noThrow);
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3114,7 +3114,7 @@ void CIRGenModule::emitDeferred(unsigned recursionLimit) {
// Emit CUDA/HIP static device variables referenced by host code only. Note we
// should not clear CUDADeviceVarODRUsedByHost since it is still needed for
// further handling.
if (getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
if ((getLangOpts().CUDA || getLangOpts().HIP) && getLangOpts().CUDAIsDevice &&
!getASTContext().CUDADeviceVarODRUsedByHost.empty()) {
llvm_unreachable("NYI");
}
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ mlir::Type CIRGenTypes::convertType(QualType T) {

// For the device-side compilation, CUDA device builtin surface/texture types
// may be represented in different types.
if (astContext.getLangOpts().CUDAIsDevice) {
// NOTE: CUDAIsDevice is true when building also HIP code.
// 1. There is no SurfaceType on HIP,
// 2. There is Texture memory on HIP but accessing the memory goes through
// calls to the runtime. e.g. for a 2D: `tex2D<float>(tex, x, y);`
if (astContext.getLangOpts().CUDA && astContext.getLangOpts().CUDAIsDevice) {
if (Ty->isCUDADeviceBuiltinSurfaceType() ||
Ty->isCUDADeviceBuiltinTextureType())
llvm_unreachable("NYI");
Expand Down
14 changes: 14 additions & 0 deletions clang/test/CIR/CodeGen/HIP/simple-device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "../Inputs/cuda.h"

// RUN: %clang_cc1 -triple=amdgcn-amd-amdhsa -x hip -fcuda-is-device \
// RUN: -fclangir -emit-cir -o - %s | FileCheck %s

// This shouldn't emit.
__host__ void host_fn(int *a, int *b, int *c) {}

// CHECK-NOT: cir.func @_Z7host_fnPiS_S_

// This should emit as a normal C++ function.
__device__ void device_fn(int* a, double b, float c) {}

// CIR: cir.func @_Z9device_fnPidf