clang-tools  11.0.0
SIMDIntrinsicsCheck.cpp
Go to the documentation of this file.
1 //===--- SIMDIntrinsicsCheck.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 "SIMDIntrinsicsCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Basic/TargetInfo.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/Support/Regex.h"
16 
17 using namespace clang::ast_matchers;
18 
19 namespace clang {
20 namespace tidy {
21 namespace portability {
22 
23 namespace {
24 
25 // If the callee has parameter of VectorType or pointer to VectorType,
26 // or the return type is VectorType, we consider it a vector function
27 // and a candidate for checking.
28 AST_MATCHER(FunctionDecl, isVectorFunction) {
29  bool IsVector = Node.getReturnType()->isVectorType();
30  for (const ParmVarDecl *Parm : Node.parameters()) {
31  QualType Type = Parm->getType();
32  if (Type->isPointerType())
33  Type = Type->getPointeeType();
34  if (Type->isVectorType())
35  IsVector = true;
36  }
37  return IsVector;
38 }
39 
40 } // namespace
41 
42 static StringRef TrySuggestPPC(StringRef Name) {
43  if (!Name.consume_front("vec_"))
44  return {};
45 
46  return llvm::StringSwitch<StringRef>(Name)
47  // [simd.alg]
48  .Case("max", "$std::max")
49  .Case("min", "$std::min")
50  // [simd.binary]
51  .Case("add", "operator+ on $simd objects")
52  .Case("sub", "operator- on $simd objects")
53  .Case("mul", "operator* on $simd objects")
54  .Default({});
55 }
56 
57 static StringRef TrySuggestX86(StringRef Name) {
58  if (!(Name.consume_front("_mm_") || Name.consume_front("_mm256_") ||
59  Name.consume_front("_mm512_")))
60  return {};
61 
62  // [simd.alg]
63  if (Name.startswith("max_"))
64  return "$simd::max";
65  if (Name.startswith("min_"))
66  return "$simd::min";
67 
68  // [simd.binary]
69  if (Name.startswith("add_"))
70  return "operator+ on $simd objects";
71  if (Name.startswith("sub_"))
72  return "operator- on $simd objects";
73  if (Name.startswith("mul_"))
74  return "operator* on $simd objects";
75 
76  return {};
77 }
78 
79 SIMDIntrinsicsCheck::SIMDIntrinsicsCheck(StringRef Name,
80  ClangTidyContext *Context)
81  : ClangTidyCheck(Name, Context), Std(Options.get("Std", "")),
82  Suggest(Options.get("Suggest", false)) {}
83 
85  Options.store(Opts, "Std", "");
86  Options.store(Opts, "Suggest", 0);
87 }
88 
89 void SIMDIntrinsicsCheck::registerMatchers(MatchFinder *Finder) {
90  // If Std is not specified, infer it from the language options.
91  // libcxx implementation backports it to C++11 std::experimental::simd.
92  if (Std.empty())
93  Std = getLangOpts().CPlusPlus20 ? "std" : "std::experimental";
94 
95  Finder->addMatcher(callExpr(callee(functionDecl(
96  matchesName("^::(_mm_|_mm256_|_mm512_|vec_)"),
97  isVectorFunction())),
98  unless(isExpansionInSystemHeader()))
99  .bind("call"),
100  this);
101 }
102 
103 void SIMDIntrinsicsCheck::check(const MatchFinder::MatchResult &Result) {
104  const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
105  assert(Call != nullptr);
106  const FunctionDecl *Callee = Call->getDirectCallee();
107  if (!Callee)
108  return;
109 
110  StringRef Old = Callee->getName();
111  StringRef New;
112  llvm::Triple::ArchType Arch =
113  Result.Context->getTargetInfo().getTriple().getArch();
114 
115  // We warn or suggest if this SIMD intrinsic function has a std::simd
116  // replacement.
117  switch (Arch) {
118  default:
119  break;
120  case llvm::Triple::ppc:
121  case llvm::Triple::ppc64:
122  case llvm::Triple::ppc64le:
123  New = TrySuggestPPC(Old);
124  break;
125  case llvm::Triple::x86:
126  case llvm::Triple::x86_64:
127  New = TrySuggestX86(Old);
128  break;
129  }
130 
131  // We have found a std::simd replacement.
132  if (!New.empty()) {
133  std::string Message;
134  // If Suggest is true, give a P0214 alternative, otherwise point it out it
135  // is non-portable.
136  if (Suggest) {
137  Message = (Twine("'") + Old + "' can be replaced by " + New).str();
138  Message = llvm::Regex("\\$std").sub(Std, Message);
139  Message =
140  llvm::Regex("\\$simd").sub((Std.str() + "::simd").str(), Message);
141  } else {
142  Message = (Twine("'") + Old + "' is a non-portable " +
143  llvm::Triple::getArchTypeName(Arch) + " intrinsic function")
144  .str();
145  }
146  diag(Call->getExprLoc(), Message);
147  }
148 }
149 
150 } // namespace portability
151 } // namespace tidy
152 } // namespace clang
Type
NodeType Type
Definition: HTMLGenerator.cpp:73
clang::tidy::bugprone::Message
static const char Message[]
Definition: ReservedIdentifierCheck.cpp:31
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:114
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:475
clang::ast_matchers
Definition: AbseilMatcher.h:14
SIMDIntrinsicsCheck.h
clang::tidy::portability::SIMDIntrinsicsCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: SIMDIntrinsicsCheck.cpp:103
clang::tidy::portability::SIMDIntrinsicsCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: SIMDIntrinsicsCheck.cpp:84
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:471
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:55
Parm
Params Parm
Definition: ConfigCompileTests.cpp:29
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang::tidy::portability::TrySuggestX86
static StringRef TrySuggestX86(StringRef Name)
Definition: SIMDIntrinsicsCheck.cpp:57
clang::tidy::portability::SIMDIntrinsicsCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: SIMDIntrinsicsCheck.cpp:89
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:152
clang::tidy::portability::TrySuggestPPC
static StringRef TrySuggestPPC(StringRef Name)
Definition: SIMDIntrinsicsCheck.cpp:42
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111