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