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.
AST_MATCHER(FunctionDecl,isVectorFunction)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
TrySuggestPPC(StringRef Name)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
TrySuggestX86(StringRef Name)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
SIMDIntrinsicsCheck(StringRef Name,ClangTidyContext * Context)79 SIMDIntrinsicsCheck::SIMDIntrinsicsCheck(StringRef Name,
80 ClangTidyContext *Context)
81 : ClangTidyCheck(Name, Context), Std(Options.get("Std", "")),
82 Suggest(Options.get("Suggest", false)) {}
83
storeOptions(ClangTidyOptions::OptionMap & Opts)84 void SIMDIntrinsicsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
85 Options.store(Opts, "Std", Std);
86 Options.store(Opts, "Suggest", Suggest);
87 }
88
registerMatchers(MatchFinder * Finder)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
check(const MatchFinder::MatchResult & Result)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
153