Skip to content

Commit 850657a

Browse files
committed
NVPTX: Move InferAddressSpaces to generic code
llvm-svn: 293579
1 parent 342257e commit 850657a

File tree

10 files changed

+73
-64
lines changed

10 files changed

+73
-64
lines changed

llvm/include/llvm/InitializePasses.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ void initializeIfConverterPass(PassRegistry&);
156156
void initializeImplicitNullChecksPass(PassRegistry&);
157157
void initializeIndVarSimplifyLegacyPassPass(PassRegistry&);
158158
void initializeInductiveRangeCheckEliminationPass(PassRegistry&);
159+
void initializeInferAddressSpacesPass(PassRegistry&);
159160
void initializeInferFunctionAttrsLegacyPassPass(PassRegistry&);
160161
void initializeInlineCostAnalysisPass(PassRegistry&);
161162
void initializeInstCountPass(PassRegistry&);

llvm/include/llvm/Transforms/Scalar.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,15 @@ Pass *createLowerGuardIntrinsicPass();
410410
//
411411
Pass *createCorrelatedValuePropagationPass();
412412

413+
//===----------------------------------------------------------------------===//
414+
//
415+
// InferAddressSpaces - Modify users of addrspacecast instructions with values
416+
// in the source address space if using the destination address space is slower
417+
// on the target.
418+
//
419+
FunctionPass *createInferAddressSpacesPass();
420+
extern char &InferAddressSpacesID;
421+
413422
//===----------------------------------------------------------------------===//
414423
//
415424
// InstructionSimplifier - Remove redundant instructions.

llvm/lib/Target/NVPTX/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ set(NVPTXCodeGen_sources
1717
NVPTXISelDAGToDAG.cpp
1818
NVPTXISelLowering.cpp
1919
NVPTXImageOptimizer.cpp
20-
NVPTXInferAddressSpaces.cpp
2120
NVPTXInstrInfo.cpp
2221
NVPTXLowerAggrCopies.cpp
2322
NVPTXLowerArgs.cpp

llvm/lib/Target/NVPTX/NVPTX.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ FunctionPass *createNVPTXISelDag(NVPTXTargetMachine &TM,
4545
llvm::CodeGenOpt::Level OptLevel);
4646
ModulePass *createNVPTXAssignValidGlobalNamesPass();
4747
ModulePass *createGenericToNVVMPass();
48-
FunctionPass *createNVPTXInferAddressSpacesPass();
4948
FunctionPass *createNVVMIntrRangePass(unsigned int SmVersion);
5049
FunctionPass *createNVVMReflectPass();
5150
MachineFunctionPass *createNVPTXPrologEpilogPass();

llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ void initializeNVVMReflectPass(PassRegistry&);
5151
void initializeGenericToNVVMPass(PassRegistry&);
5252
void initializeNVPTXAllocaHoistingPass(PassRegistry &);
5353
void initializeNVPTXAssignValidGlobalNamesPass(PassRegistry&);
54-
void initializeNVPTXInferAddressSpacesPass(PassRegistry &);
5554
void initializeNVPTXLowerAggrCopiesPass(PassRegistry &);
5655
void initializeNVPTXLowerArgsPass(PassRegistry &);
5756
void initializeNVPTXLowerAllocaPass(PassRegistry &);
@@ -71,7 +70,6 @@ extern "C" void LLVMInitializeNVPTXTarget() {
7170
initializeGenericToNVVMPass(PR);
7271
initializeNVPTXAllocaHoistingPass(PR);
7372
initializeNVPTXAssignValidGlobalNamesPass(PR);
74-
initializeNVPTXInferAddressSpacesPass(PR);
7573
initializeNVPTXLowerArgsPass(PR);
7674
initializeNVPTXLowerAllocaPass(PR);
7775
initializeNVPTXLowerAggrCopiesPass(PR);
@@ -195,7 +193,7 @@ void NVPTXPassConfig::addAddressSpaceInferencePasses() {
195193
// be eliminated by SROA.
196194
addPass(createSROAPass());
197195
addPass(createNVPTXLowerAllocaPass());
198-
addPass(createNVPTXInferAddressSpacesPass());
196+
addPass(createInferAddressSpacesPass());
199197
}
200198

201199
void NVPTXPassConfig::addStraightLineScalarOptimizationPasses() {

llvm/lib/Transforms/Scalar/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_llvm_library(LLVMScalarOpts
1616
IVUsersPrinter.cpp
1717
InductiveRangeCheckElimination.cpp
1818
IndVarSimplify.cpp
19+
InferAddressSpaces.cpp
1920
JumpThreading.cpp
2021
LICM.cpp
2122
LoopAccessAnalysisPrinter.cpp

llvm/lib/Target/NVPTX/NVPTXInferAddressSpaces.cpp renamed to llvm/lib/Transforms/Scalar/InferAddressSpaces.cpp

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
//
9090
//===----------------------------------------------------------------------===//
9191

92-
#include "NVPTX.h"
92+
#include "llvm/Transforms/Scalar.h"
9393
#include "llvm/ADT/DenseSet.h"
9494
#include "llvm/ADT/Optional.h"
9595
#include "llvm/ADT/SetVector.h"
@@ -103,7 +103,7 @@
103103
#include "llvm/Transforms/Utils/Local.h"
104104
#include "llvm/Transforms/Utils/ValueMapper.h"
105105

106-
#define DEBUG_TYPE "nvptx-infer-addrspace"
106+
#define DEBUG_TYPE "infer-address-spaces"
107107

108108
using namespace llvm;
109109

@@ -112,16 +112,16 @@ static const unsigned UnknownAddressSpace = ~0u;
112112

113113
using ValueToAddrSpaceMapTy = DenseMap<const Value *, unsigned>;
114114

115-
/// \brief NVPTXInferAddressSpaces
116-
class NVPTXInferAddressSpaces: public FunctionPass {
115+
/// \brief InferAddressSpaces
116+
class InferAddressSpaces: public FunctionPass {
117117
/// Target specific address space which uses of should be replaced if
118118
/// possible.
119119
unsigned FlatAddrSpace;
120120

121121
public:
122122
static char ID;
123123

124-
NVPTXInferAddressSpaces() : FunctionPass(ID) {}
124+
InferAddressSpaces() : FunctionPass(ID) {}
125125

126126
void getAnalysisUsage(AnalysisUsage &AU) const override {
127127
AU.setPreservesCFG();
@@ -162,13 +162,13 @@ class NVPTXInferAddressSpaces: public FunctionPass {
162162
};
163163
} // end anonymous namespace
164164

165-
char NVPTXInferAddressSpaces::ID = 0;
165+
char InferAddressSpaces::ID = 0;
166166

167167
namespace llvm {
168-
void initializeNVPTXInferAddressSpacesPass(PassRegistry &);
168+
void initializeInferAddressSpacesPass(PassRegistry &);
169169
}
170-
INITIALIZE_PASS(NVPTXInferAddressSpaces, "nvptx-infer-addrspace",
171-
"Infer address spaces",
170+
171+
INITIALIZE_PASS(InferAddressSpaces, DEBUG_TYPE, "Infer address spaces",
172172
false, false)
173173

174174
// Returns true if V is an address expression.
@@ -212,9 +212,9 @@ static SmallVector<Value *, 2> getPointerOperands(const Value &V) {
212212

213213
// If V is an unvisited flat address expression, appends V to PostorderStack
214214
// and marks it as visited.
215-
void NVPTXInferAddressSpaces::appendsFlatAddressExpressionToPostorderStack(
216-
Value *V, std::vector<std::pair<Value *, bool>> *PostorderStack,
217-
DenseSet<Value *> *Visited) const {
215+
void InferAddressSpaces::appendsFlatAddressExpressionToPostorderStack(
216+
Value *V, std::vector<std::pair<Value *, bool>> *PostorderStack,
217+
DenseSet<Value *> *Visited) const {
218218
assert(V->getType()->isPointerTy());
219219
if (isAddressExpression(*V) &&
220220
V->getType()->getPointerAddressSpace() == FlatAddrSpace) {
@@ -226,7 +226,7 @@ void NVPTXInferAddressSpaces::appendsFlatAddressExpressionToPostorderStack(
226226
// Returns all flat address expressions in function F. The elements are ordered
227227
// in postorder.
228228
std::vector<Value *>
229-
NVPTXInferAddressSpaces::collectFlatAddressExpressions(Function &F) const {
229+
InferAddressSpaces::collectFlatAddressExpressions(Function &F) const {
230230
// This function implements a non-recursive postorder traversal of a partial
231231
// use-def graph of function F.
232232
std::vector<std::pair<Value*, bool>> PostorderStack;
@@ -237,10 +237,10 @@ NVPTXInferAddressSpaces::collectFlatAddressExpressions(Function &F) const {
237237
for (Instruction &I : instructions(F)) {
238238
if (isa<LoadInst>(I)) {
239239
appendsFlatAddressExpressionToPostorderStack(
240-
I.getOperand(0), &PostorderStack, &Visited);
240+
I.getOperand(0), &PostorderStack, &Visited);
241241
} else if (isa<StoreInst>(I)) {
242242
appendsFlatAddressExpressionToPostorderStack(
243-
I.getOperand(1), &PostorderStack, &Visited);
243+
I.getOperand(1), &PostorderStack, &Visited);
244244
}
245245
}
246246

@@ -257,7 +257,7 @@ NVPTXInferAddressSpaces::collectFlatAddressExpressions(Function &F) const {
257257
PostorderStack.back().second = true;
258258
for (Value *PtrOperand : getPointerOperands(*PostorderStack.back().first)) {
259259
appendsFlatAddressExpressionToPostorderStack(
260-
PtrOperand, &PostorderStack, &Visited);
260+
PtrOperand, &PostorderStack, &Visited);
261261
}
262262
}
263263
return Postorder;
@@ -267,16 +267,16 @@ NVPTXInferAddressSpaces::collectFlatAddressExpressions(Function &F) const {
267267
// of OperandUse.get() in the new address space. If the clone is not ready yet,
268268
// returns an undef in the new address space as a placeholder.
269269
static Value *operandWithNewAddressSpaceOrCreateUndef(
270-
const Use &OperandUse, unsigned NewAddrSpace,
271-
const ValueToValueMapTy &ValueWithNewAddrSpace,
272-
SmallVectorImpl<const Use *> *UndefUsesToFix) {
270+
const Use &OperandUse, unsigned NewAddrSpace,
271+
const ValueToValueMapTy &ValueWithNewAddrSpace,
272+
SmallVectorImpl<const Use *> *UndefUsesToFix) {
273273
Value *Operand = OperandUse.get();
274274
if (Value *NewOperand = ValueWithNewAddrSpace.lookup(Operand))
275275
return NewOperand;
276276

277277
UndefUsesToFix->push_back(&OperandUse);
278278
return UndefValue::get(
279-
Operand->getType()->getPointerElementType()->getPointerTo(NewAddrSpace));
279+
Operand->getType()->getPointerElementType()->getPointerTo(NewAddrSpace));
280280
}
281281

282282
// Returns a clone of `I` with its operands converted to those specified in
@@ -289,11 +289,11 @@ static Value *operandWithNewAddressSpaceOrCreateUndef(
289289
// from a pointer whose type already matches. Therefore, this function returns a
290290
// Value* instead of an Instruction*.
291291
static Value *cloneInstructionWithNewAddressSpace(
292-
Instruction *I, unsigned NewAddrSpace,
293-
const ValueToValueMapTy &ValueWithNewAddrSpace,
294-
SmallVectorImpl<const Use *> *UndefUsesToFix) {
292+
Instruction *I, unsigned NewAddrSpace,
293+
const ValueToValueMapTy &ValueWithNewAddrSpace,
294+
SmallVectorImpl<const Use *> *UndefUsesToFix) {
295295
Type *NewPtrType =
296-
I->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
296+
I->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
297297

298298
if (I->getOpcode() == Instruction::AddrSpaceCast) {
299299
Value *Src = I->getOperand(0);
@@ -313,7 +313,7 @@ static Value *cloneInstructionWithNewAddressSpace(
313313
NewPointerOperands.push_back(nullptr);
314314
else
315315
NewPointerOperands.push_back(operandWithNewAddressSpaceOrCreateUndef(
316-
OperandUse, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix));
316+
OperandUse, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix));
317317
}
318318

319319
switch (I->getOpcode()) {
@@ -333,8 +333,8 @@ static Value *cloneInstructionWithNewAddressSpace(
333333
case Instruction::GetElementPtr: {
334334
GetElementPtrInst *GEP = cast<GetElementPtrInst>(I);
335335
GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
336-
GEP->getSourceElementType(), NewPointerOperands[0],
337-
SmallVector<Value *, 4>(GEP->idx_begin(), GEP->idx_end()));
336+
GEP->getSourceElementType(), NewPointerOperands[0],
337+
SmallVector<Value *, 4>(GEP->idx_begin(), GEP->idx_end()));
338338
NewGEP->setIsInBounds(GEP->isInBounds());
339339
return NewGEP;
340340
}
@@ -347,10 +347,10 @@ static Value *cloneInstructionWithNewAddressSpace(
347347
// constant expression `CE` with its operands replaced as specified in
348348
// ValueWithNewAddrSpace.
349349
static Value *cloneConstantExprWithNewAddressSpace(
350-
ConstantExpr *CE, unsigned NewAddrSpace,
351-
const ValueToValueMapTy &ValueWithNewAddrSpace) {
350+
ConstantExpr *CE, unsigned NewAddrSpace,
351+
const ValueToValueMapTy &ValueWithNewAddrSpace) {
352352
Type *TargetType =
353-
CE->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
353+
CE->getType()->getPointerElementType()->getPointerTo(NewAddrSpace);
354354

355355
if (CE->getOpcode() == Instruction::AddrSpaceCast) {
356356
// Because CE is flat, the source address space must be specific.
@@ -382,8 +382,8 @@ static Value *cloneConstantExprWithNewAddressSpace(
382382
// Needs to specify the source type while constructing a getelementptr
383383
// constant expression.
384384
return CE->getWithOperands(
385-
NewOperands, TargetType, /*OnlyIfReduced=*/false,
386-
NewOperands[0]->getType()->getPointerElementType());
385+
NewOperands, TargetType, /*OnlyIfReduced=*/false,
386+
NewOperands[0]->getType()->getPointerElementType());
387387
}
388388

389389
return CE->getWithOperands(NewOperands, TargetType);
@@ -394,7 +394,7 @@ static Value *cloneConstantExprWithNewAddressSpace(
394394
// expression whose address space needs to be modified, in postorder.
395395
//
396396
// See cloneInstructionWithNewAddressSpace for the meaning of UndefUsesToFix.
397-
Value *NVPTXInferAddressSpaces::cloneValueWithNewAddressSpace(
397+
Value *InferAddressSpaces::cloneValueWithNewAddressSpace(
398398
Value *V, unsigned NewAddrSpace,
399399
const ValueToValueMapTy &ValueWithNewAddrSpace,
400400
SmallVectorImpl<const Use *> *UndefUsesToFix) const {
@@ -404,7 +404,7 @@ Value *NVPTXInferAddressSpaces::cloneValueWithNewAddressSpace(
404404

405405
if (Instruction *I = dyn_cast<Instruction>(V)) {
406406
Value *NewV = cloneInstructionWithNewAddressSpace(
407-
I, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix);
407+
I, NewAddrSpace, ValueWithNewAddrSpace, UndefUsesToFix);
408408
if (Instruction *NewI = dyn_cast<Instruction>(NewV)) {
409409
if (NewI->getParent() == nullptr) {
410410
NewI->insertBefore(I);
@@ -415,13 +415,13 @@ Value *NVPTXInferAddressSpaces::cloneValueWithNewAddressSpace(
415415
}
416416

417417
return cloneConstantExprWithNewAddressSpace(
418-
cast<ConstantExpr>(V), NewAddrSpace, ValueWithNewAddrSpace);
418+
cast<ConstantExpr>(V), NewAddrSpace, ValueWithNewAddrSpace);
419419
}
420420

421421
// Defines the join operation on the address space lattice (see the file header
422422
// comments).
423-
unsigned NVPTXInferAddressSpaces::joinAddressSpaces(unsigned AS1,
424-
unsigned AS2) const {
423+
unsigned InferAddressSpaces::joinAddressSpaces(unsigned AS1,
424+
unsigned AS2) const {
425425
if (AS1 == FlatAddrSpace || AS2 == FlatAddrSpace)
426426
return FlatAddrSpace;
427427

@@ -434,7 +434,7 @@ unsigned NVPTXInferAddressSpaces::joinAddressSpaces(unsigned AS1,
434434
return (AS1 == AS2) ? AS1 : FlatAddrSpace;
435435
}
436436

437-
bool NVPTXInferAddressSpaces::runOnFunction(Function &F) {
437+
bool InferAddressSpaces::runOnFunction(Function &F) {
438438
if (skipFunction(F))
439439
return false;
440440

@@ -456,9 +456,9 @@ bool NVPTXInferAddressSpaces::runOnFunction(Function &F) {
456456
return rewriteWithNewAddressSpaces(Postorder, InferredAddrSpace, &F);
457457
}
458458

459-
void NVPTXInferAddressSpaces::inferAddressSpaces(
460-
const std::vector<Value *> &Postorder,
461-
ValueToAddrSpaceMapTy *InferredAddrSpace) const {
459+
void InferAddressSpaces::inferAddressSpaces(
460+
const std::vector<Value *> &Postorder,
461+
ValueToAddrSpaceMapTy *InferredAddrSpace) const {
462462
SetVector<Value *> Worklist(Postorder.begin(), Postorder.end());
463463
// Initially, all expressions are in the uninitialized address space.
464464
for (Value *V : Postorder)
@@ -490,8 +490,8 @@ void NVPTXInferAddressSpaces::inferAddressSpaces(
490490
continue;
491491

492492
// Function updateAddressSpace moves the address space down a lattice
493-
// path. Therefore, nothing to do if User is already inferred as flat
494-
// (the bottom element in the lattice).
493+
// path. Therefore, nothing to do if User is already inferred as flat (the
494+
// bottom element in the lattice).
495495
if (Pos->second == FlatAddrSpace)
496496
continue;
497497

@@ -500,8 +500,8 @@ void NVPTXInferAddressSpaces::inferAddressSpaces(
500500
}
501501
}
502502

503-
Optional<unsigned> NVPTXInferAddressSpaces::updateAddressSpace(
504-
const Value &V, const ValueToAddrSpaceMapTy &InferredAddrSpace) const {
503+
Optional<unsigned> InferAddressSpaces::updateAddressSpace(
504+
const Value &V, const ValueToAddrSpaceMapTy &InferredAddrSpace) const {
505505
assert(InferredAddrSpace.count(&V));
506506

507507
// The new inferred address space equals the join of the address spaces
@@ -514,7 +514,8 @@ Optional<unsigned> NVPTXInferAddressSpaces::updateAddressSpace(
514514
else
515515
OperandAS = PtrOperand->getType()->getPointerAddressSpace();
516516
NewAS = joinAddressSpaces(NewAS, OperandAS);
517-
// join(flat, *) = flat. So we can break if NewAS is already generic.
517+
518+
// join(flat, *) = flat. So we can break if NewAS is already flat.
518519
if (NewAS == FlatAddrSpace)
519520
break;
520521
}
@@ -526,9 +527,9 @@ Optional<unsigned> NVPTXInferAddressSpaces::updateAddressSpace(
526527
return NewAS;
527528
}
528529

529-
bool NVPTXInferAddressSpaces::rewriteWithNewAddressSpaces(
530-
const std::vector<Value *> &Postorder,
531-
const ValueToAddrSpaceMapTy &InferredAddrSpace, Function *F) const {
530+
bool InferAddressSpaces::rewriteWithNewAddressSpaces(
531+
const std::vector<Value *> &Postorder,
532+
const ValueToAddrSpaceMapTy &InferredAddrSpace, Function *F) const {
532533
// For each address expression to be modified, creates a clone of it with its
533534
// pointer operands converted to the new address space. Since the pointer
534535
// operands are converted, the clone is naturally in the new address space by
@@ -539,7 +540,7 @@ bool NVPTXInferAddressSpaces::rewriteWithNewAddressSpaces(
539540
unsigned NewAddrSpace = InferredAddrSpace.lookup(V);
540541
if (V->getType()->getPointerAddressSpace() != NewAddrSpace) {
541542
ValueWithNewAddrSpace[V] = cloneValueWithNewAddressSpace(
542-
V, NewAddrSpace, ValueWithNewAddrSpace, &UndefUsesToFix);
543+
V, NewAddrSpace, ValueWithNewAddrSpace, &UndefUsesToFix);
543544
}
544545
}
545546

@@ -577,15 +578,15 @@ bool NVPTXInferAddressSpaces::rewriteWithNewAddressSpaces(
577578
// so the resultant load/store is still valid.
578579
U->set(NewV);
579580
} else if (isa<Instruction>(U->getUser())) {
580-
// Otherwise, replaces the use with generic(NewV).
581+
// Otherwise, replaces the use with flat(NewV).
581582
// TODO: Some optimization opportunities are missed. For example, in
582583
// %0 = icmp eq float* %p, %q
583584
// if both p and q are inferred to be shared, we can rewrite %0 as
584585
// %0 = icmp eq float addrspace(3)* %new_p, %new_q
585586
// instead of currently
586-
// %generic_p = addrspacecast float addrspace(3)* %new_p to float*
587-
// %generic_q = addrspacecast float addrspace(3)* %new_q to float*
588-
// %0 = icmp eq float* %generic_p, %generic_q
587+
// %flat_p = addrspacecast float addrspace(3)* %new_p to float*
588+
// %flat_q = addrspacecast float addrspace(3)* %new_q to float*
589+
// %0 = icmp eq float* %flat_p, %flat_q
589590
if (Instruction *I = dyn_cast<Instruction>(V)) {
590591
BasicBlock::iterator InsertPos = std::next(I->getIterator());
591592
while (isa<PHINode>(InsertPos))
@@ -604,6 +605,6 @@ bool NVPTXInferAddressSpaces::rewriteWithNewAddressSpaces(
604605
return true;
605606
}
606607

607-
FunctionPass *llvm::createNVPTXInferAddressSpacesPass() {
608-
return new NVPTXInferAddressSpaces();
608+
FunctionPass *llvm::createInferAddressSpacesPass() {
609+
return new InferAddressSpaces();
609610
}

0 commit comments

Comments
 (0)