clang-tools  15.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 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace altera {
18 
19 void SingleWorkItemBarrierCheck::registerMatchers(MatchFinder *Finder) {
20  // Find any function that calls barrier but does not call an ID function.
21  // hasAttr(attr::Kind::OpenCLKernel) restricts it to only kernel functions.
22  // FIXME: Have it accept all functions but check for a parameter that gets an
23  // ID from one of the four ID functions.
24  Finder->addMatcher(
25  // Find function declarations...
26  functionDecl(
27  allOf(
28  // That are OpenCL kernels...
29  hasAttr(attr::Kind::OpenCLKernel),
30  // And call a barrier function (either 1.x or 2.x version)...
31  forEachDescendant(callExpr(callee(functionDecl(hasAnyName(
32  "barrier", "work_group_barrier"))))
33  .bind("barrier")),
34  // But do not call an ID function.
35  unless(hasDescendant(callExpr(callee(functionDecl(
36  hasAnyName("get_global_id", "get_local_id", "get_group_id",
37  "get_local_linear_id"))))))))
38  .bind("function"),
39  this);
40 }
41 
42 void SingleWorkItemBarrierCheck::check(const MatchFinder::MatchResult &Result) {
43  const auto *MatchedDecl = Result.Nodes.getNodeAs<FunctionDecl>("function");
44  const auto *MatchedBarrier = Result.Nodes.getNodeAs<CallExpr>("barrier");
45  if (AOCVersion < 1701) {
46  // get_group_id and get_local_linear_id were added at/after v17.01
47  diag(MatchedDecl->getLocation(),
48  "kernel function %0 does not call 'get_global_id' or 'get_local_id' "
49  "and will be treated as a single work-item")
50  << MatchedDecl;
51  diag(MatchedBarrier->getBeginLoc(),
52  "barrier call is in a single work-item and may error out",
53  DiagnosticIDs::Note);
54  } else {
55  // If reqd_work_group_size is anything other than (1,1,1), it will be
56  // interpreted as an NDRange in AOC version >= 17.1.
57  bool IsNDRange = false;
58  if (MatchedDecl->hasAttr<ReqdWorkGroupSizeAttr>()) {
59  const auto *Attribute = MatchedDecl->getAttr<ReqdWorkGroupSizeAttr>();
60  if (Attribute->getXDim() > 1 || Attribute->getYDim() > 1 ||
61  Attribute->getZDim() > 1)
62  IsNDRange = true;
63  }
64  if (IsNDRange) // No warning if kernel is treated as an NDRange.
65  return;
66  diag(MatchedDecl->getLocation(),
67  "kernel function %0 does not call an ID function and may be a viable "
68  "single work-item, but will be forced to execute as an NDRange")
69  << MatchedDecl;
70  diag(MatchedBarrier->getBeginLoc(),
71  "barrier call will force NDRange execution; if single work-item "
72  "semantics are desired a mem_fence may be more efficient",
73  DiagnosticIDs::Note);
74  }
75 }
76 
77 void SingleWorkItemBarrierCheck::storeOptions(
79  Options.store(Opts, "AOCVersion", AOCVersion);
80 }
81 
82 } // namespace altera
83 } // namespace tidy
84 } // namespace clang
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::clangd::check
bool check(llvm::StringRef File, llvm::Optional< Range > LineRange, const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts, bool EnableCodeCompletion)
Definition: Check.cpp:273
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
SingleWorkItemBarrierCheck.h