clang-tools 20.0.0git
SingleWorkItemBarrierCheck.cpp
Go to the documentation of this file.
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::altera {
16
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 // 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"))))
30 .bind("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")))))))
35 .bind("function"),
36 this);
37}
38
39void 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")
47 << MatchedDecl;
48 diag(MatchedBarrier->getBeginLoc(),
49 "barrier call is in a single work-item and may error out",
50 DiagnosticIDs::Note);
51 } else {
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)
59 IsNDRange = true;
60 }
61 if (IsNDRange) // No warning if kernel is treated as an NDRange.
62 return;
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")
66 << MatchedDecl;
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",
70 DiagnosticIDs::Note);
71 }
72}
73
76 Options.store(Opts, "AOCVersion", AOCVersion);
77}
78
79} // namespace clang::tidy::altera
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
llvm::StringMap< ClangTidyValue > OptionMap