Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 1 | //===--- SIMDIntrinsicsCheck.cpp - clang-tidy------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 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 { |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 20 | namespace portability { |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 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) |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 86 | : ClangTidyCheck(Name, Context), Std(Options.get("Std", "")), |
| 87 | Suggest(Options.get("Suggest", 0) != 0) {} |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 88 | |
| 89 | void SIMDIntrinsicsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 90 | Options.store(Opts, "Std", ""); |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 91 | Options.store(Opts, "Suggest", 0); |
| 92 | } |
| 93 | |
| 94 | void SIMDIntrinsicsCheck::registerMatchers(MatchFinder *Finder) { |
| 95 | if (!getLangOpts().CPlusPlus11) |
| 96 | return; |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 97 | // If Std is not specified, infer it from the language options. |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 98 | // libcxx implementation backports it to C++11 std::experimental::simd. |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 99 | if (Std.empty()) |
| 100 | Std = getLangOpts().CPlusPlus2a ? "std" : "std::experimental"; |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 101 | |
Alexander Kornienko | 976e0c0 | 2018-11-25 02:41:01 +0000 | [diff] [blame] | 102 | Finder->addMatcher(callExpr(callee(functionDecl( |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 103 | matchesName("^::(_mm_|_mm256_|_mm512_|vec_)"), |
Alexander Kornienko | 976e0c0 | 2018-11-25 02:41:01 +0000 | [diff] [blame] | 104 | isVectorFunction())), |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 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 | |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 122 | // We warn or suggest if this SIMD intrinsic function has a std::simd |
| 123 | // replacement. |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 124 | switch (Arch) { |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 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; |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 138 | // We have found a std::simd replacement. |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 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); |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 146 | Message = |
| 147 | llvm::Regex("\\$simd").sub((Std.str() + "::simd").str(), Message); |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 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 | |
Fangrui Song | c0e768d | 2018-03-07 16:57:42 +0000 | [diff] [blame] | 157 | } // namespace portability |
Fangrui Song | 258a959 | 2018-02-15 17:56:43 +0000 | [diff] [blame] | 158 | } // namespace tidy |
| 159 | } // namespace clang |