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

[SYCLomatic] When the launch kernel is called has parent statement, then call the kernel in a anonymous lambda function. #2010

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 4 additions & 7 deletions clang/lib/DPCT/ASTTraversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8576,14 +8576,11 @@ void KernelCallRule::runRule(
}
if (!LaunchKernelCall)
return;

if (DpctGlobalInfo::getInstance().buildLaunchKernelInfo(LaunchKernelCall)) {
std::string Repl = "";
if (IsAssigned)
Repl = "0";
emplaceTransformation(
new ReplaceStmt(LaunchKernelCall, true, false, std::move(Repl)));
if (!IsAssigned)
removeTrailingSemicolon(LaunchKernelCall, Result);
if (DpctGlobalInfo::getInstance().buildLaunchKernelInfo(LaunchKernelCall,
IsAssigned)) {
emplaceTransformation(new ReplaceStmt(LaunchKernelCall, true, false, ""));
}
}
}
Expand Down
23 changes: 19 additions & 4 deletions clang/lib/DPCT/AnalysisInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1973,14 +1973,15 @@ void DpctGlobalInfo::emplaceReplacements(ReplTy &ReplSetsCUDA /*out*/,
}
}
std::shared_ptr<KernelCallExpr>
DpctGlobalInfo::buildLaunchKernelInfo(const CallExpr *LaunchKernelCall) {
auto LocInfo = getLocInfo(LaunchKernelCall->getBeginLoc());
DpctGlobalInfo::buildLaunchKernelInfo(const CallExpr *LaunchKernelCall, bool IsAssigned) {
auto DefRange = getDefinitionRange(LaunchKernelCall->getBeginLoc(), LaunchKernelCall->getEndLoc());
auto LocInfo = getLocInfo(DefRange.getBegin());
auto FileInfo = insertFile(LocInfo.first);
if (FileInfo->findNode<KernelCallExpr>(LocInfo.second))
return std::shared_ptr<KernelCallExpr>();

auto KernelInfo =
KernelCallExpr::buildFromCudaLaunchKernel(LocInfo, LaunchKernelCall);
KernelCallExpr::buildFromCudaLaunchKernel(LocInfo, LaunchKernelCall, IsAssigned);
if (KernelInfo) {
FileInfo->insertNode(LocInfo.second, KernelInfo);
} else {
Expand Down Expand Up @@ -5192,6 +5193,9 @@ void KernelCallExpr::print(KernelPrinter &Printer) {
Block = std::move(Printer.block(true));
}
printSubmit(Printer);
if (NeedDefaultRetValue) {
Printer.line("return 0;");
}
Block.reset();
if (!getEvent().empty() && isSync())
Printer.line(getEvent(), "->wait();");
Expand Down Expand Up @@ -5587,14 +5591,19 @@ std::string KernelCallExpr::getReplacement() {
}
std::shared_ptr<KernelCallExpr> KernelCallExpr::buildFromCudaLaunchKernel(
const std::pair<clang::tooling::UnifiedPath, unsigned> &LocInfo,
const CallExpr *CE) {
const CallExpr *CE, bool IsAssigned) {
auto LaunchFD = CE->getDirectCallee();
if (!LaunchFD || (LaunchFD->getName() != "cudaLaunchKernel" &&
LaunchFD->getName() != "cudaLaunchCooperativeKernel")) {
return std::shared_ptr<KernelCallExpr>();
}
auto Kernel = std::shared_ptr<KernelCallExpr>(
new KernelCallExpr(LocInfo.second, LocInfo.first));
// Call the lambda function with default return value.
if (IsAssigned) {
Kernel->setNeedDefaultRet();
Kernel->setNeedAddLambda();
}
Kernel->buildLocationInfo(CE);
Kernel->buildExecutionConfig(
ArrayRef<const Expr *>{CE->getArg(1), CE->getArg(2), CE->getArg(4),
Expand Down Expand Up @@ -5728,6 +5737,12 @@ void KernelCallExpr::setNeedAddLambda(const CUDAKernelCallExpr *KernelCall) {
NeedLambda = true;
}
}
void KernelCallExpr::setNeedAddLambda() {
NeedLambda = true;
}
void KernelCallExpr::setNeedDefaultRet() {
NeedDefaultRetValue = true;
}
void KernelCallExpr::buildNeedBracesInfo(const CallExpr *KernelCall) {
NeedBraces = true;
auto &Context = dpct::DpctGlobalInfo::getContext();
Expand Down
7 changes: 5 additions & 2 deletions clang/lib/DPCT/AnalysisInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ class DpctGlobalInfo {
// Emplace stored replacements into replacement set.
void emplaceReplacements(ReplTy &ReplSetsCUDA /*out*/,
ReplTy &ReplSetsSYCL /*out*/);
std::shared_ptr<KernelCallExpr> buildLaunchKernelInfo(const CallExpr *);
std::shared_ptr<KernelCallExpr> buildLaunchKernelInfo(const CallExpr *, bool IsAssigned = false);
void insertCudaMalloc(const CallExpr *CE);
void insertCublasAlloc(const CallExpr *CE);
std::shared_ptr<CudaMallocInfo> findCudaMalloc(const Expr *CE);
Expand Down Expand Up @@ -2696,6 +2696,7 @@ class KernelCallExpr : public CallFunctionExpr {
public:
bool IsInMacroDefine = false;
bool NeedLambda = false;
bool NeedDefaultRetValue = false;

private:
struct ArgInfo {
Expand Down Expand Up @@ -2762,7 +2763,7 @@ class KernelCallExpr : public CallFunctionExpr {

static std::shared_ptr<KernelCallExpr> buildFromCudaLaunchKernel(
const std::pair<clang::tooling::UnifiedPath, unsigned> &LocInfo,
const CallExpr *);
const CallExpr *, bool IsAssigned = false);
static std::shared_ptr<KernelCallExpr>
buildForWrapper(clang::tooling::UnifiedPath, const FunctionDecl *,
std::shared_ptr<DeviceFunctionInfo>);
Expand All @@ -2786,6 +2787,8 @@ class KernelCallExpr : public CallFunctionExpr {
void buildKernelInfo(const CUDAKernelCallExpr *KernelCall);
void setIsInMacroDefine(const CUDAKernelCallExpr *KernelCall);
void setNeedAddLambda(const CUDAKernelCallExpr *KernelCall);
void setNeedAddLambda();
void setNeedDefaultRet();
void buildNeedBracesInfo(const CallExpr *KernelCall);
void buildLocationInfo(const CallExpr *KernelCall);
template <class ArgsRange>
Expand Down
7 changes: 5 additions & 2 deletions clang/test/dpct/launch_kernel/k.inc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ void foo38() {
//CHECK-NEXT:/*
//CHECK-NEXT:DPCT1123:{{[0-9]+}}: The kernel function pointer cannot be used in the device code. You need to call the kernel function with the correct argument(s) directly. According to the kernel function definition, adjusting the dimension of the sycl::nd_item may also be required.
//CHECK-NEXT:*/
//CHECK-NEXT:CHECK_1([&](){
//CHECK-NEXT:stream->parallel_for(
//CHECK-NEXT:sycl::nd_range<3>(sycl::range<3>(z, y, x) * sycl::range<3>(1, 1, block), sycl::range<3>(1, 1, block)),
//CHECK-NEXT:[=](sycl::nd_item<3> item_ct1) {
//CHECK-NEXT: ((void*)&kernel38<T>)();
//CHECK-NEXT:});CHECK_1(0);
//CHECK-NEXT: ((void*)&kernel38<T>)();
//CHECK-NEXT:});
//CHECK-NEXT: return 0;
//CHECK-NEXT:}());
cudaStream_t stream;
CHECK_1(cudaLaunchKernel((void*)&kernel38<T>, dim3(x, y, z), block, args, shared, stream));
}
Expand Down
26 changes: 19 additions & 7 deletions clang/test/dpct/macro_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -1328,14 +1328,26 @@ void foo38() {
int z;
int shared;
cudaStream_t stream;
// CHECK:stream->parallel_for(
//CHECK-NEXT: sycl::nd_range<3>(sycl::range<3>(z, y, x) * sycl::range<3>(1, 1, block),
//CHECK-NEXT: sycl::range<3>(1, 1, block)),
//CHECK-NEXT: [=](sycl::nd_item<3> item_ct1) {
//CHECK-NEXT: ((void *)&kernel38<T>)();
//CHECK-NEXT: });
//CHECK-NEXT:CHECK_1(0);
//CHECK:CHECK_1([&]() {
//CHECK-NEXT: stream->parallel_for(
//CHECK-NEXT: sycl::nd_range<3>(sycl::range<3>(z, y, x) * sycl::range<3>(1, 1, block),
//CHECK-NEXT: sycl::range<3>(1, 1, block)),
//CHECK-NEXT: [=](sycl::nd_item<3> item_ct1) {
//CHECK-NEXT: ((void *)&kernel38<T>)();
//CHECK-NEXT: });
//CHECK-NEXT: return 0;
//CHECK-NEXT: }());
CHECK_1(cudaLaunchKernel((void*)&kernel38<T>, dim3(x, y, z), block, args, shared, stream));
//CHECK:dpct::err0 status = [&]() {
//CHECK-NEXT: stream->parallel_for(
//CHECK-NEXT: sycl::nd_range<3>(sycl::range<3>(z, y, x) * sycl::range<3>(1, 1, block),
//CHECK-NEXT: sycl::range<3>(1, 1, block)),
//CHECK-NEXT: [=](sycl::nd_item<3> item_ct1) {
//CHECK-NEXT: ((void *)&kernel38<T>)();
//CHECK-NEXT: });
//CHECK-NEXT: return 0;
//CHECK-NEXT: }();
cudaError_t status = cudaLaunchKernel((void*)&kernel38<T>, dim3(x, y, z), block, args, shared, stream);
}
#undef CHECK_1
#undef CHECK_2
Expand Down
Loading