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