Skip to content

[CIR][CUDA] Generate kernel calls #1348

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

Merged
merged 1 commit into from
Feb 14, 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
20 changes: 20 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCUDARuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,23 @@ void CIRGenCUDARuntime::emitDeviceStub(CIRGenFunction &cgf, cir::FuncOp fn,
else
emitDeviceStubBodyLegacy(cgf, fn, args);
}

RValue CIRGenCUDARuntime::emitCUDAKernelCallExpr(CIRGenFunction &cgf,
const CUDAKernelCallExpr *expr,
ReturnValueSlot retValue) {
auto builder = cgm.getBuilder();
mlir::Location loc =
cgf.currSrcLoc ? cgf.currSrcLoc.value() : builder.getUnknownLoc();

cgf.emitIfOnBoolExpr(
expr->getConfig(),
[&](mlir::OpBuilder &b, mlir::Location l) {
CIRGenCallee callee = cgf.emitCallee(expr->getCallee());
cgf.emitCall(expr->getCallee()->getType(), callee, expr, retValue);
b.create<cir::YieldOp>(loc);
},
loc, [](mlir::OpBuilder &b, mlir::Location l) {},
std::optional<mlir::Location>());

return RValue::get(nullptr);
}
6 changes: 6 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenCUDARuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace clang::CIRGen {
class CIRGenFunction;
class CIRGenModule;
class FunctionArgList;
class RValue;
class ReturnValueSlot;

class CIRGenCUDARuntime {
protected:
Expand All @@ -40,6 +42,10 @@ class CIRGenCUDARuntime {

virtual void emitDeviceStub(CIRGenFunction &cgf, cir::FuncOp fn,
FunctionArgList &args);

virtual RValue emitCUDAKernelCallExpr(CIRGenFunction &cgf,
const CUDAKernelCallExpr *expr,
ReturnValueSlot retValue);
};

} // namespace clang::CIRGen
Expand Down
9 changes: 7 additions & 2 deletions clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,10 @@ static CIRGenCallee emitDirectCallee(CIRGenModule &CGM, GlobalDecl GD) {

auto CalleePtr = emitFunctionDeclPointer(CGM, GD);

assert(!CGM.getLangOpts().CUDA && "NYI");
// For HIP, the device stub should be converted to handle.
if (CGM.getLangOpts().HIP && !CGM.getLangOpts().CUDAIsDevice &&
FD->hasAttr<CUDAGlobalAttr>())
llvm_unreachable("NYI");

return CIRGenCallee::forDirect(CalleePtr, GD);
}
Expand Down Expand Up @@ -1405,7 +1408,9 @@ RValue CIRGenFunction::emitCallExpr(const clang::CallExpr *E,
if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
return emitCXXMemberCallExpr(CE, ReturnValue);

assert(!dyn_cast<CUDAKernelCallExpr>(E) && "CUDA NYI");
if (const auto *CE = dyn_cast<CUDAKernelCallExpr>(E))
return CGM.getCUDARuntime().emitCUDAKernelCallExpr(*this, CE, ReturnValue);

if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
if (const CXXMethodDecl *MD =
dyn_cast_or_null<CXXMethodDecl>(CE->getCalleeDecl()))
Expand Down
15 changes: 15 additions & 0 deletions clang/test/CIR/CodeGen/CUDA/simple.cu
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ __global__ void global_fn(int a) {}
// CIR-HOST: cir.call @__cudaPopCallConfiguration
// CIR-HOST: cir.get_global @_Z24__device_stub__global_fni
// CIR-HOST: cir.call @cudaLaunchKernel

int main() {
global_fn<<<1, 1>>>(1);
}
// CIR-DEVICE-NOT: cir.func @main()

// CIR-HOST: cir.func @main()
// CIR-HOST: cir.call @_ZN4dim3C1Ejjj
// CIR-HOST: cir.call @_ZN4dim3C1Ejjj
// CIR-HOST: [[Push:%[0-9]+]] = cir.call @__cudaPushCallConfiguration
// CIR-HOST: [[ConfigOK:%[0-9]+]] = cir.cast(int_to_bool, [[Push]]
// CIR-HOST: cir.if [[ConfigOK]] {
// CIR-HOST: [[Arg:%[0-9]+]] = cir.const #cir.int<1>
// CIR-HOST: cir.call @_Z24__device_stub__global_fni([[Arg]])
// CIR-HOST: }
Loading