14#include "llvm/IR/Dominators.h"
15#include "llvm/IR/Module.h"
24 const std::string Name =
"__kmpc_fork_call";
26 Type *KMPCMicroTy = StructType::getTypeByName(
M->getContext(),
"kmpc_micro");
30 Type *MicroParams[] = {
Builder.getInt32Ty()->getPointerTo(),
31 Builder.getInt32Ty()->getPointerTo()};
33 KMPCMicroTy = FunctionType::get(
Builder.getVoidTy(), MicroParams,
true);
39 StructType::getTypeByName(
M->getContext(),
"struct.ident_t");
41 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
42 Type *Params[] = {IdentTy->getPointerTo(),
Builder.getInt32Ty(),
43 KMPCMicroTy->getPointerTo()};
45 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
true);
46 F = Function::Create(Ty, Linkage, Name,
M);
49 Value *Task =
Builder.CreatePointerBitCastOrAddrSpaceCast(
50 SubFn, KMPCMicroTy->getPointerTo());
60 CallInst *Call =
Builder.CreateCall(F, Args);
79 std::vector<Type *> Arguments = {
Builder.getInt32Ty()->getPointerTo(),
80 Builder.getInt32Ty()->getPointerTo(),
86 FunctionType *FT = FunctionType::get(
Builder.getVoidTy(), Arguments,
false);
87 Function *SubFn = Function::Create(FT, Function::InternalLinkage,
88 F->getName() +
"_polly_subfn",
M);
90 Function::arg_iterator AI = SubFn->arg_begin();
91 AI->setName(
"polly.kmpc.global_tid");
93 AI->setName(
"polly.kmpc.bound_tid");
95 AI->setName(
"polly.kmpc.lb");
97 AI->setName(
"polly.kmpc.ub");
99 AI->setName(
"polly.kmpc.inc");
101 AI->setName(
"polly.kmpc.shared");
131std::tuple<Value *, Function *>
133 AllocaInst *StructData,
134 SetVector<Value *> Data,
ValueMapT &Map) {
136 LLVMContext &Context = SubFn->getContext();
139 BasicBlock *PrevBB =
Builder.GetInsertBlock();
142 BasicBlock *HeaderBB = BasicBlock::Create(Context,
"polly.par.setup", SubFn);
143 BasicBlock *ExitBB = BasicBlock::Create(Context,
"polly.par.exit", SubFn);
144 BasicBlock *CheckNextBB =
145 BasicBlock::Create(Context,
"polly.par.checkNext", SubFn);
146 BasicBlock *PreHeaderBB =
147 BasicBlock::Create(Context,
"polly.par.loadIVBounds", SubFn);
149 DT.addNewBlock(HeaderBB, PrevBB);
150 DT.addNewBlock(ExitBB, HeaderBB);
151 DT.addNewBlock(CheckNextBB, HeaderBB);
152 DT.addNewBlock(PreHeaderBB, HeaderBB);
155 Builder.SetInsertPoint(HeaderBB);
159 "polly.par.lastIterPtr");
164 Function::arg_iterator AI = SubFn->arg_begin();
174 Value *Stride = &*AI;
176 Value *Shared = &*AI;
180 const auto Alignment = llvm::Align(
is64BitArch() ? 8 : 4);
182 "polly.par.global_tid");
184 Builder.CreateAlignedStore(LB, LBPtr, Alignment);
185 Builder.CreateAlignedStore(UB, UBPtr, Alignment);
186 Builder.CreateAlignedStore(
Builder.getInt32(0), IsLastPtr, Alignment);
187 Builder.CreateAlignedStore(Stride, StridePtr, Alignment);
192 "polly.indvar.UBAdjusted");
200 switch (Scheduling) {
210 Value *HasIteration =
211 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_EQ, HasWork,
212 Builder.getInt32(1),
"polly.hasIteration");
213 Builder.CreateCondBr(HasIteration, PreHeaderBB, ExitBB);
215 Builder.SetInsertPoint(CheckNextBB);
218 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_EQ, HasWork,
219 Builder.getInt32(1),
"polly.hasWork");
220 Builder.CreateCondBr(HasIteration, PreHeaderBB, ExitBB);
222 Builder.SetInsertPoint(PreHeaderBB);
233 Builder.CreateAlignedStore(AdjustedUB, UBPtr, Alignment);
237 LongType, StridePtr, Alignment,
"polly.kmpc.stride");
242 "polly.indvar.UB.temp");
245 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_SLE, UB, AdjustedUB,
246 "polly.indvar.UB.inRange");
247 UB =
Builder.CreateSelect(UBInRange, UB, AdjustedUB,
"polly.indvar.UB");
248 Builder.CreateAlignedStore(UB, UBPtr, Alignment);
251 llvm::CmpInst::Predicate::ICMP_SLE, LB, UB,
"polly.hasIteration");
252 Builder.CreateCondBr(HasIteration, PreHeaderBB, ExitBB);
255 Builder.SetInsertPoint(PreHeaderBB);
257 "polly.indvar.LB.entry");
259 "polly.indvar.UB.entry");
262 Builder.SetInsertPoint(CheckNextBB);
266 Builder.CreateAdd(LB, ChunkedStride,
"polly.indvar.nextLB");
269 Value *NextUBOutOfBounds =
270 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_SGT, NextUB,
271 AdjustedUB,
"polly.indvar.nextUB.outOfBounds");
272 NextUB =
Builder.CreateSelect(NextUBOutOfBounds, AdjustedUB, NextUB,
273 "polly.indvar.nextUB");
275 Builder.CreateAlignedStore(NextLB, LBPtr, Alignment);
276 Builder.CreateAlignedStore(NextUB, UBPtr, Alignment);
279 Builder.CreateICmp(llvm::CmpInst::Predicate::ICMP_SLE, NextLB,
280 AdjustedUB,
"polly.hasWork");
281 Builder.CreateCondBr(HasWork, PreHeaderBB, ExitBB);
286 Builder.SetInsertPoint(PreHeaderBB);
295 ICmpInst::ICMP_SLE,
nullptr,
true,
298 BasicBlock::iterator LoopBody =
Builder.GetInsertPoint();
301 Builder.SetInsertPoint(ExitBB);
308 Builder.SetInsertPoint(&*LoopBody);
310 return std::make_tuple(IV, SubFn);
314 const std::string Name =
"__kmpc_global_thread_num";
319 StructType *IdentTy =
320 StructType::getTypeByName(
M->getContext(),
"struct.ident_t");
322 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
323 Type *Params[] = {IdentTy->getPointerTo()};
325 FunctionType *Ty = FunctionType::get(
Builder.getInt32Ty(), Params,
false);
326 F = Function::Create(Ty, Linkage, Name,
M);
336 const std::string Name =
"__kmpc_push_num_threads";
341 StructType *IdentTy =
342 StructType::getTypeByName(
M->getContext(),
"struct.ident_t");
344 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
345 Type *Params[] = {IdentTy->getPointerTo(),
Builder.getInt32Ty(),
348 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
false);
349 F = Function::Create(Ty, Linkage, Name,
M);
354 CallInst *Call =
Builder.CreateCall(F, Args);
360 Value *LBPtr, Value *UBPtr,
363 const std::string Name =
364 is64BitArch() ?
"__kmpc_for_static_init_8" :
"__kmpc_for_static_init_4";
366 StructType *IdentTy =
367 StructType::getTypeByName(
M->getContext(),
"struct.ident_t");
371 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
373 Type *Params[] = {IdentTy->getPointerTo(),
376 Builder.getInt32Ty()->getPointerTo(),
383 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
false);
384 F = Function::Create(Ty, Linkage, Name,
M);
400 CallInst *Call =
Builder.CreateCall(F, Args);
405 const std::string Name =
"__kmpc_for_static_fini";
407 StructType *IdentTy =
408 StructType::getTypeByName(
M->getContext(),
"struct.ident_t");
412 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
413 Type *Params[] = {IdentTy->getPointerTo(),
Builder.getInt32Ty()};
414 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
false);
415 F = Function::Create(Ty, Linkage, Name,
M);
420 CallInst *Call =
Builder.CreateCall(F, Args);
425 Value *LB, Value *UB,
428 const std::string Name =
429 is64BitArch() ?
"__kmpc_dispatch_init_8" :
"__kmpc_dispatch_init_4";
431 StructType *IdentTy =
432 StructType::getTypeByName(
M->getContext(),
"struct.ident_t");
436 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
438 Type *Params[] = {IdentTy->getPointerTo(),
446 FunctionType *Ty = FunctionType::get(
Builder.getVoidTy(), Params,
false);
447 F = Function::Create(Ty, Linkage, Name,
M);
461 CallInst *Call =
Builder.CreateCall(F, Args);
470 const std::string Name =
471 is64BitArch() ?
"__kmpc_dispatch_next_8" :
"__kmpc_dispatch_next_4";
473 StructType *IdentTy =
474 StructType::getTypeByName(
M->getContext(),
"struct.ident_t");
478 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
480 Type *Params[] = {IdentTy->getPointerTo(),
482 Builder.getInt32Ty()->getPointerTo(),
487 FunctionType *Ty = FunctionType::get(
Builder.getInt32Ty(), Params,
false);
488 F = Function::Create(Ty, Linkage, Name,
M);
494 CallInst *Call =
Builder.CreateCall(F, Args);
502 const std::string LocName =
".loc.dummy";
503 GlobalVariable *SourceLocDummy =
M->getGlobalVariable(LocName);
505 if (SourceLocDummy ==
nullptr) {
506 const std::string StructName =
"struct.ident_t";
507 StructType *IdentTy =
508 StructType::getTypeByName(
M->getContext(), StructName);
518 StructType::create(
M->getContext(), LocMembers, StructName,
false);
521 const auto ArrayType =
522 llvm::ArrayType::get(
Builder.getInt8Ty(), 23);
525 GlobalVariable *StrVar =
526 new GlobalVariable(*
M, ArrayType,
true, GlobalValue::PrivateLinkage,
527 nullptr,
".str.ident");
528 StrVar->setAlignment(llvm::Align(1));
530 SourceLocDummy =
new GlobalVariable(
531 *
M, IdentTy,
true, GlobalValue::PrivateLinkage,
nullptr, LocName);
532 SourceLocDummy->setAlignment(llvm::Align(8));
535 Constant *InitStr = ConstantDataArray::getString(
536 M->getContext(),
"Source location dummy.",
true);
538 Constant *StrPtr =
static_cast<Constant *
>(
Builder.CreateInBoundsGEP(
541 Constant *LocInitStruct = ConstantStruct::get(
546 StrVar->setInitializer(InitStr);
547 SourceLocDummy->setInitializer(LocInitStruct);
550 return SourceLocDummy;
554 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.
DominatorTree & DT
The dominance tree of the current function we need to update.
Function * createSubFnDefinition()
Create the definition of the parallel subfunction.
Module * M
The current module.
LoopInfo & LI
The loop info of the current function we need to update.
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.
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.