blob: d9e3d263634760cfcf6ae29b26d8dd06de15d2d6 [file] [log] [blame]
Fangrui Song258a9592018-02-15 17:56:43 +00001//===--- SIMDIntrinsicsCheck.cpp - clang-tidy------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Fangrui Song258a9592018-02-15 17:56:43 +00006//
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
16using namespace clang::ast_matchers;
17
18namespace clang {
19namespace tidy {
Fangrui Songc0e768d2018-03-07 16:57:42 +000020namespace portability {
Fangrui Song258a9592018-02-15 17:56:43 +000021
22namespace {
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.
27AST_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
41static 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
62static 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
84SIMDIntrinsicsCheck::SIMDIntrinsicsCheck(StringRef Name,
85 ClangTidyContext *Context)
Fangrui Songc0e768d2018-03-07 16:57:42 +000086 : ClangTidyCheck(Name, Context), Std(Options.get("Std", "")),
87 Suggest(Options.get("Suggest", 0) != 0) {}
Fangrui Song258a9592018-02-15 17:56:43 +000088
89void SIMDIntrinsicsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Fangrui Songc0e768d2018-03-07 16:57:42 +000090 Options.store(Opts, "Std", "");
Fangrui Song258a9592018-02-15 17:56:43 +000091 Options.store(Opts, "Suggest", 0);
92}
93
94void SIMDIntrinsicsCheck::registerMatchers(MatchFinder *Finder) {
95 if (!getLangOpts().CPlusPlus11)
96 return;
Fangrui Songc0e768d2018-03-07 16:57:42 +000097 // If Std is not specified, infer it from the language options.
Fangrui Song258a9592018-02-15 17:56:43 +000098 // libcxx implementation backports it to C++11 std::experimental::simd.
Fangrui Songc0e768d2018-03-07 16:57:42 +000099 if (Std.empty())
100 Std = getLangOpts().CPlusPlus2a ? "std" : "std::experimental";
Fangrui Song258a9592018-02-15 17:56:43 +0000101
Alexander Kornienko976e0c02018-11-25 02:41:01 +0000102 Finder->addMatcher(callExpr(callee(functionDecl(
Fangrui Song258a9592018-02-15 17:56:43 +0000103 matchesName("^::(_mm_|_mm256_|_mm512_|vec_)"),
Alexander Kornienko976e0c02018-11-25 02:41:01 +0000104 isVectorFunction())),
Fangrui Song258a9592018-02-15 17:56:43 +0000105 unless(isExpansionInSystemHeader()))
106 .bind("call"),
107 this);
108}
109
110void 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
Fangrui Songc0e768d2018-03-07 16:57:42 +0000122 // We warn or suggest if this SIMD intrinsic function has a std::simd
123 // replacement.
Fangrui Song258a9592018-02-15 17:56:43 +0000124 switch (Arch) {
Fangrui Songc0e768d2018-03-07 16:57:42 +0000125 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;
Fangrui Song258a9592018-02-15 17:56:43 +0000136 }
137
Fangrui Songc0e768d2018-03-07 16:57:42 +0000138 // We have found a std::simd replacement.
Fangrui Song258a9592018-02-15 17:56:43 +0000139 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);
Fangrui Songc0e768d2018-03-07 16:57:42 +0000146 Message =
147 llvm::Regex("\\$simd").sub((Std.str() + "::simd").str(), Message);
Fangrui Song258a9592018-02-15 17:56:43 +0000148 } 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
Fangrui Songc0e768d2018-03-07 16:57:42 +0000157} // namespace portability
Fangrui Song258a9592018-02-15 17:56:43 +0000158} // namespace tidy
159} // namespace clang