17#include "llvm/Analysis/LoopInfo.h"
18#include "llvm/IR/DataLayout.h"
19#include "llvm/IR/DebugInfoMetadata.h"
20#include "llvm/IR/Dominators.h"
21#include "llvm/IR/Module.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Transforms/Utils/BasicBlockUtils.h"
32static cl::opt<int, true>
34 cl::desc(
"Number of threads to use (0 = auto)"),
40 cl::desc(
"Scheduling type of parallel OpenMP for loops"),
41 cl::values(clEnumValN(OMPGeneralSchedulingType::StaticChunked,
"static",
43 clEnumValN(OMPGeneralSchedulingType::Dynamic,
"dynamic",
44 "Dynamic scheduling"),
45 clEnumValN(OMPGeneralSchedulingType::Guided,
"guided",
47 clEnumValN(OMPGeneralSchedulingType::Runtime,
"runtime",
48 "Runtime determined (OMP_SCHEDULE)")),
50 cl::init(OMPGeneralSchedulingType::Runtime), cl::Optional,
53static cl::opt<int, true>
55 cl::desc(
"Chunksize to use by the OpenMP runtime calls"),
85 DominatorTree &DT, BasicBlock *&ExitBB,
86 ICmpInst::Predicate Predicate,
88 bool LoopVectDisabled) {
89 Function *F = Builder.GetInsertBlock()->getParent();
90 LLVMContext &Context = F->getContext();
92 assert(LB->getType() == UB->getType() &&
"Types of loop bounds do not match");
93 IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType());
94 assert(LoopIVType &&
"UB is not integer?");
96 BasicBlock *BeforeBB = Builder.GetInsertBlock();
98 UseGuard ? BasicBlock::Create(Context,
"polly.loop_if", F) :
nullptr;
99 BasicBlock *HeaderBB = BasicBlock::Create(Context,
"polly.loop_header", F);
100 BasicBlock *PreHeaderBB =
101 BasicBlock::Create(Context,
"polly.loop_preheader", F);
104 Loop *OuterLoop = LI.getLoopFor(BeforeBB);
105 Loop *NewLoop = LI.AllocateLoop();
108 OuterLoop->addChildLoop(NewLoop);
110 LI.addTopLevelLoop(NewLoop);
114 OuterLoop->addBasicBlockToLoop(GuardBB, LI);
115 OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI);
118 NewLoop->addBasicBlockToLoop(HeaderBB, LI);
123 Annotator->
pushLoop(NewLoop, Parallel);
126 ExitBB = SplitBlock(BeforeBB, &*Builder.GetInsertPoint(), &DT, &LI);
127 ExitBB->setName(
"polly.loop_exit");
131 BeforeBB->getTerminator()->setSuccessor(0, GuardBB);
132 DT.addNewBlock(GuardBB, BeforeBB);
135 Builder.SetInsertPoint(GuardBB);
137 LoopGuard = Builder.CreateICmp(Predicate, LB, UB);
138 LoopGuard->setName(
"polly.loop_guard");
139 Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB);
140 DT.addNewBlock(PreHeaderBB, GuardBB);
142 BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB);
143 DT.addNewBlock(PreHeaderBB, BeforeBB);
147 Builder.SetInsertPoint(PreHeaderBB);
148 Builder.CreateBr(HeaderBB);
151 DT.addNewBlock(HeaderBB, PreHeaderBB);
152 Builder.SetInsertPoint(HeaderBB);
153 PHINode *IV = Builder.CreatePHI(LoopIVType, 2,
"polly.indvar");
154 IV->addIncoming(LB, PreHeaderBB);
155 Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType);
156 Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride,
"polly.indvar_next");
157 Value *LoopCondition =
158 Builder.CreateICmp(Predicate, IncrementedIV, UB,
"polly.loop_cond");
161 BranchInst *
B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB);
165 IV->addIncoming(IncrementedIV, HeaderBB);
167 DT.changeImmediateDominator(ExitBB, GuardBB);
169 DT.changeImmediateDominator(ExitBB, HeaderBB);
172 Builder.SetInsertPoint(HeaderBB->getFirstNonPHI());
177 Value *LB, Value *UB, Value *Stride, SetVector<Value *> &UsedValues,
178 ValueMapT &Map, BasicBlock::iterator *LoopBody) {
181 BasicBlock::iterator BeforeLoop =
Builder.GetInsertPoint();
185 std::tie(IV, SubFn) =
createSubFn(Stride, Struct, UsedValues, Map);
186 *LoopBody =
Builder.GetInsertPoint();
187 Builder.SetInsertPoint(&*BeforeLoop);
205 std::string FunctionName = SubFn->getName().str();
206 std::replace(FunctionName.begin(), FunctionName.end(),
'.',
'_');
207 SubFn->setName(FunctionName);
217 SmallVector<Type *, 8> Members;
219 for (
Value *V : Values)
220 Members.push_back(V->getType());
222 const DataLayout &DL =
Builder.GetInsertBlock()->getModule()->getDataLayout();
227 BasicBlock &EntryBB =
Builder.GetInsertBlock()->getParent()->getEntryBlock();
228 BasicBlock::iterator IP = EntryBB.getFirstInsertionPt();
229 StructType *Ty = StructType::get(
Builder.getContext(), Members);
230 AllocaInst *Struct =
new AllocaInst(Ty, DL.getAllocaAddrSpace(),
nullptr,
231 "polly.par.userContext", IP);
233 for (
unsigned i = 0; i < Values.size(); i++) {
234 Value *Address =
Builder.CreateStructGEP(Ty, Struct, i);
235 Address->setName(
"polly.subfn.storeaddr." + Values[i]->getName());
236 Builder.CreateStore(Values[i], Address);
243 SetVector<Value *> OldValues, Type *Ty, Value *Struct,
ValueMapT &Map) {
244 for (
unsigned i = 0; i < OldValues.size(); i++) {
245 Value *Address =
Builder.CreateStructGEP(Ty, Struct, i);
246 Type *ElemTy = cast<GetElementPtrInst>(Address)->getResultElementType();
248 NewValue->setName(
"polly.subfunc.arg." + OldValues[i]->getName());
249 Map[OldValues[i]] = NewValue;
257 LLVMContext &
Ctx = F->getContext();
258 DISubprogram *DILScope =
259 dyn_cast_or_null<DISubprogram>(F->getMetadata(LLVMContext::MD_dbg));
262 return DILocation::get(
Ctx, 0, 0, DILScope);
polly dump Polly Dump Function
static cl::opt< int, true > XPollyChunkSize("polly-scheduling-chunksize", cl::desc("Chunksize to use by the OpenMP runtime calls"), cl::Hidden, cl::location(polly::PollyChunkSize), cl::init(0), cl::Optional, cl::cat(PollyCategory))
static cl::opt< int, true > XPollyNumThreads("polly-num-threads", cl::desc("Number of threads to use (0 = auto)"), cl::Hidden, cl::location(polly::PollyNumThreads), cl::init(0), cl::cat(PollyCategory))
static cl::opt< OMPGeneralSchedulingType, true > XPollyScheduling("polly-scheduling", cl::desc("Scheduling type of parallel OpenMP for loops"), cl::values(clEnumValN(OMPGeneralSchedulingType::StaticChunked, "static", "Static scheduling"), clEnumValN(OMPGeneralSchedulingType::Dynamic, "dynamic", "Dynamic scheduling"), clEnumValN(OMPGeneralSchedulingType::Guided, "guided", "Guided scheduling"), clEnumValN(OMPGeneralSchedulingType::Runtime, "runtime", "Runtime determined (OMP_SCHEDULE)")), cl::Hidden, cl::location(polly::PollyScheduling), cl::init(OMPGeneralSchedulingType::Runtime), cl::Optional, cl::cat(PollyCategory))
llvm::cl::OptionCategory PollyCategory
PollyIRBuilder & Builder
The IR builder we use to create instructions.
Function * createSubFnDefinition()
Create the definition of the parallel subfunction.
virtual void deployParallelExecution(Function *SubFn, Value *SubFnParam, Value *LB, Value *UB, Value *Stride)=0
Create the runtime library calls for spawn and join of the worker threads.
Value * createParallelLoop(Value *LB, Value *UB, Value *Stride, SetVector< Value * > &Values, ValueMapT &VMap, BasicBlock::iterator *LoopBody)
Create a parallel loop.
virtual std::tuple< Value *, Function * > createSubFn(Value *Stride, AllocaInst *Struct, SetVector< Value * > UsedValues, ValueMapT &VMap)=0
Create the parallel subfunction.
Type * LongType
The type of a "long" on this hardware used for backend calls.
void extractValuesFromStruct(SetVector< Value * > Values, Type *Ty, Value *Struct, ValueMapT &VMap)
Extract all values from the Struct and construct the mapping.
virtual Function * prepareSubFnDefinition(Function *F) const =0
Prepare the definition of the parallel subfunction.
AllocaInst * storeValuesIntoStruct(SetVector< Value * > &Values)
Create a struct for all Values and store them in there.
Helper class to annotate newly generated SCoPs with metadata.
void annotateLoopLatch(llvm::BranchInst *B, llvm::Loop *L, bool IsParallel, bool IsLoopVectorizerDisabled) const
Annotate the loop latch B wrt. L.
void pushLoop(llvm::Loop *L, bool IsParallel)
Add a new loop L which is parallel if IsParallel is true.
This file contains the declaration of the PolyhedralInfo class, which will provide an interface to ex...
OMPGeneralSchedulingType PollyScheduling
StringRef PollySkipFnAttr
A function attribute which will cause Polly to skip the function.
llvm::DebugLoc createDebugLocForGeneratedCode(Function *F)
Create a DebugLoc representing generated instructions.
@ Value
MemoryKind::Value: Models an llvm::Value.
Value * createLoop(Value *LowerBound, Value *UpperBound, Value *Stride, PollyIRBuilder &Builder, LoopInfo &LI, DominatorTree &DT, BasicBlock *&ExitBlock, ICmpInst::Predicate Predicate, ScopAnnotator *Annotator=nullptr, bool Parallel=false, bool UseGuard=true, bool LoopVectDisabled=false)
Create a scalar do/for-style loop.
OMPGeneralSchedulingType
General scheduling types of parallel OpenMP for loops.
llvm::IRBuilder< llvm::ConstantFolder, IRInserter > PollyIRBuilder
llvm::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::AssertingVH< llvm::Value > > ValueMapT
Type to remap values.