| 1 |
//===--- SingleWorkItemBarrierCheck.cpp - clang-tidy-----------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "SingleWorkItemBarrierCheck.h" |
| 10 |
#include "clang/AST/ASTContext.h" |
| 11 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 |
|
| 13 |
using namespace clang::ast_matchers; |
| 14 |
|
| 15 |
namespace clang::tidy::altera { |
| 16 |
|
| 17 |
void SingleWorkItemBarrierCheck::registerMatchers(MatchFinder *Finder) { |
| 18 |
// Find any function that calls barrier but does not call an ID function. |
| 19 |
// hasAttr(attr::Kind::OpenCLKernel) restricts it to only kernel functions. |
| 20 |
// FIXME: Have it accept all functions but check for a parameter that gets an |
| 21 |
// ID from one of the four ID functions. |
| 22 |
Finder->addMatcher( |
| 23 |
// Find function declarations... |
| 24 |
functionDecl( |
| 25 |
allOf( |
| 26 |
// That are OpenCL kernels... |
| 27 |
hasAttr(attr::Kind::OpenCLKernel), |
| 28 |
// And call a barrier function (either 1.x or 2.x version)... |
| 29 |
forEachDescendant(callExpr(callee(functionDecl(hasAnyName( |
| 30 |
"barrier", "work_group_barrier")))) |
| 31 |
.bind("barrier")), |
| 32 |
// But do not call an ID function. |
| 33 |
unless(hasDescendant(callExpr(callee(functionDecl( |
| 34 |
hasAnyName("get_global_id", "get_local_id", "get_group_id", |
| 35 |
"get_local_linear_id")))))))) |
| 36 |
.bind("function"), |
| 37 |
this); |
| 38 |
} |
| 39 |
|
| 40 |
void SingleWorkItemBarrierCheck::check(const MatchFinder::MatchResult &Result) { |
| 41 |
const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("function"); |
| 42 |
const auto *MatchedBarrier = Result.Nodes.getNodeAs<CallExpr>("barrier"); |
| 43 |
if (AOCVersion < 1701) { |
| 44 |
// get_group_id and get_local_linear_id were added at/after v17.01 |
| 45 |
diag(MatchedDecl->getLocation(), |
| 46 |
"kernel function %0 does not call 'get_global_id' or 'get_local_id' " |
| 47 |
"and will be treated as a single work-item") |
| 48 |
<< MatchedDecl; |
| 49 |
diag(MatchedBarrier->getBeginLoc(), |
| 50 |
"barrier call is in a single work-item and may error out", |
| 51 |
DiagnosticIDs::Note); |
| 52 |
} else { |
| 53 |
// If reqd_work_group_size is anything other than (1,1,1), it will be |
| 54 |
// interpreted as an NDRange in AOC version >= 17.1. |
| 55 |
bool IsNDRange = false; |
| 56 |
if (MatchedDecl->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 57 |
const auto *Attribute = MatchedDecl->getAttr<ReqdWorkGroupSizeAttr>(); |
| 58 |
if (Attribute->getXDim() > 1 || Attribute->getYDim() > 1 || |
| 59 |
Attribute->getZDim() > 1) |
| 60 |
IsNDRange = true; |
| 61 |
} |
| 62 |
if (IsNDRange) // No warning if kernel is treated as an NDRange. |
| 63 |
return; |
| 64 |
diag(MatchedDecl->getLocation(), |
| 65 |
"kernel function %0 does not call an ID function and may be a viable " |
| 66 |
"single work-item, but will be forced to execute as an NDRange") |
| 67 |
<< MatchedDecl; |
| 68 |
diag(MatchedBarrier->getBeginLoc(), |
| 69 |
"barrier call will force NDRange execution; if single work-item " |
| 70 |
"semantics are desired a mem_fence may be more efficient", |
| 71 |
DiagnosticIDs::Note); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void SingleWorkItemBarrierCheck::storeOptions( |
| 76 |
ClangTidyOptions::OptionMap &Opts) { |
| 77 |
Options.store(Opts, "AOCVersion", AOCVersion); |
| 78 |
} |
| 79 |
|
| 80 |
} // namespace clang::tidy::altera |