Skip to content

Commit

Permalink
Merge branch 'SYCLomatic' into fix_mma
Browse files Browse the repository at this point in the history
  • Loading branch information
intwanghao committed Jun 5, 2024
2 parents ece3a14 + 0be2d34 commit b6329c8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 33 deletions.
11 changes: 4 additions & 7 deletions clang/lib/DPCT/ASTTraversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8575,14 +8575,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
35 changes: 21 additions & 14 deletions clang/lib/DPCT/AnalysisInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1971,14 +1971,17 @@ 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);
auto KernelInfo = KernelCallExpr::buildFromCudaLaunchKernel(
LocInfo, LaunchKernelCall, IsAssigned);
if (KernelInfo) {
FileInfo->insertNode(LocInfo.second, KernelInfo);
} else {
Expand Down Expand Up @@ -5182,17 +5185,14 @@ const std::string &KernelCallExpr::ArgInfo::getTypeString() const {
}
void KernelCallExpr::print(KernelPrinter &Printer) {
std::unique_ptr<KernelPrinter::Block> Block;
if (!OuterStmts.empty()) {
if (NeedBraces)
Block = std::move(Printer.block(true));
else
Block = std::move(Printer.block(false));
OuterStmts.print(Printer);
}
if (NeedLambda) {
if (NeedLambda)
Block = std::move(Printer.block(true));
}
else if (!OuterStmts.empty())
Block = std::move(Printer.block(NeedBraces));
OuterStmts.print(Printer);
printSubmit(Printer);
if (NeedDefaultRetValue)
Printer.line("return 0;");
Block.reset();
if (!getEvent().empty() && isSync())
Printer.line(getEvent(), "->wait();");
Expand Down Expand Up @@ -5588,14 +5588,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 @@ -5729,6 +5734,8 @@ 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
8 changes: 6 additions & 2 deletions clang/lib/DPCT/AnalysisInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,8 @@ 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 @@ -2694,6 +2695,7 @@ class KernelCallExpr : public CallFunctionExpr {
public:
bool IsInMacroDefine = false;
bool NeedLambda = false;
bool NeedDefaultRetValue = false;

private:
struct ArgInfo {
Expand Down Expand Up @@ -2760,7 +2762,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 @@ -2784,6 +2786,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
2 changes: 1 addition & 1 deletion clang/runtime/dpct-rt/include/dpct/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ template <class T, int dimensions, bool IsImageArray = false> class image_wrappe
IsImageArray>::accessor_t;

image_wrapper() { set_channel(image_channel::create<T>()); }
~image_wrapper() { detach(); }
~image_wrapper() override { detach(); }

/// Get image accessor.
accessor_t get_access(sycl::handler &cgh, sycl::queue &q = get_default_queue()) {
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

0 comments on commit b6329c8

Please sign in to comment.