From cbf730999468e266e49231f8efe61ac8989afefc Mon Sep 17 00:00:00 2001 From: Deepak Raj H R Date: Mon, 2 Sep 2024 12:25:01 +0530 Subject: [PATCH] [SYCLomatic] Fix incorrect typecast of `sycl::malloc` return type in case of CUDA type redefination (#2146) Co-authored-by: Jiang, Zhiwei --- clang/lib/DPCT/CallExprRewriterCommon.h | 7 ++++++- clang/test/dpct/cu_mem_alloc_typedef.cu | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 clang/test/dpct/cu_mem_alloc_typedef.cu diff --git a/clang/lib/DPCT/CallExprRewriterCommon.h b/clang/lib/DPCT/CallExprRewriterCommon.h index 4656e6299df9..818e1f53fc93 100644 --- a/clang/lib/DPCT/CallExprRewriterCommon.h +++ b/clang/lib/DPCT/CallExprRewriterCommon.h @@ -24,6 +24,7 @@ #include "clang/Basic/LangOptions.h" #include "clang/Driver/Driver.h" #include "clang/Driver/Options.h" +#include #include extern clang::tooling::UnifiedPath DpctInstallPath; // Installation directory for this tool @@ -792,7 +793,11 @@ inline std::function getDerefedType(size_t Idx) DerefQT = ET->getNamedType(); if (const auto *TDT = dyn_cast(DerefQT)) { auto *TDecl = TDT->getDecl(); - if (dpct::DpctGlobalInfo::isInCudaPath(TDecl->getLocation())) + const auto Redecls = TDecl->redecls(); + auto IsDeclInCudaHeader = [](const TypedefNameDecl * D) { + return dpct::DpctGlobalInfo::isInCudaPath(D->getLocation()); + }; + if (std::any_of(Redecls.begin(), Redecls.end(), IsDeclInCudaHeader)) break; DerefQT = TDecl->getUnderlyingType(); } diff --git a/clang/test/dpct/cu_mem_alloc_typedef.cu b/clang/test/dpct/cu_mem_alloc_typedef.cu new file mode 100644 index 000000000000..7c2d041f4daf --- /dev/null +++ b/clang/test/dpct/cu_mem_alloc_typedef.cu @@ -0,0 +1,13 @@ +// UNSUPPORTED: system-linux +// RUN: dpct --format-range=none -out-root %T/cu_mem_alloc_typedef %s --cuda-include-path="%cuda-path/include" -- -std=c++14 -x cuda --cuda-host-only +// RUN: FileCheck %s --match-full-lines --input-file %T/cu_mem_alloc_typedef/cu_mem_alloc_typedef.dp.cpp +#include +#include + +typedef uint64_t CUdeviceptr; + +// CHECK: void foo(dpct::device_ptr ptr) { +void foo(CUdeviceptr ptr) { + // CHECK: ptr = (dpct::device_ptr)sycl::malloc_device(1024, dpct::get_in_order_queue()); + cuMemAlloc(&ptr, 1024); +}