2013-11-12 23:47:20 +01:00
|
|
|
//===---- Delinearization.cpp - MultiDimensional Index Delinearization ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements an analysis pass that tries to delinearize all GEP
|
|
|
|
// instructions in all loops using the SCEV analysis functionality. This pass is
|
|
|
|
// only used for testing purposes: if your pass needs delinearization, please
|
|
|
|
// use the on-demand SCEVAddRecExpr::delinearize() function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-01-07 12:48:04 +01:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2016-04-18 11:17:29 +02:00
|
|
|
#include "llvm/IR/Constants.h"
|
2013-11-12 23:47:20 +01:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
2014-03-04 11:30:26 +01:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2013-11-12 23:47:20 +01:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2014-01-07 12:48:04 +01:00
|
|
|
#include "llvm/Pass.h"
|
2013-11-12 23:47:20 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 04:48:03 +02:00
|
|
|
#define DL_NAME "delinearize"
|
|
|
|
#define DEBUG_TYPE DL_NAME
|
|
|
|
|
2013-11-13 16:35:17 +01:00
|
|
|
namespace {
|
|
|
|
|
2013-11-12 23:47:20 +01:00
|
|
|
class Delinearization : public FunctionPass {
|
|
|
|
Delinearization(const Delinearization &); // do not implement
|
|
|
|
protected:
|
|
|
|
Function *F;
|
|
|
|
LoopInfo *LI;
|
|
|
|
ScalarEvolution *SE;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
|
|
|
Delinearization() : FunctionPass(ID) {
|
|
|
|
initializeDelinearizationPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2014-03-05 08:30:04 +01:00
|
|
|
bool runOnFunction(Function &F) override;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
2014-04-15 06:59:12 +02:00
|
|
|
void print(raw_ostream &O, const Module *M = nullptr) const override;
|
2013-11-12 23:47:20 +01:00
|
|
|
};
|
|
|
|
|
2013-11-13 16:35:17 +01:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2013-11-12 23:47:20 +01:00
|
|
|
void Delinearization::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2015-01-17 15:16:18 +01:00
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
[PM] Port ScalarEvolution to the new pass manager.
This change makes ScalarEvolution a stand-alone object and just produces
one from a pass as needed. Making this work well requires making the
object movable, using references instead of overwritten pointers in
a number of places, and other refactorings.
I've also wired it up to the new pass manager and added a RUN line to
a test to exercise it under the new pass manager. This includes basic
printing support much like with other analyses.
But there is a big and somewhat scary change here. Prior to this patch
ScalarEvolution was never *actually* invalidated!!! Re-running the pass
just re-wired up the various other analyses and didn't remove any of the
existing entries in the SCEV caches or clear out anything at all. This
might seem OK as everything in SCEV that can uses ValueHandles to track
updates to the values that serve as SCEV keys. However, this still means
that as we ran SCEV over each function in the module, we kept
accumulating more and more SCEVs into the cache. At the end, we would
have a SCEV cache with every value that we ever needed a SCEV for in the
entire module!!! Yowzers. The releaseMemory routine would dump all of
this, but that isn't realy called during normal runs of the pipeline as
far as I can see.
To make matters worse, there *is* actually a key that we don't update
with value handles -- there is a map keyed off of Loop*s. Because
LoopInfo *does* release its memory from run to run, it is entirely
possible to run SCEV over one function, then over another function, and
then lookup a Loop* from the second function but find an entry inserted
for the first function! Ouch.
To make matters still worse, there are plenty of updates that *don't*
trip a value handle. It seems incredibly unlikely that today GVN or
another pass that invalidates SCEV can update values in *just* such
a way that a subsequent run of SCEV will incorrectly find lookups in
a cache, but it is theoretically possible and would be a nightmare to
debug.
With this refactoring, I've fixed all this by actually destroying and
recreating the ScalarEvolution object from run to run. Technically, this
could increase the amount of malloc traffic we see, but then again it is
also technically correct. ;] I don't actually think we're suffering from
tons of malloc traffic from SCEV because if we were, the fact that we
never clear the memory would seem more likely to have come up as an
actual problem before now. So, I've made the simple fix here. If in fact
there are serious issues with too much allocation and deallocation,
I can work on a clever fix that preserves the allocations (while
clearing the data) between each run, but I'd prefer to do that kind of
optimization with a test case / benchmark that shows why we need such
cleverness (and that can test that we actually make it faster). It's
possible that this will make some things faster by making the SCEV
caches have higher locality (due to being significantly smaller) so
until there is a clear benchmark, I think the simple change is best.
Differential Revision: http://reviews.llvm.org/D12063
llvm-svn: 245193
2015-08-17 04:08:17 +02:00
|
|
|
AU.addRequired<ScalarEvolutionWrapperPass>();
|
2013-11-12 23:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Delinearization::runOnFunction(Function &F) {
|
|
|
|
this->F = &F;
|
[PM] Port ScalarEvolution to the new pass manager.
This change makes ScalarEvolution a stand-alone object and just produces
one from a pass as needed. Making this work well requires making the
object movable, using references instead of overwritten pointers in
a number of places, and other refactorings.
I've also wired it up to the new pass manager and added a RUN line to
a test to exercise it under the new pass manager. This includes basic
printing support much like with other analyses.
But there is a big and somewhat scary change here. Prior to this patch
ScalarEvolution was never *actually* invalidated!!! Re-running the pass
just re-wired up the various other analyses and didn't remove any of the
existing entries in the SCEV caches or clear out anything at all. This
might seem OK as everything in SCEV that can uses ValueHandles to track
updates to the values that serve as SCEV keys. However, this still means
that as we ran SCEV over each function in the module, we kept
accumulating more and more SCEVs into the cache. At the end, we would
have a SCEV cache with every value that we ever needed a SCEV for in the
entire module!!! Yowzers. The releaseMemory routine would dump all of
this, but that isn't realy called during normal runs of the pipeline as
far as I can see.
To make matters worse, there *is* actually a key that we don't update
with value handles -- there is a map keyed off of Loop*s. Because
LoopInfo *does* release its memory from run to run, it is entirely
possible to run SCEV over one function, then over another function, and
then lookup a Loop* from the second function but find an entry inserted
for the first function! Ouch.
To make matters still worse, there are plenty of updates that *don't*
trip a value handle. It seems incredibly unlikely that today GVN or
another pass that invalidates SCEV can update values in *just* such
a way that a subsequent run of SCEV will incorrectly find lookups in
a cache, but it is theoretically possible and would be a nightmare to
debug.
With this refactoring, I've fixed all this by actually destroying and
recreating the ScalarEvolution object from run to run. Technically, this
could increase the amount of malloc traffic we see, but then again it is
also technically correct. ;] I don't actually think we're suffering from
tons of malloc traffic from SCEV because if we were, the fact that we
never clear the memory would seem more likely to have come up as an
actual problem before now. So, I've made the simple fix here. If in fact
there are serious issues with too much allocation and deallocation,
I can work on a clever fix that preserves the allocations (while
clearing the data) between each run, but I'd prefer to do that kind of
optimization with a test case / benchmark that shows why we need such
cleverness (and that can test that we actually make it faster). It's
possible that this will make some things faster by making the SCEV
caches have higher locality (due to being significantly smaller) so
until there is a clear benchmark, I think the simple change is best.
Differential Revision: http://reviews.llvm.org/D12063
llvm-svn: 245193
2015-08-17 04:08:17 +02:00
|
|
|
SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
|
2015-01-17 15:16:18 +01:00
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
2013-11-12 23:47:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Delinearization::print(raw_ostream &O, const Module *) const {
|
|
|
|
O << "Delinearization on function " << F->getName() << ":\n";
|
|
|
|
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
|
|
|
|
Instruction *Inst = &(*I);
|
2013-11-13 23:37:58 +01:00
|
|
|
|
|
|
|
// Only analyze loads and stores.
|
2013-11-12 23:47:20 +01:00
|
|
|
if (!isa<StoreInst>(Inst) && !isa<LoadInst>(Inst) &&
|
|
|
|
!isa<GetElementPtrInst>(Inst))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const BasicBlock *BB = Inst->getParent();
|
|
|
|
// Delinearize the memory access as analyzed in all the surrounding loops.
|
|
|
|
// Do not analyze memory accesses outside loops.
|
2014-04-15 06:59:12 +02:00
|
|
|
for (Loop *L = LI->getLoopFor(BB); L != nullptr; L = L->getParentLoop()) {
|
2018-03-09 22:05:58 +01:00
|
|
|
const SCEV *AccessFn = SE->getSCEVAtScope(getPointerOperand(Inst), L);
|
2014-05-28 00:41:51 +02:00
|
|
|
|
|
|
|
const SCEVUnknown *BasePointer =
|
|
|
|
dyn_cast<SCEVUnknown>(SE->getPointerBase(AccessFn));
|
|
|
|
// Do not delinearize if we cannot find the base pointer.
|
|
|
|
if (!BasePointer)
|
|
|
|
break;
|
|
|
|
AccessFn = SE->getMinusSCEV(AccessFn, BasePointer);
|
|
|
|
|
2014-04-09 09:53:49 +02:00
|
|
|
O << "\n";
|
|
|
|
O << "Inst:" << *Inst << "\n";
|
|
|
|
O << "In Loop with Header: " << L->getHeader()->getName() << "\n";
|
2015-10-12 10:02:00 +02:00
|
|
|
O << "AccessFunction: " << *AccessFn << "\n";
|
2013-11-12 23:47:20 +01:00
|
|
|
|
|
|
|
SmallVector<const SCEV *, 3> Subscripts, Sizes;
|
2015-10-12 10:02:00 +02:00
|
|
|
SE->delinearize(AccessFn, Subscripts, Sizes, SE->getElementSize(Inst));
|
2014-05-28 00:41:45 +02:00
|
|
|
if (Subscripts.size() == 0 || Sizes.size() == 0 ||
|
2014-05-07 20:01:20 +02:00
|
|
|
Subscripts.size() != Sizes.size()) {
|
2013-11-12 23:47:20 +01:00
|
|
|
O << "failed to delinearize\n";
|
|
|
|
continue;
|
|
|
|
}
|
2014-05-28 00:41:51 +02:00
|
|
|
|
|
|
|
O << "Base offset: " << *BasePointer << "\n";
|
2013-11-12 23:47:20 +01:00
|
|
|
O << "ArrayDecl[UnknownSize]";
|
2014-05-07 20:01:20 +02:00
|
|
|
int Size = Subscripts.size();
|
2013-11-12 23:47:20 +01:00
|
|
|
for (int i = 0; i < Size - 1; i++)
|
|
|
|
O << "[" << *Sizes[i] << "]";
|
|
|
|
O << " with elements of " << *Sizes[Size - 1] << " bytes.\n";
|
|
|
|
|
|
|
|
O << "ArrayRef";
|
|
|
|
for (int i = 0; i < Size; i++)
|
|
|
|
O << "[" << *Subscripts[i] << "]";
|
|
|
|
O << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char Delinearization::ID = 0;
|
|
|
|
static const char delinearization_name[] = "Delinearization";
|
|
|
|
INITIALIZE_PASS_BEGIN(Delinearization, DL_NAME, delinearization_name, true,
|
|
|
|
true)
|
2015-01-17 15:16:18 +01:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
2013-11-12 23:47:20 +01:00
|
|
|
INITIALIZE_PASS_END(Delinearization, DL_NAME, delinearization_name, true, true)
|
|
|
|
|
|
|
|
FunctionPass *llvm::createDelinearizationPass() { return new Delinearization; }
|