SIMDIntrinsicsCheck.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "SIMDIntrinsicsCheck.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/ASTMatchers/ASTMatchFinder.h"
  11. #include "clang/Basic/TargetInfo.h"
  12. #include "llvm/ADT/StringMap.h"
  13. #include "llvm/ADT/Triple.h"
  14. #include "llvm/Support/ManagedStatic.h"
  15. #include "llvm/Support/Regex.h"
  16. using namespace clang::ast_matchers;
  17. namespace clang::tidy::portability {
  18. namespace {
  19. // If the callee has parameter of VectorType or pointer to VectorType,
  20. // or the return type is VectorType, we consider it a vector function
  21. // and a candidate for checking.
  22. AST_MATCHER(FunctionDecl, isVectorFunction) {
  23. bool IsVector = Node.getReturnType()->isVectorType();
  24. for (const ParmVarDecl *Parm : Node.parameters()) {
  25. QualType Type = Parm->getType();
  26. if (Type->isPointerType())
  27. Type = Type->getPointeeType();
  28. if (Type->isVectorType())
  29. IsVector = true;
  30. }
  31. return IsVector;
  32. }
  33. } // namespace
  34. static StringRef trySuggestPpc(StringRef Name) {
  35. if (!Name.consume_front("vec_"))
  36. return {};
  37. return llvm::StringSwitch<StringRef>(Name)
  38. // [simd.alg]
  39. .Case("max", "$std::max")
  40. .Case("min", "$std::min")
  41. // [simd.binary]
  42. .Case("add", "operator+ on $simd objects")
  43. .Case("sub", "operator- on $simd objects")
  44. .Case("mul", "operator* on $simd objects")
  45. .Default({});
  46. }
  47. static StringRef trySuggestX86(StringRef Name) {
  48. if (!(Name.consume_front("_mm_") || Name.consume_front("_mm256_") ||
  49. Name.consume_front("_mm512_")))
  50. return {};
  51. // [simd.alg]
  52. if (Name.startswith("max_"))
  53. return "$simd::max";
  54. if (Name.startswith("min_"))
  55. return "$simd::min";
  56. // [simd.binary]
  57. if (Name.startswith("add_"))
  58. return "operator+ on $simd objects";
  59. if (Name.startswith("sub_"))
  60. return "operator- on $simd objects";
  61. if (Name.startswith("mul_"))
  62. return "operator* on $simd objects";
  63. return {};
  64. }
  65. SIMDIntrinsicsCheck::SIMDIntrinsicsCheck(StringRef Name,
  66. ClangTidyContext *Context)
  67. : ClangTidyCheck(Name, Context), Std(Options.get("Std", "")),
  68. Suggest(Options.get("Suggest", false)) {}
  69. void SIMDIntrinsicsCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
  70. Options.store(Opts, "Std", Std);
  71. Options.store(Opts, "Suggest", Suggest);
  72. }
  73. void SIMDIntrinsicsCheck::registerMatchers(MatchFinder *Finder) {
  74. // If Std is not specified, infer it from the language options.
  75. // libcxx implementation backports it to C++11 std::experimental::simd.
  76. if (Std.empty())
  77. Std = getLangOpts().CPlusPlus20 ? "std" : "std::experimental";
  78. Finder->addMatcher(callExpr(callee(functionDecl(
  79. matchesName("^::(_mm_|_mm256_|_mm512_|vec_)"),
  80. isVectorFunction())),
  81. unless(isExpansionInSystemHeader()))
  82. .bind("call"),
  83. this);
  84. }
  85. void SIMDIntrinsicsCheck::check(const MatchFinder::MatchResult &Result) {
  86. const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call");
  87. assert(Call != nullptr);
  88. const FunctionDecl *Callee = Call->getDirectCallee();
  89. if (!Callee)
  90. return;
  91. StringRef Old = Callee->getName();
  92. StringRef New;
  93. llvm::Triple::ArchType Arch =
  94. Result.Context->getTargetInfo().getTriple().getArch();
  95. // We warn or suggest if this SIMD intrinsic function has a std::simd
  96. // replacement.
  97. switch (Arch) {
  98. default:
  99. break;
  100. case llvm::Triple::ppc:
  101. case llvm::Triple::ppc64:
  102. case llvm::Triple::ppc64le:
  103. New = trySuggestPpc(Old);
  104. break;
  105. case llvm::Triple::x86:
  106. case llvm::Triple::x86_64:
  107. New = trySuggestX86(Old);
  108. break;
  109. }
  110. // We have found a std::simd replacement.
  111. if (!New.empty()) {
  112. // If Suggest is true, give a P0214 alternative, otherwise point it out it
  113. // is non-portable.
  114. if (Suggest) {
  115. static const llvm::Regex StdRegex("\\$std"), SimdRegex("\\$simd");
  116. diag(Call->getExprLoc(), "'%0' can be replaced by %1")
  117. << Old
  118. << SimdRegex.sub(SmallString<32>({Std, "::simd"}),
  119. StdRegex.sub(Std, New));
  120. } else {
  121. diag("'%0' is a non-portable %1 intrinsic function")
  122. << Old << llvm::Triple::getArchTypeName(Arch);
  123. }
  124. }
  125. }
  126. } // namespace clang::tidy::portability