LLVM  15.0.0git
Macros | Functions | Variables
OMPIRBuilder.cpp File Reference
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/CodeMetrics.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/Value.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/CodeExtractor.h"
#include "llvm/Transforms/Utils/LoopPeel.h"
#include "llvm/Transforms/Utils/UnrollLoop.h"
#include <cstdint>
#include "llvm/Frontend/OpenMP/OMPKinds.def"
Include dependency graph for OMPIRBuilder.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "openmp-ir-builder"
 
#define OMP_ATTRS_SET(VarName, AttrSet)   AttributeSet VarName = AttrSet;
 
#define OMP_RTL_ATTRS(Enum, FnAttrSet, RetAttrSet, ArgAttrSets)
 
#define OMP_RTL(Enum, Str, IsVarArg, ReturnType, ...)
 
#define OMP_RTL(Enum, Str, ...)
 
#define OMP_CANCEL_KIND(Enum, Str, DirectiveEnum, Value)
 
#define OMP_TYPE(VarName, InitValue)   VarName = InitValue;
 
#define OMP_ARRAY_TYPE(VarName, ElemTy, ArraySize)
 
#define OMP_FUNCTION_TYPE(VarName, IsVarArg, ReturnType, ...)
 
#define OMP_STRUCT_TYPE(VarName, StructName, ...)
 

Functions

static bool isConflictIP (IRBuilder<>::InsertPoint IP1, IRBuilder<>::InsertPoint IP2)
 Return whether IP1 and IP2 are ambiguous, i.e. More...
 
static bool isValidWorkshareLoopScheduleType (OMPScheduleType SchedType)
 
static OMPScheduleType getOpenMPBaseScheduleType (llvm::omp::ScheduleKind ClauseKind, bool HasChunks, bool HasSimdModifier)
 Determine which scheduling algorithm to use, determined from schedule clause arguments. More...
 
static OMPScheduleType getOpenMPOrderingScheduleType (OMPScheduleType BaseScheduleType, bool HasOrderedClause)
 Adds ordering modifier flags to schedule type. More...
 
static OMPScheduleType getOpenMPMonotonicityScheduleType (OMPScheduleType ScheduleType, bool HasSimdModifier, bool HasMonotonic, bool HasNonmonotonic, bool HasOrderedClause)
 Adds monotonicity modifier flags to schedule type. More...
 
static OMPScheduleType computeOpenMPScheduleType (ScheduleKind ClauseKind, bool HasChunks, bool HasSimdModifier, bool HasMonotonicModifier, bool HasNonmonotonicModifier, bool HasOrderedClause)
 Determine the schedule type using schedule and ordering clause arguments. More...
 
static void redirectTo (BasicBlock *Source, BasicBlock *Target, DebugLoc DL)
 Make Source branch to Target. More...
 
FunctiongetFreshReductionFunc (Module &M)
 Create a function with a unique name and a "void (i8*, i8*)" signature in the given module and return it. More...
 
static FunctionCallee getKmpcForStaticInitForType (Type *Ty, Module &M, OpenMPIRBuilder &OMPBuilder)
 
static FunctionCallee getKmpcForDynamicInitForType (Type *Ty, Module &M, OpenMPIRBuilder &OMPBuilder)
 Returns an LLVM function to call for initializing loop bounds using OpenMP dynamic scheduling depending on type. More...
 
static FunctionCallee getKmpcForDynamicNextForType (Type *Ty, Module &M, OpenMPIRBuilder &OMPBuilder)
 Returns an LLVM function to call for updating the next loop using OpenMP dynamic scheduling depending on type. More...
 
static FunctionCallee getKmpcForDynamicFiniForType (Type *Ty, Module &M, OpenMPIRBuilder &OMPBuilder)
 Returns an LLVM function to call for finalizing the dynamic loop using depending on type. More...
 
static void redirectAllPredecessorsTo (BasicBlock *OldTarget, BasicBlock *NewTarget, DebugLoc DL)
 Redirect all edges that branch to OldTarget to NewTarget. More...
 
static void removeUnusedBlocksFromParent (ArrayRef< BasicBlock * > BBs)
 Determine which blocks in BBs are reachable from outside and remove the ones that are not reachable from the function. More...
 
static void addLoopMetadata (CanonicalLoopInfo *Loop, ArrayRef< Metadata * > Properties)
 Attach loop metadata Properties to the loop described by Loop. More...
 
static void addSimdMetadata (BasicBlock *Block, MDNode *AccessGroup, LoopInfo &LI)
 Attach llvm.access.group metadata to the memref instructions of Block. More...
 
static std::unique_ptr< TargetMachinecreateTargetMachine (Function *F, CodeGenOpt::Level OptLevel)
 Create the TargetMachine object to query the backend for optimization preferences. More...
 
static int32_t computeHeuristicUnrollFactor (CanonicalLoopInfo *CLI)
 Heuristically determine the best-performant unroll factor for CLI. More...
 

Variables

static cl::opt< bool > OptimisticAttributes ("openmp-ir-builder-optimistic-attributes", cl::Hidden, cl::desc("Use optimistic attributes describing " "'as-if' properties of runtime calls."), cl::init(false))
 
static cl::opt< doubleUnrollThresholdFactor ("openmp-ir-builder-unroll-threshold-factor", cl::Hidden, cl::desc("Factor for the unroll threshold to account for code " "simplifications still taking place"), cl::init(1.5))
 

Detailed Description

This file implements the OpenMPIRBuilder class, which is used as a convenient way to create LLVM instructions for OpenMP directives.

Definition in file OMPIRBuilder.cpp.

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "openmp-ir-builder"

Definition at line 43 of file OMPIRBuilder.cpp.

◆ OMP_ARRAY_TYPE

#define OMP_ARRAY_TYPE (   VarName,
  ElemTy,
  ArraySize 
)
Value:
VarName##Ty = ArrayType::get(ElemTy, ArraySize); \
VarName##PtrTy = PointerType::getUnqual(VarName##Ty);

◆ OMP_ATTRS_SET

#define OMP_ATTRS_SET (   VarName,
  AttrSet 
)    AttributeSet VarName = AttrSet;

◆ OMP_CANCEL_KIND

#define OMP_CANCEL_KIND (   Enum,
  Str,
  DirectiveEnum,
  Value 
)
Value:
case DirectiveEnum: \
CancelKind = Builder.getInt32(Value); \
break;

◆ OMP_FUNCTION_TYPE

#define OMP_FUNCTION_TYPE (   VarName,
  IsVarArg,
  ReturnType,
  ... 
)
Value:
VarName = FunctionType::get(ReturnType, {__VA_ARGS__}, IsVarArg); \
VarName##Ptr = PointerType::getUnqual(VarName);

◆ OMP_RTL [1/2]

#define OMP_RTL (   Enum,
  Str,
  IsVarArg,
  ReturnType,
  ... 
)
Value:
case Enum: \
IsVarArg); \
Fn = M.getFunction(Str); \
break;

◆ OMP_RTL [2/2]

#define OMP_RTL (   Enum,
  Str,
  ... 
)
Value:
case Enum: \
Fn = Function::Create(FnTy, GlobalValue::ExternalLinkage, Str, M); \
break;

◆ OMP_RTL_ATTRS

#define OMP_RTL_ATTRS (   Enum,
  FnAttrSet,
  RetAttrSet,
  ArgAttrSets 
)
Value:
case Enum: \
FnAttrs = FnAttrs.addAttributes(Ctx, FnAttrSet); \
RetAttrs = RetAttrs.addAttributes(Ctx, RetAttrSet); \
for (size_t ArgNo = 0; ArgNo < ArgAttrSets.size(); ++ArgNo) \
ArgAttrs[ArgNo] = \
ArgAttrs[ArgNo].addAttributes(Ctx, ArgAttrSets[ArgNo]); \
Fn.setAttributes(AttributeList::get(Ctx, FnAttrs, RetAttrs, ArgAttrs)); \
break;

◆ OMP_STRUCT_TYPE

#define OMP_STRUCT_TYPE (   VarName,
  StructName,
  ... 
)
Value:
T = StructType::getTypeByName(Ctx, StructName); \
if (!T) \
T = StructType::create(Ctx, {__VA_ARGS__}, StructName); \
VarName = T; \
VarName##Ptr = PointerType::getUnqual(T);

◆ OMP_TYPE

#define OMP_TYPE (   VarName,
  InitValue 
)    VarName = InitValue;

Function Documentation

◆ addLoopMetadata()

static void addLoopMetadata ( CanonicalLoopInfo Loop,
ArrayRef< Metadata * >  Properties 
)
static

◆ addSimdMetadata()

static void addSimdMetadata ( BasicBlock Block,
MDNode AccessGroup,
LoopInfo LI 
)
static

Attach llvm.access.group metadata to the memref instructions of Block.

Definition at line 2637 of file OMPIRBuilder.cpp.

References I.

Referenced by llvm::OpenMPIRBuilder::applySimd().

◆ computeHeuristicUnrollFactor()

static int32_t computeHeuristicUnrollFactor ( CanonicalLoopInfo CLI)
static

Heuristically determine the best-performant unroll factor for CLI.

This depends on the target processor. We are re-using the same heuristics as the LoopUnrollPass.

Definition at line 2750 of file OMPIRBuilder.cpp.

References llvm::CodeGenOpt::Aggressive, llvm::ApproximateLoopSize(), assert(), BB, llvm::TargetTransformInfo::UnrollingPreferences::BEInsns, llvm::LoopBase< BlockT, LoopT >::blocks(), llvm::CodeMetrics::collectEphemeralValues(), llvm::computeUnrollCount(), llvm::MCID::Convergent, llvm::TargetTransformInfo::UnrollingPreferences::Count, createTargetMachine(), llvm::dbgs(), F, FAM, llvm::TargetTransformInfo::UnrollingPreferences::Force, llvm::gatherPeelingPreferences(), llvm::gatherUnrollingPreferences(), llvm::CanonicalLoopInfo::getFunction(), llvm::CanonicalLoopInfo::getHeader(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), LLVM_DEBUG, llvm::SPII::Load, llvm::None, llvm::TargetTransformInfo::UnrollingPreferences::OptSizeThreshold, llvm::TargetTransformInfo::UnrollingPreferences::PartialOptSizeThreshold, llvm::TargetTransformInfo::UnrollingPreferences::PartialThreshold, llvm::AnalysisManager< IRUnitT, ExtraArgTs >::registerPass(), llvm::AssumptionAnalysis::run(), llvm::DominatorTreeAnalysis::run(), llvm::LoopAnalysis::run(), llvm::ScalarEvolutionAnalysis::run(), llvm::TargetIRAnalysis::run(), llvm::SPII::Store, llvm::Value::stripPointerCasts(), llvm::TargetTransformInfo::UnrollingPreferences::Threshold, TM, and UnrollThresholdFactor.

Referenced by llvm::OpenMPIRBuilder::unrollLoopPartial().

◆ computeOpenMPScheduleType()

static OMPScheduleType computeOpenMPScheduleType ( ScheduleKind  ClauseKind,
bool  HasChunks,
bool  HasSimdModifier,
bool  HasMonotonicModifier,
bool  HasNonmonotonicModifier,
bool  HasOrderedClause 
)
static

Determine the schedule type using schedule and ordering clause arguments.

Definition at line 219 of file OMPIRBuilder.cpp.

References assert(), getOpenMPBaseScheduleType(), getOpenMPMonotonicityScheduleType(), getOpenMPOrderingScheduleType(), and isValidWorkshareLoopScheduleType().

Referenced by llvm::OpenMPIRBuilder::applyWorkshareLoop().

◆ createTargetMachine()

static std::unique_ptr<TargetMachine> createTargetMachine ( Function F,
CodeGenOpt::Level  OptLevel 
)
static

Create the TargetMachine object to query the backend for optimization preferences.

Ideally, this would be passed from the front-end to the OpenMPBuilder, but e.g. Clang does not pass it to its CodeGen layer and creates it only when needed for the LLVM pass pipline. We use some default options to avoid having to pass too many settings from the frontend that probably do not matter.

Currently, TargetMachine is only used sometimes by the unrollLoopPartial method. If we are going to use TargetMachine for more purposes, especially those that are sensitive to TargetOptions, RelocModel and CodeModel, it might become be worth requiring front-ends to pass on their TargetMachine, or at least cache it between methods. Note that while fontends such as Clang have just a single main TargetMachine per translation unit, "target-cpu" and "target-features" that determine the TargetMachine are per-function and can be overrided using attribute((target("OPTIONS"))).

Definition at line 2729 of file OMPIRBuilder.cpp.

References llvm::Target::createTargetMachine(), F, llvm::TargetRegistry::lookupTarget(), M, llvm::None, and Options.

Referenced by computeHeuristicUnrollFactor(), and LLVMCreateTargetMachine().

◆ getFreshReductionFunc()

Function* getFreshReductionFunc ( Module M)

Create a function with a unique name and a "void (i8*, i8*)" signature in the given module and return it.

Definition at line 1387 of file OMPIRBuilder.cpp.

References llvm::Function::Create(), llvm::FunctionType::get(), llvm::Type::getInt8PtrTy(), llvm::Type::getVoidTy(), llvm::GlobalValue::InternalLinkage, and M.

Referenced by llvm::OpenMPIRBuilder::createReductions().

◆ getKmpcForDynamicFiniForType()

static FunctionCallee getKmpcForDynamicFiniForType ( Type Ty,
Module M,
OpenMPIRBuilder OMPBuilder 
)
static

Returns an LLVM function to call for finalizing the dynamic loop using depending on type.

Only i32 and i64 are supported by the runtime. Always interpret integers as unsigned similarly to CanonicalLoopInfo.

Definition at line 2111 of file OMPIRBuilder.cpp.

References llvm::Type::getIntegerBitWidth(), llvm::OpenMPIRBuilder::getOrCreateRuntimeFunction(), llvm_unreachable, and M.

◆ getKmpcForDynamicInitForType()

static FunctionCallee getKmpcForDynamicInitForType ( Type Ty,
Module M,
OpenMPIRBuilder OMPBuilder 
)
static

Returns an LLVM function to call for initializing loop bounds using OpenMP dynamic scheduling depending on type.

Only i32 and i64 are supported by the runtime. Always interpret integers as unsigned similarly to CanonicalLoopInfo.

Definition at line 2080 of file OMPIRBuilder.cpp.

References llvm::Type::getIntegerBitWidth(), llvm::OpenMPIRBuilder::getOrCreateRuntimeFunction(), llvm_unreachable, and M.

◆ getKmpcForDynamicNextForType()

static FunctionCallee getKmpcForDynamicNextForType ( Type Ty,
Module M,
OpenMPIRBuilder OMPBuilder 
)
static

Returns an LLVM function to call for updating the next loop using OpenMP dynamic scheduling depending on type.

Only i32 and i64 are supported by the runtime. Always interpret integers as unsigned similarly to CanonicalLoopInfo.

Definition at line 2096 of file OMPIRBuilder.cpp.

References llvm::Type::getIntegerBitWidth(), llvm::OpenMPIRBuilder::getOrCreateRuntimeFunction(), llvm_unreachable, and M.

◆ getKmpcForStaticInitForType()

static FunctionCallee getKmpcForStaticInitForType ( Type Ty,
Module M,
OpenMPIRBuilder OMPBuilder 
)
static

◆ getOpenMPBaseScheduleType()

static OMPScheduleType getOpenMPBaseScheduleType ( llvm::omp::ScheduleKind  ClauseKind,
bool  HasChunks,
bool  HasSimdModifier 
)
static

◆ getOpenMPMonotonicityScheduleType()

static OMPScheduleType getOpenMPMonotonicityScheduleType ( OMPScheduleType  ScheduleType,
bool  HasSimdModifier,
bool  HasMonotonic,
bool  HasNonmonotonic,
bool  HasOrderedClause 
)
static

◆ getOpenMPOrderingScheduleType()

static OMPScheduleType getOpenMPOrderingScheduleType ( OMPScheduleType  BaseScheduleType,
bool  HasOrderedClause 
)
static

◆ isConflictIP()

static bool isConflictIP ( IRBuilder<>::InsertPoint  IP1,
IRBuilder<>::InsertPoint  IP2 
)
static

Return whether IP1 and IP2 are ambiguous, i.e.

that inserting instructions at position IP1 may change the meaning of IP2 or vice-versa. This is because an InsertPoint stores the instruction before something is inserted. For instance, if both point to the same instruction, two IRBuilders alternating creating instruction will cause the instructions to be interleaved.

Definition at line 66 of file OMPIRBuilder.cpp.

Referenced by llvm::OpenMPIRBuilder::createAtomicUpdate(), llvm::OpenMPIRBuilder::createParallel(), and llvm::OpenMPIRBuilder::createSections().

◆ isValidWorkshareLoopScheduleType()

static bool isValidWorkshareLoopScheduleType ( OMPScheduleType  SchedType)
static

Definition at line 73 of file OMPIRBuilder.cpp.

References llvm::omp::MonotonicityMask, llvm::omp::NomergeOrderedAuto, llvm::omp::NomergeOrderedDynamicChunked, llvm::omp::NomergeOrderedGuidedChunked, llvm::omp::NomergeOrderedRuntime, llvm::omp::NomergeOrderedStatic, llvm::omp::NomergeOrderedStaticChunked, llvm::omp::NomergeOrderedTrapezoidal, llvm::omp::NomergeUnorderedAuto, llvm::omp::NomergeUnorderedBalanced, llvm::omp::NomergeUnorderedDynamicChunked, llvm::omp::NomergeUnorderedGreedy, llvm::omp::NomergeUnorderedGuidedAnalyticalChunked, llvm::omp::NomergeUnorderedGuidedChunked, llvm::omp::NomergeUnorderedGuidedIterativeChunked, llvm::omp::NomergeUnorderedRuntime, llvm::omp::NomergeUnorderedStatic, llvm::omp::NomergeUnorderedStaticChunked, llvm::omp::NomergeUnorderedSteal, llvm::omp::NomergeUnorderedTrapezoidal, llvm::omp::OrderdTrapezoidal, llvm::omp::OrderedAuto, llvm::omp::OrderedDynamicChunked, llvm::omp::OrderedGuidedChunked, llvm::omp::OrderedRuntime, llvm::omp::OrderedStatic, llvm::omp::OrderedStaticChunked, llvm::omp::UnorderedAuto, llvm::omp::UnorderedBalanced, llvm::omp::UnorderedDynamicChunked, llvm::omp::UnorderedGreedy, llvm::omp::UnorderedGuidedAnalyticalChunked, llvm::omp::UnorderedGuidedChunked, llvm::omp::UnorderedGuidedIterativeChunked, llvm::omp::UnorderedGuidedSimd, llvm::omp::UnorderedRuntime, llvm::omp::UnorderedRuntimeSimd, llvm::omp::UnorderedStatic, llvm::omp::UnorderedStaticBalancedChunked, llvm::omp::UnorderedStaticChunked, llvm::omp::UnorderedSteal, and llvm::omp::UnorderedTrapezoidal.

Referenced by computeOpenMPScheduleType().

◆ redirectAllPredecessorsTo()

static void redirectAllPredecessorsTo ( BasicBlock OldTarget,
BasicBlock NewTarget,
DebugLoc  DL 
)
static

Redirect all edges that branch to OldTarget to NewTarget.

That is, after this OldTarget will be orphaned.

Definition at line 2251 of file OMPIRBuilder.cpp.

References DL, llvm::make_early_inc_range(), llvm::predecessors(), and redirectTo().

Referenced by llvm::OpenMPIRBuilder::collapseLoops(), and llvm::OpenMPIRBuilder::tileLoops().

◆ redirectTo()

static void redirectTo ( BasicBlock Source,
BasicBlock Target,
DebugLoc  DL 
)
static

Make Source branch to Target.

Handles two situations:

  • Source already has an unconditional branch.
  • Source is a degenerate block (no terminator because the BB is the current head of the IR construction).

Definition at line 240 of file OMPIRBuilder.cpp.

References assert(), llvm::BranchInst::Create(), DL, llvm::BasicBlock::removePredecessor(), llvm::Sched::Source, and llvm::M68kBeads::Term.

Referenced by llvm::OpenMPIRBuilder::collapseLoops(), redirectAllPredecessorsTo(), and llvm::OpenMPIRBuilder::tileLoops().

◆ removeUnusedBlocksFromParent()

static void removeUnusedBlocksFromParent ( ArrayRef< BasicBlock * >  BBs)
static

Determine which blocks in BBs are reachable from outside and remove the ones that are not reachable from the function.

Definition at line 2259 of file OMPIRBuilder.cpp.

References BB, llvm::ArrayRef< T >::begin(), llvm::DeleteDeadBlocks(), llvm::ArrayRef< T >::end(), and llvm::make_early_inc_range().

Referenced by llvm::OpenMPIRBuilder::collapseLoops(), and llvm::OpenMPIRBuilder::tileLoops().

Variable Documentation

◆ OptimisticAttributes

cl::opt<bool> OptimisticAttributes("openmp-ir-builder-optimistic-attributes", cl::Hidden, cl::desc("Use optimistic attributes describing " "'as-if' properties of runtime calls."), cl::init(false))
static

◆ UnrollThresholdFactor

cl::opt<double> UnrollThresholdFactor("openmp-ir-builder-unroll-threshold-factor", cl::Hidden, cl::desc("Factor for the unroll threshold to account for code " "simplifications still taking place"), cl::init(1.5))
static
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
T
llvm::tgtok::VarName
@ VarName
Definition: TGLexer.h:71
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::ARM::WinEH::ReturnType
ReturnType
Definition: ARMWinEH.h:25
Builder
assume Assume Builder
Definition: AssumeBundleBuilder.cpp:651
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::pdb::PDB_SymType::Enum
@ Enum
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::Value
LLVM Value Representation.
Definition: Value.h:74