Skip to content

Commit 1206a52

Browse files
authored
[SYCLomatic] Fix the migration of __syncthreads in the un-enabled if constexpr branch (#2795)
Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
1 parent fed9d2b commit 1206a52

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

clang/lib/DPCT/RulesLang/RulesLang.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7761,6 +7761,31 @@ void SyncThreadsMigrationRule::registerMatcher(MatchFinder &MF) {
77617761
this);
77627762
}
77637763

7764+
bool SyncThreadsMigrationRule::noCorrespondingCEInInstantiatedTemplates(
7765+
const FunctionTemplateDecl *FTD, const CallExpr *CE,
7766+
const std::string &FuncName) {
7767+
const auto &SM = DpctGlobalInfo::getSourceManager();
7768+
auto CEMatcher = ast_matchers::findAll(
7769+
ast_matchers::callExpr(callee(functionDecl(hasName(FuncName))))
7770+
.bind("call"));
7771+
SourceLocation CELocation = SM.getSpellingLoc(CE->getBeginLoc());
7772+
for (const auto &Spec : FTD->specializations()) {
7773+
if (!(Spec->hasBody()))
7774+
continue;
7775+
auto MatchedResults = ast_matchers::match(CEMatcher, *(Spec->getBody()),
7776+
DpctGlobalInfo::getContext());
7777+
for (auto &Node : MatchedResults) {
7778+
if (const auto *MatchedCE = Node.getNodeAs<CallExpr>("call")) {
7779+
SourceLocation MatchedCELocation =
7780+
SM.getSpellingLoc(MatchedCE->getBeginLoc());
7781+
if (CELocation == MatchedCELocation)
7782+
return false;
7783+
}
7784+
}
7785+
}
7786+
return true;
7787+
}
7788+
77647789
void SyncThreadsMigrationRule::runRule(const MatchFinder::MatchResult &Result) {
77657790
static std::map<std::string, bool> LocationResultMapForTemplate;
77667791
auto emplaceReplacement = [&](BarrierFenceSpaceAnalyzerResult Res,
@@ -7802,7 +7827,8 @@ void SyncThreadsMigrationRule::runRule(const MatchFinder::MatchResult &Result) {
78027827
BarrierFenceSpaceAnalyzer A;
78037828
const FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate();
78047829
if (FTD) {
7805-
if (FTD->specializations().empty()) {
7830+
if (FTD->specializations().empty() ||
7831+
noCorrespondingCEInInstantiatedTemplates(FTD, CE, FuncName)) {
78067832
emplaceReplacement(A.analyze(CE), CE);
78077833
}
78087834
} else {

clang/lib/DPCT/RulesLang/RulesLang.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "clang/Frontend/CompilerInstance.h"
2222

2323
#include <algorithm>
24+
#include <string>
2425
#include <unordered_set>
2526

2627
namespace clang {
@@ -809,6 +810,9 @@ class SyncThreadsMigrationRule
809810
public:
810811
void registerMatcher(ast_matchers::MatchFinder &MF) override;
811812
void runRule(const ast_matchers::MatchFinder::MatchResult &Result);
813+
bool noCorrespondingCEInInstantiatedTemplates(const FunctionTemplateDecl *FTD,
814+
const CallExpr *CE,
815+
const std::string &FuncName);
812816
};
813817

814818
/// Migrate Function Attributes to Sycl kernel info, defined in

clang/test/dpct/syncthreads.cu

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,20 @@ __global__ void test21(float *ptr1, float *ptr2, int step1, int step2) {
462462
idx2 += step2;
463463
}
464464
}
465+
466+
template <typename T, const bool B> __device__ void test_22_d() {
467+
// CHECK: if constexpr (B) {
468+
// CHECK-NEXT: /*
469+
// CHECK-NEXT: DPCT1065:{{[0-9]+}}: Consider replacing sycl::nd_item::barrier() with sycl::nd_item::barrier(sycl::access::fence_space::local_space) for better performance if there is no access to global memory.
470+
// CHECK-NEXT: */
471+
// CHECK-NEXT: sycl::ext::oneapi::this_work_item::get_nd_item<3>().barrier();
472+
// CHECK-NEXT: }
473+
if constexpr (B) {
474+
__syncthreads();
475+
}
476+
}
477+
478+
__global__ void test_22() {
479+
test_22_d<int, false>();
480+
test_22_d<float, false>();
481+
}

0 commit comments

Comments
 (0)