14#include "llvm/Analysis/LoopInfo.h"
15#include "llvm/IR/Dominators.h"
16#include "llvm/IR/Module.h"
25 const std::string Name =
"__kmpc_fork_call";
27 Type *KMPCMicroTy = StructType::getTypeByName(
M->getContext(),
"kmpc_micro");
33 KMPCMicroTy = FunctionType::get(
Builder.getVoidTy(), MicroParams,
true);
38 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
42 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
true);
43 F = Function::Create(Ty, Linkage, Name,
M);
47 Builder.CreatePointerBitCastOrAddrSpaceCast(SubFn,
Builder.getPtrTy(0));
57 CallInst *Call =
Builder.CreateCall(F, Args);
76 std::vector<Type *> Arguments = {
80 FunctionType *FT = FunctionType::get(
Builder.getVoidTy(), Arguments,
false);
81 Function *SubFn = Function::Create(FT, Function::InternalLinkage,
82 F->getName() +
"_polly_subfn",
M);
84 Function::arg_iterator AI = SubFn->arg_begin();
85 AI->setName(
"polly.kmpc.global_tid");
87 AI->setName(
"polly.kmpc.bound_tid");
89 AI->setName(
"polly.kmpc.lb");
91 AI->setName(
"polly.kmpc.ub");
93 AI->setName(
"polly.kmpc.inc");
95 AI->setName(
"polly.kmpc.shared");
125std::tuple<Value *, Function *>
127 AllocaInst *StructData,
128 SetVector<Value *> Data,
ValueMapT &Map) {
130 LLVMContext &Context = SubFn->getContext();
133 BasicBlock *HeaderBB = BasicBlock::Create(Context,
"polly.par.setup", SubFn);
134 SubFnDT = std::make_unique<DominatorTree>(*SubFn);
137 BasicBlock *ExitBB = BasicBlock::Create(Context,
"polly.par.exit", SubFn);
138 BasicBlock *CheckNextBB =
139 BasicBlock::Create(Context,
"polly.par.checkNext", SubFn);
140 BasicBlock *PreHeaderBB =
141 BasicBlock::Create(Context,
"polly.par.loadIVBounds", SubFn);
143 SubFnDT->addNewBlock(ExitBB, HeaderBB);
144 SubFnDT->addNewBlock(CheckNextBB, HeaderBB);
145 SubFnDT->addNewBlock(PreHeaderBB, HeaderBB);
148 Builder.SetInsertPoint(HeaderBB);
152 "polly.par.lastIterPtr");
157 Function::arg_iterator AI = SubFn->arg_begin();
167 Value *Stride = &*AI;
169 Value *Shared = &*AI;
173 const auto Alignment = llvm::Align(
is64BitArch() ? 8 : 4);
175 "polly.par.global_tid");
177 Builder.CreateAlignedStore(LB, LBPtr, Alignment);
178 Builder.CreateAlignedStore(UB, UBPtr, Alignment);
179 Builder.CreateAlignedStore(
Builder.getInt32(0), IsLastPtr, Alignment);
180 Builder.CreateAlignedStore(Stride, StridePtr, Alignment);
185 "polly.indvar.UBAdjusted");
193 switch (Scheduling) {
203 Value *HasIteration =
204 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_EQ, HasWork,
205 Builder.getInt32(1),
"polly.hasIteration");
206 Builder.CreateCondBr(HasIteration, PreHeaderBB, ExitBB);
208 Builder.SetInsertPoint(CheckNextBB);
211 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_EQ, HasWork,
212 Builder.getInt32(1),
"polly.hasWork");
213 Builder.CreateCondBr(HasIteration, PreHeaderBB, ExitBB);
215 Builder.SetInsertPoint(PreHeaderBB);
226 Builder.CreateAlignedStore(AdjustedUB, UBPtr, Alignment);
230 LongType, StridePtr, Alignment,
"polly.kmpc.stride");
235 "polly.indvar.UB.temp");
238 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_SLE, UB, AdjustedUB,
239 "polly.indvar.UB.inRange");
240 UB =
Builder.CreateSelect(UBInRange, UB, AdjustedUB,
"polly.indvar.UB");
241 Builder.CreateAlignedStore(UB, UBPtr, Alignment);
244 llvm::CmpInst::Predicate::ICMP_SLE, LB, UB,
"polly.hasIteration");
245 Builder.CreateCondBr(HasIteration, PreHeaderBB, ExitBB);
248 Builder.SetInsertPoint(PreHeaderBB);
250 "polly.indvar.LB.entry");
252 "polly.indvar.UB.entry");
255 Builder.SetInsertPoint(CheckNextBB);
259 Builder.CreateAdd(LB, ChunkedStride,
"polly.indvar.nextLB");
262 Value *NextUBOutOfBounds =
263 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_SGT, NextUB,
264 AdjustedUB,
"polly.indvar.nextUB.outOfBounds");
265 NextUB =
Builder.CreateSelect(NextUBOutOfBounds, AdjustedUB, NextUB,
266 "polly.indvar.nextUB");
268 Builder.CreateAlignedStore(NextLB, LBPtr, Alignment);
269 Builder.CreateAlignedStore(NextUB, UBPtr, Alignment);
272 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_SLE, NextLB,
273 AdjustedUB,
"polly.hasWork");
274 Builder.CreateCondBr(HasWork, PreHeaderBB, ExitBB);
279 Builder.SetInsertPoint(PreHeaderBB);
288 *
SubFnDT, AfterBB, ICmpInst::ICMP_SLE,
nullptr,
true,
291 BasicBlock::iterator LoopBody =
Builder.GetInsertPoint();
294 Builder.SetInsertPoint(ExitBB);
301 Builder.SetInsertPoint(&*LoopBody);
307 return std::make_tuple(IV, SubFn);
311 const std::string Name =
"__kmpc_global_thread_num";
316 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
317 Type *Params[] = {
Builder.getPtrTy(0)};
319 FunctionType *Ty = FunctionType::get(
Builder.getInt32Ty(), Params,
false);
320 F = Function::Create(Ty, Linkage, Name,
M);
330 const std::string Name =
"__kmpc_push_num_threads";
335 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
339 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
false);
340 F = Function::Create(Ty, Linkage, Name,
M);
345 CallInst *Call =
Builder.CreateCall(F, Args);
351 Value *LBPtr, Value *UBPtr,
354 const std::string Name =
355 is64BitArch() ?
"__kmpc_for_static_init_8" :
"__kmpc_for_static_init_4";
360 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
362 Type *Params[] = {
Builder.getPtrTy(0),
372 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
false);
373 F = Function::Create(Ty, Linkage, Name,
M);
389 CallInst *Call =
Builder.CreateCall(F, Args);
394 const std::string Name =
"__kmpc_for_static_fini";
399 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
401 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
false);
402 F = Function::Create(Ty, Linkage, Name,
M);
407 CallInst *Call =
Builder.CreateCall(F, Args);
412 Value *LB, Value *UB,
415 const std::string Name =
416 is64BitArch() ?
"__kmpc_dispatch_init_8" :
"__kmpc_dispatch_init_4";
421 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
423 Type *Params[] = {
Builder.getPtrTy(0),
431 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
false);
432 F = Function::Create(Ty, Linkage, Name,
M);
446 CallInst *Call =
Builder.CreateCall(F, Args);
455 const std::string Name =
456 is64BitArch() ?
"__kmpc_dispatch_next_8" :
"__kmpc_dispatch_next_4";
461 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
467 FunctionType *Ty = FunctionType::get(
Builder.getInt32Ty(), Params,
false);
468 F = Function::Create(Ty, Linkage, Name,
M);
474 CallInst *Call =
Builder.CreateCall(F, Args);
482 const std::string LocName =
".loc.dummy";
483 GlobalVariable *SourceLocDummy =
M->getGlobalVariable(LocName);
485 if (SourceLocDummy ==
nullptr) {
486 const std::string StructName =
"struct.ident_t";
487 StructType *IdentTy =
488 StructType::getTypeByName(
M->getContext(), StructName);
498 StructType::create(
M->getContext(), LocMembers, StructName,
false);
501 const auto ArrayType =
502 llvm::ArrayType::get(
Builder.getInt8Ty(), 23);
505 GlobalVariable *StrVar =
506 new GlobalVariable(*
M, ArrayType,
true, GlobalValue::PrivateLinkage,
507 nullptr,
".str.ident");
508 StrVar->setAlignment(llvm::Align(1));
510 SourceLocDummy =
new GlobalVariable(
511 *
M, IdentTy,
true, GlobalValue::PrivateLinkage,
nullptr, LocName);
512 SourceLocDummy->setAlignment(llvm::Align(8));
515 Constant *InitStr = ConstantDataArray::getString(
516 M->getContext(),
"Source location dummy.",
true);
518 Constant *StrPtr =
static_cast<Constant *
>(
Builder.CreateInBoundsGEP(
521 Constant *LocInitStruct = ConstantStruct::get(
526 StrVar->setInitializer(InitStr);
527 SourceLocDummy->setInitializer(LocInitStruct);
530 return SourceLocDummy;
534 return (
LongType->getIntegerBitWidth() == 64);
polly dump Polly Dump Function
void createCallStaticFini(Value *GlobalThreadID)
Create a runtime library call to mark the end of a statically scheduled loop.
Function * prepareSubFnDefinition(Function *F) const override
Prepare the definition of the parallel subfunction.
void createCallSpawnThreads(Value *SubFn, Value *SubFnParam, Value *LB, Value *UB, Value *Stride)
Create a runtime library call to spawn the worker threads.
void createCallDispatchInit(Value *GlobalThreadID, Value *LB, Value *UB, Value *Inc, Value *ChunkSize)
Create a runtime library call to prepare the OpenMP runtime.
std::tuple< Value *, Function * > createSubFn(Value *Stride, AllocaInst *Struct, SetVector< Value * > UsedValues, ValueMapT &VMap) override
Create the parallel subfunction.
void createCallStaticInit(Value *GlobalThreadID, Value *IsLastPtr, Value *LBPtr, Value *UBPtr, Value *StridePtr, Value *ChunkSize)
Create a runtime library call to prepare the OpenMP runtime.
GlobalVariable * createSourceLocation()
Create the current source location.
void deployParallelExecution(Function *SubFn, Value *SubFnParam, Value *LB, Value *UB, Value *Stride) override
Create the runtime library calls for spawn and join of the worker threads.
void createCallPushNumThreads(Value *GlobalThreadID, Value *NumThreads)
Create a runtime library call to request a number of threads.
OMPGeneralSchedulingType getSchedType(int ChunkSize, OMPGeneralSchedulingType Scheduling) const
Convert the combination of given chunk size and scheduling type (which might have been set via the co...
Value * createCallDispatchNext(Value *GlobalThreadID, Value *IsLastPtr, Value *LBPtr, Value *UBPtr, Value *StridePtr)
Create a runtime library call to retrieve the next (dynamically) allocated chunk of work for this thr...
bool is64BitArch()
Returns True if 'LongType' is 64bit wide, otherwise: False.
GlobalValue * SourceLocationInfo
The source location struct of this loop.
Value * createCallGlobalThreadNum()
Create a runtime library call to get the current global thread number.
PollyIRBuilder & Builder
The IR builder we use to create instructions.
Function * createSubFnDefinition()
Create the definition of the parallel subfunction.
std::unique_ptr< DominatorTree > SubFnDT
The dominance tree for the generated subfunction.
Module * M
The current module.
llvm::DebugLoc DLGenerated
Debug location for generated code without direct link to any specific line.
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.
std::unique_ptr< LoopInfo > SubFnLI
The loop info for the generated subfunction.
This file contains the declaration of the PolyhedralInfo class, which will provide an interface to ex...
OMPGeneralSchedulingType PollyScheduling
@ 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::DenseMap< llvm::AssertingVH< llvm::Value >, llvm::AssertingVH< llvm::Value > > ValueMapT
Type to remap values.