Changeset View
Changeset View
Standalone View
Standalone View
lib/CodeGen/ManagedMemoryRewrite.cpp
Show First 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | |||||
// already expanded. | // already expanded. | ||||
static void expandConstantExpr(ConstantExpr *Cur, PollyIRBuilder &Builder, | static void expandConstantExpr(ConstantExpr *Cur, PollyIRBuilder &Builder, | ||||
Instruction *Parent, int index, | Instruction *Parent, int index, | ||||
SmallPtrSet<Instruction *, 4> &Expands) { | SmallPtrSet<Instruction *, 4> &Expands) { | ||||
assert(Cur && "invalid constant expression passed"); | assert(Cur && "invalid constant expression passed"); | ||||
Instruction *I = Cur->getAsInstruction(); | Instruction *I = Cur->getAsInstruction(); | ||||
assert(I && "unable to convert ConstantExpr to Instruction"); | assert(I && "unable to convert ConstantExpr to Instruction"); | ||||
DEBUG(dbgs() << "Expanding ConstantExpression: " << *Cur | DEBUG(dbgs() << "Expanding ConstantExpression: (" << *Cur | ||||
<< " | in Instruction: " << *I << "\n";); | << ") in Instruction: (" << *I << ")\n";); | ||||
// Invalidate `Cur` so that no one after this point uses `Cur`. Rather, | // Invalidate `Cur` so that no one after this point uses `Cur`. Rather, | ||||
// they should mutate `I`. | // they should mutate `I`. | ||||
Cur = nullptr; | Cur = nullptr; | ||||
Expands.insert(I); | Expands.insert(I); | ||||
Parent->setOperand(index, I); | Parent->setOperand(index, I); | ||||
▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | replaceGlobalArray(Module &M, const DataLayout &DL, GlobalVariable &Array, | ||||
PointerType *ElemPtrTy = ElemTy->getPointerTo(); | PointerType *ElemPtrTy = ElemTy->getPointerTo(); | ||||
// We only wish to replace arrays that are visible in the module they | // We only wish to replace arrays that are visible in the module they | ||||
// inhabit. Otherwise, our type edit from [T] to T* would be illegal across | // inhabit. Otherwise, our type edit from [T] to T* would be illegal across | ||||
// modules. | // modules. | ||||
const bool OnlyVisibleInsideModule = Array.hasPrivateLinkage() || | const bool OnlyVisibleInsideModule = Array.hasPrivateLinkage() || | ||||
Array.hasInternalLinkage() || | Array.hasInternalLinkage() || | ||||
IgnoreLinkageForGlobals; | IgnoreLinkageForGlobals; | ||||
if (!OnlyVisibleInsideModule) | if (!OnlyVisibleInsideModule) { | ||||
DEBUG(dbgs() << "Not rewriting (" << Array | |||||
<< ") to managed memory " | |||||
"because it could be visible externally. To force rewrite, " | |||||
"use -polly-acc-rewrite-ignore-linkage-for-globals.\n"); | |||||
return; | return; | ||||
} | |||||
if (!Array.hasInitializer() || | if (!Array.hasInitializer() || | ||||
!isa<ConstantAggregateZero>(Array.getInitializer())) | !isa<ConstantAggregateZero>(Array.getInitializer())) { | ||||
DEBUG(dbgs() << "Not rewriting (" << Array | |||||
<< ") to managed memory " | |||||
"because it has an initializer which is " | |||||
"not a zeroinitializer.\n"); | |||||
return; | return; | ||||
} | |||||
// At this point, we have committed to replacing this array. | // At this point, we have committed to replacing this array. | ||||
ReplacedGlobals.insert(&Array); | ReplacedGlobals.insert(&Array); | ||||
std::string NewName = Array.getName(); | std::string NewName = Array.getName(); | ||||
NewName += ".toptr"; | NewName += ".toptr"; | ||||
GlobalVariable *ReplacementToArr = | GlobalVariable *ReplacementToArr = | ||||
cast<GlobalVariable>(M.getOrInsertGlobal(NewName, ElemPtrTy)); | cast<GlobalVariable>(M.getOrInsertGlobal(NewName, ElemPtrTy)); | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
// cudaMallocManaged. | // cudaMallocManaged. | ||||
static void getAllocasToBeManaged(Function &F, | static void getAllocasToBeManaged(Function &F, | ||||
SmallSet<AllocaInst *, 4> &Allocas) { | SmallSet<AllocaInst *, 4> &Allocas) { | ||||
for (BasicBlock &BB : F) { | for (BasicBlock &BB : F) { | ||||
for (Instruction &I : BB) { | for (Instruction &I : BB) { | ||||
auto *Alloca = dyn_cast<AllocaInst>(&I); | auto *Alloca = dyn_cast<AllocaInst>(&I); | ||||
if (!Alloca) | if (!Alloca) | ||||
continue; | continue; | ||||
dbgs() << "Checking if " << *Alloca << "may be captured: "; | DEBUG(dbgs() << "Checking if (" << *Alloca << ") may be captured: "); | ||||
if (PointerMayBeCaptured(Alloca, /* ReturnCaptures */ false, | if (PointerMayBeCaptured(Alloca, /* ReturnCaptures */ false, | ||||
/* StoreCaptures */ true)) { | /* StoreCaptures */ true)) { | ||||
Allocas.insert(Alloca); | Allocas.insert(Alloca); | ||||
DEBUG(dbgs() << "YES (captured)\n"); | DEBUG(dbgs() << "YES (captured).\n"); | ||||
} else { | } else { | ||||
DEBUG(dbgs() << "NO (not captured)\n"); | DEBUG(dbgs() << "NO (not captured).\n"); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
static void rewriteAllocaAsManagedMemory(AllocaInst *Alloca, | static void rewriteAllocaAsManagedMemory(AllocaInst *Alloca, | ||||
const DataLayout &DL) { | const DataLayout &DL) { | ||||
DEBUG(dbgs() << "rewriting: " << *Alloca << " to managed mem.\n"); | DEBUG(dbgs() << "rewriting: (" << *Alloca << ") to managed mem.\n"); | ||||
Module *M = Alloca->getModule(); | Module *M = Alloca->getModule(); | ||||
assert(M && "Alloca does not have a module"); | assert(M && "Alloca does not have a module"); | ||||
PollyIRBuilder Builder(M->getContext()); | PollyIRBuilder Builder(M->getContext()); | ||||
Builder.SetInsertPoint(Alloca); | Builder.SetInsertPoint(Alloca); | ||||
Value *MallocManagedFn = getOrCreatePollyMallocManaged(*Alloca->getModule()); | Value *MallocManagedFn = getOrCreatePollyMallocManaged(*Alloca->getModule()); | ||||
const int Size = DL.getTypeAllocSize(Alloca->getType()->getElementType()); | const int Size = DL.getTypeAllocSize(Alloca->getType()->getElementType()); | ||||
▲ Show 20 Lines • Show All 127 Lines • Show Last 20 Lines |