IRBuilder.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //===------ PollyIRBuilder.cpp --------------------------------------------===//
  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. // The Polly IRBuilder file contains Polly specific extensions for the IRBuilder
  10. // that are used e.g. to emit the llvm.loop.parallel metadata.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "polly/CodeGen/IRBuilder.h"
  14. #include "polly/ScopInfo.h"
  15. #include "polly/Support/ScopHelper.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/IR/Metadata.h"
  18. using namespace llvm;
  19. using namespace polly;
  20. static const int MaxArraysInAliasScops = 10;
  21. /// Get a self referencing id metadata node.
  22. ///
  23. /// The MDNode looks like this (if arg0/arg1 are not null):
  24. ///
  25. /// '!n = distinct !{!n, arg0, arg1}'
  26. ///
  27. /// @return The self referencing id metadata node.
  28. static MDNode *getID(LLVMContext &Ctx, Metadata *arg0 = nullptr,
  29. Metadata *arg1 = nullptr) {
  30. MDNode *ID;
  31. SmallVector<Metadata *, 3> Args;
  32. // Reserve operand 0 for loop id self reference.
  33. Args.push_back(nullptr);
  34. if (arg0)
  35. Args.push_back(arg0);
  36. if (arg1)
  37. Args.push_back(arg1);
  38. ID = MDNode::getDistinct(Ctx, Args);
  39. ID->replaceOperandWith(0, ID);
  40. return ID;
  41. }
  42. ScopAnnotator::ScopAnnotator() : SE(nullptr), AliasScopeDomain(nullptr) {
  43. // Push an empty staging BandAttr.
  44. LoopAttrEnv.emplace_back();
  45. }
  46. ScopAnnotator::~ScopAnnotator() {
  47. assert(LoopAttrEnv.size() == 1 && "Loop stack imbalance");
  48. assert(!getStagingAttrEnv() && "Forgot to clear staging attr env");
  49. }
  50. void ScopAnnotator::buildAliasScopes(Scop &S) {
  51. SE = S.getSE();
  52. LLVMContext &Ctx = SE->getContext();
  53. AliasScopeDomain = getID(Ctx, MDString::get(Ctx, "polly.alias.scope.domain"));
  54. AliasScopeMap.clear();
  55. OtherAliasScopeListMap.clear();
  56. // We are only interested in arrays, but no scalar references. Scalars should
  57. // be handled easily by basicaa.
  58. SmallVector<ScopArrayInfo *, 10> Arrays;
  59. for (ScopArrayInfo *Array : S.arrays())
  60. if (Array->isArrayKind())
  61. Arrays.push_back(Array);
  62. // The construction of alias scopes is quadratic in the number of arrays
  63. // involved. In case of too many arrays, skip the construction of alias
  64. // information to avoid quadratic increases in compile time and code size.
  65. if (Arrays.size() > MaxArraysInAliasScops)
  66. return;
  67. std::string AliasScopeStr = "polly.alias.scope.";
  68. for (const ScopArrayInfo *Array : Arrays) {
  69. assert(Array->getBasePtr() && "Base pointer must be present");
  70. AliasScopeMap[Array->getBasePtr()] =
  71. getID(Ctx, AliasScopeDomain,
  72. MDString::get(Ctx, (AliasScopeStr + Array->getName()).c_str()));
  73. }
  74. for (const ScopArrayInfo *Array : Arrays) {
  75. MDNode *AliasScopeList = MDNode::get(Ctx, {});
  76. for (const auto &AliasScopePair : AliasScopeMap) {
  77. if (Array->getBasePtr() == AliasScopePair.first)
  78. continue;
  79. Metadata *Args = {AliasScopePair.second};
  80. AliasScopeList =
  81. MDNode::concatenate(AliasScopeList, MDNode::get(Ctx, Args));
  82. }
  83. OtherAliasScopeListMap[Array->getBasePtr()] = AliasScopeList;
  84. }
  85. }
  86. void ScopAnnotator::pushLoop(Loop *L, bool IsParallel) {
  87. ActiveLoops.push_back(L);
  88. if (IsParallel) {
  89. LLVMContext &Ctx = SE->getContext();
  90. MDNode *AccessGroup = MDNode::getDistinct(Ctx, {});
  91. ParallelLoops.push_back(AccessGroup);
  92. }
  93. // Open an empty BandAttr context for loops nested in this one.
  94. LoopAttrEnv.emplace_back();
  95. }
  96. void ScopAnnotator::popLoop(bool IsParallel) {
  97. ActiveLoops.pop_back();
  98. if (IsParallel) {
  99. assert(!ParallelLoops.empty() && "Expected a parallel loop to pop");
  100. ParallelLoops.pop_back();
  101. }
  102. // Exit the subloop context.
  103. assert(!getStagingAttrEnv() && "Forgot to clear staging attr env");
  104. assert(LoopAttrEnv.size() >= 2 && "Popped too many");
  105. LoopAttrEnv.pop_back();
  106. }
  107. void ScopAnnotator::annotateLoopLatch(BranchInst *B, Loop *L, bool IsParallel,
  108. bool IsLoopVectorizerDisabled) const {
  109. LLVMContext &Ctx = SE->getContext();
  110. SmallVector<Metadata *, 3> Args;
  111. // For the LoopID self-reference.
  112. Args.push_back(nullptr);
  113. // Add the user-defined loop properties to the annotation, if any. Any
  114. // additional properties are appended.
  115. // FIXME: What to do if these conflict?
  116. MDNode *MData = nullptr;
  117. if (BandAttr *AttrEnv = getActiveAttrEnv()) {
  118. MData = AttrEnv->Metadata;
  119. if (MData)
  120. llvm::append_range(Args, drop_begin(MData->operands(), 1));
  121. }
  122. if (IsLoopVectorizerDisabled) {
  123. MDString *PropName = MDString::get(Ctx, "llvm.loop.vectorize.enable");
  124. ConstantInt *FalseValue = ConstantInt::get(Type::getInt1Ty(Ctx), 0);
  125. ValueAsMetadata *PropValue = ValueAsMetadata::get(FalseValue);
  126. Args.push_back(MDNode::get(Ctx, {PropName, PropValue}));
  127. }
  128. if (IsParallel) {
  129. MDString *PropName = MDString::get(Ctx, "llvm.loop.parallel_accesses");
  130. MDNode *AccGroup = ParallelLoops.back();
  131. Args.push_back(MDNode::get(Ctx, {PropName, AccGroup}));
  132. }
  133. // No metadata to annotate.
  134. if (!MData && Args.size() <= 1)
  135. return;
  136. // Reuse the MData node if possible, this will avoid having to create another
  137. // one that cannot be merged because LoopIDs are 'distinct'. However, we have
  138. // to create a new one if we add properties.
  139. if (!MData || Args.size() > MData->getNumOperands()) {
  140. MData = MDNode::getDistinct(Ctx, Args);
  141. MData->replaceOperandWith(0, MData);
  142. }
  143. B->setMetadata(LLVMContext::MD_loop, MData);
  144. }
  145. /// Get the pointer operand
  146. ///
  147. /// @param Inst The instruction to be analyzed.
  148. /// @return the pointer operand in case @p Inst is a memory access
  149. /// instruction and nullptr otherwise.
  150. static llvm::Value *getMemAccInstPointerOperand(Instruction *Inst) {
  151. auto MemInst = MemAccInst::dyn_cast(Inst);
  152. if (!MemInst)
  153. return nullptr;
  154. return MemInst.getPointerOperand();
  155. }
  156. /// Find the base pointer of an array access.
  157. ///
  158. /// This should be equivalent to ScalarEvolution::getPointerBase, which we
  159. /// cannot use here the IR is still under construction which ScalarEvolution
  160. /// assumes to not be modified.
  161. static Value *findBasePtr(Value *Val) {
  162. while (true) {
  163. if (auto *Gep = dyn_cast<GEPOperator>(Val)) {
  164. Val = Gep->getPointerOperand();
  165. continue;
  166. }
  167. if (auto *Cast = dyn_cast<BitCastOperator>(Val)) {
  168. Val = Cast->getOperand(0);
  169. continue;
  170. }
  171. break;
  172. }
  173. return Val;
  174. }
  175. void ScopAnnotator::annotate(Instruction *Inst) {
  176. if (!Inst->mayReadOrWriteMemory())
  177. return;
  178. switch (ParallelLoops.size()) {
  179. case 0:
  180. // Not parallel to anything: no access group needed.
  181. break;
  182. case 1:
  183. // Single parallel loop: use directly.
  184. Inst->setMetadata(LLVMContext::MD_access_group,
  185. cast<MDNode>(ParallelLoops.front()));
  186. break;
  187. default:
  188. // Parallel to multiple loops: refer to list of access groups.
  189. Inst->setMetadata(LLVMContext::MD_access_group,
  190. MDNode::get(SE->getContext(),
  191. ArrayRef<Metadata *>(
  192. (Metadata *const *)ParallelLoops.data(),
  193. ParallelLoops.size())));
  194. break;
  195. }
  196. // TODO: Use the ScopArrayInfo once available here.
  197. if (!AliasScopeDomain)
  198. return;
  199. // Do not apply annotations on memory operations that take more than one
  200. // pointer. It would be ambiguous to which pointer the annotation applies.
  201. // FIXME: How can we specify annotations for all pointer arguments?
  202. if (isa<CallInst>(Inst) && !isa<MemSetInst>(Inst))
  203. return;
  204. auto *Ptr = getMemAccInstPointerOperand(Inst);
  205. if (!Ptr)
  206. return;
  207. Value *BasePtr = findBasePtr(Ptr);
  208. if (!BasePtr)
  209. return;
  210. auto AliasScope = AliasScopeMap.lookup(BasePtr);
  211. if (!AliasScope) {
  212. BasePtr = AlternativeAliasBases.lookup(BasePtr);
  213. if (!BasePtr)
  214. return;
  215. AliasScope = AliasScopeMap.lookup(BasePtr);
  216. if (!AliasScope)
  217. return;
  218. }
  219. assert(OtherAliasScopeListMap.count(BasePtr) &&
  220. "BasePtr either expected in AliasScopeMap and OtherAlias...Map");
  221. auto *OtherAliasScopeList = OtherAliasScopeListMap[BasePtr];
  222. Inst->setMetadata("alias.scope", MDNode::get(SE->getContext(), AliasScope));
  223. Inst->setMetadata("noalias", OtherAliasScopeList);
  224. }