|
| 1 | +//===-- NVPTXIncreaseAlignment.cpp - Increase alignment for local arrays --===// |
| 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 | +// A simple pass that looks at local memory arrays that are statically |
| 10 | +// sized and sets an appropriate alignment for them. This enables vectorization |
| 11 | +// of loads/stores to these arrays if not explicitly specified by the client. |
| 12 | +// |
| 13 | +// TODO: Ideally we should do a bin-packing of local arrays to maximize |
| 14 | +// alignments while minimizing holes. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#include "NVPTX.h" |
| 19 | +#include "llvm/IR/DataLayout.h" |
| 20 | +#include "llvm/IR/Instructions.h" |
| 21 | +#include "llvm/IR/Module.h" |
| 22 | +#include "llvm/Pass.h" |
| 23 | +#include "llvm/Support/CommandLine.h" |
| 24 | +#include "llvm/Support/MathExtras.h" |
| 25 | + |
| 26 | +using namespace llvm; |
| 27 | + |
| 28 | +static cl::opt<bool> |
| 29 | + MaxLocalArrayAlignment("nvptx-use-max-local-array-alignment", |
| 30 | + cl::init(false), cl::Hidden, |
| 31 | + cl::desc("Use maximum alignment for local memory")); |
| 32 | + |
| 33 | +static constexpr Align MaxPTXArrayAlignment = Align::Constant<16>(); |
| 34 | + |
| 35 | +/// Get the maximum useful alignment for an array. This is more likely to |
| 36 | +/// produce holes in the local memory. |
| 37 | +/// |
| 38 | +/// Choose an alignment large enough that the entire array could be loaded with |
| 39 | +/// a single vector load (if possible). Cap the alignment at MaxPTXArrayAlignment. |
| 40 | +static Align getAggressiveArrayAlignment(const unsigned ArraySize) { |
| 41 | + return std::min(MaxPTXArrayAlignment, Align(PowerOf2Ceil(ArraySize))); |
| 42 | +} |
| 43 | + |
| 44 | +/// Get the alignment of arrays that reduces the chances of leaving holes when |
| 45 | +/// arrays are allocated within a contiguous memory buffer (like shared memory |
| 46 | +/// and stack). Holes are still possible before and after the array allocation. |
| 47 | +/// |
| 48 | +/// Choose the largest alignment such that the array size is a multiple of the |
| 49 | +/// alignment. If all elements of the buffer are allocated in order of |
| 50 | +/// alignment (higher to lower) no holes will be left. |
| 51 | +static Align getConservativeArrayAlignment(const unsigned ArraySize) { |
| 52 | + return commonAlignment(MaxPTXArrayAlignment, ArraySize); |
| 53 | +} |
| 54 | + |
| 55 | +/// Find a better alignment for local arrays |
| 56 | +static bool updateAllocaAlignment(const DataLayout &DL, |
| 57 | + AllocaInst *Alloca) { |
| 58 | + // Looking for statically sized local arrays |
| 59 | + if (!Alloca->isStaticAlloca()) |
| 60 | + return false; |
| 61 | + |
| 62 | + // For now, we only support array allocas |
| 63 | + if (!(Alloca->isArrayAllocation() || Alloca->getAllocatedType()->isArrayTy())) |
| 64 | + return false; |
| 65 | + |
| 66 | + const auto ArraySize = Alloca->getAllocationSize(DL); |
| 67 | + if (!(ArraySize && ArraySize->isFixed())) |
| 68 | + return false; |
| 69 | + |
| 70 | + const auto ArraySizeValue = ArraySize->getFixedValue(); |
| 71 | + const Align PreferredAlignment = |
| 72 | + MaxLocalArrayAlignment ? getAggressiveArrayAlignment(ArraySizeValue) |
| 73 | + : getConservativeArrayAlignment(ArraySizeValue); |
| 74 | + |
| 75 | + if (PreferredAlignment > Alloca->getAlign()) { |
| 76 | + Alloca->setAlignment(PreferredAlignment); |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + return false; |
| 81 | +} |
| 82 | + |
| 83 | +static bool runSetLocalArrayAlignment(Function &F) { |
| 84 | + bool Changed = false; |
| 85 | + const DataLayout &DL = F.getParent()->getDataLayout(); |
| 86 | + |
| 87 | + BasicBlock &EntryBB = F.getEntryBlock(); |
| 88 | + for (Instruction &I : EntryBB) |
| 89 | + if (AllocaInst *Alloca = dyn_cast<AllocaInst>(&I)) |
| 90 | + Changed |= updateAllocaAlignment(DL, Alloca); |
| 91 | + |
| 92 | + return Changed; |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +namespace { |
| 97 | +struct NVPTXIncreaseLocalAlignmentLegacyPass : public FunctionPass { |
| 98 | + static char ID; |
| 99 | + NVPTXIncreaseLocalAlignmentLegacyPass() : FunctionPass(ID) {} |
| 100 | + |
| 101 | + bool runOnFunction(Function &F) override; |
| 102 | +}; |
| 103 | +} // namespace |
| 104 | + |
| 105 | +char NVPTXIncreaseLocalAlignmentLegacyPass::ID = 0; |
| 106 | +INITIALIZE_PASS(NVPTXIncreaseLocalAlignmentLegacyPass, "nvptx-increase-local-alignment", |
| 107 | + "Increase alignment for statically sized alloca arrays", false, |
| 108 | + false) |
| 109 | + |
| 110 | +FunctionPass *llvm::createNVPTXIncreaseLocalAlignmentPass() { |
| 111 | + return new NVPTXIncreaseLocalAlignmentLegacyPass(); |
| 112 | +} |
| 113 | + |
| 114 | +bool NVPTXIncreaseLocalAlignmentLegacyPass::runOnFunction(Function &F) { |
| 115 | + return runSetLocalArrayAlignment(F); |
| 116 | +} |
| 117 | + |
| 118 | +PreservedAnalyses |
| 119 | +NVPTXIncreaseLocalAlignmentPass::run(Function &F, FunctionAnalysisManager &AM) { |
| 120 | + bool Changed = runSetLocalArrayAlignment(F); |
| 121 | + |
| 122 | + if (!Changed) |
| 123 | + return PreservedAnalyses::all(); |
| 124 | + |
| 125 | + PreservedAnalyses PA; |
| 126 | + PA.preserveSet<CFGAnalyses>(); |
| 127 | + return PA; |
| 128 | +} |
0 commit comments