brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.5 KiB · c9df658 Raw
84 lines · cpp
1//===----------------------------------------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "SingleWorkItemBarrierCheck.h"10#include "clang/AST/ASTContext.h"11#include "clang/ASTMatchers/ASTMatchFinder.h"12 13using namespace clang::ast_matchers;14 15namespace clang::tidy::altera {16 17void SingleWorkItemBarrierCheck::registerMatchers(MatchFinder *Finder) {18  // Find any function that calls barrier but does not call an ID function.19  // hasAttr(attr::Kind::DeviceKernel) restricts it to only kernel functions.20  // FIXME: Have it accept all functions but check for a parameter that gets an21  // ID from one of the four ID functions.22  Finder->addMatcher(23      // Find function declarations...24      functionDecl(25          // That are device kernels...26          hasAttr(attr::Kind::DeviceKernel),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.0144    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 be53    // 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      auto Eval = [&](Expr *E) {58        return E->EvaluateKnownConstInt(MatchedDecl->getASTContext())59            .getExtValue();60      };61      if (Eval(Attribute->getXDim()) > 1 || Eval(Attribute->getYDim()) > 1 ||62          Eval(Attribute->getZDim()) > 1)63        IsNDRange = true;64    }65    if (IsNDRange) // No warning if kernel is treated as an NDRange.66      return;67    diag(MatchedDecl->getLocation(),68         "kernel function %0 does not call an ID function and may be a viable "69         "single work-item, but will be forced to execute as an NDRange")70        << MatchedDecl;71    diag(MatchedBarrier->getBeginLoc(),72         "barrier call will force NDRange execution; if single work-item "73         "semantics are desired a mem_fence may be more efficient",74         DiagnosticIDs::Note);75  }76}77 78void SingleWorkItemBarrierCheck::storeOptions(79    ClangTidyOptions::OptionMap &Opts) {80  Options.store(Opts, "AOCVersion", AOCVersion);81}82 83} // namespace clang::tidy::altera84