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][CUDA] Initial support for device compilation #1311

Merged
merged 1 commit into from
Feb 6, 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
4 changes: 2 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1639,9 +1639,9 @@ static void getTrivialDefaultFunctionAttributes(
// TODO: NoThrow attribute should be added for other GPU modes CUDA, SYCL,
// HIP, OpenMP offload.
// AFAIK, neither of them support exceptions in device code.
if ((langOpts.CUDA && langOpts.CUDAIsDevice) || langOpts.SYCLIsDevice)
if (langOpts.SYCLIsDevice)
llvm_unreachable("NYI");
if (langOpts.OpenCL) {
if (langOpts.OpenCL || (langOpts.CUDA && langOpts.CUDAIsDevice)) {
auto noThrow = cir::NoThrowAttr::get(CGM.getBuilder().getContext());
funcAttrs.set(noThrow.getMnemonic(), noThrow);
}
Expand Down
39 changes: 25 additions & 14 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,16 +516,32 @@ void CIRGenModule::emitGlobal(GlobalDecl GD) {
assert(!Global->hasAttr<CPUDispatchAttr>() && "NYI");

if (langOpts.CUDA) {
if (langOpts.CUDAIsDevice)
llvm_unreachable("NYI");
if (langOpts.CUDAIsDevice) {
// This will implicitly mark templates and their
// specializations as __host__ __device__.
if (langOpts.OffloadImplicitHostDeviceTemplates)
llvm_unreachable("NYI");

if (dyn_cast<VarDecl>(Global))
llvm_unreachable("NYI");
// This maps some parallel standard libraries implicitly
// to GPU, even when they are not marked __device__.
if (langOpts.HIPStdPar)
llvm_unreachable("NYI");

// We must skip __device__ functions when compiling for host.
if (!Global->hasAttr<CUDAHostAttr>() && Global->hasAttr<CUDADeviceAttr>()) {
return;
if (Global->hasAttr<CUDAGlobalAttr>())
llvm_unreachable("NYI");

if (!Global->hasAttr<CUDADeviceAttr>())
return;
} else {
// We must skip __device__ functions when compiling for host.
if (!Global->hasAttr<CUDAHostAttr>() &&
Global->hasAttr<CUDADeviceAttr>()) {
return;
}
}

if (dyn_cast<VarDecl>(Global))
llvm_unreachable("NYI");
}

if (langOpts.OpenMP) {
Expand Down Expand Up @@ -2415,8 +2431,6 @@ StringRef CIRGenModule::getMangledName(GlobalDecl GD) {
}
}

assert(!langOpts.CUDAIsDevice && "NYI");

// Keep the first result in the case of a mangling collision.
const auto *ND = cast<NamedDecl>(GD.getDecl());
std::string MangledName = getMangledNameImpl(*this, GD, ND);
Expand Down Expand Up @@ -3099,7 +3113,8 @@ 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().CUDAIsDevice &&
!getASTContext().CUDADeviceVarODRUsedByHost.empty()) {
llvm_unreachable("NYI");
}

Expand Down Expand Up @@ -3392,10 +3407,6 @@ void CIRGenModule::Release() {
llvm_unreachable("NYI");
}

if (langOpts.CUDAIsDevice && getTriple().isNVPTX()) {
llvm_unreachable("NYI");
}

if (langOpts.EHAsynch)
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.
assert(!astContext.getLangOpts().CUDAIsDevice && "not implemented");
if (astContext.getLangOpts().CUDAIsDevice) {
if (Ty->isCUDADeviceBuiltinSurfaceType() ||
Ty->isCUDADeviceBuiltinTextureType())
llvm_unreachable("NYI");
}

if (const auto *recordType = dyn_cast<RecordType>(T))
return convertRecordDeclType(recordType->getDecl());
Expand Down
14 changes: 14 additions & 0 deletions clang/test/CIR/CodeGen/CUDA/simple-device.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "../Inputs/cuda.h"

// RUN: %clang_cc1 -triple nvptx64-nvidia-cuda -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