Skip to content

Commit 791bb1d

Browse files
committed
address comments
1 parent 0e92b67 commit 791bb1d

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

llvm/lib/Target/NVPTX/NVPTXIncreaseAlignment.cpp

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//===----------------------------------------------------------------------===//
88
//
99
// A simple pass that looks at local memory arrays that are statically
10-
// sized and sets an appropriate alignment for them. This enables vectorization
10+
// sized and potentially increases their alignment. This enables vectorization
1111
// of loads/stores to these arrays if not explicitly specified by the client.
1212
//
1313
// TODO: Ideally we should do a bin-packing of local arrays to maximize
@@ -16,12 +16,15 @@
1616
//===----------------------------------------------------------------------===//
1717

1818
#include "NVPTX.h"
19+
#include "llvm/Analysis/TargetTransformInfo.h"
1920
#include "llvm/IR/DataLayout.h"
2021
#include "llvm/IR/Instructions.h"
2122
#include "llvm/IR/Module.h"
23+
#include "llvm/IR/PassManager.h"
2224
#include "llvm/Pass.h"
2325
#include "llvm/Support/CommandLine.h"
2426
#include "llvm/Support/MathExtras.h"
27+
#include "llvm/Support/NVPTXAddrSpace.h"
2528

2629
using namespace llvm;
2730

@@ -30,16 +33,30 @@ static cl::opt<bool>
3033
cl::init(false), cl::Hidden,
3134
cl::desc("Use maximum alignment for local memory"));
3235

33-
static constexpr Align MaxPTXArrayAlignment = Align::Constant<16>();
36+
namespace {
37+
struct NVPTXIncreaseLocalAlignment {
38+
const Align MaxAlign;
39+
40+
NVPTXIncreaseLocalAlignment(const TargetTransformInfo &TTI)
41+
: MaxAlign(TTI.getLoadStoreVecRegBitWidth(NVPTXAS::ADDRESS_SPACE_LOCAL)) {
42+
}
43+
44+
bool run(Function &F);
45+
bool updateAllocaAlignment(AllocaInst *Alloca, const DataLayout &DL);
46+
Align getAggressiveArrayAlignment(unsigned ArraySize);
47+
Align getConservativeArrayAlignment(unsigned ArraySize);
48+
};
49+
} // namespace
3450

3551
/// Get the maximum useful alignment for an array. This is more likely to
3652
/// produce holes in the local memory.
3753
///
3854
/// Choose an alignment large enough that the entire array could be loaded with
3955
/// a single vector load (if possible). Cap the alignment at
4056
/// MaxPTXArrayAlignment.
41-
static Align getAggressiveArrayAlignment(const unsigned ArraySize) {
42-
return std::min(MaxPTXArrayAlignment, Align(PowerOf2Ceil(ArraySize)));
57+
Align NVPTXIncreaseLocalAlignment::getAggressiveArrayAlignment(
58+
const unsigned ArraySize) {
59+
return std::min(MaxAlign, Align(PowerOf2Ceil(ArraySize)));
4360
}
4461

4562
/// Get the alignment of arrays that reduces the chances of leaving holes when
@@ -49,20 +66,18 @@ static Align getAggressiveArrayAlignment(const unsigned ArraySize) {
4966
/// Choose the largest alignment such that the array size is a multiple of the
5067
/// alignment. If all elements of the buffer are allocated in order of
5168
/// alignment (higher to lower) no holes will be left.
52-
static Align getConservativeArrayAlignment(const unsigned ArraySize) {
53-
return commonAlignment(MaxPTXArrayAlignment, ArraySize);
69+
Align NVPTXIncreaseLocalAlignment::getConservativeArrayAlignment(
70+
const unsigned ArraySize) {
71+
return commonAlignment(MaxAlign, ArraySize);
5472
}
5573

5674
/// Find a better alignment for local arrays
57-
static bool updateAllocaAlignment(const DataLayout &DL, AllocaInst *Alloca) {
75+
bool NVPTXIncreaseLocalAlignment::updateAllocaAlignment(AllocaInst *Alloca,
76+
const DataLayout &DL) {
5877
// Looking for statically sized local arrays
5978
if (!Alloca->isStaticAlloca())
6079
return false;
6180

62-
// For now, we only support array allocas
63-
if (!(Alloca->isArrayAllocation() || Alloca->getAllocatedType()->isArrayTy()))
64-
return false;
65-
6681
const auto ArraySize = Alloca->getAllocationSize(DL);
6782
if (!(ArraySize && ArraySize->isFixed()))
6883
return false;
@@ -80,14 +95,14 @@ static bool updateAllocaAlignment(const DataLayout &DL, AllocaInst *Alloca) {
8095
return false;
8196
}
8297

83-
static bool runSetLocalArrayAlignment(Function &F) {
98+
bool NVPTXIncreaseLocalAlignment::run(Function &F) {
8499
bool Changed = false;
85-
const DataLayout &DL = F.getParent()->getDataLayout();
100+
const auto &DL = F.getParent()->getDataLayout();
86101

87102
BasicBlock &EntryBB = F.getEntryBlock();
88103
for (Instruction &I : EntryBB)
89104
if (AllocaInst *Alloca = dyn_cast<AllocaInst>(&I))
90-
Changed |= updateAllocaAlignment(DL, Alloca);
105+
Changed |= updateAllocaAlignment(Alloca, DL);
91106

92107
return Changed;
93108
}
@@ -115,12 +130,15 @@ FunctionPass *llvm::createNVPTXIncreaseLocalAlignmentPass() {
115130
}
116131

117132
bool NVPTXIncreaseLocalAlignmentLegacyPass::runOnFunction(Function &F) {
118-
return runSetLocalArrayAlignment(F);
133+
const auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
134+
return NVPTXIncreaseLocalAlignment(TTI).run(F);
119135
}
120136

121137
PreservedAnalyses
122-
NVPTXIncreaseLocalAlignmentPass::run(Function &F, FunctionAnalysisManager &AM) {
123-
bool Changed = runSetLocalArrayAlignment(F);
138+
NVPTXIncreaseLocalAlignmentPass::run(Function &F,
139+
FunctionAnalysisManager &FAM) {
140+
const auto &TTI = FAM.getResult<TargetIRAnalysis>(F);
141+
bool Changed = NVPTXIncreaseLocalAlignment(TTI).run(F);
124142

125143
if (!Changed)
126144
return PreservedAnalyses::all();

0 commit comments

Comments
 (0)