1 //===--- SingleWorkItemBarrierCheck.cpp - clang-tidy-----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "SingleWorkItemBarrierCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers
;
15 namespace clang::tidy::altera
{
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.
23 // Find function declarations...
25 // That are OpenCL kernels...
26 hasAttr(attr::Kind::OpenCLKernel
),
27 // And call a barrier function (either 1.x or 2.x version)...
28 forEachDescendant(callExpr(callee(functionDecl(hasAnyName(
29 "barrier", "work_group_barrier"))))
31 // But do not call an ID function.
32 unless(hasDescendant(callExpr(callee(functionDecl(
33 hasAnyName("get_global_id", "get_local_id", "get_group_id",
34 "get_local_linear_id")))))))
39 void SingleWorkItemBarrierCheck::check(const MatchFinder::MatchResult
&Result
) {
40 const auto *MatchedDecl
= Result
.Nodes
.getNodeAs
<FunctionDecl
>("function");
41 const auto *MatchedBarrier
= Result
.Nodes
.getNodeAs
<CallExpr
>("barrier");
42 if (AOCVersion
< 1701) {
43 // get_group_id and get_local_linear_id were added at/after v17.01
44 diag(MatchedDecl
->getLocation(),
45 "kernel function %0 does not call 'get_global_id' or 'get_local_id' "
46 "and will be treated as a single work-item")
48 diag(MatchedBarrier
->getBeginLoc(),
49 "barrier call is in a single work-item and may error out",
52 // If reqd_work_group_size is anything other than (1,1,1), it will be
53 // interpreted as an NDRange in AOC version >= 17.1.
54 bool IsNDRange
= false;
55 if (MatchedDecl
->hasAttr
<ReqdWorkGroupSizeAttr
>()) {
56 const auto *Attribute
= MatchedDecl
->getAttr
<ReqdWorkGroupSizeAttr
>();
57 if (Attribute
->getXDim() > 1 || Attribute
->getYDim() > 1 ||
58 Attribute
->getZDim() > 1)
61 if (IsNDRange
) // No warning if kernel is treated as an NDRange.
63 diag(MatchedDecl
->getLocation(),
64 "kernel function %0 does not call an ID function and may be a viable "
65 "single work-item, but will be forced to execute as an NDRange")
67 diag(MatchedBarrier
->getBeginLoc(),
68 "barrier call will force NDRange execution; if single work-item "
69 "semantics are desired a mem_fence may be more efficient",
74 void SingleWorkItemBarrierCheck::storeOptions(
75 ClangTidyOptions::OptionMap
&Opts
) {
76 Options
.store(Opts
, "AOCVersion", AOCVersion
);
79 } // namespace clang::tidy::altera