LLVM 24.0.0git
AMDGPUPromoteAlloca.cpp
Go to the documentation of this file.
1//===-- AMDGPUPromoteAlloca.cpp - Promote Allocas -------------------------===//
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// Eliminates allocas by either converting them into vectors or by migrating
10// them to local address space.
11//
12// Two passes are exposed by this file:
13// - "promote-alloca-to-vector", which runs early in the pipeline and only
14// promotes to vector. Promotion to vector is almost always profitable
15// except when the alloca is too big and the promotion would result in
16// very high register pressure.
17// - "promote-alloca", which does both promotion to vector and LDS and runs
18// much later in the pipeline. This runs after SROA because promoting to
19// LDS is of course less profitable than getting rid of the alloca or
20// vectorizing it, thus we only want to do it when the only alternative is
21// lowering the alloca to stack.
22//
23// Note that both of them exist for the old and new PMs. The new PM passes are
24// declared in AMDGPU.h and the legacy PM ones are declared here.s
25//
26//===----------------------------------------------------------------------===//
27
28#include "AMDGPU.h"
29#include "GCNSubtarget.h"
31#include "llvm/ADT/STLExtras.h"
38#include "llvm/IR/IRBuilder.h"
40#include "llvm/IR/IntrinsicsAMDGPU.h"
41#include "llvm/IR/IntrinsicsR600.h"
44#include "llvm/Pass.h"
48
49#define DEBUG_TYPE "amdgpu-promote-alloca"
50
51using namespace llvm;
52
53namespace {
54
55static cl::opt<bool>
56 DisablePromoteAllocaToVector("disable-promote-alloca-to-vector",
57 cl::desc("Disable promote alloca to vector"),
58 cl::init(false));
59
60static cl::opt<bool>
61 DisablePromoteAllocaToLDS("disable-promote-alloca-to-lds",
62 cl::desc("Disable promote alloca to LDS"),
63 cl::init(false));
64
65static cl::opt<unsigned> PromoteAllocaToVectorLimit(
66 "amdgpu-promote-alloca-to-vector-limit",
67 cl::desc("Maximum byte size to consider promote alloca to vector"),
68 cl::init(0));
69
70static cl::opt<unsigned> PromoteAllocaToVectorMaxRegs(
71 "amdgpu-promote-alloca-to-vector-max-regs",
73 "Maximum vector size (in 32b registers) to use when promoting alloca"),
74 cl::init(32));
75
76// Use up to 1/4 of available register budget for vectorization.
77// FIXME: Increase the limit for whole function budgets? Perhaps x2?
78static cl::opt<unsigned> PromoteAllocaToVectorVGPRRatio(
79 "amdgpu-promote-alloca-to-vector-vgpr-ratio",
80 cl::desc("Ratio of VGPRs to budget for promoting alloca to vectors"),
81 cl::init(4));
82
84 LoopUserWeight("promote-alloca-vector-loop-user-weight",
85 cl::desc("The bonus weight of users of allocas within loop "
86 "when sorting profitable allocas"),
87 cl::init(4));
88
89// We support vector indices of the form ((A * stride) >> shift) + B
90// VarIndex is A, VarMul is stride, VarShift is shift and ConstIndex is B. All
91// parts are optional.
92struct GEPToVectorIndex {
93 WeakTrackingVH VarIndex = nullptr; // defaults to 0
94 ConstantInt *VarMul = nullptr; // defaults to 1
95 ConstantInt *VarShift = nullptr; // defaults to 0
96 ConstantInt *ConstIndex = nullptr; // defaults to 0
97 Value *Full = nullptr;
98};
99
100struct MemTransferInfo {
101 ConstantInt *SrcIndex = nullptr;
102 ConstantInt *DestIndex = nullptr;
103};
104
105// Analysis for planning the different strategies of alloca promotion.
106struct AllocaAnalysis {
107 AllocaInst *Alloca = nullptr;
108 DenseSet<Value *> Pointers;
110 unsigned Score = 0;
111 bool HaveSelectOrPHI = false;
112 struct {
113 FixedVectorType *Ty = nullptr;
115 SmallVector<Instruction *> UsersToRemove;
118 } Vector;
119 struct {
120 bool Enable = false;
121 SmallVector<User *> Worklist;
122 } LDS;
123
124 explicit AllocaAnalysis(AllocaInst *Alloca) : Alloca(Alloca) {}
125};
126
127// Shared implementation which can do both promotion to vector and to LDS.
128class AMDGPUPromoteAllocaImpl {
129private:
130 const TargetMachine &TM;
131 LoopInfo &LI;
132 Module &Mod;
133 const DataLayout &DL;
134
135 // FIXME: This should be per-kernel.
136 uint32_t LocalMemLimit = 0;
137 uint32_t CurrentLocalMemUsage = 0;
138 unsigned MaxVGPRs;
139 unsigned VGPRBudgetRatio;
140 unsigned MaxVectorRegs;
141
142 bool IsAMDGCN = false;
143 bool IsAMDHSA = false;
144
145 std::pair<Value *, Value *> getLocalSizeYZ(IRBuilder<> &Builder);
146 Value *getWorkitemID(IRBuilder<> &Builder, unsigned N);
147
148 bool collectAllocaUses(AllocaAnalysis &AA) const;
149
150 /// Val is a derived pointer from Alloca. OpIdx0/OpIdx1 are the operand
151 /// indices to an instruction with 2 pointer inputs (e.g. select, icmp).
152 /// Returns true if both operands are derived from the same alloca. Val should
153 /// be the same value as one of the input operands of UseInst.
154 bool binaryOpIsDerivedFromSameAlloca(Value *Alloca, Value *Val,
155 Instruction *UseInst, int OpIdx0,
156 int OpIdx1) const;
157
158 /// Check whether we have enough local memory for promotion.
159 bool hasSufficientLocalMem(const Function &F);
160
161 FixedVectorType *getVectorTypeForAlloca(Type *AllocaTy) const;
162 void analyzePromoteToVector(AllocaAnalysis &AA) const;
163 void promoteAllocaToVector(AllocaAnalysis &AA);
164 void analyzePromoteToLDS(AllocaAnalysis &AA) const;
165 bool tryPromoteAllocaToLDS(AllocaAnalysis &AA, bool SufficientLDS,
166 SetVector<IntrinsicInst *> &DeferredIntrs);
167 void
168 finishDeferredAllocaToLDSPromotion(SetVector<IntrinsicInst *> &DeferredIntrs);
169
170 void scoreAlloca(AllocaAnalysis &AA) const;
171
172 void setFunctionLimits(const Function &F);
173
174public:
175 AMDGPUPromoteAllocaImpl(TargetMachine &TM, Module &M, LoopInfo &LI)
176 : TM(TM), LI(LI), Mod(M), DL(M.getDataLayout()) {
177 const Triple &TT = M.getTargetTriple();
178 IsAMDGCN = TT.isAMDGCN();
179 IsAMDHSA = TT.getOS() == Triple::AMDHSA;
180 }
181
182 bool run(Function &F, bool PromoteToLDS);
183};
184
185// FIXME: This can create globals so should be a module pass.
186class AMDGPUPromoteAlloca : public FunctionPass {
187public:
188 static char ID;
189
190 AMDGPUPromoteAlloca() : FunctionPass(ID) {}
191
192 bool runOnFunction(Function &F) override {
193 if (skipFunction(F))
194 return false;
195 if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>())
196 return AMDGPUPromoteAllocaImpl(
197 TPC->getTM<TargetMachine>(), *F.getParent(),
198 getAnalysis<LoopInfoWrapperPass>().getLoopInfo())
199 .run(F, /*PromoteToLDS*/ true);
200 return false;
201 }
202
203 StringRef getPassName() const override { return "AMDGPU Promote Alloca"; }
204
205 void getAnalysisUsage(AnalysisUsage &AU) const override {
206 AU.setPreservesCFG();
209 }
210};
211
212static unsigned getMaxVGPRs(unsigned LDSBytes, const TargetMachine &TM,
213 const Function &F) {
214 const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F);
215
216 unsigned DynamicVGPRBlockSize = AMDGPU::getDynamicVGPRBlockSize(F);
217 unsigned MaxVGPRs = ST.getMaxNumVGPRs(
218 ST.getWavesPerEU(ST.getFlatWorkGroupSizes(F), LDSBytes, F).first,
219 DynamicVGPRBlockSize);
220
221 // A non-entry function has only 32 caller preserved registers.
222 // Do not promote alloca which will force spilling unless we know the function
223 // will be inlined.
224 if (!F.hasFnAttribute(Attribute::AlwaysInline) &&
225 !AMDGPU::isEntryFunctionCC(F.getCallingConv()))
226 MaxVGPRs = std::min(MaxVGPRs, 32u);
227 return MaxVGPRs;
228}
229
230} // end anonymous namespace
231
232char AMDGPUPromoteAlloca::ID = 0;
233
235 "AMDGPU promote alloca to vector or LDS", false, false)
236// Move LDS uses from functions to kernels before promote alloca for accurate
237// estimation of LDS available
238INITIALIZE_PASS_DEPENDENCY(AMDGPULowerModuleLDSLegacy)
240INITIALIZE_PASS_END(AMDGPUPromoteAlloca, DEBUG_TYPE,
241 "AMDGPU promote alloca to vector or LDS", false, false)
242
243char &llvm::AMDGPUPromoteAllocaID = AMDGPUPromoteAlloca::ID;
244
247 auto &LI = AM.getResult<LoopAnalysis>(F);
248 bool Changed = AMDGPUPromoteAllocaImpl(TM, *F.getParent(), LI)
249 .run(F, /*PromoteToLDS=*/true);
250 if (Changed) {
253 return PA;
254 }
255 return PreservedAnalyses::all();
256}
257
260 auto &LI = AM.getResult<LoopAnalysis>(F);
261 bool Changed = AMDGPUPromoteAllocaImpl(TM, *F.getParent(), LI)
262 .run(F, /*PromoteToLDS=*/false);
263 if (Changed) {
266 return PA;
267 }
268 return PreservedAnalyses::all();
269}
270
272 return new AMDGPUPromoteAlloca();
273}
274
275bool AMDGPUPromoteAllocaImpl::collectAllocaUses(AllocaAnalysis &AA) const {
276 const auto RejectUser = [&](Instruction *Inst, Twine Msg) {
277 LLVM_DEBUG(dbgs() << " Cannot promote alloca: " << Msg << "\n"
278 << " " << *Inst << "\n");
279 return false;
280 };
281
282 SmallVector<Instruction *, 4> WorkList({AA.Alloca});
283 while (!WorkList.empty()) {
284 auto *Cur = WorkList.pop_back_val();
285 if (find(AA.Pointers, Cur) != AA.Pointers.end())
286 continue;
287 AA.Pointers.insert(Cur);
288 for (auto &U : Cur->uses()) {
289 auto *Inst = cast<Instruction>(U.getUser());
290 if (isa<StoreInst>(Inst)) {
291 if (U.getOperandNo() != StoreInst::getPointerOperandIndex()) {
292 return RejectUser(Inst, "pointer escapes via store");
293 }
294 }
295 AA.Uses.push_back(&U);
296
297 if (isa<GetElementPtrInst>(U.getUser())) {
298 WorkList.push_back(Inst);
299 } else if (auto *SI = dyn_cast<SelectInst>(Inst)) {
300 // Only promote a select if we know that the other select operand is
301 // from another pointer that will also be promoted.
302 if (!binaryOpIsDerivedFromSameAlloca(AA.Alloca, Cur, SI, 1, 2))
303 return RejectUser(Inst, "select from mixed objects");
304 WorkList.push_back(Inst);
305 AA.HaveSelectOrPHI = true;
306 } else if (auto *Phi = dyn_cast<PHINode>(Inst)) {
307 // Repeat for phis.
308
309 // TODO: Handle more complex cases. We should be able to replace loops
310 // over arrays.
311 switch (Phi->getNumIncomingValues()) {
312 case 1:
313 break;
314 case 2:
315 if (!binaryOpIsDerivedFromSameAlloca(AA.Alloca, Cur, Phi, 0, 1))
316 return RejectUser(Inst, "phi from mixed objects");
317 break;
318 default:
319 return RejectUser(Inst, "phi with too many operands");
320 }
321
322 WorkList.push_back(Inst);
323 AA.HaveSelectOrPHI = true;
324 }
325 }
326 }
327 return true;
328}
329
330void AMDGPUPromoteAllocaImpl::scoreAlloca(AllocaAnalysis &AA) const {
331 LLVM_DEBUG(dbgs() << "Scoring: " << *AA.Alloca << "\n");
332 unsigned Score = 0;
333 // Increment score by one for each user + a bonus for users within loops.
334 for (auto *U : AA.Uses) {
335 Instruction *Inst = cast<Instruction>(U->getUser());
336 if (isa<GetElementPtrInst>(Inst) || isa<SelectInst>(Inst) ||
337 isa<PHINode>(Inst))
338 continue;
339 unsigned UserScore =
340 1 + (LoopUserWeight * LI.getLoopDepth(Inst->getParent()));
341 LLVM_DEBUG(dbgs() << " [+" << UserScore << "]:\t" << *Inst << "\n");
342 Score += UserScore;
343 }
344 LLVM_DEBUG(dbgs() << " => Final Score:" << Score << "\n");
345 AA.Score = Score;
346}
347
348void AMDGPUPromoteAllocaImpl::setFunctionLimits(const Function &F) {
349 // Load per function limits, overriding with global options where appropriate.
350 // R600 register tuples/aliasing are fragile with large vector promotions so
351 // apply architecture specific limit here.
352 const int R600MaxVectorRegs = 16;
353 MaxVectorRegs = F.getFnAttributeAsParsedInteger(
354 "amdgpu-promote-alloca-to-vector-max-regs",
355 IsAMDGCN ? PromoteAllocaToVectorMaxRegs : R600MaxVectorRegs);
356 if (PromoteAllocaToVectorMaxRegs.getNumOccurrences())
357 MaxVectorRegs = PromoteAllocaToVectorMaxRegs;
358 VGPRBudgetRatio = F.getFnAttributeAsParsedInteger(
359 "amdgpu-promote-alloca-to-vector-vgpr-ratio",
360 PromoteAllocaToVectorVGPRRatio);
361 if (PromoteAllocaToVectorVGPRRatio.getNumOccurrences())
362 VGPRBudgetRatio = PromoteAllocaToVectorVGPRRatio;
363}
364
365bool AMDGPUPromoteAllocaImpl::run(Function &F, bool PromoteToLDS) {
366 if (DisablePromoteAllocaToLDS && DisablePromoteAllocaToVector)
367 return false;
368
369 bool SufficientLDS = PromoteToLDS && hasSufficientLocalMem(F);
370 MaxVGPRs = IsAMDGCN ? getMaxVGPRs(CurrentLocalMemUsage, TM, F) : 128;
371 setFunctionLimits(F);
372
373 unsigned VectorizationBudget =
374 (PromoteAllocaToVectorLimit ? PromoteAllocaToVectorLimit * 8
375 : (MaxVGPRs * 32)) /
376 VGPRBudgetRatio;
377
378 std::vector<AllocaAnalysis> Allocas;
379 for (Instruction &I : F.getEntryBlock()) {
380 if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
381 // Array allocations are probably not worth handling, since an allocation
382 // of the array type is the canonical form.
383 if (!AI->isStaticAlloca() || AI->isArrayAllocation())
384 continue;
385
386 LLVM_DEBUG(dbgs() << "Analyzing: " << *AI << '\n');
387
388 AllocaAnalysis AA{AI};
389 if (collectAllocaUses(AA)) {
390 analyzePromoteToVector(AA);
391 if (PromoteToLDS)
392 analyzePromoteToLDS(AA);
393 if (AA.Vector.Ty || AA.LDS.Enable) {
394 scoreAlloca(AA);
395 Allocas.push_back(std::move(AA));
396 }
397 }
398 }
399 }
400
401 stable_sort(Allocas,
402 [](const auto &A, const auto &B) { return A.Score > B.Score; });
403
404 // clang-format off
406 dbgs() << "Sorted Worklist:\n";
407 for (const auto &AA : Allocas)
408 dbgs() << " " << *AA.Alloca << "\n";
409 );
410 // clang-format on
411
412 bool Changed = false;
413 SetVector<IntrinsicInst *> DeferredIntrs;
414 for (AllocaAnalysis &AA : Allocas) {
415 if (AA.Vector.Ty) {
416 std::optional<TypeSize> Size = AA.Alloca->getAllocationSize(DL);
417 assert(Size); // Expected to succeed on non-array alloca.
418 const unsigned AllocaCost = Size->getFixedValue() * 8;
419 // First, check if we have enough budget to vectorize this alloca.
420 if (AllocaCost <= VectorizationBudget) {
421 promoteAllocaToVector(AA);
422 Changed = true;
423 assert((VectorizationBudget - AllocaCost) < VectorizationBudget &&
424 "Underflow!");
425 VectorizationBudget -= AllocaCost;
426 LLVM_DEBUG(dbgs() << " Remaining vectorization budget:"
427 << VectorizationBudget << "\n");
428 continue;
429 } else {
430 LLVM_DEBUG(dbgs() << "Alloca too big for vectorization (size:"
431 << AllocaCost << ", budget:" << VectorizationBudget
432 << "): " << *AA.Alloca << "\n");
433 }
434 }
435
436 if (AA.LDS.Enable &&
437 tryPromoteAllocaToLDS(AA, SufficientLDS, DeferredIntrs))
438 Changed = true;
439 }
440 finishDeferredAllocaToLDSPromotion(DeferredIntrs);
441
442 // NOTE: tryPromoteAllocaToVector removes the alloca, so Allocas contains
443 // dangling pointers. If we want to reuse it past this point, the loop above
444 // would need to be updated to remove successfully promoted allocas.
445
446 return Changed;
447}
448
449// Checks if the instruction I is a memset user of the alloca AI that we can
450// deal with. Currently, only non-volatile memsets that affect the whole alloca
451// are handled.
453 const DataLayout &DL) {
454 using namespace PatternMatch;
455 // For now we only care about non-volatile memsets that affect the whole type
456 // (start at index 0 and fill the whole alloca).
457 //
458 // TODO: Now that we moved to PromoteAlloca we could handle any memsets
459 // (except maybe volatile ones?) - we just need to use shufflevector if it
460 // only affects a subset of the vector.
461 const unsigned Size = DL.getTypeStoreSize(AI->getAllocatedType());
462 return I->getOperand(0) == AI &&
463 match(I->getOperand(2), m_SpecificInt(Size)) && !I->isVolatile();
464}
465
466static Value *calculateVectorIndex(Value *Ptr, AllocaAnalysis &AA) {
467 IRBuilder<> B(Ptr->getContext());
468
469 Ptr = Ptr->stripPointerCasts();
470 if (Ptr == AA.Alloca)
471 return B.getInt32(0);
472
473 auto *GEP = cast<GetElementPtrInst>(Ptr);
474 auto I = AA.Vector.GEPVectorIdx.find(GEP);
475 assert(I != AA.Vector.GEPVectorIdx.end() && "Must have entry for GEP!");
476
477 if (!I->second.Full) {
478 Value *Result = nullptr;
479 B.SetInsertPoint(GEP);
480
481 if (I->second.VarIndex) {
482 Result = I->second.VarIndex;
483 Result = B.CreateSExtOrTrunc(Result, B.getInt32Ty());
484
485 if (I->second.VarMul)
486 Result = B.CreateMul(Result, I->second.VarMul);
487
488 if (I->second.VarShift)
489 Result = B.CreateAShr(Result, I->second.VarShift, "", /*isExact*/ true);
490 }
491
492 if (I->second.ConstIndex) {
493 if (Result)
494 Result = B.CreateAdd(Result, I->second.ConstIndex);
495 else
496 Result = I->second.ConstIndex;
497 }
498
499 if (!Result)
500 Result = B.getInt32(0);
501
502 I->second.Full = Result;
503 }
504
505 return I->second.Full;
506}
507
508static std::optional<GEPToVectorIndex>
510 Type *VecElemTy, const DataLayout &DL) {
511 // TODO: Extracting a "multiple of X" from a GEP might be a useful generic
512 // helper.
513 LLVMContext &Ctx = GEP->getContext();
514 unsigned BW = DL.getIndexTypeSizeInBits(GEP->getType());
516 APInt ConstOffset(BW, 0);
517
518 // Walk backwards through nested GEPs to collect both constant and variable
519 // offsets, so that nested vector GEP chains can be lowered in one step.
520 //
521 // Given this IR fragment as input:
522 //
523 // %0 = alloca [10 x <2 x i32>], align 8, addrspace(5)
524 // %1 = getelementptr [10 x <2 x i32>], ptr addrspace(5) %0, i32 0, i32 %j
525 // %2 = getelementptr i8, ptr addrspace(5) %1, i32 4
526 // %3 = load i32, ptr addrspace(5) %2, align 4
527 //
528 // Combine both GEP operations in a single pass, producing:
529 // BasePtr = %0
530 // ConstOffset = 4
531 // VarOffsets = { %j -> element_size(<2 x i32>) }
532 //
533 // That lets us emit a single buffer_load directly into a VGPR, without ever
534 // allocating scratch memory for the intermediate pointer.
535 Value *CurPtr = GEP;
536 while (auto *CurGEP = dyn_cast<GetElementPtrInst>(CurPtr)) {
537 if (!CurGEP->collectOffset(DL, BW, VarOffsets, ConstOffset))
538 return {};
539
540 // Move to the next outer pointer.
541 CurPtr = CurGEP->getPointerOperand();
542 }
543
544 assert(CurPtr == Alloca && "GEP not based on alloca");
545
546 int64_t VecElemSize = DL.getTypeAllocSize(VecElemTy);
547 if (VarOffsets.size() > 1)
548 return {};
549
550 // We support vector indices of the form ((VarIndex * stride) >> shift) + B.
551 // IndexQuot represents B. Check that the constant offset is a multiple
552 // of the vector element size.
553 if (ConstOffset.srem(VecElemSize) != 0)
554 return {};
555 APInt IndexQuot = ConstOffset.sdiv(VecElemSize);
556
557 GEPToVectorIndex Result;
558
559 if (!ConstOffset.isZero())
560 Result.ConstIndex = ConstantInt::get(Ctx, IndexQuot.sextOrTrunc(BW));
561
562 // If there are no variable offsets, only a constant offset, then we're done.
563 if (VarOffsets.empty())
564 return Result;
565
566 // Scale is the stride in the (A * stride) part. Check that there is only one
567 // variable offset and extract the scale factor.
568 const auto &VarOffset = VarOffsets.front();
569 auto ScaleOpt = VarOffset.second.tryZExtValue();
570 if (!ScaleOpt || *ScaleOpt == 0)
571 return {};
572
573 uint64_t Scale = *ScaleOpt;
574 Result.VarIndex = VarOffset.first;
575 auto *OffsetType = dyn_cast<IntegerType>(Result.VarIndex->getType());
576 if (!OffsetType)
577 return {};
578
579 // The vector index for the variable part is: VarIndex * Scale / VecElemSize.
580 if (Scale >= (uint64_t)VecElemSize) {
581 if (Scale % VecElemSize != 0)
582 return {};
583
584 // Scale is a multiple of VecElemSize, so the index is just: VarIndex *
585 // (Scale / VecElemSize).
586 uint64_t VarMul = Scale / VecElemSize;
587 // Only the multiplier is needed.
588 if (VarMul != 1)
589 Result.VarMul = ConstantInt::get(Ctx, APInt(BW, VarMul));
590 } else {
591 if ((uint64_t)VecElemSize % Scale != 0)
592 return {};
593
594 // VecElemSize is a multiple of Scale, so the index is just: VarIndex /
595 // (VecElemSize / Scale).
596 uint64_t Divisor = VecElemSize / Scale;
597 // The divisor must be a power of 2 so we can use a right shift.
598 if (!isPowerOf2_64(Divisor))
599 return {};
600
601 // VarIndex must be known to be divisible by that divisor.
602 KnownBits KB = computeKnownBits(VarOffset.first, DL);
603 if (KB.countMinTrailingZeros() < Log2_64(Divisor))
604 return {};
605
606 Result.VarShift = ConstantInt::get(Ctx, APInt(BW, Log2_64(Divisor)));
607 }
608
609 return Result;
610}
611
612/// Promotes a single user of the alloca to a vector form.
613///
614/// \param Inst Instruction to be promoted.
615/// \param DL Module Data Layout.
616/// \param AA Alloca Analysis.
617/// \param VecStoreSize Size of \p VectorTy in bytes.
618/// \param ElementSize Size of \p VectorTy element type in bytes.
619/// \param CurVal Current value of the vector (e.g. last stored value)
620/// \param[out] DeferredLoads \p Inst is added to this vector if it can't
621/// be promoted now. This happens when promoting requires \p
622/// CurVal, but \p CurVal is nullptr.
623/// \return the stored value if \p Inst would have written to the alloca, or
624/// nullptr otherwise.
626 AllocaAnalysis &AA,
627 unsigned VecStoreSize,
628 unsigned ElementSize,
629 function_ref<Value *()> GetCurVal) {
630 // Note: we use InstSimplifyFolder because it can leverage the DataLayout
631 // to do more folding, especially in the case of vector splats.
634 Builder.SetInsertPoint(Inst);
635
636 Type *VecEltTy = AA.Vector.Ty->getElementType();
637
638 switch (Inst->getOpcode()) {
639 case Instruction::Load: {
640 Value *CurVal = GetCurVal();
641 Value *Index =
643
644 // We're loading the full vector.
645 Type *AccessTy = Inst->getType();
646 TypeSize AccessSize = DL.getTypeStoreSize(AccessTy);
647 if (Constant *CI = dyn_cast<Constant>(Index)) {
648 if (CI->isNullValue() && AccessSize == VecStoreSize) {
649 Inst->replaceAllUsesWith(
650 Builder.CreateBitPreservingCastChain(DL, CurVal, AccessTy));
651 return nullptr;
652 }
653 }
654
655 // Loading a subvector, or a scalar that spans several elements.
656 TypeSize EltSize = DL.getTypeStoreSize(VecEltTy);
657 assert(AccessSize.isKnownMultipleOf(EltSize) &&
658 "promotable access must cover a whole number of elements");
659 const unsigned NumLoadedElts = AccessSize / EltSize;
660 if (NumLoadedElts > 1) {
661 auto *SubVecTy = FixedVectorType::get(VecEltTy, NumLoadedElts);
662 assert(DL.getTypeStoreSize(SubVecTy) == DL.getTypeStoreSize(AccessTy));
663
664 // If idx is dynamic, then sandwich load with bitcasts.
665 // ie. VectorTy SubVecTy AccessTy
666 // <64 x i8> -> <16 x i8> <8 x i16>
667 // <64 x i8> -> <4 x i128> -> i128 -> <8 x i16>
668 // Extracting subvector with dynamic index has very large expansion in
669 // the amdgpu backend. Limit to pow2.
670 FixedVectorType *VectorTy = AA.Vector.Ty;
671 TypeSize NumBits = DL.getTypeStoreSize(SubVecTy) * 8u;
672 uint64_t LoadAlign = cast<LoadInst>(Inst)->getAlign().value();
673 bool IsAlignedLoad = NumBits <= (LoadAlign * 8u);
674 unsigned TotalNumElts = VectorTy->getNumElements();
675 bool IsProperlyDivisible = TotalNumElts % NumLoadedElts == 0;
676 if (!isa<ConstantInt>(Index) &&
677 llvm::isPowerOf2_32(SubVecTy->getNumElements()) &&
678 IsProperlyDivisible && IsAlignedLoad) {
679 IntegerType *NewElemTy = Builder.getIntNTy(NumBits);
680 const unsigned NewNumElts =
681 DL.getTypeStoreSize(VectorTy) * 8u / NumBits;
682 const unsigned LShrAmt = llvm::Log2_32(SubVecTy->getNumElements());
683 FixedVectorType *BitCastTy =
684 FixedVectorType::get(NewElemTy, NewNumElts);
685 Value *BCVal =
686 Builder.CreateBitPreservingCastChain(DL, CurVal, BitCastTy);
687 Value *NewIdx = Builder.CreateLShr(
688 Index, ConstantInt::get(Index->getType(), LShrAmt));
689 Value *ExtVal = Builder.CreateExtractElement(BCVal, NewIdx);
690 Value *BCOut =
691 Builder.CreateBitPreservingCastChain(DL, ExtVal, AccessTy);
692 Inst->replaceAllUsesWith(BCOut);
693 return nullptr;
694 }
695
696 Value *SubVec = PoisonValue::get(SubVecTy);
697 for (unsigned K = 0; K < NumLoadedElts; ++K) {
698 Value *CurIdx =
699 Builder.CreateAdd(Index, ConstantInt::get(Index->getType(), K));
700 SubVec = Builder.CreateInsertElement(
701 SubVec, Builder.CreateExtractElement(CurVal, CurIdx), K);
702 }
703
704 Inst->replaceAllUsesWith(
705 Builder.CreateBitPreservingCastChain(DL, SubVec, AccessTy));
706 return nullptr;
707 }
708
709 // We're loading one element.
710 Value *ExtractElement = Builder.CreateExtractElement(CurVal, Index);
711 if (AccessTy != VecEltTy)
712 ExtractElement = Builder.CreateBitOrPointerCast(ExtractElement, AccessTy);
713
714 Inst->replaceAllUsesWith(ExtractElement);
715 return nullptr;
716 }
717 case Instruction::Store: {
718 // For stores, it's a bit trickier and it depends on whether we're storing
719 // the full vector or not. If we're storing the full vector, we don't need
720 // to know the current value. If this is a store of a single element, we
721 // need to know the value.
723 Value *Index = calculateVectorIndex(SI->getPointerOperand(), AA);
724 Value *Val = SI->getValueOperand();
725
726 // We're storing the full vector, we can handle this without knowing CurVal.
727 Type *AccessTy = Val->getType();
728 TypeSize AccessSize = DL.getTypeStoreSize(AccessTy);
729 if (Constant *CI = dyn_cast<Constant>(Index))
730 if (CI->isNullValue() && AccessSize == VecStoreSize)
731 return Builder.CreateBitPreservingCastChain(DL, Val, AA.Vector.Ty);
732
733 // Storing a subvector, or a scalar that spans several elements.
734 TypeSize EltSize = DL.getTypeStoreSize(VecEltTy);
735 assert(AccessSize.isKnownMultipleOf(EltSize) &&
736 "promotable access must cover a whole number of elements");
737 const unsigned NumWrittenElts = AccessSize / EltSize;
738 if (NumWrittenElts > 1) {
739 const unsigned NumVecElts = AA.Vector.Ty->getNumElements();
740 auto *SubVecTy = FixedVectorType::get(VecEltTy, NumWrittenElts);
741 assert(DL.getTypeStoreSize(SubVecTy) == DL.getTypeStoreSize(AccessTy));
742
743 Val = Builder.CreateBitPreservingCastChain(DL, Val, SubVecTy);
744 Value *CurVec = GetCurVal();
745 for (unsigned K = 0, NumElts = std::min(NumWrittenElts, NumVecElts);
746 K < NumElts; ++K) {
747 Value *CurIdx =
748 Builder.CreateAdd(Index, ConstantInt::get(Index->getType(), K));
749 CurVec = Builder.CreateInsertElement(
750 CurVec, Builder.CreateExtractElement(Val, K), CurIdx);
751 }
752 return CurVec;
753 }
754
755 if (Val->getType() != VecEltTy)
756 Val = Builder.CreateBitOrPointerCast(Val, VecEltTy);
757 return Builder.CreateInsertElement(GetCurVal(), Val, Index);
758 }
759 case Instruction::Call: {
760 if (auto *MTI = dyn_cast<MemTransferInst>(Inst)) {
761 // For memcpy, we need to know curval.
762 ConstantInt *Length = cast<ConstantInt>(MTI->getLength());
763 unsigned NumCopied = Length->getZExtValue() / ElementSize;
764 MemTransferInfo *TI = &AA.Vector.TransferInfo[MTI];
765 unsigned SrcBegin = TI->SrcIndex->getZExtValue();
766 unsigned DestBegin = TI->DestIndex->getZExtValue();
767
768 SmallVector<int> Mask;
769 for (unsigned Idx = 0; Idx < AA.Vector.Ty->getNumElements(); ++Idx) {
770 if (Idx >= DestBegin && Idx < DestBegin + NumCopied) {
771 Mask.push_back(SrcBegin < AA.Vector.Ty->getNumElements()
772 ? SrcBegin++
774 } else {
775 Mask.push_back(Idx);
776 }
777 }
778
779 return Builder.CreateShuffleVector(GetCurVal(), Mask);
780 }
781
782 if (auto *MSI = dyn_cast<MemSetInst>(Inst)) {
783 // For memset, we don't need to know the previous value because we
784 // currently only allow memsets that cover the whole alloca.
785 Value *Elt = MSI->getOperand(1);
786 const unsigned BytesPerElt = DL.getTypeStoreSize(VecEltTy);
787 if (BytesPerElt > 1) {
788 Value *EltBytes = Builder.CreateVectorSplat(BytesPerElt, Elt);
789
790 // If the element type of the vector is a pointer, we need to first cast
791 // to an integer, then use a PtrCast.
792 if (VecEltTy->isPointerTy()) {
793 Type *PtrInt = Builder.getIntNTy(BytesPerElt * 8);
794 Elt = Builder.CreateBitCast(EltBytes, PtrInt);
795 Elt = Builder.CreateIntToPtr(Elt, VecEltTy);
796 } else
797 Elt = Builder.CreateBitCast(EltBytes, VecEltTy);
798 }
799
800 return Builder.CreateVectorSplat(AA.Vector.Ty->getElementCount(), Elt);
801 }
802
803 if (auto *Intr = dyn_cast<IntrinsicInst>(Inst)) {
804 if (Intr->getIntrinsicID() == Intrinsic::objectsize) {
805 Intr->replaceAllUsesWith(
806 Builder.getIntN(Intr->getType()->getIntegerBitWidth(),
807 DL.getTypeAllocSize(AA.Vector.Ty)));
808 return nullptr;
809 }
810 }
811
812 llvm_unreachable("Unsupported call when promoting alloca to vector");
813 }
814
815 default:
816 llvm_unreachable("Inconsistency in instructions promotable to vector");
817 }
818
819 llvm_unreachable("Did not return after promoting instruction!");
820}
821
822static bool isSupportedAccessType(FixedVectorType *VecTy, Type *AccessTy,
823 const DataLayout &DL) {
824 // An access that covers several elements can work if its size is a multiple
825 // of the size of the alloca's vector element type, since it can be split
826 // across consecutive elements. This covers accesses by a vector type, as well
827 // as scalar accesses that are wider than one element, which happens when an
828 // object is written one element at a time but read back in wider pieces.
829 //
830 // Examples:
831 // - VecTy = <8 x float>, AccessTy = <4 x float> -> OK
832 // - VecTy = <4 x double>, AccessTy = <2 x float> -> OK
833 // - VecTy = <4 x double>, AccessTy = <3 x float> -> NOT OK
834 // - 3*32 is not a multiple of 64
835 // - VecTy = <8 x i32>, AccessTy = i64 -> OK
836 //
837 // We could handle more complicated cases, but it'd make things a lot more
838 // complicated.
839 if (isa<FixedVectorType>(AccessTy) || AccessTy->isIntegerTy() ||
840 AccessTy->isFloatingPointTy()) {
841 TypeSize AccTS = DL.getTypeStoreSize(AccessTy);
842 TypeSize VecTS = DL.getTypeStoreSize(VecTy->getElementType());
843 // If the type size and the store size don't match, we would need to do more
844 // than just bitcast to translate between an extracted/insertable subvectors
845 // and the accessed value.
846 if (AccTS * 8 == DL.getTypeSizeInBits(AccessTy) && AccTS > VecTS &&
847 AccTS.isKnownMultipleOf(VecTS))
848 return true;
849 }
850
851 // An access that covers exactly one element only needs a cast.
853 DL);
854}
855
856/// Iterates over an instruction worklist that may contain multiple instructions
857/// from the same basic block, but in a different order.
858template <typename InstContainer>
859static void forEachWorkListItem(const InstContainer &WorkList,
860 std::function<void(Instruction *)> Fn) {
861 // Bucket up uses of the alloca by the block they occur in.
862 // This is important because we have to handle multiple defs/uses in a block
863 // ourselves: SSAUpdater is purely for cross-block references.
865 for (Instruction *User : WorkList)
866 UsesByBlock[User->getParent()].insert(User);
867
868 for (Instruction *User : WorkList) {
869 BasicBlock *BB = User->getParent();
870 auto &BlockUses = UsesByBlock[BB];
871
872 // Already processed, skip.
873 if (BlockUses.empty())
874 continue;
875
876 // Only user in the block, directly process it.
877 if (BlockUses.size() == 1) {
878 Fn(User);
879 continue;
880 }
881
882 // Multiple users in the block, do a linear scan to see users in order.
883 for (Instruction &Inst : *BB) {
884 if (!BlockUses.contains(&Inst))
885 continue;
886
887 Fn(&Inst);
888 }
889
890 // Clear the block so we know it's been processed.
891 BlockUses.clear();
892 }
893}
894
895/// Find an insert point after an alloca, after all other allocas clustered at
896/// the start of the block.
899 for (BasicBlock::iterator E = BB.end(); I != E && isa<AllocaInst>(*I); ++I)
900 ;
901 return I;
902}
903
905AMDGPUPromoteAllocaImpl::getVectorTypeForAlloca(Type *AllocaTy) const {
906 if (DisablePromoteAllocaToVector) {
907 LLVM_DEBUG(dbgs() << " Promote alloca to vectors is disabled\n");
908 return nullptr;
909 }
910
911 auto *VectorTy = dyn_cast<FixedVectorType>(AllocaTy);
912 if (auto *ArrayTy = dyn_cast<ArrayType>(AllocaTy)) {
913 uint64_t NumElems = 1;
914 Type *ElemTy;
915 do {
916 NumElems *= ArrayTy->getNumElements();
917 ElemTy = ArrayTy->getElementType();
918 } while ((ArrayTy = dyn_cast<ArrayType>(ElemTy)));
919
920 // Check for array of vectors
921 auto *InnerVectorTy = dyn_cast<FixedVectorType>(ElemTy);
922 if (InnerVectorTy) {
923 NumElems *= InnerVectorTy->getNumElements();
924 ElemTy = InnerVectorTy->getElementType();
925 }
926
927 if (VectorType::isValidElementType(ElemTy) && NumElems > 0) {
928 unsigned ElementSize = DL.getTypeSizeInBits(ElemTy) / 8;
929 if (ElementSize > 0) {
930 unsigned AllocaSize = DL.getTypeStoreSize(AllocaTy);
931 // Expand vector if required to match padding of inner type,
932 // i.e. odd size subvectors.
933 // Storage size of new vector must match that of alloca for correct
934 // behaviour of byte offsets and GEP computation.
935 if (NumElems * ElementSize != AllocaSize)
936 NumElems = AllocaSize / ElementSize;
937 if (NumElems > 0 && (AllocaSize % ElementSize) == 0)
938 VectorTy = FixedVectorType::get(ElemTy, NumElems);
939 }
940 }
941 }
942 if (!VectorTy) {
943 LLVM_DEBUG(dbgs() << " Cannot convert type to vector\n");
944 return nullptr;
945 }
946
947 const unsigned MaxElements =
948 (MaxVectorRegs * 32) / DL.getTypeSizeInBits(VectorTy->getElementType());
949
950 if (VectorTy->getNumElements() > MaxElements ||
951 VectorTy->getNumElements() < 2) {
952 LLVM_DEBUG(dbgs() << " " << *VectorTy
953 << " has an unsupported number of elements\n");
954 return nullptr;
955 }
956
957 Type *VecEltTy = VectorTy->getElementType();
958 unsigned ElementSizeInBits = DL.getTypeSizeInBits(VecEltTy);
959 if (ElementSizeInBits != DL.getTypeAllocSizeInBits(VecEltTy)) {
960 LLVM_DEBUG(dbgs() << " Cannot convert to vector if the allocation size "
961 "does not match the type's size\n");
962 return nullptr;
963 }
964
965 return VectorTy;
966}
967
968void AMDGPUPromoteAllocaImpl::analyzePromoteToVector(AllocaAnalysis &AA) const {
969 if (AA.HaveSelectOrPHI) {
970 LLVM_DEBUG(dbgs() << " Cannot convert to vector due to select or phi\n");
971 return;
972 }
973
974 Type *AllocaTy = AA.Alloca->getAllocatedType();
975 AA.Vector.Ty = getVectorTypeForAlloca(AllocaTy);
976 if (!AA.Vector.Ty)
977 return;
978
979 const auto RejectUser = [&](Instruction *Inst, Twine Msg) {
980 LLVM_DEBUG(dbgs() << " Cannot promote alloca to vector: " << Msg << "\n"
981 << " " << *Inst << "\n");
982 AA.Vector.Ty = nullptr;
983 };
984
985 Type *VecEltTy = AA.Vector.Ty->getElementType();
986 unsigned ElementSize = DL.getTypeSizeInBits(VecEltTy) / 8;
987 assert(ElementSize > 0);
988 for (auto *U : AA.Uses) {
989 Instruction *Inst = cast<Instruction>(U->getUser());
990
991 if (Value *Ptr = getLoadStorePointerOperand(Inst)) {
992 assert(!isa<StoreInst>(Inst) ||
993 U->getOperandNo() == StoreInst::getPointerOperandIndex());
994
995 Type *AccessTy = getLoadStoreType(Inst);
996 if (AccessTy->isAggregateType())
997 return RejectUser(Inst, "unsupported load/store as aggregate");
998 assert(!AccessTy->isAggregateType() || AccessTy->isArrayTy());
999
1000 // Check that this is a simple access of a vector element.
1001 bool IsSimple = isa<LoadInst>(Inst) ? cast<LoadInst>(Inst)->isSimple()
1002 : cast<StoreInst>(Inst)->isSimple();
1003 if (!IsSimple)
1004 return RejectUser(Inst, "not a simple load or store");
1005
1006 Ptr = Ptr->stripPointerCasts();
1007
1008 // Alloca already accessed as vector.
1009 if (Ptr == AA.Alloca &&
1010 DL.getTypeStoreSize(AA.Alloca->getAllocatedType()) ==
1011 DL.getTypeStoreSize(AccessTy)) {
1012 AA.Vector.Worklist.push_back(Inst);
1013 continue;
1014 }
1015
1016 if (!isSupportedAccessType(AA.Vector.Ty, AccessTy, DL))
1017 return RejectUser(Inst, "not a supported access type");
1018
1019 AA.Vector.Worklist.push_back(Inst);
1020 continue;
1021 }
1022
1023 if (auto *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
1024 // If we can't compute a vector index from this GEP, then we can't
1025 // promote this alloca to vector.
1026 auto Index = computeGEPToVectorIndex(GEP, AA.Alloca, VecEltTy, DL);
1027 if (!Index)
1028 return RejectUser(Inst, "cannot compute vector index for GEP");
1029
1030 AA.Vector.GEPVectorIdx[GEP] = std::move(Index.value());
1031 AA.Vector.UsersToRemove.push_back(Inst);
1032 continue;
1033 }
1034
1035 if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst);
1036 MSI && isSupportedMemset(MSI, AA.Alloca, DL)) {
1037 AA.Vector.Worklist.push_back(Inst);
1038 continue;
1039 }
1040
1041 if (MemTransferInst *TransferInst = dyn_cast<MemTransferInst>(Inst)) {
1042 if (TransferInst->isVolatile())
1043 return RejectUser(Inst, "mem transfer inst is volatile");
1044
1045 ConstantInt *Len = dyn_cast<ConstantInt>(TransferInst->getLength());
1046 if (!Len || (Len->getZExtValue() % ElementSize))
1047 return RejectUser(Inst, "mem transfer inst length is non-constant or "
1048 "not a multiple of the vector element size");
1049
1050 auto getConstIndexIntoAlloca = [&](Value *Ptr) -> ConstantInt * {
1051 if (Ptr == AA.Alloca)
1052 return ConstantInt::get(Ptr->getContext(), APInt(32, 0));
1053
1055 const auto &GEPI = AA.Vector.GEPVectorIdx.find(GEP)->second;
1056 if (GEPI.VarIndex)
1057 return nullptr;
1058 if (GEPI.ConstIndex)
1059 return GEPI.ConstIndex;
1060 return ConstantInt::get(Ptr->getContext(), APInt(32, 0));
1061 };
1062
1063 MemTransferInfo *TI =
1064 &AA.Vector.TransferInfo.try_emplace(TransferInst).first->second;
1065 unsigned OpNum = U->getOperandNo();
1066 if (OpNum == 0) {
1067 Value *Dest = TransferInst->getDest();
1068 ConstantInt *Index = getConstIndexIntoAlloca(Dest);
1069 if (!Index)
1070 return RejectUser(Inst, "could not calculate constant dest index");
1071 TI->DestIndex = Index;
1072 } else {
1073 assert(OpNum == 1);
1074 Value *Src = TransferInst->getSource();
1075 ConstantInt *Index = getConstIndexIntoAlloca(Src);
1076 if (!Index)
1077 return RejectUser(Inst, "could not calculate constant src index");
1078 TI->SrcIndex = Index;
1079 }
1080 continue;
1081 }
1082
1083 if (auto *Intr = dyn_cast<IntrinsicInst>(Inst)) {
1084 if (Intr->getIntrinsicID() == Intrinsic::objectsize) {
1085 AA.Vector.Worklist.push_back(Inst);
1086 continue;
1087 }
1088 }
1089
1090 // Ignore assume-like intrinsics and comparisons used in assumes.
1091 if (isAssumeLikeIntrinsic(Inst)) {
1092 if (!Inst->use_empty())
1093 return RejectUser(Inst, "assume-like intrinsic cannot have any users");
1094 AA.Vector.UsersToRemove.push_back(Inst);
1095 continue;
1096 }
1097
1098 if (isa<ICmpInst>(Inst) && all_of(Inst->users(), [](User *U) {
1099 return isAssumeLikeIntrinsic(cast<Instruction>(U));
1100 })) {
1101 AA.Vector.UsersToRemove.push_back(Inst);
1102 continue;
1103 }
1104
1105 return RejectUser(Inst, "unhandled alloca user");
1106 }
1107
1108 // Follow-up check to ensure we've seen both sides of all transfer insts.
1109 for (const auto &Entry : AA.Vector.TransferInfo) {
1110 const MemTransferInfo &TI = Entry.second;
1111 if (!TI.SrcIndex || !TI.DestIndex)
1112 return RejectUser(Entry.first,
1113 "mem transfer inst between different objects");
1114 AA.Vector.Worklist.push_back(Entry.first);
1115 }
1116}
1117
1118void AMDGPUPromoteAllocaImpl::promoteAllocaToVector(AllocaAnalysis &AA) {
1119 LLVM_DEBUG(dbgs() << "Promoting to vectors: " << *AA.Alloca << '\n');
1120 LLVM_DEBUG(dbgs() << " type conversion: " << *AA.Alloca->getAllocatedType()
1121 << " -> " << *AA.Vector.Ty << '\n');
1122 const unsigned VecStoreSize = DL.getTypeStoreSize(AA.Vector.Ty);
1123
1124 Type *VecEltTy = AA.Vector.Ty->getElementType();
1125 const unsigned ElementSize = DL.getTypeSizeInBits(VecEltTy) / 8;
1126
1127 // Alloca is uninitialized memory. Imitate that by making the first value
1128 // undef.
1129 SSAUpdater Updater;
1130 Updater.Initialize(AA.Vector.Ty, "promotealloca");
1131
1132 BasicBlock *EntryBB = AA.Alloca->getParent();
1133 BasicBlock::iterator InitInsertPos =
1134 skipToNonAllocaInsertPt(*EntryBB, AA.Alloca->getIterator());
1135 IRBuilder<> Builder(&*InitInsertPos);
1136 Value *AllocaInitValue = Builder.CreateFreeze(PoisonValue::get(AA.Vector.Ty));
1137 AllocaInitValue->takeName(AA.Alloca);
1138
1139 Updater.AddAvailableValue(AA.Alloca->getParent(), AllocaInitValue);
1140
1141 // First handle the initial worklist, in basic block order.
1142 //
1143 // Insert a placeholder whenever we need the vector value at the top of a
1144 // basic block.
1146 forEachWorkListItem(AA.Vector.Worklist, [&](Instruction *I) {
1147 BasicBlock *BB = I->getParent();
1148 auto GetCurVal = [&]() -> Value * {
1149 if (Value *CurVal = Updater.FindValueForBlock(BB))
1150 return CurVal;
1151
1152 if (!Placeholders.empty() && Placeholders.back()->getParent() == BB)
1153 return Placeholders.back();
1154
1155 // If the current value in the basic block is not yet known, insert a
1156 // placeholder that we will replace later.
1157 IRBuilder<> Builder(I);
1158 auto *Placeholder = cast<Instruction>(Builder.CreateFreeze(
1159 PoisonValue::get(AA.Vector.Ty), "promotealloca.placeholder"));
1160 Placeholders.insert(Placeholder);
1161 return Placeholders.back();
1162 };
1163
1164 Value *Result = promoteAllocaUserToVector(I, DL, AA, VecStoreSize,
1165 ElementSize, GetCurVal);
1166 // If the returned result is a placeholder, it means the instruction does
1167 // not really modify the alloca. So no need to make it being available value
1168 // to SSAUpdater.
1169 // This will stop placeholder being cached in SSAUpdater. The cached
1170 // placeholder may cause stale pointer being referenced when doing
1171 // placeholder replacement.
1172 if (Result && (!isa<Instruction>(Result) ||
1173 !Placeholders.contains(cast<Instruction>(Result))))
1174 Updater.AddAvailableValue(BB, Result);
1175 });
1176
1177 // Now fixup the placeholders.
1178 for (Instruction *Placeholder : Placeholders) {
1179 Placeholder->replaceAllUsesWith(
1180 Updater.GetValueInMiddleOfBlock(Placeholder->getParent()));
1181 Placeholder->eraseFromParent();
1182 }
1183
1184 // Delete all instructions.
1185 for (Instruction *I : AA.Vector.Worklist) {
1186 assert(I->use_empty());
1187 I->eraseFromParent();
1188 }
1189
1190 // Delete all the users that are known to be removeable.
1191 for (Instruction *I : reverse(AA.Vector.UsersToRemove)) {
1192 I->dropDroppableUses();
1193 assert(I->use_empty());
1194 I->eraseFromParent();
1195 }
1196
1197 // Alloca should now be dead too.
1198 assert(AA.Alloca->use_empty());
1199 AA.Alloca->eraseFromParent();
1200}
1201
1202std::pair<Value *, Value *>
1203AMDGPUPromoteAllocaImpl::getLocalSizeYZ(IRBuilder<> &Builder) {
1204 Function &F = *Builder.GetInsertBlock()->getParent();
1206
1207 if (!IsAMDHSA) {
1208 CallInst *LocalSizeY = Builder.CreateIntrinsicWithoutFolding(
1209 Intrinsic::r600_read_local_size_y, {});
1210 CallInst *LocalSizeZ = Builder.CreateIntrinsicWithoutFolding(
1211 Intrinsic::r600_read_local_size_z, {});
1212
1213 ST.makeLIDRangeMetadata(LocalSizeY);
1214 ST.makeLIDRangeMetadata(LocalSizeZ);
1215
1216 return std::pair(LocalSizeY, LocalSizeZ);
1217 }
1218
1219 // We must read the size out of the dispatch pointer.
1220 assert(IsAMDGCN);
1221
1222 // We are indexing into this struct, and want to extract the workgroup_size_*
1223 // fields.
1224 //
1225 // typedef struct hsa_kernel_dispatch_packet_s {
1226 // uint16_t header;
1227 // uint16_t setup;
1228 // uint16_t workgroup_size_x ;
1229 // uint16_t workgroup_size_y;
1230 // uint16_t workgroup_size_z;
1231 // uint16_t reserved0;
1232 // uint32_t grid_size_x ;
1233 // uint32_t grid_size_y ;
1234 // uint32_t grid_size_z;
1235 //
1236 // uint32_t private_segment_size;
1237 // uint32_t group_segment_size;
1238 // uint64_t kernel_object;
1239 //
1240 // #ifdef HSA_LARGE_MODEL
1241 // void *kernarg_address;
1242 // #elif defined HSA_LITTLE_ENDIAN
1243 // void *kernarg_address;
1244 // uint32_t reserved1;
1245 // #else
1246 // uint32_t reserved1;
1247 // void *kernarg_address;
1248 // #endif
1249 // uint64_t reserved2;
1250 // hsa_signal_t completion_signal; // uint64_t wrapper
1251 // } hsa_kernel_dispatch_packet_t
1252 //
1253 CallInst *DispatchPtr =
1254 Builder.CreateIntrinsicWithoutFolding(Intrinsic::amdgcn_dispatch_ptr, {});
1255 DispatchPtr->addRetAttr(Attribute::NoAlias);
1256 DispatchPtr->addRetAttr(Attribute::NonNull);
1257 F.removeFnAttr("amdgpu-no-dispatch-ptr");
1258
1259 // Size of the dispatch packet struct.
1260 DispatchPtr->addDereferenceableRetAttr(64);
1261
1262 Type *I32Ty = Type::getInt32Ty(Mod.getContext());
1263
1264 // We could do a single 64-bit load here, but it's likely that the basic
1265 // 32-bit and extract sequence is already present, and it is probably easier
1266 // to CSE this. The loads should be mergeable later anyway.
1267 Value *GEPXY = Builder.CreateConstInBoundsGEP1_64(I32Ty, DispatchPtr, 1);
1268 LoadInst *LoadXY = Builder.CreateAlignedLoad(I32Ty, GEPXY, Align(4));
1269
1270 Value *GEPZU = Builder.CreateConstInBoundsGEP1_64(I32Ty, DispatchPtr, 2);
1271 LoadInst *LoadZU = Builder.CreateAlignedLoad(I32Ty, GEPZU, Align(4));
1272
1273 MDNode *MD = MDNode::get(Mod.getContext(), {});
1274 LoadXY->setMetadata(LLVMContext::MD_invariant_load, MD);
1275 LoadZU->setMetadata(LLVMContext::MD_invariant_load, MD);
1276 ST.makeLIDRangeMetadata(LoadZU);
1277
1278 // Extract y component. Upper half of LoadZU should be zero already.
1279 Value *Y = Builder.CreateLShr(LoadXY, 16);
1280
1281 return std::pair(Y, LoadZU);
1282}
1283
1284Value *AMDGPUPromoteAllocaImpl::getWorkitemID(IRBuilder<> &Builder,
1285 unsigned N) {
1286 Function *F = Builder.GetInsertBlock()->getParent();
1289 StringRef AttrName;
1290
1291 switch (N) {
1292 case 0:
1293 IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_x
1294 : (Intrinsic::ID)Intrinsic::r600_read_tidig_x;
1295 AttrName = "amdgpu-no-workitem-id-x";
1296 break;
1297 case 1:
1298 IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_y
1299 : (Intrinsic::ID)Intrinsic::r600_read_tidig_y;
1300 AttrName = "amdgpu-no-workitem-id-y";
1301 break;
1302
1303 case 2:
1304 IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_z
1305 : (Intrinsic::ID)Intrinsic::r600_read_tidig_z;
1306 AttrName = "amdgpu-no-workitem-id-z";
1307 break;
1308 default:
1309 llvm_unreachable("invalid dimension");
1310 }
1311
1312 Function *WorkitemIdFn = Intrinsic::getOrInsertDeclaration(&Mod, IntrID);
1313 CallInst *CI = Builder.CreateCall(WorkitemIdFn);
1314 ST.makeLIDRangeMetadata(CI);
1315 F->removeFnAttr(AttrName);
1316
1317 return CI;
1318}
1319
1320static bool isCallPromotable(CallInst *CI) {
1322 if (!II)
1323 return false;
1324
1325 switch (II->getIntrinsicID()) {
1326 case Intrinsic::memcpy:
1327 case Intrinsic::memmove:
1328 case Intrinsic::memset:
1329 case Intrinsic::lifetime_start:
1330 case Intrinsic::lifetime_end:
1331 case Intrinsic::invariant_start:
1332 case Intrinsic::invariant_end:
1333 case Intrinsic::launder_invariant_group:
1334 case Intrinsic::strip_invariant_group:
1335 case Intrinsic::objectsize:
1336 return true;
1337 default:
1338 return false;
1339 }
1340}
1341
1342bool AMDGPUPromoteAllocaImpl::binaryOpIsDerivedFromSameAlloca(
1343 Value *BaseAlloca, Value *Val, Instruction *Inst, int OpIdx0,
1344 int OpIdx1) const {
1345 // Figure out which operand is the one we might not be promoting.
1346 Value *OtherOp = Inst->getOperand(OpIdx0);
1347 if (Val == OtherOp)
1348 OtherOp = Inst->getOperand(OpIdx1);
1349
1351 return true;
1352
1353 // TODO: getUnderlyingObject will not work on a vector getelementptr
1354 Value *OtherObj = getUnderlyingObject(OtherOp);
1355 if (!isa<AllocaInst>(OtherObj))
1356 return false;
1357
1358 // TODO: We should be able to replace undefs with the right pointer type.
1359
1360 // TODO: If we know the other base object is another promotable
1361 // alloca, not necessarily this alloca, we can do this. The
1362 // important part is both must have the same address space at
1363 // the end.
1364 if (OtherObj != BaseAlloca) {
1365 LLVM_DEBUG(
1366 dbgs() << "Found a binary instruction with another alloca object\n");
1367 return false;
1368 }
1369
1370 return true;
1371}
1372
1373void AMDGPUPromoteAllocaImpl::analyzePromoteToLDS(AllocaAnalysis &AA) const {
1374 if (DisablePromoteAllocaToLDS) {
1375 LLVM_DEBUG(dbgs() << " Promote alloca to LDS is disabled\n");
1376 return;
1377 }
1378
1379 // Don't promote the alloca to LDS for shader calling conventions as the work
1380 // item ID intrinsics are not supported for these calling conventions.
1381 // Furthermore not all LDS is available for some of the stages.
1382 const Function &ContainingFunction = *AA.Alloca->getFunction();
1383 CallingConv::ID CC = ContainingFunction.getCallingConv();
1384
1385 switch (CC) {
1388 break;
1389 default:
1390 LLVM_DEBUG(
1391 dbgs()
1392 << " promote alloca to LDS not supported with calling convention.\n");
1393 return;
1394 }
1395
1396 for (Use *Use : AA.Uses) {
1397 auto *User = Use->getUser();
1398
1399 if (CallInst *CI = dyn_cast<CallInst>(User)) {
1400 if (!isCallPromotable(CI))
1401 return;
1402
1403 if (find(AA.LDS.Worklist, User) == AA.LDS.Worklist.end())
1404 AA.LDS.Worklist.push_back(User);
1405 continue;
1406 }
1407
1409 if (UseInst->getOpcode() == Instruction::PtrToInt)
1410 return;
1411
1412 if (LoadInst *LI = dyn_cast<LoadInst>(UseInst)) {
1413 if (LI->isVolatile())
1414 return;
1415 continue;
1416 }
1417
1418 if (StoreInst *SI = dyn_cast<StoreInst>(UseInst)) {
1419 if (SI->isVolatile())
1420 return;
1421 continue;
1422 }
1423
1424 if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(UseInst)) {
1425 if (RMW->isVolatile())
1426 return;
1427 continue;
1428 }
1429
1430 if (AtomicCmpXchgInst *CAS = dyn_cast<AtomicCmpXchgInst>(UseInst)) {
1431 if (CAS->isVolatile())
1432 return;
1433 continue;
1434 }
1435
1436 // Only promote a select if we know that the other select operand
1437 // is from another pointer that will also be promoted.
1438 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
1439 if (!binaryOpIsDerivedFromSameAlloca(AA.Alloca, Use->get(), ICmp, 0, 1))
1440 return;
1441
1442 // May need to rewrite constant operands.
1443 if (find(AA.LDS.Worklist, User) == AA.LDS.Worklist.end())
1444 AA.LDS.Worklist.push_back(ICmp);
1445 continue;
1446 }
1447
1449 // Be conservative if an address could be computed outside the bounds of
1450 // the alloca.
1451 if (!GEP->isInBounds())
1452 return;
1454 // Do not promote vector/aggregate type instructions. It is hard to track
1455 // their users.
1456
1457 // Do not promote addrspacecast.
1458 //
1459 // TODO: If we know the address is only observed through flat pointers, we
1460 // could still promote.
1461 return;
1462 }
1463
1464 if (find(AA.LDS.Worklist, User) == AA.LDS.Worklist.end())
1465 AA.LDS.Worklist.push_back(User);
1466 }
1467
1468 AA.LDS.Enable = true;
1469}
1470
1471bool AMDGPUPromoteAllocaImpl::hasSufficientLocalMem(const Function &F) {
1472
1473 FunctionType *FTy = F.getFunctionType();
1475
1476 // If the function has any arguments in the local address space, then it's
1477 // possible these arguments require the entire local memory space, so
1478 // we cannot use local memory in the pass.
1479 for (Type *ParamTy : FTy->params()) {
1480 PointerType *PtrTy = dyn_cast<PointerType>(ParamTy);
1481 if (PtrTy && PtrTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
1482 LocalMemLimit = 0;
1483 LLVM_DEBUG(dbgs() << "Function has local memory argument. Promoting to "
1484 "local memory disabled.\n");
1485 return false;
1486 }
1487 }
1488
1489 LocalMemLimit = ST.getAddressableLocalMemorySize();
1490 if (LocalMemLimit == 0)
1491 return false;
1492
1494 SmallPtrSet<const Constant *, 8> VisitedConstants;
1496
1497 auto visitUsers = [&](const GlobalVariable *GV, const Constant *Val) -> bool {
1498 for (const User *U : Val->users()) {
1499 if (const Instruction *Use = dyn_cast<Instruction>(U)) {
1500 if (Use->getFunction() == &F)
1501 return true;
1502 } else {
1503 const Constant *C = cast<Constant>(U);
1504 if (VisitedConstants.insert(C).second)
1505 Stack.push_back(C);
1506 }
1507 }
1508
1509 return false;
1510 };
1511
1512 for (GlobalVariable &GV : Mod.globals()) {
1514 continue;
1515
1516 if (visitUsers(&GV, &GV)) {
1517 UsedLDS.insert(&GV);
1518 Stack.clear();
1519 continue;
1520 }
1521
1522 // For any ConstantExpr uses, we need to recursively search the users until
1523 // we see a function.
1524 while (!Stack.empty()) {
1525 const Constant *C = Stack.pop_back_val();
1526 if (visitUsers(&GV, C)) {
1527 UsedLDS.insert(&GV);
1528 Stack.clear();
1529 break;
1530 }
1531 }
1532 }
1533
1534 SmallVector<std::pair<uint64_t, Align>, 16> AllocatedSizes;
1535 AllocatedSizes.reserve(UsedLDS.size());
1536
1537 for (const GlobalVariable *GV : UsedLDS) {
1539 DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
1540 uint64_t AllocSize = GV->getGlobalSize(DL);
1541
1542 // HIP uses an extern unsized array in local address space for dynamically
1543 // allocated shared memory. In that case, we have to disable the promotion.
1544 if (GV->hasExternalLinkage() && AllocSize == 0) {
1545 LocalMemLimit = 0;
1546 LLVM_DEBUG(dbgs() << "Function has a reference to externally allocated "
1547 "local memory. Promoting to local memory "
1548 "disabled.\n");
1549 return false;
1550 }
1551
1552 AllocatedSizes.emplace_back(AllocSize, Alignment);
1553 }
1554
1555 // Sort to try to estimate the worst case alignment padding
1556 //
1557 // FIXME: We should really do something to fix the addresses to a more optimal
1558 // value instead
1559 llvm::sort(AllocatedSizes, llvm::less_second());
1560
1561 // Check how much local memory is being used by global objects
1562 CurrentLocalMemUsage = 0;
1563
1564 // FIXME: Try to account for padding here. The real padding and address is
1565 // currently determined from the inverse order of uses in the function when
1566 // legalizing, which could also potentially change. We try to estimate the
1567 // worst case here, but we probably should fix the addresses earlier.
1568 for (auto Alloc : AllocatedSizes) {
1569 CurrentLocalMemUsage = alignTo(CurrentLocalMemUsage, Alloc.second);
1570 CurrentLocalMemUsage += Alloc.first;
1571 }
1572
1573 unsigned MaxOccupancy =
1574 ST.getWavesPerEU(ST.getFlatWorkGroupSizes(F), CurrentLocalMemUsage, F)
1575 .second;
1576
1577 // Round up to the next tier of usage.
1578 unsigned MaxSizeWithWaveCount =
1579 ST.getMaxLocalMemSizeWithWaveCount(MaxOccupancy, F);
1580
1581 // Program may already use more LDS than is usable at maximum occupancy.
1582 if (CurrentLocalMemUsage > MaxSizeWithWaveCount)
1583 return false;
1584
1585 LocalMemLimit = MaxSizeWithWaveCount;
1586
1587 LLVM_DEBUG(dbgs() << F.getName() << " uses " << CurrentLocalMemUsage
1588 << " bytes of LDS\n"
1589 << " Rounding size to " << MaxSizeWithWaveCount
1590 << " with a maximum occupancy of " << MaxOccupancy << '\n'
1591 << " and " << (LocalMemLimit - CurrentLocalMemUsage)
1592 << " available for promotion\n");
1593
1594 return true;
1595}
1596
1597// FIXME: Should try to pick the most likely to be profitable allocas first.
1598bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToLDS(
1599 AllocaAnalysis &AA, bool SufficientLDS,
1600 SetVector<IntrinsicInst *> &DeferredIntrs) {
1601 LLVM_DEBUG(dbgs() << "Trying to promote to LDS: " << *AA.Alloca << '\n');
1602
1603 // Not likely to have sufficient local memory for promotion.
1604 if (!SufficientLDS)
1605 return false;
1606
1607 IRBuilder<> Builder(AA.Alloca);
1608
1609 const Function &ContainingFunction = *AA.Alloca->getParent()->getParent();
1610 const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(TM, ContainingFunction);
1611 unsigned WorkGroupSize = ST.getFlatWorkGroupSizes(ContainingFunction).second;
1612
1613 Align Alignment = AA.Alloca->getAlign();
1614
1615 // FIXME: This computed padding is likely wrong since it depends on inverse
1616 // usage order.
1617 //
1618 // FIXME: It is also possible that if we're allowed to use all of the memory
1619 // could end up using more than the maximum due to alignment padding.
1620
1621 uint32_t NewSize = alignTo(CurrentLocalMemUsage, Alignment);
1622 std::optional<TypeSize> ElemSize = AA.Alloca->getAllocationSize(DL);
1623 if (!ElemSize || ElemSize->isScalable())
1624 return false;
1625 TypeSize AllocSize = WorkGroupSize * *ElemSize;
1626 NewSize += AllocSize.getFixedValue();
1627
1628 if (NewSize > LocalMemLimit) {
1629 LLVM_DEBUG(dbgs() << " " << AllocSize
1630 << " bytes of local memory not available to promote\n");
1631 return false;
1632 }
1633
1634 CurrentLocalMemUsage = NewSize;
1635
1636 LLVM_DEBUG(dbgs() << "Promoting alloca to local memory\n");
1637
1638 Function *F = AA.Alloca->getFunction();
1639
1640 Type *GVTy = ArrayType::get(AA.Alloca->getAllocatedType(), WorkGroupSize);
1643 Twine(F->getName()) + Twine('.') + AA.Alloca->getName(), nullptr,
1646 GV->setAlignment(AA.Alloca->getAlign());
1647
1648 Value *TCntY, *TCntZ;
1649
1650 std::tie(TCntY, TCntZ) = getLocalSizeYZ(Builder);
1651 Value *TIdX = getWorkitemID(Builder, 0);
1652 Value *TIdY = getWorkitemID(Builder, 1);
1653 Value *TIdZ = getWorkitemID(Builder, 2);
1654
1655 Value *Tmp0 = Builder.CreateMul(TCntY, TCntZ, "", true, true);
1656 Tmp0 = Builder.CreateMul(Tmp0, TIdX);
1657 Value *Tmp1 = Builder.CreateMul(TIdY, TCntZ, "", true, true);
1658 Value *TID = Builder.CreateAdd(Tmp0, Tmp1);
1659 TID = Builder.CreateAdd(TID, TIdZ);
1660
1661 LLVMContext &Context = Mod.getContext();
1663
1664 Value *Offset = Builder.CreateInBoundsGEP(GVTy, GV, Indices);
1665 AA.Alloca->mutateType(Offset->getType());
1666 AA.Alloca->replaceAllUsesWith(Offset);
1667 AA.Alloca->eraseFromParent();
1668
1670
1671 for (Value *V : AA.LDS.Worklist) {
1673 if (!Call) {
1674 if (ICmpInst *CI = dyn_cast<ICmpInst>(V)) {
1675 Value *LHS = CI->getOperand(0);
1676 Value *RHS = CI->getOperand(1);
1677
1678 Type *NewTy = LHS->getType()->getWithNewType(NewPtrTy);
1680 CI->setOperand(0, Constant::getNullValue(NewTy));
1681
1683 CI->setOperand(1, Constant::getNullValue(NewTy));
1684
1685 continue;
1686 }
1687
1688 // The operand's value should be corrected on its own and we don't want to
1689 // touch the users.
1691 continue;
1692
1693 assert(V->getType()->isPtrOrPtrVectorTy());
1694
1695 Type *NewTy = V->getType()->getWithNewType(NewPtrTy);
1696 V->mutateType(NewTy);
1697
1698 // Adjust the types of any constant operands.
1701 SI->setOperand(1, Constant::getNullValue(NewTy));
1702
1704 SI->setOperand(2, Constant::getNullValue(NewTy));
1705 } else if (PHINode *Phi = dyn_cast<PHINode>(V)) {
1706 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
1708 Phi->getIncomingValue(I)))
1709 Phi->setIncomingValue(I, Constant::getNullValue(NewTy));
1710 }
1711 }
1712
1713 continue;
1714 }
1715
1717 Builder.SetInsertPoint(Intr);
1718 switch (Intr->getIntrinsicID()) {
1719 case Intrinsic::lifetime_start:
1720 case Intrinsic::lifetime_end:
1721 // These intrinsics are for address space 0 only
1722 Intr->eraseFromParent();
1723 continue;
1724 case Intrinsic::memcpy:
1725 case Intrinsic::memmove:
1726 // These have 2 pointer operands. In case if second pointer also needs
1727 // to be replaced we defer processing of these intrinsics until all
1728 // other values are processed.
1729 DeferredIntrs.insert(Intr);
1730 continue;
1731 case Intrinsic::memset: {
1732 MemSetInst *MemSet = cast<MemSetInst>(Intr);
1733 Builder.CreateMemSet(MemSet->getRawDest(), MemSet->getValue(),
1734 MemSet->getLength(), MemSet->getDestAlign(),
1735 MemSet->isVolatile());
1736 Intr->eraseFromParent();
1737 continue;
1738 }
1739 case Intrinsic::invariant_start:
1740 case Intrinsic::invariant_end:
1741 case Intrinsic::launder_invariant_group:
1742 case Intrinsic::strip_invariant_group: {
1743 assert(Intr->getArgOperand(Intr->arg_size() - 1)->getType() == NewPtrTy &&
1744 "pointer operand should already have been promoted");
1746 Intr->getModule(), Intr->getIntrinsicID(), NewPtrTy);
1747 Intr->mutateType(NewF->getReturnType());
1748 Intr->setCalledFunction(NewF);
1749 continue;
1750 }
1751 case Intrinsic::objectsize: {
1752 Value *Src = Intr->getOperand(0);
1753
1754 Value *NewCall = Builder.CreateIntrinsic(
1755 Intrinsic::objectsize,
1757 {Src, Intr->getOperand(1), Intr->getOperand(2), Intr->getOperand(3)});
1758 Intr->replaceAllUsesWith(NewCall);
1759 Intr->eraseFromParent();
1760 continue;
1761 }
1762 default:
1763 Intr->print(errs());
1764 llvm_unreachable("Don't know how to promote alloca intrinsic use.");
1765 }
1766 }
1767
1768 return true;
1769}
1770
1771void AMDGPUPromoteAllocaImpl::finishDeferredAllocaToLDSPromotion(
1772 SetVector<IntrinsicInst *> &DeferredIntrs) {
1773
1774 for (IntrinsicInst *Intr : DeferredIntrs) {
1775 IRBuilder<> Builder(Intr);
1776 Builder.SetInsertPoint(Intr);
1778 assert(ID == Intrinsic::memcpy || ID == Intrinsic::memmove);
1779
1781 auto *B = Builder.CreateMemTransferInst(
1782 ID, MI->getRawDest(), MI->getDestAlign(), MI->getRawSource(),
1783 MI->getSourceAlign(), MI->getLength(), MI->isVolatile());
1784
1785 for (unsigned I = 0; I != 2; ++I) {
1786 if (uint64_t Bytes = Intr->getParamDereferenceableBytes(I)) {
1787 B->addDereferenceableParamAttr(I, Bytes);
1788 }
1789 }
1790
1791 Intr->eraseFromParent();
1792 }
1793}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
static Value * promoteAllocaUserToVector(Instruction *Inst, const DataLayout &DL, AllocaAnalysis &AA, unsigned VecStoreSize, unsigned ElementSize, function_ref< Value *()> GetCurVal)
Promotes a single user of the alloca to a vector form.
AMDGPU promote alloca to vector or LDS
static bool isSupportedAccessType(FixedVectorType *VecTy, Type *AccessTy, const DataLayout &DL)
static void forEachWorkListItem(const InstContainer &WorkList, std::function< void(Instruction *)> Fn)
Iterates over an instruction worklist that may contain multiple instructions from the same basic bloc...
static std::optional< GEPToVectorIndex > computeGEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca, Type *VecElemTy, const DataLayout &DL)
static bool isSupportedMemset(MemSetInst *I, AllocaInst *AI, const DataLayout &DL)
static BasicBlock::iterator skipToNonAllocaInsertPt(BasicBlock &BB, BasicBlock::iterator I)
Find an insert point after an alloca, after all other allocas clustered at the start of the block.
static bool isCallPromotable(CallInst *CI)
static Value * calculateVectorIndex(Value *Ptr, AllocaAnalysis &AA)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
@ Enable
static bool runOnFunction(Function &F, bool PostInlining)
AMD GCN specific subclass of TargetSubtarget.
#define DEBUG_TYPE
Hexagon Common GEP
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
Remove Loads Into Fake Uses
const char * Msg
This file contains some templates that are useful if you are working with the STL at all.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
Target-Independent Code Generator Pass Configuration Options pass.
Value * RHS
Value * LHS
static const AMDGPUSubtarget & get(const MachineFunction &MF)
Class for arbitrary precision integers.
Definition APInt.h:78
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:377
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1671
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1085
LLVM_ABI APInt srem(const APInt &RHS) const
Function for signed remainder operation.
Definition APInt.cpp:1772
an instruction to allocate memory on the stack
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:278
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
An instruction that atomically checks whether a specified value is in a memory location,...
an instruction that atomically reads a memory location, combines it with another value,...
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
uint64_t getParamDereferenceableBytes(unsigned i) const
Extract the number of dereferenceable bytes for a call or parameter (0=unknown).
void addDereferenceableRetAttr(uint64_t Bytes)
adds the dereferenceable attribute to the list of attributes.
void addRetAttr(Attribute::AttrKind Kind)
Adds the attribute to the return value.
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
This class represents a function call, abstracting a target machine's calling convention.
static LLVM_ABI bool isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Check whether a bitcast, inttoptr, or ptrtoint cast between these types is valid and a no-op.
This is the shared class of boolean and integer constants.
Definition Constants.h:87
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:284
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
Class to represent fixed width SIMD vectors.
unsigned getNumElements() const
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:867
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Class to represent function types.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition Function.h:273
Type * getReturnType() const
Returns the type of the ret val.
Definition Function.h:217
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
bool hasExternalLinkage() const
void setUnnamedAddr(UnnamedAddr Val)
unsigned getAddressSpace() const
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
Type * getValueType() const
MaybeAlign getAlign() const
Returns the alignment of the given variable.
LLVM_ABI uint64_t getGlobalSize(const DataLayout &DL) const
Get the size of this global variable in bytes.
Definition Globals.cpp:640
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalVariable.
This instruction compares its operands according to the predicate given to the constructor.
LLVM_ABI CallInst * CreateIntrinsicWithoutFolding(Intrinsic::ID ID, ArrayRef< Type * > OverloadTypes, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="", ArrayRef< OperandBundleDef > OpBundles={})
Create a call to intrinsic ID with Args, mangled using OverloadTypes.
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition IRBuilder.h:1944
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition IRBuilder.h:1542
BasicBlock * GetInsertBlock() const
Definition IRBuilder.h:175
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition IRBuilder.h:2029
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, MaybeAlign Align, bool isVolatile=false, const AAMDNodes &AAInfo=AAMDNodes())
Create and insert a memset to the specified pointer and the specified value.
Definition IRBuilder.h:608
LLVM_ABI Value * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > OverloadTypes, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="", ArrayRef< OperandBundleDef > OpBundles={}, function_ref< void(CallInst *)> SetFn=[](CallInst *) {})
Variant to create a possibly constant-folded intrinsic.
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1432
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args={}, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition IRBuilder.h:2571
Value * CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, const Twine &Name="")
Definition IRBuilder.h:2071
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition IRBuilder.h:181
LLVM_ABI CallInst * CreateMemTransferInst(Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src, MaybeAlign SrcAlign, Value *Size, bool isVolatile=false, const AAMDNodes &AAInfo=AAMDNodes())
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition IRBuilder.h:1466
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2910
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Class to represent integer types.
A wrapper class for inspecting calls to intrinsic functions.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:594
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:619
Metadata node.
Definition Metadata.h:1069
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1567
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
bool empty() const
Definition MapVector.h:79
size_type size() const
Definition MapVector.h:58
std::pair< KeyT, ValueT > & front()
Definition MapVector.h:81
Value * getLength() const
Value * getRawDest() const
MaybeAlign getDestAlign() const
bool isVolatile() const
Value * getValue() const
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
This class wraps the llvm.memcpy/memmove intrinsics.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Pass.cpp:113
Class to represent pointers.
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
Definition Type.cpp:911
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition SSAUpdater.h:39
LLVM_ABI void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
LLVM_ABI Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block.
LLVM_ABI void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
This class represents the LLVM 'select' instruction.
A vector that has set insertion semantics.
Definition SetVector.h:57
bool contains(const_arg_type key) const
Check if the SetVector contains the given key.
Definition SetVector.h:258
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
size_type size() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:345
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
static unsigned getPointerOperandIndex()
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Primary interface to the complete machine description for the target machine.
const STC & getSubtarget(const Function &F) const
This method returns a pointer to the specified type of TargetSubtargetInfo.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:279
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:309
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition Type.h:319
LLVM_ABI Type * getWithNewType(Type *EltTy) const
Given vector type, change the element type, whilst keeping the old number of elements.
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:313
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
void setOperand(unsigned i, Value *Val)
Definition User.h:212
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:713
bool use_empty() const
Definition Value.h:346
void mutateType(Type *Ty)
Mutate the type of this Value to be of the specified type.
Definition Value.h:807
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:400
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Type * getElementType() const
Value handle that is nullable, but tries to track the Value.
constexpr bool isKnownMultipleOf(ScalarTy RHS) const
This function tells the caller whether the element count is known at compile time to be a multiple of...
Definition TypeSize.h:180
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
@ LOCAL_ADDRESS
Address space for local memory.
LLVM_READNONE constexpr bool isEntryFunctionCC(CallingConv::ID CC)
unsigned getDynamicVGPRBlockSize(const Function &F)
@ Entry
Definition COFF.h:862
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
@ SPIR_KERNEL
Used for SPIR kernel functions.
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
initializer< Ty > init(const Ty &Val)
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
@ Length
Definition DWP.cpp:577
void stable_sort(R &&Range)
Definition STLExtras.h:2116
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1765
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:332
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:326
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr int PoisonMaskElem
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
FunctionPass * createAMDGPUPromoteAlloca()
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
char & AMDGPUPromoteAllocaID
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
#define N
AMDGPUPromoteAllocaPass(TargetMachine &TM)
Definition AMDGPU.h:278
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:256
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:342
Function object to check whether the second component of a container supported by std::get (like std:...
Definition STLExtras.h:1448