Skip to content

Commit 15fe0e8

Browse files
author
Yihan Wang
authored
[SYCLomatic] Fix a crash issue by stopping process template arg when it is a parameter pack (#408)
Signed-off-by: Wang, Yihan <[email protected]>
1 parent c43bc83 commit 15fe0e8

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

clang/lib/DPCT/ASTTraversal.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10731,6 +10731,12 @@ void MemoryMigrationRule::mallocMigration(
1073110731
void MemoryMigrationRule::memcpyMigration(
1073210732
const MatchFinder::MatchResult &Result, const CallExpr *C,
1073310733
const UnresolvedLookupExpr *ULExpr, bool IsAssigned) {
10734+
for (unsigned I = 0, E = C->getNumArgs(); I != E; ++I) {
10735+
if (isa<PackExpansionExpr>(C->getArg(I))) {
10736+
return;
10737+
}
10738+
}
10739+
1073410740
std::string Name;
1073510741
if (ULExpr) {
1073610742
Name = ULExpr->getName().getAsString();

clang/lib/DPCT/ExprAnalysis.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,12 @@ void ExprAnalysis::analyzeExpr(const CallExpr *CE) {
845845
return;
846846
auto Itr = CallExprRewriterFactoryBase::RewriterMap->find(RefString);
847847
if (Itr != CallExprRewriterFactoryBase::RewriterMap->end()) {
848+
for (unsigned I = 0, E = CE->getNumArgs(); I != E; ++I) {
849+
if (isa<PackExpansionExpr>(CE->getArg(I))) {
850+
return;
851+
}
852+
}
853+
848854
auto Rewriter = Itr->second->create(CE);
849855
auto Result = Rewriter->rewrite();
850856
BlockLevelFormatFlag = Rewriter->getBlockLevelFormatFlag();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: dpct --format-range=none -out-root %T/pack_expansion_in_arg_list %s --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only
2+
// RUN: FileCheck %s --input-file %T/pack_expansion_in_arg_list/pack_expansion_in_arg_list.dp.cpp
3+
4+
#include <cuda_runtime.h>
5+
#include <type_traits>
6+
#include <vector>
7+
8+
namespace detail {
9+
template <typename Func, typename... Args>
10+
void check_cuda_error(const char *File, unsigned Line, Func &&Fn,
11+
Args &&...Arg) {
12+
static_assert(std::is_same<decltype(Fn(Arg...)), cudaError_t>::value,
13+
"Fn must return cudaError_t");
14+
decltype(Fn(Arg...)) Ret = Fn(std::forward<Args>(Arg)...);
15+
if (Ret != cudaSuccess) {
16+
abort();
17+
}
18+
}
19+
} // namespace detail
20+
21+
#define AS_LAMBDA(func) \
22+
[&](auto &&...args) -> decltype(func( \
23+
std::forward<decltype(args)>(args)...)) { \
24+
return func(std::forward<decltype(args)>(args)...); \
25+
}
26+
27+
#define CHECK_CUDA_RET(FN, ...) \
28+
::detail::check_cuda_error(__FILE__, __LINE__, AS_LAMBDA(FN), __VA_ARGS__)
29+
30+
int *foo(size_t N, int Low, int High) {
31+
int *Buffer = nullptr;
32+
std::vector<int> Vec;
33+
34+
// 1. Crash in `clang::dpct::getSizeForMalloc`
35+
// CHECK: CHECK_CUDA_RET(cudaMalloc<int>, &Buffer, sizeof(int) * N);
36+
CHECK_CUDA_RET(cudaMalloc<int>, &Buffer, sizeof(int) * N);
37+
38+
// 2. Crash in `clang::dpct::MemoryMigrationRule::memcpyMigration`
39+
// CHECK: CHECK_CUDA_RET(cudaMemcpy, Buffer, Vec.data(), Vec.size() * sizeof(int), dpct::host_to_device);
40+
CHECK_CUDA_RET(cudaMemcpy, Buffer, Vec.data(), Vec.size() * sizeof(int), cudaMemcpyHostToDevice);
41+
return Buffer;
42+
}

0 commit comments

Comments
 (0)