LLVM 17.0.0git
|
This file implements the OpenMPIRBuilder class, which is used as a convenient way to create LLVM instructions for OpenMP directives. More...
#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/Bitcode/BitcodeReader.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.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/Cloning.h"
#include "llvm/Transforms/Utils/CodeExtractor.h"
#include "llvm/Transforms/Utils/LoopPeel.h"
#include "llvm/Transforms/Utils/UnrollLoop.h"
#include <cstdint>
#include <optional>
#include "llvm/Frontend/OpenMP/OMPKinds.def"
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, Packed, ...) |
Functions | |
static bool | isConflictIP (IRBuilder<>::InsertPoint IP1, IRBuilder<>::InsertPoint IP2) |
Return whether IP1 and IP2 are ambiguous, i.e. | |
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. | |
static OMPScheduleType | getOpenMPOrderingScheduleType (OMPScheduleType BaseScheduleType, bool HasOrderedClause) |
Adds ordering modifier flags to schedule type. | |
static OMPScheduleType | getOpenMPMonotonicityScheduleType (OMPScheduleType ScheduleType, bool HasSimdModifier, bool HasMonotonic, bool HasNonmonotonic, bool HasOrderedClause) |
Adds monotonicity modifier flags to schedule type. | |
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. | |
static void | redirectTo (BasicBlock *Source, BasicBlock *Target, DebugLoc DL) |
Make Source branch to Target . | |
Function * | getFreshReductionFunc (Module &M) |
Create a function with a unique name and a "void (i8*, i8*)" signature in the given module and return it. | |
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 . | |
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 . | |
static FunctionCallee | getKmpcForDynamicFiniForType (Type *Ty, Module &M, OpenMPIRBuilder &OMPBuilder) |
Returns an LLVM function to call for finalizing the dynamic loop using depending on type . | |
static void | redirectAllPredecessorsTo (BasicBlock *OldTarget, BasicBlock *NewTarget, DebugLoc DL) |
Redirect all edges that branch to OldTarget to NewTarget . | |
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. | |
static void | addBasicBlockMetadata (BasicBlock *BB, ArrayRef< Metadata * > Properties) |
Attach metadata Properties to the basic block described by BB . | |
static void | addLoopMetadata (CanonicalLoopInfo *Loop, ArrayRef< Metadata * > Properties) |
Attach loop metadata Properties to the loop described by Loop . | |
static void | addSimdMetadata (BasicBlock *Block, MDNode *AccessGroup, LoopInfo &LI) |
Attach llvm.access.group metadata to the memref instructions of Block . | |
static std::unique_ptr< TargetMachine > | createTargetMachine (Function *F, CodeGenOpt::Level OptLevel) |
Create the TargetMachine object to query the backend for optimization preferences. | |
static int32_t | computeHeuristicUnrollFactor (CanonicalLoopInfo *CLI) |
Heuristically determine the best-performant unroll factor for CLI . | |
static Function * | createOutlinedFunction (OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, StringRef FuncName, SmallVectorImpl< Value * > &Inputs, OpenMPIRBuilder::TargetBodyGenCallbackTy &CBFunc) |
static void | emitTargetOutlinedFunction (OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, TargetRegionEntryInfo &EntryInfo, Function *&OutlinedFn, int32_t NumTeams, int32_t NumThreads, SmallVectorImpl< Value * > &Inputs, OpenMPIRBuilder::TargetBodyGenCallbackTy &CBFunc) |
static void | emitTargetCall (IRBuilderBase &Builder, Function *OutlinedFn, SmallVectorImpl< Value * > &Args) |
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< 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)) |
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.
#define DEBUG_TYPE "openmp-ir-builder" |
Definition at line 47 of file OMPIRBuilder.cpp.
#define OMP_ARRAY_TYPE | ( | VarName, | |
ElemTy, | |||
ArraySize | |||
) |
#define OMP_ATTRS_SET | ( | VarName, | |
AttrSet | |||
) | AttributeSet VarName = AttrSet; |
#define OMP_CANCEL_KIND | ( | Enum, | |
Str, | |||
DirectiveEnum, | |||
Value | |||
) |
#define OMP_FUNCTION_TYPE | ( | VarName, | |
IsVarArg, | |||
ReturnType, | |||
... | |||
) |
#define OMP_RTL | ( | Enum, | |
Str, | |||
IsVarArg, | |||
ReturnType, | |||
... | |||
) |
#define OMP_RTL | ( | Enum, | |
Str, | |||
... | |||
) |
#define OMP_RTL_ATTRS | ( | Enum, | |
FnAttrSet, | |||
RetAttrSet, | |||
ArgAttrSets | |||
) |
#define OMP_STRUCT_TYPE | ( | VarName, | |
StructName, | |||
Packed, | |||
... | |||
) |
#define OMP_TYPE | ( | VarName, | |
InitValue | |||
) | VarName = InitValue; |
|
static |
Attach metadata Properties
to the basic block described by BB
.
If the basic block already has metadata, the basic block properties are appended.
Definition at line 2972 of file OMPIRBuilder.cpp.
References llvm::append_range(), llvm::drop_begin(), llvm::ArrayRef< T >::empty(), llvm::BasicBlock::getContext(), llvm::MDNode::getDistinct(), llvm::Instruction::getMetadata(), llvm::BasicBlock::getTerminator(), llvm::MDNode::operands(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::MDNode::replaceOperandWith(), and llvm::Instruction::setMetadata().
Referenced by addLoopMetadata(), and llvm::OpenMPIRBuilder::applySimd().
|
static |
Attach loop metadata Properties
to the loop described by Loop
.
If the loop already has metadata, the loop properties are appended.
Definition at line 2996 of file OMPIRBuilder.cpp.
References addBasicBlockMetadata(), and assert().
Referenced by llvm::OpenMPIRBuilder::applySimd(), llvm::OpenMPIRBuilder::unrollLoopFull(), llvm::OpenMPIRBuilder::unrollLoopHeuristic(), and llvm::OpenMPIRBuilder::unrollLoopPartial().
|
static |
Attach llvm.access.group metadata to the memref instructions of Block
.
Definition at line 3007 of file OMPIRBuilder.cpp.
References llvm::Block, and I.
Referenced by llvm::OpenMPIRBuilder::applySimd().
|
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 3253 of file OMPIRBuilder.cpp.
References llvm::CodeGenOpt::Aggressive, llvm::ApproximateLoopSize(), assert(), llvm::TargetTransformInfo::UnrollingPreferences::BEInsns, llvm::CodeMetrics::collectEphemeralValues(), llvm::computeUnrollCount(), 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(), llvm::InstructionCost::getValue(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::InstructionCost::isValid(), LLVM_DEBUG, llvm::TargetTransformInfo::UnrollingPreferences::OptSizeThreshold, llvm::TargetTransformInfo::UnrollingPreferences::PartialOptSizeThreshold, llvm::TargetTransformInfo::UnrollingPreferences::PartialThreshold, Ptr, llvm::AnalysisManager< IRUnitT, ExtraArgTs >::registerPass(), llvm::TargetIRAnalysis::run(), llvm::AssumptionAnalysis::run(), llvm::DominatorTreeAnalysis::run(), llvm::LoopAnalysis::run(), llvm::ScalarEvolutionAnalysis::run(), llvm::TargetTransformInfo::UnrollingPreferences::Threshold, TM, and UnrollThresholdFactor.
Referenced by llvm::OpenMPIRBuilder::unrollLoopPartial().
|
static |
Determine the schedule type using schedule and ordering clause arguments.
Definition at line 223 of file OMPIRBuilder.cpp.
References assert(), getOpenMPBaseScheduleType(), getOpenMPMonotonicityScheduleType(), getOpenMPOrderingScheduleType(), and isValidWorkshareLoopScheduleType().
Referenced by llvm::OpenMPIRBuilder::applyWorkshareLoop().
|
static |
Definition at line 4151 of file OMPIRBuilder.cpp.
References Arg, Builder, llvm::OpenMPIRBuilder::Config, llvm::Function::Create(), llvm::BasicBlock::Create(), llvm::OpenMPIRBuilder::createTargetDeinit(), llvm::OpenMPIRBuilder::createTargetInit(), llvm::FunctionType::get(), llvm::GlobalValue::InternalLinkage, llvm::OpenMPIRBuilderConfig::isEmbedded(), llvm::make_early_inc_range(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::Value::users(), and llvm::zip().
Referenced by emitTargetOutlinedFunction().
|
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 3232 of file OMPIRBuilder.cpp.
References llvm::Target::createTargetMachine(), F, llvm::TargetRegistry::lookupTarget(), and Options.
Referenced by llvm::lto::backend(), computeHeuristicUnrollFactor(), LLVMCreateTargetMachine(), splitCodeGen(), and llvm::lto::thinBackend().
|
static |
Definition at line 4220 of file OMPIRBuilder.cpp.
References Builder.
Referenced by llvm::OpenMPIRBuilder::createTarget().
|
static |
Definition at line 4202 of file OMPIRBuilder.cpp.
References Builder, createOutlinedFunction(), and llvm::OpenMPIRBuilder::emitTargetRegionFunction().
Referenced by llvm::OpenMPIRBuilder::createTarget().
Create a function with a unique name and a "void (i8*, i8*)" signature in the given module and return it.
Definition at line 1749 of file OMPIRBuilder.cpp.
References llvm::Function::Create(), llvm::FunctionType::get(), llvm::Type::getInt8PtrTy(), llvm::Type::getVoidTy(), and llvm::GlobalValue::InternalLinkage.
Referenced by llvm::OpenMPIRBuilder::createReductions().
|
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 2473 of file OMPIRBuilder.cpp.
References llvm::Type::getIntegerBitWidth(), llvm::OpenMPIRBuilder::getOrCreateRuntimeFunction(), and llvm_unreachable.
|
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 2442 of file OMPIRBuilder.cpp.
References llvm::Type::getIntegerBitWidth(), llvm::OpenMPIRBuilder::getOrCreateRuntimeFunction(), and llvm_unreachable.
|
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 2458 of file OMPIRBuilder.cpp.
References llvm::Type::getIntegerBitWidth(), llvm::OpenMPIRBuilder::getOrCreateRuntimeFunction(), and llvm_unreachable.
|
static |
Definition at line 2144 of file OMPIRBuilder.cpp.
References llvm::Type::getIntegerBitWidth(), llvm::OpenMPIRBuilder::getOrCreateRuntimeFunction(), and llvm_unreachable.
|
static |
Determine which scheduling algorithm to use, determined from schedule clause arguments.
Definition at line 139 of file OMPIRBuilder.cpp.
References llvm::omp::BaseAuto, and llvm_unreachable.
Referenced by computeOpenMPScheduleType().
|
static |
Adds monotonicity modifier flags to schedule type.
Definition at line 187 of file OMPIRBuilder.cpp.
References assert(), and llvm::omp::ModifierMask.
Referenced by computeOpenMPScheduleType().
|
static |
Adds ordering modifier flags to schedule type.
Definition at line 163 of file OMPIRBuilder.cpp.
References assert().
Referenced by computeOpenMPScheduleType().
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 70 of file OMPIRBuilder.cpp.
Referenced by llvm::OpenMPIRBuilder::createAtomicUpdate(), llvm::OpenMPIRBuilder::createParallel(), and llvm::OpenMPIRBuilder::createSections().
|
static |
Definition at line 77 of file OMPIRBuilder.cpp.
Referenced by computeOpenMPScheduleType().
|
static |
Redirect all edges that branch to OldTarget
to NewTarget
.
That is, after this OldTarget
will be orphaned.
Definition at line 2613 of file OMPIRBuilder.cpp.
References DL, llvm::make_early_inc_range(), llvm::predecessors(), and redirectTo().
Referenced by llvm::OpenMPIRBuilder::collapseLoops(), and llvm::OpenMPIRBuilder::tileLoops().
|
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 244 of file OMPIRBuilder.cpp.
References assert(), llvm::BranchInst::Create(), DL, and llvm::BasicBlock::removePredecessor().
Referenced by llvm::OpenMPIRBuilder::collapseLoops(), redirectAllPredecessorsTo(), and llvm::OpenMPIRBuilder::tileLoops().
|
static |
Determine which blocks in BBs
are reachable from outside and remove the ones that are not reachable from the function.
Definition at line 2621 of file OMPIRBuilder.cpp.
References 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().
|
static |
|
static |
Referenced by computeHeuristicUnrollFactor().