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] Emit warning for shared local memory if the data type defined by using or typedef #1822

Merged
merged 16 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
39 changes: 39 additions & 0 deletions clang/lib/DPCT/ASTTraversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9592,6 +9592,33 @@ void MemVarAnalysisRule::processTypeDeclaredLocal(
}
}

#define TYPE_CAST(Target) dyn_cast<Target>(T)
SourceLocation getTypedefOrUsingLoc(QualType QT) {
const Type *T = QT.getTypePtr();
switch (T->getTypeClass()) {
case Type::TypeClass::IncompleteArray:
return getTypedefOrUsingLoc(
TYPE_CAST(IncompleteArrayType)->getElementType());
case Type::TypeClass::ConstantArray:
return getTypedefOrUsingLoc(TYPE_CAST(ConstantArrayType)->getElementType());
case Type::TypeClass::Pointer:
return getTypedefOrUsingLoc(TYPE_CAST(PointerType)->getPointeeType());
case Type::TypeClass::Elaborated:
return getTypedefOrUsingLoc(TYPE_CAST(ElaboratedType)->desugar());
case Type::TypeClass::Typedef:
return TYPE_CAST(TypedefType)
->getDecl()
->getTypeSourceInfo()
->getTypeLoc()
.getBeginLoc();
case Type::TypeClass::Using:
return TYPE_CAST(clang::UsingType)->getFoundDecl()->getBeginLoc();
default:
return SourceLocation();
}
}
#undef TYPE_CAST

void MemVarAnalysisRule::runRule(const MatchFinder::MatchResult &Result) {
std::string CanonicalType;
if (auto MemVar = getAssistNodeAsType<VarDecl>(Result, "var")) {
Expand Down Expand Up @@ -9621,6 +9648,12 @@ void MemVarAnalysisRule::runRule(const MatchFinder::MatchResult &Result) {
emplaceTransformation(ReplaceVarDecl::getVarDeclReplacement(
MemVar, Info->getDeclarationReplacement(MemVar)));
}
if (MemVar->hasAttr<CUDASharedAttr>()) {
SourceLocation SL = getTypedefOrUsingLoc(MemVar->getType());
if (SL.isValid()) {
report(SL, Diagnostics::MOVE_TYPE_DEFINITION, true);
}
}
return;
}
auto MemVarRef = getNodeAsType<DeclRefExpr>(Result, "used");
Expand Down Expand Up @@ -9649,6 +9682,12 @@ void MemVarAnalysisRule::runRule(const MatchFinder::MatchResult &Result) {
}
}
}
if (VD->hasAttr<CUDASharedAttr>()) {
SourceLocation SL = getTypedefOrUsingLoc(VD->getType());
if (SL.isValid()) {
report(SL, Diagnostics::MOVE_TYPE_DEFINITION, true);
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions clang/lib/DPCT/Diagnostics.inc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ DEF_WARNING(VEC_IN_TEMPLATE_ARG, 1122, LOW_LEVEL, "'{0}' is migrated to '{1}' in
DEF_COMMENT(VEC_IN_TEMPLATE_ARG, 1122, LOW_LEVEL, "'{0}' is migrated to '{1}' in template declare, it may cause template function or class redefinition, please adjust the code.")
DEF_WARNING(UNDEDUCED_KERNEL_FUNCTION_POINTER, 1123, MEDIUM_LEVEL, "The kernel function pointer cannot be used in the device code. You need call the kernel function with correct argunemt(s) directly. According to the kernel function definition, adjusting the dimension of the sycl::nd_item may be also required.")
DEF_COMMENT(UNDEDUCED_KERNEL_FUNCTION_POINTER, 1123, MEDIUM_LEVEL, "The kernel function pointer cannot be used in the device code. You need call the kernel function with correct argunemt(s) directly. According to the kernel function definition, adjusting the dimension of the sycl::nd_item may be also required.")
DEF_WARNING(MOVE_TYPE_DEFINITION, 1124, MEDIUM_LEVEL, "This type is used in other places. You may need adjust the type definition location.")
DEF_COMMENT(MOVE_TYPE_DEFINITION, 1124, MEDIUM_LEVEL, "This type is used in other places. You may need adjust the type definition location.")
// clang-format on

#undef DEF_COMMENT
Expand Down
17 changes: 16 additions & 1 deletion clang/test/dpct/kernel_without_name.cu
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: dpct --format-range=none --usm-level=none -out-root %T/kernel_without_name %s --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only
// RUN: FileCheck --input-file %T/kernel_without_name/kernel_without_name.dp.cpp --match-full-lines %s
// RUN: %if build_lit %{icpx -c -fsycl %T/kernel_without_name/kernel_without_name.dp.cpp -o %T/kernel_without_name/kernel_without_name.dp.o %}
// RUN: %if build_lit %{icpx -c -fsycl -DBUILD_TEST %T/kernel_without_name/kernel_without_name.dp.cpp -o %T/kernel_without_name/kernel_without_name.dp.o %}

__global__ void testKernel(int L, int M, int N);

Expand Down Expand Up @@ -277,3 +277,18 @@ void run_foo7(T *a, const T *b, const T *c, const T *d, const T *e, const int f,
foo_kernel6<<<grid, block, 0, stream>>>(a, b, c, d, e, f, g, h);
}
}

#ifndef BUILD_TEST
template <typename T> struct kernel_type_t {
using Type = T;
};

template <typename T> __global__ void foo_kernel7() {
zhiweij1 marked this conversation as resolved.
Show resolved Hide resolved
//CHECK:/*
//CHECK-NEXT:DPCT1124:{{[0-9]+}}: This type is used in other places. You may need adjust the type definition location.
zhiweij1 marked this conversation as resolved.
Show resolved Hide resolved
//CHECK-NEXT:*/
//CHECK-NEXT:using Tk = typename kernel_type_t<T>::Type;
using Tk = typename kernel_type_t<T>::Type;
__shared__ Tk mem[256];
}
#endif
zhiweij1 marked this conversation as resolved.
Show resolved Hide resolved
Loading