MPIFunctionClassifier.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //===-- MPIFunctionClassifier.cpp - classifies MPI functions ----*- C++ -*-===//
  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. /// \file
  10. /// This file defines functionality to identify and classify MPI functions.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Checkers/MPIFunctionClassifier.h"
  14. #include "llvm/ADT/STLExtras.h"
  15. namespace clang {
  16. namespace ento {
  17. namespace mpi {
  18. void MPIFunctionClassifier::identifierInit(ASTContext &ASTCtx) {
  19. // Initialize function identifiers.
  20. initPointToPointIdentifiers(ASTCtx);
  21. initCollectiveIdentifiers(ASTCtx);
  22. initAdditionalIdentifiers(ASTCtx);
  23. }
  24. void MPIFunctionClassifier::initPointToPointIdentifiers(ASTContext &ASTCtx) {
  25. // Copy identifiers into the correct classification containers.
  26. IdentInfo_MPI_Send = &ASTCtx.Idents.get("MPI_Send");
  27. MPIPointToPointTypes.push_back(IdentInfo_MPI_Send);
  28. MPIType.push_back(IdentInfo_MPI_Send);
  29. assert(IdentInfo_MPI_Send);
  30. IdentInfo_MPI_Isend = &ASTCtx.Idents.get("MPI_Isend");
  31. MPIPointToPointTypes.push_back(IdentInfo_MPI_Isend);
  32. MPINonBlockingTypes.push_back(IdentInfo_MPI_Isend);
  33. MPIType.push_back(IdentInfo_MPI_Isend);
  34. assert(IdentInfo_MPI_Isend);
  35. IdentInfo_MPI_Ssend = &ASTCtx.Idents.get("MPI_Ssend");
  36. MPIPointToPointTypes.push_back(IdentInfo_MPI_Ssend);
  37. MPIType.push_back(IdentInfo_MPI_Ssend);
  38. assert(IdentInfo_MPI_Ssend);
  39. IdentInfo_MPI_Issend = &ASTCtx.Idents.get("MPI_Issend");
  40. MPIPointToPointTypes.push_back(IdentInfo_MPI_Issend);
  41. MPINonBlockingTypes.push_back(IdentInfo_MPI_Issend);
  42. MPIType.push_back(IdentInfo_MPI_Issend);
  43. assert(IdentInfo_MPI_Issend);
  44. IdentInfo_MPI_Bsend = &ASTCtx.Idents.get("MPI_Bsend");
  45. MPIPointToPointTypes.push_back(IdentInfo_MPI_Bsend);
  46. MPIType.push_back(IdentInfo_MPI_Bsend);
  47. assert(IdentInfo_MPI_Bsend);
  48. IdentInfo_MPI_Ibsend = &ASTCtx.Idents.get("MPI_Ibsend");
  49. MPIPointToPointTypes.push_back(IdentInfo_MPI_Ibsend);
  50. MPINonBlockingTypes.push_back(IdentInfo_MPI_Ibsend);
  51. MPIType.push_back(IdentInfo_MPI_Ibsend);
  52. assert(IdentInfo_MPI_Ibsend);
  53. IdentInfo_MPI_Rsend = &ASTCtx.Idents.get("MPI_Rsend");
  54. MPIPointToPointTypes.push_back(IdentInfo_MPI_Rsend);
  55. MPIType.push_back(IdentInfo_MPI_Rsend);
  56. assert(IdentInfo_MPI_Rsend);
  57. IdentInfo_MPI_Irsend = &ASTCtx.Idents.get("MPI_Irsend");
  58. MPIPointToPointTypes.push_back(IdentInfo_MPI_Irsend);
  59. MPIType.push_back(IdentInfo_MPI_Irsend);
  60. assert(IdentInfo_MPI_Irsend);
  61. IdentInfo_MPI_Recv = &ASTCtx.Idents.get("MPI_Recv");
  62. MPIPointToPointTypes.push_back(IdentInfo_MPI_Recv);
  63. MPIType.push_back(IdentInfo_MPI_Recv);
  64. assert(IdentInfo_MPI_Recv);
  65. IdentInfo_MPI_Irecv = &ASTCtx.Idents.get("MPI_Irecv");
  66. MPIPointToPointTypes.push_back(IdentInfo_MPI_Irecv);
  67. MPINonBlockingTypes.push_back(IdentInfo_MPI_Irecv);
  68. MPIType.push_back(IdentInfo_MPI_Irecv);
  69. assert(IdentInfo_MPI_Irecv);
  70. }
  71. void MPIFunctionClassifier::initCollectiveIdentifiers(ASTContext &ASTCtx) {
  72. // Copy identifiers into the correct classification containers.
  73. IdentInfo_MPI_Scatter = &ASTCtx.Idents.get("MPI_Scatter");
  74. MPICollectiveTypes.push_back(IdentInfo_MPI_Scatter);
  75. MPIPointToCollTypes.push_back(IdentInfo_MPI_Scatter);
  76. MPIType.push_back(IdentInfo_MPI_Scatter);
  77. assert(IdentInfo_MPI_Scatter);
  78. IdentInfo_MPI_Iscatter = &ASTCtx.Idents.get("MPI_Iscatter");
  79. MPICollectiveTypes.push_back(IdentInfo_MPI_Iscatter);
  80. MPIPointToCollTypes.push_back(IdentInfo_MPI_Iscatter);
  81. MPINonBlockingTypes.push_back(IdentInfo_MPI_Iscatter);
  82. MPIType.push_back(IdentInfo_MPI_Iscatter);
  83. assert(IdentInfo_MPI_Iscatter);
  84. IdentInfo_MPI_Gather = &ASTCtx.Idents.get("MPI_Gather");
  85. MPICollectiveTypes.push_back(IdentInfo_MPI_Gather);
  86. MPICollToPointTypes.push_back(IdentInfo_MPI_Gather);
  87. MPIType.push_back(IdentInfo_MPI_Gather);
  88. assert(IdentInfo_MPI_Gather);
  89. IdentInfo_MPI_Igather = &ASTCtx.Idents.get("MPI_Igather");
  90. MPICollectiveTypes.push_back(IdentInfo_MPI_Igather);
  91. MPICollToPointTypes.push_back(IdentInfo_MPI_Igather);
  92. MPINonBlockingTypes.push_back(IdentInfo_MPI_Igather);
  93. MPIType.push_back(IdentInfo_MPI_Igather);
  94. assert(IdentInfo_MPI_Igather);
  95. IdentInfo_MPI_Allgather = &ASTCtx.Idents.get("MPI_Allgather");
  96. MPICollectiveTypes.push_back(IdentInfo_MPI_Allgather);
  97. MPICollToCollTypes.push_back(IdentInfo_MPI_Allgather);
  98. MPIType.push_back(IdentInfo_MPI_Allgather);
  99. assert(IdentInfo_MPI_Allgather);
  100. IdentInfo_MPI_Iallgather = &ASTCtx.Idents.get("MPI_Iallgather");
  101. MPICollectiveTypes.push_back(IdentInfo_MPI_Iallgather);
  102. MPICollToCollTypes.push_back(IdentInfo_MPI_Iallgather);
  103. MPINonBlockingTypes.push_back(IdentInfo_MPI_Iallgather);
  104. MPIType.push_back(IdentInfo_MPI_Iallgather);
  105. assert(IdentInfo_MPI_Iallgather);
  106. IdentInfo_MPI_Bcast = &ASTCtx.Idents.get("MPI_Bcast");
  107. MPICollectiveTypes.push_back(IdentInfo_MPI_Bcast);
  108. MPIPointToCollTypes.push_back(IdentInfo_MPI_Bcast);
  109. MPIType.push_back(IdentInfo_MPI_Bcast);
  110. assert(IdentInfo_MPI_Bcast);
  111. IdentInfo_MPI_Ibcast = &ASTCtx.Idents.get("MPI_Ibcast");
  112. MPICollectiveTypes.push_back(IdentInfo_MPI_Ibcast);
  113. MPIPointToCollTypes.push_back(IdentInfo_MPI_Ibcast);
  114. MPINonBlockingTypes.push_back(IdentInfo_MPI_Ibcast);
  115. MPIType.push_back(IdentInfo_MPI_Ibcast);
  116. assert(IdentInfo_MPI_Ibcast);
  117. IdentInfo_MPI_Reduce = &ASTCtx.Idents.get("MPI_Reduce");
  118. MPICollectiveTypes.push_back(IdentInfo_MPI_Reduce);
  119. MPICollToPointTypes.push_back(IdentInfo_MPI_Reduce);
  120. MPIType.push_back(IdentInfo_MPI_Reduce);
  121. assert(IdentInfo_MPI_Reduce);
  122. IdentInfo_MPI_Ireduce = &ASTCtx.Idents.get("MPI_Ireduce");
  123. MPICollectiveTypes.push_back(IdentInfo_MPI_Ireduce);
  124. MPICollToPointTypes.push_back(IdentInfo_MPI_Ireduce);
  125. MPINonBlockingTypes.push_back(IdentInfo_MPI_Ireduce);
  126. MPIType.push_back(IdentInfo_MPI_Ireduce);
  127. assert(IdentInfo_MPI_Ireduce);
  128. IdentInfo_MPI_Allreduce = &ASTCtx.Idents.get("MPI_Allreduce");
  129. MPICollectiveTypes.push_back(IdentInfo_MPI_Allreduce);
  130. MPICollToCollTypes.push_back(IdentInfo_MPI_Allreduce);
  131. MPIType.push_back(IdentInfo_MPI_Allreduce);
  132. assert(IdentInfo_MPI_Allreduce);
  133. IdentInfo_MPI_Iallreduce = &ASTCtx.Idents.get("MPI_Iallreduce");
  134. MPICollectiveTypes.push_back(IdentInfo_MPI_Iallreduce);
  135. MPICollToCollTypes.push_back(IdentInfo_MPI_Iallreduce);
  136. MPINonBlockingTypes.push_back(IdentInfo_MPI_Iallreduce);
  137. MPIType.push_back(IdentInfo_MPI_Iallreduce);
  138. assert(IdentInfo_MPI_Iallreduce);
  139. IdentInfo_MPI_Alltoall = &ASTCtx.Idents.get("MPI_Alltoall");
  140. MPICollectiveTypes.push_back(IdentInfo_MPI_Alltoall);
  141. MPICollToCollTypes.push_back(IdentInfo_MPI_Alltoall);
  142. MPIType.push_back(IdentInfo_MPI_Alltoall);
  143. assert(IdentInfo_MPI_Alltoall);
  144. IdentInfo_MPI_Ialltoall = &ASTCtx.Idents.get("MPI_Ialltoall");
  145. MPICollectiveTypes.push_back(IdentInfo_MPI_Ialltoall);
  146. MPICollToCollTypes.push_back(IdentInfo_MPI_Ialltoall);
  147. MPINonBlockingTypes.push_back(IdentInfo_MPI_Ialltoall);
  148. MPIType.push_back(IdentInfo_MPI_Ialltoall);
  149. assert(IdentInfo_MPI_Ialltoall);
  150. }
  151. void MPIFunctionClassifier::initAdditionalIdentifiers(ASTContext &ASTCtx) {
  152. IdentInfo_MPI_Comm_rank = &ASTCtx.Idents.get("MPI_Comm_rank");
  153. MPIType.push_back(IdentInfo_MPI_Comm_rank);
  154. assert(IdentInfo_MPI_Comm_rank);
  155. IdentInfo_MPI_Comm_size = &ASTCtx.Idents.get("MPI_Comm_size");
  156. MPIType.push_back(IdentInfo_MPI_Comm_size);
  157. assert(IdentInfo_MPI_Comm_size);
  158. IdentInfo_MPI_Wait = &ASTCtx.Idents.get("MPI_Wait");
  159. MPIType.push_back(IdentInfo_MPI_Wait);
  160. assert(IdentInfo_MPI_Wait);
  161. IdentInfo_MPI_Waitall = &ASTCtx.Idents.get("MPI_Waitall");
  162. MPIType.push_back(IdentInfo_MPI_Waitall);
  163. assert(IdentInfo_MPI_Waitall);
  164. IdentInfo_MPI_Barrier = &ASTCtx.Idents.get("MPI_Barrier");
  165. MPICollectiveTypes.push_back(IdentInfo_MPI_Barrier);
  166. MPIType.push_back(IdentInfo_MPI_Barrier);
  167. assert(IdentInfo_MPI_Barrier);
  168. }
  169. // general identifiers
  170. bool MPIFunctionClassifier::isMPIType(const IdentifierInfo *IdentInfo) const {
  171. return llvm::is_contained(MPIType, IdentInfo);
  172. }
  173. bool MPIFunctionClassifier::isNonBlockingType(
  174. const IdentifierInfo *IdentInfo) const {
  175. return llvm::is_contained(MPINonBlockingTypes, IdentInfo);
  176. }
  177. // point-to-point identifiers
  178. bool MPIFunctionClassifier::isPointToPointType(
  179. const IdentifierInfo *IdentInfo) const {
  180. return llvm::is_contained(MPIPointToPointTypes, IdentInfo);
  181. }
  182. // collective identifiers
  183. bool MPIFunctionClassifier::isCollectiveType(
  184. const IdentifierInfo *IdentInfo) const {
  185. return llvm::is_contained(MPICollectiveTypes, IdentInfo);
  186. }
  187. bool MPIFunctionClassifier::isCollToColl(
  188. const IdentifierInfo *IdentInfo) const {
  189. return llvm::is_contained(MPICollToCollTypes, IdentInfo);
  190. }
  191. bool MPIFunctionClassifier::isScatterType(
  192. const IdentifierInfo *IdentInfo) const {
  193. return IdentInfo == IdentInfo_MPI_Scatter ||
  194. IdentInfo == IdentInfo_MPI_Iscatter;
  195. }
  196. bool MPIFunctionClassifier::isGatherType(
  197. const IdentifierInfo *IdentInfo) const {
  198. return IdentInfo == IdentInfo_MPI_Gather ||
  199. IdentInfo == IdentInfo_MPI_Igather ||
  200. IdentInfo == IdentInfo_MPI_Allgather ||
  201. IdentInfo == IdentInfo_MPI_Iallgather;
  202. }
  203. bool MPIFunctionClassifier::isAllgatherType(
  204. const IdentifierInfo *IdentInfo) const {
  205. return IdentInfo == IdentInfo_MPI_Allgather ||
  206. IdentInfo == IdentInfo_MPI_Iallgather;
  207. }
  208. bool MPIFunctionClassifier::isAlltoallType(
  209. const IdentifierInfo *IdentInfo) const {
  210. return IdentInfo == IdentInfo_MPI_Alltoall ||
  211. IdentInfo == IdentInfo_MPI_Ialltoall;
  212. }
  213. bool MPIFunctionClassifier::isBcastType(const IdentifierInfo *IdentInfo) const {
  214. return IdentInfo == IdentInfo_MPI_Bcast || IdentInfo == IdentInfo_MPI_Ibcast;
  215. }
  216. bool MPIFunctionClassifier::isReduceType(
  217. const IdentifierInfo *IdentInfo) const {
  218. return IdentInfo == IdentInfo_MPI_Reduce ||
  219. IdentInfo == IdentInfo_MPI_Ireduce ||
  220. IdentInfo == IdentInfo_MPI_Allreduce ||
  221. IdentInfo == IdentInfo_MPI_Iallreduce;
  222. }
  223. // additional identifiers
  224. bool MPIFunctionClassifier::isMPI_Wait(const IdentifierInfo *IdentInfo) const {
  225. return IdentInfo == IdentInfo_MPI_Wait;
  226. }
  227. bool MPIFunctionClassifier::isMPI_Waitall(
  228. const IdentifierInfo *IdentInfo) const {
  229. return IdentInfo == IdentInfo_MPI_Waitall;
  230. }
  231. bool MPIFunctionClassifier::isWaitType(const IdentifierInfo *IdentInfo) const {
  232. return IdentInfo == IdentInfo_MPI_Wait || IdentInfo == IdentInfo_MPI_Waitall;
  233. }
  234. } // end of namespace: mpi
  235. } // end of namespace: ento
  236. } // end of namespace: clang