diff options
Diffstat (limited to 'llvm')
-rw-r--r-- | llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h | 259 | ||||
-rw-r--r-- | llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp | 615 | ||||
-rw-r--r-- | llvm/lib/Transforms/IPO/OpenMPOpt.cpp | 35 | ||||
-rw-r--r-- | llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp | 649 |
4 files changed, 1001 insertions, 557 deletions
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h index c4735ec..3afb9d8 100644 --- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h +++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h @@ -520,6 +520,9 @@ public: /// Type used throughout for insertion points. using InsertPointTy = IRBuilder<>::InsertPoint; + /// Type used to represent an insertion point or an error value. + using InsertPointOrErrorTy = Expected<InsertPointTy>; + /// Get the create a name using the platform specific separators. /// \param Parts parts of the final name that needs separation /// The created name has a first separator between the first and second part @@ -538,7 +541,7 @@ public: /// A finalize callback knows about all objects that need finalization, e.g. /// destruction, when the scope of the currently generated construct is left /// at the time, and location, the callback is invoked. - using FinalizeCallbackTy = std::function<void(InsertPointTy CodeGenIP)>; + using FinalizeCallbackTy = std::function<Error(InsertPointTy CodeGenIP)>; struct FinalizationInfo { /// The finalization callback provided by the last in-flight invocation of @@ -589,15 +592,19 @@ public: /// not be split. /// \param CodeGenIP is the insertion point at which the body code should be /// placed. + /// + /// \return an error, if any were triggered during execution. using BodyGenCallbackTy = - function_ref<void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>; + function_ref<Error(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>; // This is created primarily for sections construct as llvm::function_ref // (BodyGenCallbackTy) is not storable (as described in the comments of // function_ref class - function_ref contains non-ownable reference // to the callable. + /// + /// \return an error, if any were triggered during execution. using StorableBodyGenCallbackTy = - std::function<void(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>; + std::function<Error(InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>; /// Callback type for loop body code generation. /// @@ -607,8 +614,10 @@ public: /// terminated with an unconditional branch to the loop /// latch. /// \param IndVar is the induction variable usable at the insertion point. + /// + /// \return an error, if any were triggered during execution. using LoopBodyGenCallbackTy = - function_ref<void(InsertPointTy CodeGenIP, Value *IndVar)>; + function_ref<Error(InsertPointTy CodeGenIP, Value *IndVar)>; /// Callback type for variable privatization (think copy & default /// constructor). @@ -628,7 +637,7 @@ public: /// /// \returns The new insertion point where code generation continues and /// \p ReplVal the replacement value. - using PrivatizeCallbackTy = function_ref<InsertPointTy( + using PrivatizeCallbackTy = function_ref<InsertPointOrErrorTy( InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value &Original, Value &Inner, Value *&ReplVal)>; @@ -658,9 +667,10 @@ public: /// \param ThreadID Optional parameter to pass in any existing ThreadID value. /// /// \returns The insertion point after the barrier. - InsertPointTy createBarrier(const LocationDescription &Loc, - omp::Directive Kind, bool ForceSimpleCall = false, - bool CheckCancelFlag = true); + InsertPointOrErrorTy createBarrier(const LocationDescription &Loc, + omp::Directive Kind, + bool ForceSimpleCall = false, + bool CheckCancelFlag = true); /// Generator for '#omp cancel' /// @@ -669,8 +679,9 @@ public: /// \param CanceledDirective The kind of directive that is cancled. /// /// \returns The insertion point after the barrier. - InsertPointTy createCancel(const LocationDescription &Loc, Value *IfCondition, - omp::Directive CanceledDirective); + InsertPointOrErrorTy createCancel(const LocationDescription &Loc, + Value *IfCondition, + omp::Directive CanceledDirective); /// Generator for '#omp parallel' /// @@ -685,7 +696,7 @@ public: /// \param IsCancellable Flag to indicate a cancellable parallel region. /// /// \returns The insertion position *after* the parallel. - IRBuilder<>::InsertPoint + InsertPointOrErrorTy createParallel(const LocationDescription &Loc, InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB, PrivatizeCallbackTy PrivCB, FinalizeCallbackTy FiniCB, Value *IfCondition, @@ -711,10 +722,10 @@ public: /// /// \returns An object representing the created control flow structure which /// can be used for loop-associated directives. - CanonicalLoopInfo *createCanonicalLoop(const LocationDescription &Loc, - LoopBodyGenCallbackTy BodyGenCB, - Value *TripCount, - const Twine &Name = "loop"); + Expected<CanonicalLoopInfo *> + createCanonicalLoop(const LocationDescription &Loc, + LoopBodyGenCallbackTy BodyGenCB, Value *TripCount, + const Twine &Name = "loop"); /// Generator for the control flow structure of an OpenMP canonical loop. /// @@ -764,12 +775,10 @@ public: /// /// \returns An object representing the created control flow structure which /// can be used for loop-associated directives. - CanonicalLoopInfo *createCanonicalLoop(const LocationDescription &Loc, - LoopBodyGenCallbackTy BodyGenCB, - Value *Start, Value *Stop, Value *Step, - bool IsSigned, bool InclusiveStop, - InsertPointTy ComputeIP = {}, - const Twine &Name = "loop"); + Expected<CanonicalLoopInfo *> createCanonicalLoop( + const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB, + Value *Start, Value *Stop, Value *Step, bool IsSigned, bool InclusiveStop, + InsertPointTy ComputeIP = {}, const Twine &Name = "loop"); /// Collapse a loop nest into a single loop. /// @@ -996,9 +1005,10 @@ private: /// the loop. /// /// \returns Point where to insert code after the workshare construct. - InsertPointTy applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI, - InsertPointTy AllocaIP, - bool NeedsBarrier); + InsertPointOrErrorTy applyStaticWorkshareLoop(DebugLoc DL, + CanonicalLoopInfo *CLI, + InsertPointTy AllocaIP, + bool NeedsBarrier); /// Modifies the canonical loop a statically-scheduled workshare loop with a /// user-specified chunk size. @@ -1013,11 +1023,11 @@ private: /// \param ChunkSize The user-specified chunk size. /// /// \returns Point where to insert code after the workshare construct. - InsertPointTy applyStaticChunkedWorkshareLoop(DebugLoc DL, - CanonicalLoopInfo *CLI, - InsertPointTy AllocaIP, - bool NeedsBarrier, - Value *ChunkSize); + InsertPointOrErrorTy applyStaticChunkedWorkshareLoop(DebugLoc DL, + CanonicalLoopInfo *CLI, + InsertPointTy AllocaIP, + bool NeedsBarrier, + Value *ChunkSize); /// Modifies the canonical loop to be a dynamically-scheduled workshare loop. /// @@ -1039,11 +1049,12 @@ private: /// scheduling. If \p nullptr, defaults to 1. /// /// \returns Point where to insert code after the workshare construct. - InsertPointTy applyDynamicWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI, - InsertPointTy AllocaIP, - omp::OMPScheduleType SchedType, - bool NeedsBarrier, - Value *Chunk = nullptr); + InsertPointOrErrorTy applyDynamicWorkshareLoop(DebugLoc DL, + CanonicalLoopInfo *CLI, + InsertPointTy AllocaIP, + omp::OMPScheduleType SchedType, + bool NeedsBarrier, + Value *Chunk = nullptr); /// Create alternative version of the loop to support if clause /// @@ -1094,7 +1105,7 @@ public: /// It corresponds to type of loop workshare OpenMP pragma. /// /// \returns Point where to insert code after the workshare construct. - InsertPointTy applyWorkshareLoop( + InsertPointOrErrorTy applyWorkshareLoop( DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP, bool NeedsBarrier, llvm::omp::ScheduleKind SchedKind = llvm::omp::OMP_SCHEDULE_Default, @@ -1251,20 +1262,21 @@ public: /// cannot be resumed until execution of the structured /// block that is associated with the generated task is /// completed. - InsertPointTy createTask(const LocationDescription &Loc, - InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB, - bool Tied = true, Value *Final = nullptr, - Value *IfCondition = nullptr, - SmallVector<DependData> Dependencies = {}); + InsertPointOrErrorTy createTask(const LocationDescription &Loc, + InsertPointTy AllocaIP, + BodyGenCallbackTy BodyGenCB, bool Tied = true, + Value *Final = nullptr, + Value *IfCondition = nullptr, + SmallVector<DependData> Dependencies = {}); /// Generator for the taskgroup construct /// /// \param Loc The location where the taskgroup construct was encountered. /// \param AllocaIP The insertion point to be used for alloca instructions. /// \param BodyGenCB Callback that will generate the region code. - InsertPointTy createTaskgroup(const LocationDescription &Loc, - InsertPointTy AllocaIP, - BodyGenCallbackTy BodyGenCB); + InsertPointOrErrorTy createTaskgroup(const LocationDescription &Loc, + InsertPointTy AllocaIP, + BodyGenCallbackTy BodyGenCB); using FileIdentifierInfoCallbackTy = std::function<std::tuple<std::string, uint64_t>()>; @@ -1302,15 +1314,15 @@ public: /// \param CodeGenIP InsertPoint for CodeGen. /// \param LHS Pass in the LHS Value to be used for CodeGen. /// \param RHS Pass in the RHS Value to be used for CodeGen. - using ReductionGenCBTy = std::function<InsertPointTy( + using ReductionGenCBTy = std::function<InsertPointOrErrorTy( InsertPointTy CodeGenIP, Value *LHS, Value *RHS, Value *&Res)>; /// Functions used to generate atomic reductions. Such functions take two /// Values representing pointers to LHS and RHS of the reduction, as well as /// the element type of these pointers. They are expected to atomically /// update the LHS to the reduced value. - using ReductionGenAtomicCBTy = - std::function<InsertPointTy(InsertPointTy, Type *, Value *, Value *)>; + using ReductionGenAtomicCBTy = std::function<InsertPointOrErrorTy( + InsertPointTy, Type *, Value *, Value *)>; /// Enum class for reduction evaluation types scalar, complex and aggregate. enum class EvalKind { Scalar, Complex, Aggregate }; @@ -1510,9 +1522,10 @@ private: /// need to be copied to the new function. /// /// \return The InterWarpCopy function. - Function *emitInterWarpCopyFunction(const LocationDescription &Loc, - ArrayRef<ReductionInfo> ReductionInfos, - AttributeList FuncAttrs); + Expected<Function *> + emitInterWarpCopyFunction(const LocationDescription &Loc, + ArrayRef<ReductionInfo> ReductionInfos, + AttributeList FuncAttrs); /// This function emits a helper that copies all the reduction variables from /// the team into the provided global buffer for the reduction variables. @@ -1604,7 +1617,7 @@ private: /// need to be copied to the new function. /// /// \return The reduction function. - Function *createReductionFunction( + Expected<Function *> createReductionFunction( StringRef ReducerName, ArrayRef<ReductionInfo> ReductionInfos, ReductionGenCBKind ReductionGenCBKind = ReductionGenCBKind::MLIR, AttributeList FuncAttrs = {}); @@ -1871,7 +1884,7 @@ public: /// \param ReductionBufNum Optional OpenMPCUDAReductionBufNumValue to be /// used for teams reduction. /// \param SrcLocInfo Source location information global. - InsertPointTy createReductionsGPU( + InsertPointOrErrorTy createReductionsGPU( const LocationDescription &Loc, InsertPointTy AllocaIP, InsertPointTy CodeGenIP, ArrayRef<ReductionInfo> ReductionInfos, bool IsNoWait = false, bool IsTeamsReduction = false, @@ -1943,10 +1956,11 @@ public: /// \param IsNoWait A flag set if the reduction is marked as nowait. /// \param IsByRef A flag set if the reduction is using reference /// or direct value. - InsertPointTy createReductions(const LocationDescription &Loc, - InsertPointTy AllocaIP, - ArrayRef<ReductionInfo> ReductionInfos, - ArrayRef<bool> IsByRef, bool IsNoWait = false); + InsertPointOrErrorTy createReductions(const LocationDescription &Loc, + InsertPointTy AllocaIP, + ArrayRef<ReductionInfo> ReductionInfos, + ArrayRef<bool> IsByRef, + bool IsNoWait = false); ///} @@ -2002,9 +2016,11 @@ public: /// \param CancelFlag Flag indicating if the cancellation is performed. /// \param CanceledDirective The kind of directive that is cancled. /// \param ExitCB Extra code to be generated in the exit block. - void emitCancelationCheckImpl(Value *CancelFlag, - omp::Directive CanceledDirective, - FinalizeCallbackTy ExitCB = {}); + /// + /// \return an error, if any were triggered during execution. + Error emitCancelationCheckImpl(Value *CancelFlag, + omp::Directive CanceledDirective, + FinalizeCallbackTy ExitCB = {}); /// Generate a target region entry call. /// @@ -2135,8 +2151,10 @@ public: /// } else { /// ElseGen(); /// } - void emitIfClause(Value *Cond, BodyGenCallbackTy ThenGen, - BodyGenCallbackTy ElseGen, InsertPointTy AllocaIP = {}); + /// + /// \return an error, if any were triggered during execution. + Error emitIfClause(Value *Cond, BodyGenCallbackTy ThenGen, + BodyGenCallbackTy ElseGen, InsertPointTy AllocaIP = {}); /// Create the global variable holding the offload mappings information. GlobalVariable *createOffloadMaptypes(SmallVectorImpl<uint64_t> &Mappings, @@ -2340,7 +2358,8 @@ public: /// is executed when the kernel launch fails. It takes an insertion point as /// parameter where the code should be emitted. It returns an insertion point /// that points right after after the emitted code. - using EmitFallbackCallbackTy = function_ref<InsertPointTy(InsertPointTy)>; + using EmitFallbackCallbackTy = + function_ref<InsertPointOrErrorTy(InsertPointTy)>; /// Generate a target region entry call and host fallback call. /// @@ -2352,7 +2371,7 @@ public: /// \param DeviceID Identifier for the device via the 'device' clause. /// \param RTLoc Source location identifier /// \param AllocaIP The insertion point to be used for alloca instructions. - InsertPointTy + InsertPointOrErrorTy emitKernelLaunch(const LocationDescription &Loc, Value *OutlinedFnID, EmitFallbackCallbackTy EmitTargetCallFallbackCB, TargetKernelArgs &Args, Value *DeviceID, Value *RTLoc, @@ -2366,9 +2385,11 @@ public: /// \param RTLoc Source location identifier /// \Param TargetTaskAllocaIP Insertion point for the alloca block of the /// generated task. + /// + /// \return an error, if any were triggered during execution. using TargetTaskBodyCallbackTy = - function_ref<void(Value *DeviceID, Value *RTLoc, - IRBuilderBase::InsertPoint TargetTaskAllocaIP)>; + function_ref<Error(Value *DeviceID, Value *RTLoc, + IRBuilderBase::InsertPoint TargetTaskAllocaIP)>; /// Generate a target-task for the target construct /// @@ -2380,7 +2401,7 @@ public: /// dependencies as specified by the 'depend' clause. /// \param HasNoWait True if the target construct had 'nowait' on it, false /// otherwise - InsertPointTy emitTargetTask( + InsertPointOrErrorTy emitTargetTask( TargetTaskBodyCallbackTy TaskBodyCB, Value *DeviceID, Value *RTLoc, OpenMPIRBuilder::InsertPointTy AllocaIP, const SmallVector<llvm::OpenMPIRBuilder::DependData> &Dependencies, @@ -2478,11 +2499,11 @@ public: /// \param CPFuncs copy functions to use for each copyprivate variable. /// /// \returns The insertion position *after* the single call. - InsertPointTy createSingle(const LocationDescription &Loc, - BodyGenCallbackTy BodyGenCB, - FinalizeCallbackTy FiniCB, bool IsNowait, - ArrayRef<llvm::Value *> CPVars = {}, - ArrayRef<llvm::Function *> CPFuncs = {}); + InsertPointOrErrorTy createSingle(const LocationDescription &Loc, + BodyGenCallbackTy BodyGenCB, + FinalizeCallbackTy FiniCB, bool IsNowait, + ArrayRef<llvm::Value *> CPVars = {}, + ArrayRef<llvm::Function *> CPFuncs = {}); /// Generator for '#omp master' /// @@ -2491,9 +2512,9 @@ public: /// \param FiniCB Callback to finalize variable copies. /// /// \returns The insertion position *after* the master. - InsertPointTy createMaster(const LocationDescription &Loc, - BodyGenCallbackTy BodyGenCB, - FinalizeCallbackTy FiniCB); + InsertPointOrErrorTy createMaster(const LocationDescription &Loc, + BodyGenCallbackTy BodyGenCB, + FinalizeCallbackTy FiniCB); /// Generator for '#omp masked' /// @@ -2502,9 +2523,9 @@ public: /// \param FiniCB Callback to finialize variable copies. /// /// \returns The insertion position *after* the masked. - InsertPointTy createMasked(const LocationDescription &Loc, - BodyGenCallbackTy BodyGenCB, - FinalizeCallbackTy FiniCB, Value *Filter); + InsertPointOrErrorTy createMasked(const LocationDescription &Loc, + BodyGenCallbackTy BodyGenCB, + FinalizeCallbackTy FiniCB, Value *Filter); /// Generator for '#omp critical' /// @@ -2515,10 +2536,10 @@ public: /// \param HintInst Hint Instruction for hint clause associated with critical /// /// \returns The insertion position *after* the critical. - InsertPointTy createCritical(const LocationDescription &Loc, - BodyGenCallbackTy BodyGenCB, - FinalizeCallbackTy FiniCB, - StringRef CriticalName, Value *HintInst); + InsertPointOrErrorTy createCritical(const LocationDescription &Loc, + BodyGenCallbackTy BodyGenCB, + FinalizeCallbackTy FiniCB, + StringRef CriticalName, Value *HintInst); /// Generator for '#omp ordered depend (source | sink)' /// @@ -2544,10 +2565,10 @@ public: /// otherwise, with simd clause; /// /// \returns The insertion position *after* the ordered. - InsertPointTy createOrderedThreadsSimd(const LocationDescription &Loc, - BodyGenCallbackTy BodyGenCB, - FinalizeCallbackTy FiniCB, - bool IsThreads); + InsertPointOrErrorTy createOrderedThreadsSimd(const LocationDescription &Loc, + BodyGenCallbackTy BodyGenCB, + FinalizeCallbackTy FiniCB, + bool IsThreads); /// Generator for '#omp sections' /// @@ -2560,12 +2581,11 @@ public: /// \param IsNowait If true, barrier - to ensure all sections are executed /// before moving forward will not be generated. /// \returns The insertion position *after* the sections. - InsertPointTy createSections(const LocationDescription &Loc, - InsertPointTy AllocaIP, - ArrayRef<StorableBodyGenCallbackTy> SectionCBs, - PrivatizeCallbackTy PrivCB, - FinalizeCallbackTy FiniCB, bool IsCancellable, - bool IsNowait); + InsertPointOrErrorTy + createSections(const LocationDescription &Loc, InsertPointTy AllocaIP, + ArrayRef<StorableBodyGenCallbackTy> SectionCBs, + PrivatizeCallbackTy PrivCB, FinalizeCallbackTy FiniCB, + bool IsCancellable, bool IsNowait); /// Generator for '#omp section' /// @@ -2573,9 +2593,9 @@ public: /// \param BodyGenCB Callback that will generate the region body code. /// \param FiniCB Callback to finalize variable copies. /// \returns The insertion position *after* the section. - InsertPointTy createSection(const LocationDescription &Loc, - BodyGenCallbackTy BodyGenCB, - FinalizeCallbackTy FiniCB); + InsertPointOrErrorTy createSection(const LocationDescription &Loc, + BodyGenCallbackTy BodyGenCB, + FinalizeCallbackTy FiniCB); /// Generator for `#omp teams` /// @@ -2589,7 +2609,7 @@ public: /// contention group created by each team. /// \param IfExpr is the integer argument value of the if condition on the /// teams clause. - InsertPointTy + InsertPointOrErrorTy createTeams(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, Value *NumTeamsLower = nullptr, Value *NumTeamsUpper = nullptr, Value *ThreadLimit = nullptr, Value *IfExpr = nullptr); @@ -2764,7 +2784,8 @@ private: public: /// Functions used to generate a function with the given name. - using FunctionGenCallback = std::function<Function *(StringRef FunctionName)>; + using FunctionGenCallback = + std::function<Expected<Function *>(StringRef FunctionName)>; /// Create a unique name for the entry function using the source location /// information of the current target region. The name will be something like: @@ -2797,10 +2818,10 @@ public: /// \param GenerateFunctionCallback The callback function to generate the code /// \param OutlinedFunction Pointer to the outlined function /// \param EntryFnIDName Name of the ID o be created - void emitTargetRegionFunction(TargetRegionEntryInfo &EntryInfo, - FunctionGenCallback &GenerateFunctionCallback, - bool IsOffloadEntry, Function *&OutlinedFn, - Constant *&OutlinedFnID); + Error emitTargetRegionFunction(TargetRegionEntryInfo &EntryInfo, + FunctionGenCallback &GenerateFunctionCallback, + bool IsOffloadEntry, Function *&OutlinedFn, + Constant *&OutlinedFnID); /// Registers the given function and sets up the attribtues of the function /// Returns the FunctionID. @@ -2851,22 +2872,22 @@ public: /// use_device_ptr and use_device_addr. /// \param CustomMapperCB Optional callback to generate code related to /// custom mappers. - OpenMPIRBuilder::InsertPointTy createTargetData( + InsertPointOrErrorTy createTargetData( const LocationDescription &Loc, InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value *DeviceID, Value *IfCond, TargetDataInfo &Info, GenMapInfoCallbackTy GenMapInfoCB, omp::RuntimeFunction *MapperFunc = nullptr, - function_ref<InsertPointTy(InsertPointTy CodeGenIP, - BodyGenTy BodyGenType)> + function_ref<InsertPointOrErrorTy(InsertPointTy CodeGenIP, + BodyGenTy BodyGenType)> BodyGenCB = nullptr, function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr, function_ref<Value *(unsigned int)> CustomMapperCB = nullptr, Value *SrcLocInfo = nullptr); - using TargetBodyGenCallbackTy = function_ref<InsertPointTy( + using TargetBodyGenCallbackTy = function_ref<InsertPointOrErrorTy( InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>; - using TargetGenArgAccessorsCallbackTy = function_ref<InsertPointTy( + using TargetGenArgAccessorsCallbackTy = function_ref<InsertPointOrErrorTy( Argument &Arg, Value *Input, Value *&RetVal, InsertPointTy AllocaIP, InsertPointTy CodeGenIP)>; @@ -2887,7 +2908,7 @@ public: /// \param Dependencies A vector of DependData objects that carry // dependency information as passed in the depend clause // \param HasNowait Whether the target construct has a `nowait` clause or not. - InsertPointTy createTarget( + InsertPointOrErrorTy createTarget( const LocationDescription &Loc, bool IsOffloadEntry, OpenMPIRBuilder::InsertPointTy AllocaIP, OpenMPIRBuilder::InsertPointTy CodeGenIP, @@ -2969,10 +2990,10 @@ private: /// should be called. /// /// \return The insertion position in exit block - InsertPointTy emitCommonDirectiveExit(omp::Directive OMPD, - InsertPointTy FinIP, - Instruction *ExitCall, - bool HasFinalize = true); + InsertPointOrErrorTy emitCommonDirectiveExit(omp::Directive OMPD, + InsertPointTy FinIP, + Instruction *ExitCall, + bool HasFinalize = true); /// Common Interface to generate OMP inlined regions /// @@ -2990,8 +3011,7 @@ private: /// \param IsCancellable if HasFinalize is set to true, indicate if the /// the directive should be cancellable. /// \return The insertion point after the region - - InsertPointTy + InsertPointOrErrorTy EmitOMPInlinedRegion(omp::Directive OMPD, Instruction *EntryCall, Instruction *ExitCall, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool Conditional = false, @@ -3027,7 +3047,7 @@ private: /// /// \returns Value to update X to. using AtomicUpdateCallbackTy = - const function_ref<Value *(Value *XOld, IRBuilder<> &IRB)>; + const function_ref<Expected<Value *>(Value *XOld, IRBuilder<> &IRB)>; private: enum AtomicKind { Read, Write, Update, Capture, Compare }; @@ -3066,7 +3086,7 @@ private: /// /// \returns A pair of the old value of X before the update, and the value /// used for the update. - std::pair<Value *, Value *> + Expected<std::pair<Value *, Value *>> emitAtomicUpdate(InsertPointTy AllocaIP, Value *X, Type *XElemTy, Value *Expr, AtomicOrdering AO, AtomicRMWInst::BinOp RMWOp, AtomicUpdateCallbackTy &UpdateOp, bool VolatileX, @@ -3143,12 +3163,11 @@ public: /// (e.g. true for X = X BinOp Expr) /// /// \return Insertion point after generated atomic update IR. - InsertPointTy createAtomicUpdate(const LocationDescription &Loc, - InsertPointTy AllocaIP, AtomicOpValue &X, - Value *Expr, AtomicOrdering AO, - AtomicRMWInst::BinOp RMWOp, - AtomicUpdateCallbackTy &UpdateOp, - bool IsXBinopExpr); + InsertPointOrErrorTy + createAtomicUpdate(const LocationDescription &Loc, InsertPointTy AllocaIP, + AtomicOpValue &X, Value *Expr, AtomicOrdering AO, + AtomicRMWInst::BinOp RMWOp, + AtomicUpdateCallbackTy &UpdateOp, bool IsXBinopExpr); /// Emit atomic update for constructs: --- Only Scalar data types /// V = X; X = X BinOp Expr , @@ -3179,7 +3198,7 @@ public: /// 'v', not an updated one. /// /// \return Insertion point after generated atomic capture IR. - InsertPointTy + InsertPointOrErrorTy createAtomicCapture(const LocationDescription &Loc, InsertPointTy AllocaIP, AtomicOpValue &X, AtomicOpValue &V, Value *Expr, AtomicOrdering AO, AtomicRMWInst::BinOp RMWOp, diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp index be93f9f..d2e4dc1 100644 --- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -945,7 +945,7 @@ Value *OpenMPIRBuilder::getOrCreateThreadID(Value *Ident) { "omp_global_thread_num"); } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createBarrier(const LocationDescription &Loc, Directive Kind, bool ForceSimpleCall, bool CheckCancelFlag) { if (!updateToLocation(Loc)) @@ -992,12 +992,13 @@ OpenMPIRBuilder::createBarrier(const LocationDescription &Loc, Directive Kind, Args); if (UseCancelBarrier && CheckCancelFlag) - emitCancelationCheckImpl(Result, OMPD_parallel); + if (Error Err = emitCancelationCheckImpl(Result, OMPD_parallel)) + return Err; return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createCancel(const LocationDescription &Loc, Value *IfCondition, omp::Directive CanceledDirective) { @@ -1029,18 +1030,22 @@ OpenMPIRBuilder::createCancel(const LocationDescription &Loc, Value *Args[] = {Ident, getOrCreateThreadID(Ident), CancelKind}; Value *Result = Builder.CreateCall( getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_cancel), Args); - auto ExitCB = [this, CanceledDirective, Loc](InsertPointTy IP) { + auto ExitCB = [this, CanceledDirective, Loc](InsertPointTy IP) -> Error { if (CanceledDirective == OMPD_parallel) { IRBuilder<>::InsertPointGuard IPG(Builder); Builder.restoreIP(IP); - createBarrier(LocationDescription(Builder.saveIP(), Loc.DL), - omp::Directive::OMPD_unknown, /* ForceSimpleCall */ false, - /* CheckCancelFlag */ false); + return createBarrier(LocationDescription(Builder.saveIP(), Loc.DL), + omp::Directive::OMPD_unknown, + /* ForceSimpleCall */ false, + /* CheckCancelFlag */ false) + .takeError(); } + return Error::success(); }; // The actual cancel logic is shared with others, e.g., cancel_barriers. - emitCancelationCheckImpl(Result, CanceledDirective, ExitCB); + if (Error Err = emitCancelationCheckImpl(Result, CanceledDirective, ExitCB)) + return Err; // Update the insertion point and remove the terminator we introduced. Builder.SetInsertPoint(UI->getParent()); @@ -1079,7 +1084,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetKernel( return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitKernelLaunch( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::emitKernelLaunch( const LocationDescription &Loc, Value *OutlinedFnID, EmitFallbackCallbackTy EmitTargetCallFallbackCB, TargetKernelArgs &Args, Value *DeviceID, Value *RTLoc, InsertPointTy AllocaIP) { @@ -1134,15 +1139,18 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitKernelLaunch( auto CurFn = Builder.GetInsertBlock()->getParent(); emitBlock(OffloadFailedBlock, CurFn); - Builder.restoreIP(EmitTargetCallFallbackCB(Builder.saveIP())); + InsertPointOrErrorTy AfterIP = EmitTargetCallFallbackCB(Builder.saveIP()); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); emitBranch(OffloadContBlock); emitBlock(OffloadContBlock, CurFn, /*IsFinished=*/true); return Builder.saveIP(); } -void OpenMPIRBuilder::emitCancelationCheckImpl(Value *CancelFlag, - omp::Directive CanceledDirective, - FinalizeCallbackTy ExitCB) { +Error OpenMPIRBuilder::emitCancelationCheckImpl( + Value *CancelFlag, omp::Directive CanceledDirective, + FinalizeCallbackTy ExitCB) { assert(isLastFinalizationInfoCancellable(CanceledDirective) && "Unexpected cancellation!"); @@ -1171,12 +1179,15 @@ void OpenMPIRBuilder::emitCancelationCheckImpl(Value *CancelFlag, // post finalization block that is known to the FiniCB callback. Builder.SetInsertPoint(CancellationBlock); if (ExitCB) - ExitCB(Builder.saveIP()); + if (Error Err = ExitCB(Builder.saveIP())) + return Err; auto &FI = FinalizationStack.back(); - FI.FiniCB(Builder.saveIP()); + if (Error Err = FI.FiniCB(Builder.saveIP())) + return Err; // The continuation block is where code generation continues. Builder.SetInsertPoint(NonCancellationBlock, NonCancellationBlock->begin()); + return Error::success(); } // Callback used to create OpenMP runtime calls to support @@ -1355,7 +1366,7 @@ hostParallelCallback(OpenMPIRBuilder *OMPIRBuilder, Function &OutlinedFn, } } -IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createParallel( const LocationDescription &Loc, InsertPointTy OuterAllocaIP, BodyGenCallbackTy BodyGenCB, PrivatizeCallbackTy PrivCB, FinalizeCallbackTy FiniCB, Value *IfCondition, Value *NumThreads, @@ -1496,7 +1507,8 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel( // Let the caller create the body. assert(BodyGenCB && "Expected body generation callback!"); InsertPointTy CodeGenIP(PRegBodyBB, PRegBodyBB->begin()); - BodyGenCB(InnerAllocaIP, CodeGenIP); + if (Error Err = BodyGenCB(InnerAllocaIP, CodeGenIP)) + return Err; LLVM_DEBUG(dbgs() << "After body codegen: " << *OuterFn << "\n"); @@ -1565,10 +1577,10 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel( FunctionCallee TIDRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_global_thread_num); - auto PrivHelper = [&](Value &V) { + auto PrivHelper = [&](Value &V) -> Error { if (&V == TIDAddr || &V == ZeroAddr) { OI.ExcludeArgsFromAggregate.push_back(&V); - return; + return Error::success(); } SetVector<Use *> Uses; @@ -1608,8 +1620,11 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel( if (CI && CI->getCalledFunction() == TIDRTLFn.getCallee()) { ReplacementValue = PrivTID; } else { - Builder.restoreIP( - PrivCB(InnerAllocaIP, Builder.saveIP(), V, *Inner, ReplacementValue)); + InsertPointOrErrorTy AfterIP = + PrivCB(InnerAllocaIP, Builder.saveIP(), V, *Inner, ReplacementValue); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); InnerAllocaIP = { InnerAllocaIP.getBlock(), InnerAllocaIP.getBlock()->getTerminator()->getIterator()}; @@ -1617,11 +1632,13 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel( assert(ReplacementValue && "Expected copy/create callback to set replacement value!"); if (ReplacementValue == &V) - return; + return Error::success(); } for (Use *UPtr : Uses) UPtr->set(ReplacementValue); + + return Error::success(); }; // Reset the inner alloca insertion as it will be used for loading the values @@ -1640,7 +1657,8 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel( for (Value *Input : Inputs) { LLVM_DEBUG(dbgs() << "Captured input: " << *Input << "\n"); - PrivHelper(*Input); + if (Error Err = PrivHelper(*Input)) + return Err; } LLVM_DEBUG({ for (Value *Output : Outputs) @@ -1666,7 +1684,8 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel( Instruction *PRegPreFiniTI = PRegPreFiniBB->getTerminator(); InsertPointTy PreFiniIP(PRegPreFiniBB, PRegPreFiniTI->getIterator()); - FiniCB(PreFiniIP); + if (Error Err = FiniCB(PreFiniIP)) + return Err; // Register the outlined info. addOutlineInfo(std::move(OI)); @@ -1797,7 +1816,7 @@ static Value *emitTaskDependencies( return DepArray; } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTask(const LocationDescription &Loc, InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB, bool Tied, Value *Final, Value *IfCondition, @@ -1833,7 +1852,8 @@ OpenMPIRBuilder::createTask(const LocationDescription &Loc, InsertPointTy TaskAllocaIP = InsertPointTy(TaskAllocaBB, TaskAllocaBB->begin()); InsertPointTy TaskBodyIP = InsertPointTy(TaskBodyBB, TaskBodyBB->begin()); - BodyGenCB(TaskAllocaIP, TaskBodyIP); + if (Error Err = BodyGenCB(TaskAllocaIP, TaskBodyIP)) + return Err; OutlineInfo OI; OI.EntryBB = TaskAllocaBB; @@ -2048,7 +2068,7 @@ OpenMPIRBuilder::createTask(const LocationDescription &Loc, return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTaskgroup(const LocationDescription &Loc, InsertPointTy AllocaIP, BodyGenCallbackTy BodyGenCB) { @@ -2066,7 +2086,8 @@ OpenMPIRBuilder::createTaskgroup(const LocationDescription &Loc, Builder.CreateCall(TaskgroupFn, {Ident, ThreadID}); BasicBlock *TaskgroupExitBB = splitBB(Builder, true, "taskgroup.exit"); - BodyGenCB(AllocaIP, Builder.saveIP()); + if (Error Err = BodyGenCB(AllocaIP, Builder.saveIP())) + return Err; Builder.SetInsertPoint(TaskgroupExitBB); // Emit the @__kmpc_end_taskgroup runtime call to end the taskgroup @@ -2077,7 +2098,7 @@ OpenMPIRBuilder::createTaskgroup(const LocationDescription &Loc, return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createSections( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createSections( const LocationDescription &Loc, InsertPointTy AllocaIP, ArrayRef<StorableBodyGenCallbackTy> SectionCBs, PrivatizeCallbackTy PrivCB, FinalizeCallbackTy FiniCB, bool IsCancellable, bool IsNowait) { @@ -2124,7 +2145,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createSections( // ... // section_loop.after: // <FiniCB>; - auto LoopBodyGenCB = [&](InsertPointTy CodeGenIP, Value *IndVar) { + auto LoopBodyGenCB = [&](InsertPointTy CodeGenIP, Value *IndVar) -> Error { Builder.restoreIP(CodeGenIP); BasicBlock *Continue = splitBBWithSuffix(Builder, /*CreateBranch=*/false, ".sections.after"); @@ -2138,12 +2159,14 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createSections( SwitchStmt->addCase(Builder.getInt32(CaseNumber), CaseBB); Builder.SetInsertPoint(CaseBB); BranchInst *CaseEndBr = Builder.CreateBr(Continue); - SectionCB(InsertPointTy(), - {CaseEndBr->getParent(), CaseEndBr->getIterator()}); + if (Error Err = SectionCB(InsertPointTy(), {CaseEndBr->getParent(), + CaseEndBr->getIterator()})) + return Err; CaseNumber++; } // remove the existing terminator from body BB since there can be no // terminators after switch/case + return Error::success(); }; // Loop body ends here // LowerBound, UpperBound, and STride for createCanonicalLoop @@ -2151,10 +2174,16 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createSections( Value *LB = ConstantInt::get(I32Ty, 0); Value *UB = ConstantInt::get(I32Ty, SectionCBs.size()); Value *ST = ConstantInt::get(I32Ty, 1); - llvm::CanonicalLoopInfo *LoopInfo = createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopInfo = createCanonicalLoop( Loc, LoopBodyGenCB, LB, UB, ST, true, false, AllocaIP, "section_loop"); - InsertPointTy AfterIP = - applyStaticWorkshareLoop(Loc.DL, LoopInfo, AllocaIP, !IsNowait); + if (!LoopInfo) + return LoopInfo.takeError(); + + InsertPointOrErrorTy WsloopIP = + applyStaticWorkshareLoop(Loc.DL, *LoopInfo, AllocaIP, !IsNowait); + if (!WsloopIP) + return WsloopIP.takeError(); + InsertPointTy AfterIP = *WsloopIP; // Apply the finalization callback in LoopAfterBB auto FiniInfo = FinalizationStack.pop_back_val(); @@ -2164,14 +2193,15 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createSections( Builder.restoreIP(AfterIP); BasicBlock *FiniBB = splitBBWithSuffix(Builder, /*CreateBranch=*/true, "sections.fini"); - CB(Builder.saveIP()); + if (Error Err = CB(Builder.saveIP())) + return Err; AfterIP = {FiniBB, FiniBB->begin()}; } return AfterIP; } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createSection(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB) { @@ -2502,7 +2532,7 @@ void OpenMPIRBuilder::emitReductionListCopy( } } -Function *OpenMPIRBuilder::emitInterWarpCopyFunction( +Expected<Function *> OpenMPIRBuilder::emitInterWarpCopyFunction( const LocationDescription &Loc, ArrayRef<ReductionInfo> ReductionInfos, AttributeList FuncAttrs) { InsertPointTy SavedIP = Builder.saveIP(); @@ -2621,10 +2651,13 @@ Function *OpenMPIRBuilder::emitInterWarpCopyFunction( } // kmpc_barrier. - createBarrier(LocationDescription(Builder.saveIP(), Loc.DL), - omp::Directive::OMPD_unknown, - /* ForceSimpleCall */ false, - /* CheckCancelFlag */ true); + InsertPointOrErrorTy BarrierIP1 = + createBarrier(LocationDescription(Builder.saveIP(), Loc.DL), + omp::Directive::OMPD_unknown, + /* ForceSimpleCall */ false, + /* CheckCancelFlag */ true); + if (!BarrierIP1) + return BarrierIP1.takeError(); BasicBlock *ThenBB = BasicBlock::Create(Ctx, "then"); BasicBlock *ElseBB = BasicBlock::Create(Ctx, "else"); BasicBlock *MergeBB = BasicBlock::Create(Ctx, "ifcont"); @@ -2666,10 +2699,13 @@ Function *OpenMPIRBuilder::emitInterWarpCopyFunction( // endif emitBlock(MergeBB, Builder.GetInsertBlock()->getParent()); - createBarrier(LocationDescription(Builder.saveIP(), Loc.DL), - omp::Directive::OMPD_unknown, - /* ForceSimpleCall */ false, - /* CheckCancelFlag */ true); + InsertPointOrErrorTy BarrierIP2 = + createBarrier(LocationDescription(Builder.saveIP(), Loc.DL), + omp::Directive::OMPD_unknown, + /* ForceSimpleCall */ false, + /* CheckCancelFlag */ true); + if (!BarrierIP2) + return BarrierIP2.takeError(); // Warp 0 copies reduce element from transfer medium BasicBlock *W0ThenBB = BasicBlock::Create(Ctx, "then"); @@ -3286,7 +3322,7 @@ std::string OpenMPIRBuilder::getReductionFuncName(StringRef Name) const { return (Name + Suffix).str(); } -Function *OpenMPIRBuilder::createReductionFunction( +Expected<Function *> OpenMPIRBuilder::createReductionFunction( StringRef ReducerName, ArrayRef<ReductionInfo> ReductionInfos, ReductionGenCBKind ReductionGenCBKind, AttributeList FuncAttrs) { auto *FuncTy = FunctionType::get(Builder.getVoidTy(), @@ -3352,7 +3388,10 @@ Function *OpenMPIRBuilder::createReductionFunction( Value *LHS = Builder.CreateLoad(RI.ElementType, LHSPtr); Value *RHS = Builder.CreateLoad(RI.ElementType, RHSPtr); Value *Reduced; - RI.ReductionGen(Builder.saveIP(), LHS, RHS, Reduced); + InsertPointOrErrorTy AfterIP = + RI.ReductionGen(Builder.saveIP(), LHS, RHS, Reduced); + if (!AfterIP) + return AfterIP.takeError(); if (!Builder.GetInsertBlock()) return ReductionFunc; Builder.CreateStore(Reduced, LHSPtr); @@ -3405,7 +3444,7 @@ checkReductionInfos(ArrayRef<OpenMPIRBuilder::ReductionInfo> ReductionInfos, } } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createReductionsGPU( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductionsGPU( const LocationDescription &Loc, InsertPointTy AllocaIP, InsertPointTy CodeGenIP, ArrayRef<ReductionInfo> ReductionInfos, bool IsNoWait, bool IsTeamsReduction, bool HasDistribute, @@ -3435,11 +3474,13 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createReductionsGPU( AttrBldr.removeAttribute(Attribute::OptimizeNone); FuncAttrs = FuncAttrs.addFnAttributes(Ctx, AttrBldr); - Function *ReductionFunc = nullptr; CodeGenIP = Builder.saveIP(); - ReductionFunc = + Expected<Function *> ReductionResult = createReductionFunction(Builder.GetInsertBlock()->getParent()->getName(), ReductionInfos, ReductionGenCBKind, FuncAttrs); + if (!ReductionResult) + return ReductionResult.takeError(); + Function *ReductionFunc = *ReductionResult; Builder.restoreIP(CodeGenIP); // Set the grid value in the config needed for lowering later on @@ -3480,7 +3521,11 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createReductionsGPU( CodeGenIP = Builder.saveIP(); Function *SarFunc = emitShuffleAndReduceFunction(ReductionInfos, ReductionFunc, FuncAttrs); - Function *WcFunc = emitInterWarpCopyFunction(Loc, ReductionInfos, FuncAttrs); + Expected<Function *> CopyResult = + emitInterWarpCopyFunction(Loc, ReductionInfos, FuncAttrs); + if (!CopyResult) + return CopyResult.takeError(); + Function *WcFunc = *CopyResult; Builder.restoreIP(CodeGenIP); Value *RL = Builder.CreatePointerBitCastOrAddrSpaceCast(ReductionList, PtrTy); @@ -3595,7 +3640,7 @@ static Function *getFreshReductionFunc(Module &M) { ".omp.reduction.func", &M); } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createReductions(const LocationDescription &Loc, InsertPointTy AllocaIP, ArrayRef<ReductionInfo> ReductionInfos, @@ -3688,7 +3733,7 @@ OpenMPIRBuilder::createReductions(const LocationDescription &Loc, Type *ValueType = RI.ElementType; // We have one less load for by-ref case because that load is now inside of // the reduction region - Value *RedValue = nullptr; + Value *RedValue = RI.Variable; if (!IsByRef[En.index()]) { RedValue = Builder.CreateLoad(ValueType, RI.Variable, "red.value." + Twine(En.index())); @@ -3697,13 +3742,12 @@ OpenMPIRBuilder::createReductions(const LocationDescription &Loc, Builder.CreateLoad(ValueType, RI.PrivateVariable, "red.private.value." + Twine(En.index())); Value *Reduced; - if (IsByRef[En.index()]) { - Builder.restoreIP(RI.ReductionGen(Builder.saveIP(), RI.Variable, - PrivateRedValue, Reduced)); - } else { - Builder.restoreIP(RI.ReductionGen(Builder.saveIP(), RedValue, - PrivateRedValue, Reduced)); - } + InsertPointOrErrorTy AfterIP = + RI.ReductionGen(Builder.saveIP(), RedValue, PrivateRedValue, Reduced); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); + if (!Builder.GetInsertBlock()) return InsertPointTy(); // for by-ref case, the load is inside of the reduction region @@ -3722,8 +3766,11 @@ OpenMPIRBuilder::createReductions(const LocationDescription &Loc, Builder.SetInsertPoint(AtomicRedBlock); if (CanGenerateAtomic && llvm::none_of(IsByRef, [](bool P) { return P; })) { for (const ReductionInfo &RI : ReductionInfos) { - Builder.restoreIP(RI.AtomicReductionGen(Builder.saveIP(), RI.ElementType, - RI.Variable, RI.PrivateVariable)); + InsertPointOrErrorTy AfterIP = RI.AtomicReductionGen( + Builder.saveIP(), RI.ElementType, RI.Variable, RI.PrivateVariable); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); if (!Builder.GetInsertBlock()) return InsertPointTy(); } @@ -3755,7 +3802,11 @@ OpenMPIRBuilder::createReductions(const LocationDescription &Loc, Builder.CreateBitCast(RHSI8Ptr, RI.PrivateVariable->getType()); Value *RHS = Builder.CreateLoad(RI.ElementType, RHSPtr); Value *Reduced; - Builder.restoreIP(RI.ReductionGen(Builder.saveIP(), LHS, RHS, Reduced)); + InsertPointOrErrorTy AfterIP = + RI.ReductionGen(Builder.saveIP(), LHS, RHS, Reduced); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); if (!Builder.GetInsertBlock()) return InsertPointTy(); // store is inside of the reduction region when using by-ref @@ -3768,11 +3819,10 @@ OpenMPIRBuilder::createReductions(const LocationDescription &Loc, return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createMaster(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB) { - if (!updateToLocation(Loc)) return Loc.IP; @@ -3793,7 +3843,7 @@ OpenMPIRBuilder::createMaster(const LocationDescription &Loc, /*Conditional*/ true, /*hasFinalize*/ true); } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createMasked(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, Value *Filter) { @@ -3884,7 +3934,7 @@ CanonicalLoopInfo *OpenMPIRBuilder::createLoopSkeleton( return CL; } -CanonicalLoopInfo * +Expected<CanonicalLoopInfo *> OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB, Value *TripCount, const Twine &Name) { @@ -3906,7 +3956,8 @@ OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc, // Emit the body content. We do it after connecting the loop to the CFG to // avoid that the callback encounters degenerate BBs. - BodyGenCB(CL->getBodyIP(), CL->getIndVar()); + if (Error Err = BodyGenCB(CL->getBodyIP(), CL->getIndVar())) + return Err; #ifndef NDEBUG CL->assertOK(); @@ -3914,7 +3965,7 @@ OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc, return CL; } -CanonicalLoopInfo *OpenMPIRBuilder::createCanonicalLoop( +Expected<CanonicalLoopInfo *> OpenMPIRBuilder::createCanonicalLoop( const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB, Value *Start, Value *Stop, Value *Step, bool IsSigned, bool InclusiveStop, InsertPointTy ComputeIP, const Twine &Name) { @@ -3979,7 +4030,7 @@ CanonicalLoopInfo *OpenMPIRBuilder::createCanonicalLoop( Builder.restoreIP(CodeGenIP); Value *Span = Builder.CreateMul(IV, Step); Value *IndVar = Builder.CreateAdd(Span, Start); - BodyGenCB(Builder.saveIP(), IndVar); + return BodyGenCB(Builder.saveIP(), IndVar); }; LocationDescription LoopLoc = ComputeIP.isSet() ? Loc.IP : Builder.saveIP(); return createCanonicalLoop(LoopLoc, BodyGen, TripCount, Name); @@ -4001,7 +4052,7 @@ static FunctionCallee getKmpcForStaticInitForType(Type *Ty, Module &M, llvm_unreachable("unknown OpenMP loop iterator bitwidth"); } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP, bool NeedsBarrier) { @@ -4078,10 +4129,14 @@ OpenMPIRBuilder::applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI, Builder.CreateCall(StaticFini, {SrcLoc, ThreadNum}); // Add the barrier if requested. - if (NeedsBarrier) - createBarrier(LocationDescription(Builder.saveIP(), DL), - omp::Directive::OMPD_for, /* ForceSimpleCall */ false, - /* CheckCancelFlag */ false); + if (NeedsBarrier) { + InsertPointOrErrorTy BarrierIP = + createBarrier(LocationDescription(Builder.saveIP(), DL), + omp::Directive::OMPD_for, /* ForceSimpleCall */ false, + /* CheckCancelFlag */ false); + if (!BarrierIP) + return BarrierIP.takeError(); + } InsertPointTy AfterIP = CLI->getAfterIP(); CLI->invalidate(); @@ -4089,9 +4144,12 @@ OpenMPIRBuilder::applyStaticWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI, return AfterIP; } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyStaticChunkedWorkshareLoop( - DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP, - bool NeedsBarrier, Value *ChunkSize) { +OpenMPIRBuilder::InsertPointOrErrorTy +OpenMPIRBuilder::applyStaticChunkedWorkshareLoop(DebugLoc DL, + CanonicalLoopInfo *CLI, + InsertPointTy AllocaIP, + bool NeedsBarrier, + Value *ChunkSize) { assert(CLI->isValid() && "Requires a valid canonical loop"); assert(ChunkSize && "Chunk size is required"); @@ -4167,12 +4225,23 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyStaticChunkedWorkshareLoop( // Create outer "dispatch" loop for enumerating the chunks. BasicBlock *DispatchEnter = splitBB(Builder, true); Value *DispatchCounter; - CanonicalLoopInfo *DispatchCLI = createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = createCanonicalLoop( {Builder.saveIP(), DL}, - [&](InsertPointTy BodyIP, Value *Counter) { DispatchCounter = Counter; }, + [&](InsertPointTy BodyIP, Value *Counter) { + DispatchCounter = Counter; + return Error::success(); + }, FirstChunkStart, CastedTripCount, NextChunkStride, /*IsSigned=*/false, /*InclusiveStop=*/false, /*ComputeIP=*/{}, "dispatch"); + if (!LoopResult) { + // It is safe to assume this didn't return an error because the callback + // passed into createCanonicalLoop is the only possible error source, and it + // always returns success. Need to still cast the result into bool to avoid + // runtime errors. + llvm_unreachable("unexpected error creating canonical loop"); + } + CanonicalLoopInfo *DispatchCLI = *LoopResult; // Remember the BasicBlocks of the dispatch loop we need, then invalidate to // not have to preserve the canonical invariant. @@ -4219,9 +4288,13 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyStaticChunkedWorkshareLoop( Builder.CreateCall(StaticFini, {SrcLoc, ThreadNum}); // Add the barrier if requested. - if (NeedsBarrier) - createBarrier(LocationDescription(Builder.saveIP(), DL), OMPD_for, - /*ForceSimpleCall=*/false, /*CheckCancelFlag=*/false); + if (NeedsBarrier) { + InsertPointOrErrorTy AfterIP = + createBarrier(LocationDescription(Builder.saveIP(), DL), OMPD_for, + /*ForceSimpleCall=*/false, /*CheckCancelFlag=*/false); + if (!AfterIP) + return AfterIP.takeError(); + } #ifndef NDEBUG // Even though we currently do not support applying additional methods to it, @@ -4229,7 +4302,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyStaticChunkedWorkshareLoop( CLI->assertOK(); #endif - return {DispatchAfter, DispatchAfter->getFirstInsertionPt()}; + return InsertPointTy(DispatchAfter, DispatchAfter->getFirstInsertionPt()); } // Returns an LLVM function to call for executing an OpenMP static worksharing @@ -4462,7 +4535,7 @@ OpenMPIRBuilder::applyWorkshareLoopTarget(DebugLoc DL, CanonicalLoopInfo *CLI, return CLI->getAfterIP(); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyWorkshareLoop( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::applyWorkshareLoop( DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP, bool NeedsBarrier, omp::ScheduleKind SchedKind, Value *ChunkSize, bool HasSimdModifier, bool HasMonotonicModifier, @@ -4563,9 +4636,11 @@ getKmpcForDynamicFiniForType(Type *Ty, Module &M, OpenMPIRBuilder &OMPBuilder) { llvm_unreachable("unknown OpenMP loop iterator bitwidth"); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyDynamicWorkshareLoop( - DebugLoc DL, CanonicalLoopInfo *CLI, InsertPointTy AllocaIP, - OMPScheduleType SchedType, bool NeedsBarrier, Value *Chunk) { +OpenMPIRBuilder::InsertPointOrErrorTy +OpenMPIRBuilder::applyDynamicWorkshareLoop(DebugLoc DL, CanonicalLoopInfo *CLI, + InsertPointTy AllocaIP, + OMPScheduleType SchedType, + bool NeedsBarrier, Value *Chunk) { assert(CLI->isValid() && "Requires a valid canonical loop"); assert(!isConflictIP(AllocaIP, CLI->getPreheaderIP()) && "Require dedicated allocate IP"); @@ -4681,9 +4756,12 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::applyDynamicWorkshareLoop( // Add the barrier if requested. if (NeedsBarrier) { Builder.SetInsertPoint(&Exit->back()); - createBarrier(LocationDescription(Builder.saveIP(), DL), - omp::Directive::OMPD_for, /* ForceSimpleCall */ false, - /* CheckCancelFlag */ false); + InsertPointOrErrorTy BarrierIP = + createBarrier(LocationDescription(Builder.saveIP(), DL), + omp::Directive::OMPD_for, /* ForceSimpleCall */ false, + /* CheckCancelFlag */ false); + if (!BarrierIP) + return BarrierIP.takeError(); } CLI->invalidate(); @@ -5542,7 +5620,7 @@ OpenMPIRBuilder::createCopyPrivate(const LocationDescription &Loc, return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createSingle( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createSingle( const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool IsNowait, ArrayRef<llvm::Value *> CPVars, ArrayRef<llvm::Function *> CPFuncs) { @@ -5571,14 +5649,17 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createSingle( Function *ExitRTLFn = getOrCreateRuntimeFunctionPtr(OMPRTL___kmpc_end_single); Instruction *ExitCall = Builder.CreateCall(ExitRTLFn, Args); - auto FiniCBWrapper = [&](InsertPointTy IP) { - FiniCB(IP); + auto FiniCBWrapper = [&](InsertPointTy IP) -> Error { + if (Error Err = FiniCB(IP)) + return Err; // The thread that executes the single region must set `DidIt` to 1. // This is used by __kmpc_copyprivate, to know if the caller is the // single thread or not. if (DidIt) Builder.CreateStore(Builder.getInt32(1), DidIt); + + return Error::success(); }; // generates the following: @@ -5589,9 +5670,12 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createSingle( // __kmpc_copyprivate // __kmpc_barrier - EmitOMPInlinedRegion(OMPD, EntryCall, ExitCall, BodyGenCB, FiniCBWrapper, - /*Conditional*/ true, - /*hasFinalize*/ true); + InsertPointOrErrorTy AfterIP = + EmitOMPInlinedRegion(OMPD, EntryCall, ExitCall, BodyGenCB, FiniCBWrapper, + /*Conditional*/ true, + /*hasFinalize*/ true); + if (!AfterIP) + return AfterIP.takeError(); if (DidIt) { for (size_t I = 0, E = CPVars.size(); I < E; ++I) @@ -5600,14 +5684,18 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createSingle( /*BufSize=*/ConstantInt::get(Int64, 0), CPVars[I], CPFuncs[I], DidIt); // NOTE __kmpc_copyprivate already inserts a barrier - } else if (!IsNowait) - createBarrier(LocationDescription(Builder.saveIP(), Loc.DL), - omp::Directive::OMPD_unknown, /* ForceSimpleCall */ false, - /* CheckCancelFlag */ false); + } else if (!IsNowait) { + InsertPointOrErrorTy AfterIP = + createBarrier(LocationDescription(Builder.saveIP(), Loc.DL), + omp::Directive::OMPD_unknown, /* ForceSimpleCall */ false, + /* CheckCancelFlag */ false); + if (!AfterIP) + return AfterIP.takeError(); + } return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createCritical( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createCritical( const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, StringRef CriticalName, Value *HintInst) { @@ -5688,7 +5776,7 @@ OpenMPIRBuilder::createOrderedDepend(const LocationDescription &Loc, return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createOrderedThreadsSimd( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createOrderedThreadsSimd( const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool IsThreads) { if (!updateToLocation(Loc)) @@ -5717,7 +5805,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createOrderedThreadsSimd( /*Conditional*/ false, /*hasFinalize*/ true); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::EmitOMPInlinedRegion( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::EmitOMPInlinedRegion( Directive OMPD, Instruction *EntryCall, Instruction *ExitCall, BodyGenCallbackTy BodyGenCB, FinalizeCallbackTy FiniCB, bool Conditional, bool HasFinalize, bool IsCancellable) { @@ -5739,15 +5827,19 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::EmitOMPInlinedRegion( emitCommonDirectiveEntry(OMPD, EntryCall, ExitBB, Conditional); // generate body - BodyGenCB(/* AllocaIP */ InsertPointTy(), - /* CodeGenIP */ Builder.saveIP()); + if (Error Err = BodyGenCB(/* AllocaIP */ InsertPointTy(), + /* CodeGenIP */ Builder.saveIP())) + return Err; // emit exit call and do any needed finalization. auto FinIP = InsertPointTy(FiniBB, FiniBB->getFirstInsertionPt()); assert(FiniBB->getTerminator()->getNumSuccessors() == 1 && FiniBB->getTerminator()->getSuccessor(0) == ExitBB && "Unexpected control flow graph state!!"); - emitCommonDirectiveExit(OMPD, FinIP, ExitCall, HasFinalize); + InsertPointOrErrorTy AfterIP = + emitCommonDirectiveExit(OMPD, FinIP, ExitCall, HasFinalize); + if (!AfterIP) + return AfterIP.takeError(); assert(FiniBB->getUniquePredecessor()->getUniqueSuccessor() == FiniBB && "Unexpected Control Flow State!"); MergeBlockIntoPredecessor(FiniBB); @@ -5796,7 +5888,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitCommonDirectiveEntry( return IRBuilder<>::InsertPoint(ExitBB, ExitBB->getFirstInsertionPt()); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitCommonDirectiveExit( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::emitCommonDirectiveExit( omp::Directive OMPD, InsertPointTy FinIP, Instruction *ExitCall, bool HasFinalize) { @@ -5810,7 +5902,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitCommonDirectiveExit( FinalizationInfo Fi = FinalizationStack.pop_back_val(); assert(Fi.DK == OMPD && "Unexpected Directive for Finalization call!"); - Fi.FiniCB(FinIP); + if (Error Err = Fi.FiniCB(FinIP)) + return Err; BasicBlock *FiniBB = FinIP.getBlock(); Instruction *FiniBBTI = FiniBB->getTerminator(); @@ -6319,7 +6412,7 @@ Constant *OpenMPIRBuilder::createTargetRegionEntryAddr(Function *OutlinedFn, Constant::getNullValue(Builder.getInt8Ty()), EntryFnName); } -void OpenMPIRBuilder::emitTargetRegionFunction( +Error OpenMPIRBuilder::emitTargetRegionFunction( TargetRegionEntryInfo &EntryInfo, FunctionGenCallback &GenerateFunctionCallback, bool IsOffloadEntry, Function *&OutlinedFn, Constant *&OutlinedFnID) { @@ -6327,15 +6420,20 @@ void OpenMPIRBuilder::emitTargetRegionFunction( SmallString<64> EntryFnName; OffloadInfoManager.getTargetRegionEntryFnName(EntryFnName, EntryInfo); - OutlinedFn = Config.isTargetDevice() || !Config.openMPOffloadMandatory() - ? GenerateFunctionCallback(EntryFnName) - : nullptr; + if (Config.isTargetDevice() || !Config.openMPOffloadMandatory()) { + Expected<Function *> CBResult = GenerateFunctionCallback(EntryFnName); + if (!CBResult) + return CBResult.takeError(); + OutlinedFn = *CBResult; + } else { + OutlinedFn = nullptr; + } // If this target outline function is not an offload entry, we don't need to // register it. This may be in the case of a false if clause, or if there are // no OpenMP targets. if (!IsOffloadEntry) - return; + return Error::success(); std::string EntryFnIDName = Config.isTargetDevice() @@ -6344,6 +6442,7 @@ void OpenMPIRBuilder::emitTargetRegionFunction( OutlinedFnID = registerTargetRegionFunction(EntryInfo, OutlinedFn, EntryFnName, EntryFnIDName); + return Error::success(); } Constant *OpenMPIRBuilder::registerTargetRegionFunction( @@ -6359,12 +6458,13 @@ Constant *OpenMPIRBuilder::registerTargetRegionFunction( return OutlinedFnID; } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTargetData( const LocationDescription &Loc, InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value *DeviceID, Value *IfCond, TargetDataInfo &Info, GenMapInfoCallbackTy GenMapInfoCB, omp::RuntimeFunction *MapperFunc, - function_ref<InsertPointTy(InsertPointTy CodeGenIP, BodyGenTy BodyGenType)> + function_ref<InsertPointOrErrorTy(InsertPointTy CodeGenIP, + BodyGenTy BodyGenType)> BodyGenCB, function_ref<void(unsigned int, Value *)> DeviceAddrCB, function_ref<Value *(unsigned int)> CustomMapperCB, Value *SrcLocInfo) { @@ -6374,8 +6474,13 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData( Builder.restoreIP(CodeGenIP); // Disable TargetData CodeGen on Device pass. if (Config.IsTargetDevice.value_or(false)) { - if (BodyGenCB) - Builder.restoreIP(BodyGenCB(Builder.saveIP(), BodyGenTy::NoPriv)); + if (BodyGenCB) { + InsertPointOrErrorTy AfterIP = + BodyGenCB(Builder.saveIP(), BodyGenTy::NoPriv); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); + } return Builder.saveIP(); } @@ -6384,7 +6489,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData( // Generate the code for the opening of the data environment. Capture all the // arguments of the runtime call by reference because they are used in the // closing of the region. - auto BeginThenGen = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { + auto BeginThenGen = [&](InsertPointTy AllocaIP, + InsertPointTy CodeGenIP) -> Error { MapInfo = &GenMapInfoCB(Builder.saveIP()); emitOffloadingArrays(AllocaIP, Builder.saveIP(), *MapInfo, Info, /*IsNonContiguous=*/true, DeviceAddrCB, @@ -6413,7 +6519,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData( if (IsStandAlone) { assert(MapperFunc && "MapperFunc missing for standalone target data"); - auto TaskBodyCB = [&](Value *, Value *, IRBuilderBase::InsertPoint) { + auto TaskBodyCB = [&](Value *, Value *, + IRBuilderBase::InsertPoint) -> Error { if (Info.HasNoWait) { OffloadingArgs.append({llvm::Constant::getNullValue(Int32), llvm::Constant::getNullValue(VoidPtr), @@ -6431,16 +6538,20 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData( emitBlock(OffloadContBlock, CurFn, /*IsFinished=*/true); Builder.restoreIP(Builder.saveIP()); } + return Error::success(); }; bool RequiresOuterTargetTask = Info.HasNoWait; - - if (!RequiresOuterTargetTask) - TaskBodyCB(/*DeviceID=*/nullptr, /*RTLoc=*/nullptr, - /*TargetTaskAllocaIP=*/{}); - else - emitTargetTask(TaskBodyCB, DeviceID, SrcLocInfo, AllocaIP, - /*Dependencies=*/{}, Info.HasNoWait); + if (!RequiresOuterTargetTask) { + Error Err = TaskBodyCB(/*DeviceID=*/nullptr, /*RTLoc=*/nullptr, + /*TargetTaskAllocaIP=*/{}); + assert(!Err && "TaskBodyCB expected to succeed"); + } else { + InsertPointOrErrorTy AfterIP = + emitTargetTask(TaskBodyCB, DeviceID, SrcLocInfo, AllocaIP, + /*Dependencies=*/{}, Info.HasNoWait); + assert(AfterIP && "TaskBodyCB expected to succeed"); + } } else { Function *BeginMapperFunc = getOrCreateRuntimeFunctionPtr( omp::OMPRTL___tgt_target_data_begin_mapper); @@ -6458,15 +6569,26 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData( // If device pointer privatization is required, emit the body of the // region here. It will have to be duplicated: with and without // privatization. - Builder.restoreIP(BodyGenCB(Builder.saveIP(), BodyGenTy::Priv)); + InsertPointOrErrorTy AfterIP = + BodyGenCB(Builder.saveIP(), BodyGenTy::Priv); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); } + return Error::success(); }; // If we need device pointer privatization, we need to emit the body of the // region with no privatization in the 'else' branch of the conditional. // Otherwise, we don't have to do anything. - auto BeginElseGen = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { - Builder.restoreIP(BodyGenCB(Builder.saveIP(), BodyGenTy::DupNoPriv)); + auto BeginElseGen = [&](InsertPointTy AllocaIP, + InsertPointTy CodeGenIP) -> Error { + InsertPointOrErrorTy AfterIP = + BodyGenCB(Builder.saveIP(), BodyGenTy::DupNoPriv); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); + return Error::success(); }; // Generate code for the closing of the data region. @@ -6494,35 +6616,45 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData( getOrCreateRuntimeFunctionPtr(omp::OMPRTL___tgt_target_data_end_mapper); Builder.CreateCall(EndMapperFunc, OffloadingArgs); + return Error::success(); }; // We don't have to do anything to close the region if the if clause evaluates // to false. - auto EndElseGen = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) {}; + auto EndElseGen = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { + return Error::success(); + }; - if (BodyGenCB) { - if (IfCond) { - emitIfClause(IfCond, BeginThenGen, BeginElseGen, AllocaIP); - } else { - BeginThenGen(AllocaIP, Builder.saveIP()); + Error Err = [&]() -> Error { + if (BodyGenCB) { + Error Err = [&]() { + if (IfCond) + return emitIfClause(IfCond, BeginThenGen, BeginElseGen, AllocaIP); + return BeginThenGen(AllocaIP, Builder.saveIP()); + }(); + + if (Err) + return Err; + + // If we don't require privatization of device pointers, we emit the body + // in between the runtime calls. This avoids duplicating the body code. + InsertPointOrErrorTy AfterIP = + BodyGenCB(Builder.saveIP(), BodyGenTy::NoPriv); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); + + if (IfCond) + return emitIfClause(IfCond, EndThenGen, EndElseGen, AllocaIP); + return EndThenGen(AllocaIP, Builder.saveIP()); } + if (IfCond) + return emitIfClause(IfCond, BeginThenGen, EndElseGen, AllocaIP); + return BeginThenGen(AllocaIP, Builder.saveIP()); + }(); - // If we don't require privatization of device pointers, we emit the body in - // between the runtime calls. This avoids duplicating the body code. - Builder.restoreIP(BodyGenCB(Builder.saveIP(), BodyGenTy::NoPriv)); - - if (IfCond) { - emitIfClause(IfCond, EndThenGen, EndElseGen, AllocaIP); - } else { - EndThenGen(AllocaIP, Builder.saveIP()); - } - } else { - if (IfCond) { - emitIfClause(IfCond, BeginThenGen, EndElseGen, AllocaIP); - } else { - BeginThenGen(AllocaIP, Builder.saveIP()); - } - } + if (Err) + return Err; return Builder.saveIP(); } @@ -6591,7 +6723,7 @@ FunctionCallee OpenMPIRBuilder::createDispatchDeinitFunction() { return getOrCreateRuntimeFunction(M, omp::OMPRTL___kmpc_dispatch_deinit); } -static Function *createOutlinedFunction( +static Expected<Function *> createOutlinedFunction( OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, StringRef FuncName, SmallVectorImpl<Value *> &Inputs, OpenMPIRBuilder::TargetBodyGenCallbackTy &CBFunc, @@ -6671,7 +6803,11 @@ static Function *createOutlinedFunction( OMPBuilder.ConstantAllocaRaiseCandidates.emplace_back(Func); // Insert target deinit call in the device compilation pass. - Builder.restoreIP(CBFunc(Builder.saveIP(), Builder.saveIP())); + llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + CBFunc(Builder.saveIP(), Builder.saveIP()); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); if (OMPBuilder.Config.isTargetDevice()) OMPBuilder.createTargetDeinit(Builder); @@ -6726,8 +6862,11 @@ static Function *createOutlinedFunction( Argument &Arg = std::get<1>(InArg); Value *InputCopy = nullptr; - Builder.restoreIP( - ArgAccessorFuncCB(Arg, Input, InputCopy, AllocaIP, Builder.saveIP())); + llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + ArgAccessorFuncCB(Arg, Input, InputCopy, AllocaIP, Builder.saveIP()); + if (!AfterIP) + return AfterIP.takeError(); + Builder.restoreIP(*AfterIP); // In certain cases a Global may be set up for replacement, however, this // Global may be used in multiple arguments to the kernel, just segmented @@ -6847,7 +6986,8 @@ static Function *emitTargetTaskProxyFunction(OpenMPIRBuilder &OMPBuilder, Builder.CreateRetVoid(); return ProxyFn; } -static void emitTargetOutlinedFunction( + +static Error emitTargetOutlinedFunction( OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, bool IsOffloadEntry, TargetRegionEntryInfo &EntryInfo, Function *&OutlinedFn, Constant *&OutlinedFnID, SmallVectorImpl<Value *> &Inputs, @@ -6861,11 +7001,12 @@ static void emitTargetOutlinedFunction( CBFunc, ArgAccessorFuncCB); }; - OMPBuilder.emitTargetRegionFunction(EntryInfo, GenerateOutlinedFunction, - IsOffloadEntry, OutlinedFn, OutlinedFnID); + return OMPBuilder.emitTargetRegionFunction( + EntryInfo, GenerateOutlinedFunction, IsOffloadEntry, OutlinedFn, + OutlinedFnID); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetTask( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::emitTargetTask( TargetTaskBodyCallbackTy TaskBodyCB, Value *DeviceID, Value *RTLoc, OpenMPIRBuilder::InsertPointTy AllocaIP, const SmallVector<llvm::OpenMPIRBuilder::DependData> &Dependencies, @@ -6983,7 +7124,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetTask( Builder.restoreIP(TargetTaskBodyIP); - TaskBodyCB(DeviceID, RTLoc, TargetTaskAllocaIP); + if (Error Err = TaskBodyCB(DeviceID, RTLoc, TargetTaskAllocaIP)) + return Err; OI.ExitBB = Builder.saveIP().getBlock(); OI.PostOutlineCB = [this, ToBeDeleted, Dependencies, HasNoWait, @@ -7161,8 +7303,8 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, // Generate a function call to the host fallback implementation of the target // region. This is called by the host when no offload entry was generated for // the target region and when the offloading call fails at runtime. - auto &&EmitTargetCallFallbackCB = - [&](OpenMPIRBuilder::InsertPointTy IP) -> OpenMPIRBuilder::InsertPointTy { + auto &&EmitTargetCallFallbackCB = [&](OpenMPIRBuilder::InsertPointTy IP) + -> OpenMPIRBuilder::InsertPointOrErrorTy { Builder.restoreIP(IP); Builder.CreateCall(OutlinedFn, Args); return Builder.saveIP(); @@ -7173,9 +7315,10 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, OpenMPIRBuilder::TargetKernelArgs KArgs; - auto TaskBodyCB = [&](Value *DeviceID, Value *RTLoc, - IRBuilderBase::InsertPoint TargetTaskAllocaIP) { - if (OutlinedFnID) { + auto TaskBodyCB = + [&](Value *DeviceID, Value *RTLoc, + IRBuilderBase::InsertPoint TargetTaskAllocaIP) -> Error { + llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = [&]() { // emitKernelLaunch makes the necessary runtime call to offload the // kernel. We then outline all that code into a separate function // ('kernel_launch_function' in the pseudo code above). This function is @@ -7183,31 +7326,41 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, // '@.omp_target_task_proxy_func' in the pseudo code above) // "@.omp_target_task_proxy_func' is generated by // emitTargetTaskProxyFunction. - Builder.restoreIP(OMPBuilder.emitKernelLaunch( - Builder, OutlinedFnID, EmitTargetCallFallbackCB, KArgs, DeviceID, - RTLoc, TargetTaskAllocaIP)); - } else { - // When OutlinedFnID is set to nullptr, then it's not an offloading - // call. In this case, we execute the host implementation directly. - OMPBuilder.Builder.restoreIP( - EmitTargetCallFallbackCB(OMPBuilder.Builder.saveIP())); - } + if (OutlinedFnID) + return OMPBuilder.emitKernelLaunch(Builder, OutlinedFnID, + EmitTargetCallFallbackCB, KArgs, + DeviceID, RTLoc, TargetTaskAllocaIP); + // When OutlinedFnID is set to nullptr, then it's not an offloading call. + // In this case, we execute the host implementation directly. + return EmitTargetCallFallbackCB(OMPBuilder.Builder.saveIP()); + }(); + + if (!AfterIP) + return AfterIP.takeError(); + + OMPBuilder.Builder.restoreIP(*AfterIP); + return Error::success(); }; // If we don't have an ID for the target region, it means an offload entry // wasn't created. In this case we just run the host fallback directly. if (!OutlinedFnID) { - if (RequiresOuterTargetTask) { - // Arguments that are intended to be directly forwarded to an - // emitKernelLaunch call are pased as nullptr, since OutlinedFnID=nullptr - // results in that call not being done. - Builder.restoreIP(OMPBuilder.emitTargetTask(TaskBodyCB, - /*DeviceID=*/nullptr, - /*RTLoc=*/nullptr, AllocaIP, - Dependencies, HasNoWait)); - } else { - Builder.restoreIP(EmitTargetCallFallbackCB(Builder.saveIP())); - } + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = [&]() { + if (RequiresOuterTargetTask) { + // Arguments that are intended to be directly forwarded to an + // emitKernelLaunch call are pased as nullptr, since + // OutlinedFnID=nullptr results in that call not being done. + return OMPBuilder.emitTargetTask(TaskBodyCB, /*DeviceID=*/nullptr, + /*RTLoc=*/nullptr, AllocaIP, + Dependencies, HasNoWait); + } + return EmitTargetCallFallbackCB(Builder.saveIP()); + }(); + + // Assume no error was returned because EmitTargetCallFallbackCB doesn't + // produce any. The 'if' check enables accessing the returned value. + if (AfterIP) + Builder.restoreIP(*AfterIP); return; } @@ -7247,17 +7400,24 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder, // The presence of certain clauses on the target directive require the // explicit generation of the target task. - if (RequiresOuterTargetTask) { - Builder.restoreIP(OMPBuilder.emitTargetTask( - TaskBodyCB, DeviceID, RTLoc, AllocaIP, Dependencies, HasNoWait)); - } else { - Builder.restoreIP(OMPBuilder.emitKernelLaunch( - Builder, OutlinedFnID, EmitTargetCallFallbackCB, KArgs, DeviceID, RTLoc, - AllocaIP)); - } + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = [&]() { + if (RequiresOuterTargetTask) + return OMPBuilder.emitTargetTask(TaskBodyCB, DeviceID, RTLoc, AllocaIP, + Dependencies, HasNoWait); + + return OMPBuilder.emitKernelLaunch(Builder, OutlinedFnID, + EmitTargetCallFallbackCB, KArgs, + DeviceID, RTLoc, AllocaIP); + }(); + + // Assume no error was returned because TaskBodyCB and + // EmitTargetCallFallbackCB don't produce any. The 'if' check enables + // accessing the returned value. + if (AfterIP) + Builder.restoreIP(*AfterIP); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTarget( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTarget( const LocationDescription &Loc, bool IsOffloadEntry, InsertPointTy AllocaIP, InsertPointTy CodeGenIP, TargetRegionEntryInfo &EntryInfo, ArrayRef<int32_t> NumTeams, ArrayRef<int32_t> NumThreads, @@ -7276,9 +7436,10 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTarget( // The target region is outlined into its own function. The LLVM IR for // the target region itself is generated using the callbacks CBFunc // and ArgAccessorFuncCB - emitTargetOutlinedFunction(*this, Builder, IsOffloadEntry, EntryInfo, - OutlinedFn, OutlinedFnID, Args, CBFunc, - ArgAccessorFuncCB); + if (Error Err = emitTargetOutlinedFunction( + *this, Builder, IsOffloadEntry, EntryInfo, OutlinedFn, OutlinedFnID, + Args, CBFunc, ArgAccessorFuncCB)) + return Err; // If we are not on the target device, then we need to generate code // to make a remote call (offload) to the previously outlined function @@ -7767,18 +7928,17 @@ void OpenMPIRBuilder::emitBlock(BasicBlock *BB, Function *CurFn, Builder.SetInsertPoint(BB); } -void OpenMPIRBuilder::emitIfClause(Value *Cond, BodyGenCallbackTy ThenGen, - BodyGenCallbackTy ElseGen, - InsertPointTy AllocaIP) { +Error OpenMPIRBuilder::emitIfClause(Value *Cond, BodyGenCallbackTy ThenGen, + BodyGenCallbackTy ElseGen, + InsertPointTy AllocaIP) { // If the condition constant folds and can be elided, try to avoid emitting // the condition and the dead arm of the if/else. if (auto *CI = dyn_cast<ConstantInt>(Cond)) { auto CondConstant = CI->getSExtValue(); if (CondConstant) - ThenGen(AllocaIP, Builder.saveIP()); - else - ElseGen(AllocaIP, Builder.saveIP()); - return; + return ThenGen(AllocaIP, Builder.saveIP()); + + return ElseGen(AllocaIP, Builder.saveIP()); } Function *CurFn = Builder.GetInsertBlock()->getParent(); @@ -7791,16 +7951,19 @@ void OpenMPIRBuilder::emitIfClause(Value *Cond, BodyGenCallbackTy ThenGen, Builder.CreateCondBr(Cond, ThenBlock, ElseBlock); // Emit the 'then' code. emitBlock(ThenBlock, CurFn); - ThenGen(AllocaIP, Builder.saveIP()); + if (Error Err = ThenGen(AllocaIP, Builder.saveIP())) + return Err; emitBranch(ContBlock); // Emit the 'else' code if present. // There is no need to emit line number for unconditional branch. emitBlock(ElseBlock, CurFn); - ElseGen(AllocaIP, Builder.saveIP()); + if (Error Err = ElseGen(AllocaIP, Builder.saveIP())) + return Err; // There is no need to emit line number for unconditional branch. emitBranch(ContBlock); // Emit the continuation block for code after the if. emitBlock(ContBlock, CurFn, /*IsFinished=*/true); + return Error::success(); } bool OpenMPIRBuilder::checkAndEmitFlushAfterAtomic( @@ -7948,7 +8111,7 @@ OpenMPIRBuilder::createAtomicWrite(const LocationDescription &Loc, return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicUpdate( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createAtomicUpdate( const LocationDescription &Loc, InsertPointTy AllocaIP, AtomicOpValue &X, Value *Expr, AtomicOrdering AO, AtomicRMWInst::BinOp RMWOp, AtomicUpdateCallbackTy &UpdateOp, bool IsXBinopExpr) { @@ -7969,8 +8132,11 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicUpdate( "OpenMP atomic does not support LT or GT operations"); }); - emitAtomicUpdate(AllocaIP, X.Var, X.ElemTy, Expr, AO, RMWOp, UpdateOp, - X.IsVolatile, IsXBinopExpr); + Expected<std::pair<Value *, Value *>> AtomicResult = + emitAtomicUpdate(AllocaIP, X.Var, X.ElemTy, Expr, AO, RMWOp, UpdateOp, + X.IsVolatile, IsXBinopExpr); + if (!AtomicResult) + return AtomicResult.takeError(); checkAndEmitFlushAfterAtomic(Loc, AO, AtomicKind::Update); return Builder.saveIP(); } @@ -8010,7 +8176,7 @@ Value *OpenMPIRBuilder::emitRMWOpAsInstruction(Value *Src1, Value *Src2, llvm_unreachable("Unsupported atomic update operation"); } -std::pair<Value *, Value *> OpenMPIRBuilder::emitAtomicUpdate( +Expected<std::pair<Value *, Value *>> OpenMPIRBuilder::emitAtomicUpdate( InsertPointTy AllocaIP, Value *X, Type *XElemTy, Value *Expr, AtomicOrdering AO, AtomicRMWInst::BinOp RMWOp, AtomicUpdateCallbackTy &UpdateOp, bool VolatileX, bool IsXBinopExpr) { @@ -8072,7 +8238,10 @@ std::pair<Value *, Value *> OpenMPIRBuilder::emitAtomicUpdate( llvm::PHINode *PHI = Builder.CreatePHI(OldVal->getType(), 2); PHI->addIncoming(AtomicLoadRes.first, CurBB); Value *OldExprVal = PHI; - Value *Upd = UpdateOp(OldExprVal, Builder); + Expected<Value *> CBResult = UpdateOp(OldExprVal, Builder); + if (!CBResult) + return CBResult.takeError(); + Value *Upd = *CBResult; Builder.CreateStore(Upd, NewAtomicAddr); AtomicOrdering Failure = llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO); @@ -8129,7 +8298,10 @@ std::pair<Value *, Value *> OpenMPIRBuilder::emitAtomicUpdate( } } - Value *Upd = UpdateOp(OldExprVal, Builder); + Expected<Value *> CBResult = UpdateOp(OldExprVal, Builder); + if (!CBResult) + return CBResult.takeError(); + Value *Upd = *CBResult; Builder.CreateStore(Upd, NewAtomicAddr); LoadInst *DesiredVal = Builder.CreateLoad(IntCastTy, NewAtomicAddr); AtomicOrdering Failure = @@ -8158,7 +8330,7 @@ std::pair<Value *, Value *> OpenMPIRBuilder::emitAtomicUpdate( return Res; } -OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCapture( +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createAtomicCapture( const LocationDescription &Loc, InsertPointTy AllocaIP, AtomicOpValue &X, AtomicOpValue &V, Value *Expr, AtomicOrdering AO, AtomicRMWInst::BinOp RMWOp, AtomicUpdateCallbackTy &UpdateOp, @@ -8181,11 +8353,13 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCapture( // If UpdateExpr is 'x' updated with some `expr` not based on 'x', // 'x' is simply atomically rewritten with 'expr'. AtomicRMWInst::BinOp AtomicOp = (UpdateExpr ? RMWOp : AtomicRMWInst::Xchg); - std::pair<Value *, Value *> Result = + Expected<std::pair<Value *, Value *>> AtomicResult = emitAtomicUpdate(AllocaIP, X.Var, X.ElemTy, Expr, AO, AtomicOp, UpdateOp, X.IsVolatile, IsXBinopExpr); - - Value *CapturedVal = (IsPostfixUpdate ? Result.first : Result.second); + if (!AtomicResult) + return AtomicResult.takeError(); + Value *CapturedVal = + (IsPostfixUpdate ? AtomicResult->first : AtomicResult->second); Builder.CreateStore(CapturedVal, V.Var, V.IsVolatile); checkAndEmitFlushAfterAtomic(Loc, AO, AtomicKind::Capture); @@ -8380,7 +8554,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createAtomicCompare( return Builder.saveIP(); } -OpenMPIRBuilder::InsertPointTy +OpenMPIRBuilder::InsertPointOrErrorTy OpenMPIRBuilder::createTeams(const LocationDescription &Loc, BodyGenCallbackTy BodyGenCB, Value *NumTeamsLower, Value *NumTeamsUpper, Value *ThreadLimit, @@ -8463,7 +8637,8 @@ OpenMPIRBuilder::createTeams(const LocationDescription &Loc, // Generate the body of teams. InsertPointTy AllocaIP(AllocaBB, AllocaBB->begin()); InsertPointTy CodeGenIP(BodyBB, BodyBB->begin()); - BodyGenCB(AllocaIP, CodeGenIP); + if (Error Err = BodyGenCB(AllocaIP, CodeGenIP)) + return Err; OutlineInfo OI; OI.EntryBB = AllocaBB; diff --git a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp index 35664a5..9e25620 100644 --- a/llvm/lib/Transforms/IPO/OpenMPOpt.cpp +++ b/llvm/lib/Transforms/IPO/OpenMPOpt.cpp @@ -1093,6 +1093,7 @@ private: CGStartBB->getTerminator()->setSuccessor(0, StartBB); assert(EndBB != nullptr && "EndBB should not be null"); EndBB->getTerminator()->setSuccessor(0, CGEndBB); + return Error::success(); }; auto PrivCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value &, @@ -1101,7 +1102,7 @@ private: return CodeGenIP; }; - auto FiniCB = [&](InsertPointTy CodeGenIP) {}; + auto FiniCB = [&](InsertPointTy CodeGenIP) { return Error::success(); }; /// Create a sequential execution region within a merged parallel region, /// encapsulated in a master construct with a barrier for synchronization. @@ -1132,8 +1133,9 @@ private: CGStartBB->getTerminator()->setSuccessor(0, SeqStartBB); assert(SeqEndBB != nullptr && "SeqEndBB should not be null"); SeqEndBB->getTerminator()->setSuccessor(0, CGEndBB); + return Error::success(); }; - auto FiniCB = [&](InsertPointTy CodeGenIP) {}; + auto FiniCB = [&](InsertPointTy CodeGenIP) { return Error::success(); }; // Find outputs from the sequential region to outside users and // broadcast their values to them. @@ -1176,12 +1178,15 @@ private: OpenMPIRBuilder::LocationDescription Loc( InsertPointTy(ParentBB, ParentBB->end()), DL); - InsertPointTy SeqAfterIP = + OpenMPIRBuilder::InsertPointOrErrorTy SeqAfterIP = OMPInfoCache.OMPBuilder.createMaster(Loc, BodyGenCB, FiniCB); + assert(SeqAfterIP && "Unexpected error creating master"); - OMPInfoCache.OMPBuilder.createBarrier(SeqAfterIP, OMPD_parallel); + OpenMPIRBuilder::InsertPointOrErrorTy BarrierAfterIP = + OMPInfoCache.OMPBuilder.createBarrier(*SeqAfterIP, OMPD_parallel); + assert(BarrierAfterIP && "Unexpected error creating barrier"); - BranchInst::Create(SeqAfterBB, SeqAfterIP.getBlock()); + BranchInst::Create(SeqAfterBB, SeqAfterIP->getBlock()); LLVM_DEBUG(dbgs() << TAG << "After sequential inlining " << *OuterFn << "\n"); @@ -1251,10 +1256,12 @@ private: OriginalFn->getEntryBlock().getFirstInsertionPt()); // Create the merged parallel region with default proc binding, to // avoid overriding binding settings, and without explicit cancellation. - InsertPointTy AfterIP = OMPInfoCache.OMPBuilder.createParallel( - Loc, AllocaIP, BodyGenCB, PrivCB, FiniCB, nullptr, nullptr, - OMP_PROC_BIND_default, /* IsCancellable */ false); - BranchInst::Create(AfterBB, AfterIP.getBlock()); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPInfoCache.OMPBuilder.createParallel( + Loc, AllocaIP, BodyGenCB, PrivCB, FiniCB, nullptr, nullptr, + OMP_PROC_BIND_default, /* IsCancellable */ false); + assert(AfterIP && "Unexpected error creating parallel"); + BranchInst::Create(AfterBB, AfterIP->getBlock()); // Perform the actual outlining. OMPInfoCache.OMPBuilder.finalize(OriginalFn); @@ -1290,10 +1297,12 @@ private: if (CI != MergableCIs.back()) { // TODO: Remove barrier if the merged parallel region includes the // 'nowait' clause. - OMPInfoCache.OMPBuilder.createBarrier( - InsertPointTy(NewCI->getParent(), - NewCI->getNextNode()->getIterator()), - OMPD_parallel); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPInfoCache.OMPBuilder.createBarrier( + InsertPointTy(NewCI->getParent(), + NewCI->getNextNode()->getIterator()), + OMPD_parallel); + assert(AfterIP && "Unexpected error creating barrier"); } CI->eraseFromParent(); diff --git a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp index fe04cbb..630cd03 100644 --- a/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp +++ b/llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp @@ -27,6 +27,20 @@ using namespace llvm; using namespace omp; +// Wrapper lambdas to allow using EXPECT*() macros inside of error-returning +// callbacks. +#define FINICB_WRAPPER(cb) \ + [&cb](InsertPointTy IP) -> Error { \ + cb(IP); \ + return Error::success(); \ + } + +#define BODYGENCB_WRAPPER(cb) \ + [&cb](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) -> Error { \ + cb(AllocaIP, CodeGenIP); \ + return Error::success(); \ + } + namespace { /// Create an instruction that uses the values in \p Values. We use "printf" @@ -218,9 +232,13 @@ protected: CallInst *CallInst = createPrintfCall(Builder, "%d\\n", {LC}); if (Call) *Call = CallInst; + + return Error::success(); }; - CanonicalLoopInfo *Loop = + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop(Loc, LoopBodyGenCB, CastedTripCount); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *Loop = *LoopResult; // Finalize the function. Builder.restoreIP(Loop->getAfterIP()); @@ -327,14 +345,18 @@ TEST_F(OpenMPIRBuilderTest, CreateBarrier) { IRBuilder<> Builder(BB); - OMPBuilder.createBarrier({IRBuilder<>::InsertPoint()}, OMPD_for); + OpenMPIRBuilder::InsertPointOrErrorTy BarrierIP1 = + OMPBuilder.createBarrier({IRBuilder<>::InsertPoint()}, OMPD_for); + assert(BarrierIP1 && "unexpected error"); EXPECT_TRUE(M->global_empty()); EXPECT_EQ(M->size(), 1U); EXPECT_EQ(F->size(), 1U); EXPECT_EQ(BB->size(), 0U); OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP()}); - OMPBuilder.createBarrier(Loc, OMPD_for); + OpenMPIRBuilder::InsertPointOrErrorTy BarrierIP2 = + OMPBuilder.createBarrier(Loc, OMPD_for); + assert(BarrierIP2 && "unexpected error"); EXPECT_FALSE(M->global_empty()); EXPECT_EQ(M->size(), 3U); EXPECT_EQ(F->size(), 1U); @@ -372,13 +394,15 @@ TEST_F(OpenMPIRBuilderTest, CreateCancel) { ASSERT_EQ(IP.getBlock()->end(), IP.getPoint()); BranchInst::Create(CBB, IP.getBlock()); }; - OMPBuilder.pushFinalizationCB({FiniCB, OMPD_parallel, true}); + OMPBuilder.pushFinalizationCB({FINICB_WRAPPER(FiniCB), OMPD_parallel, true}); IRBuilder<> Builder(BB); OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP()}); - auto NewIP = OMPBuilder.createCancel(Loc, nullptr, OMPD_parallel); - Builder.restoreIP(NewIP); + OpenMPIRBuilder::InsertPointOrErrorTy NewIP = + OMPBuilder.createCancel(Loc, nullptr, OMPD_parallel); + assert(NewIP && "unexpected error"); + Builder.restoreIP(*NewIP); EXPECT_FALSE(M->global_empty()); EXPECT_EQ(M->size(), 4U); EXPECT_EQ(F->size(), 4U); @@ -400,7 +424,7 @@ TEST_F(OpenMPIRBuilderTest, CreateCancel) { EXPECT_EQ(Cancel->getNumUses(), 1U); Instruction *CancelBBTI = Cancel->getParent()->getTerminator(); EXPECT_EQ(CancelBBTI->getNumSuccessors(), 2U); - EXPECT_EQ(CancelBBTI->getSuccessor(0), NewIP.getBlock()); + EXPECT_EQ(CancelBBTI->getSuccessor(0), NewIP->getBlock()); EXPECT_EQ(CancelBBTI->getSuccessor(1)->size(), 3U); CallInst *GTID1 = dyn_cast<CallInst>(&CancelBBTI->getSuccessor(1)->front()); EXPECT_NE(GTID1, nullptr); @@ -439,13 +463,15 @@ TEST_F(OpenMPIRBuilderTest, CreateCancelIfCond) { ASSERT_EQ(IP.getBlock()->end(), IP.getPoint()); BranchInst::Create(CBB, IP.getBlock()); }; - OMPBuilder.pushFinalizationCB({FiniCB, OMPD_parallel, true}); + OMPBuilder.pushFinalizationCB({FINICB_WRAPPER(FiniCB), OMPD_parallel, true}); IRBuilder<> Builder(BB); OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP()}); - auto NewIP = OMPBuilder.createCancel(Loc, Builder.getTrue(), OMPD_parallel); - Builder.restoreIP(NewIP); + OpenMPIRBuilder::InsertPointOrErrorTy NewIP = + OMPBuilder.createCancel(Loc, Builder.getTrue(), OMPD_parallel); + assert(NewIP && "unexpected error"); + Builder.restoreIP(*NewIP); EXPECT_FALSE(M->global_empty()); EXPECT_EQ(M->size(), 4U); EXPECT_EQ(F->size(), 7U); @@ -473,7 +499,7 @@ TEST_F(OpenMPIRBuilderTest, CreateCancelIfCond) { EXPECT_EQ(CancelBBTI->getNumSuccessors(), 2U); EXPECT_EQ(CancelBBTI->getSuccessor(0)->size(), 1U); EXPECT_EQ(CancelBBTI->getSuccessor(0)->getUniqueSuccessor(), - NewIP.getBlock()); + NewIP->getBlock()); EXPECT_EQ(CancelBBTI->getSuccessor(1)->size(), 3U); CallInst *GTID1 = dyn_cast<CallInst>(&CancelBBTI->getSuccessor(1)->front()); EXPECT_NE(GTID1, nullptr); @@ -512,13 +538,15 @@ TEST_F(OpenMPIRBuilderTest, CreateCancelBarrier) { ASSERT_EQ(IP.getBlock()->end(), IP.getPoint()); BranchInst::Create(CBB, IP.getBlock()); }; - OMPBuilder.pushFinalizationCB({FiniCB, OMPD_parallel, true}); + OMPBuilder.pushFinalizationCB({FINICB_WRAPPER(FiniCB), OMPD_parallel, true}); IRBuilder<> Builder(BB); OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP()}); - auto NewIP = OMPBuilder.createBarrier(Loc, OMPD_for); - Builder.restoreIP(NewIP); + OpenMPIRBuilder::InsertPointOrErrorTy NewIP = + OMPBuilder.createBarrier(Loc, OMPD_for); + assert(NewIP && "unexpected error"); + Builder.restoreIP(*NewIP); EXPECT_FALSE(M->global_empty()); EXPECT_EQ(M->size(), 3U); EXPECT_EQ(F->size(), 4U); @@ -540,7 +568,7 @@ TEST_F(OpenMPIRBuilderTest, CreateCancelBarrier) { EXPECT_EQ(Barrier->getNumUses(), 1U); Instruction *BarrierBBTI = Barrier->getParent()->getTerminator(); EXPECT_EQ(BarrierBBTI->getNumSuccessors(), 2U); - EXPECT_EQ(BarrierBBTI->getSuccessor(0), NewIP.getBlock()); + EXPECT_EQ(BarrierBBTI->getSuccessor(0), NewIP->getBlock()); EXPECT_EQ(BarrierBBTI->getSuccessor(1)->size(), 1U); EXPECT_EQ(BarrierBBTI->getSuccessor(1)->getTerminator()->getNumSuccessors(), 1U); @@ -563,7 +591,9 @@ TEST_F(OpenMPIRBuilderTest, DbgLoc) { IRBuilder<> Builder(BB); OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL}); - OMPBuilder.createBarrier(Loc, OMPD_for); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createBarrier(Loc, OMPD_for); + assert(AfterIP && "unexpected error"); CallInst *GTID = dyn_cast<CallInst>(&BB->front()); CallInst *Barrier = dyn_cast<CallInst>(GTID->getNextNode()); EXPECT_EQ(GTID->getDebugLoc(), DL); @@ -627,6 +657,7 @@ TEST_F(OpenMPIRBuilderTest, ParallelSimpleGPU) { Instruction *ThenTerm, *ElseTerm; SplitBlockAndInsertIfThenElse(Cmp, CodeGenIP.getBlock()->getTerminator(), &ThenTerm, &ElseTerm); + return Error::success(); }; auto PrivCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, @@ -654,19 +685,23 @@ TEST_F(OpenMPIRBuilderTest, ParallelSimpleGPU) { return CodeGenIP; }; - auto FiniCB = [&](InsertPointTy CodeGenIP) { ++NumFinalizationPoints; }; + auto FiniCB = [&](InsertPointTy CodeGenIP) { + ++NumFinalizationPoints; + return Error::success(); + }; IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()); - IRBuilder<>::InsertPoint AfterIP = + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createParallel(Loc, AllocaIP, BodyGenCB, PrivCB, FiniCB, nullptr, nullptr, OMP_PROC_BIND_default, false); + assert(AfterIP && "unexpected error"); EXPECT_EQ(NumBodiesGenerated, 1U); EXPECT_EQ(NumPrivatizedVars, 1U); EXPECT_EQ(NumFinalizationPoints, 1U); - Builder.restoreIP(AfterIP); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -735,6 +770,7 @@ TEST_F(OpenMPIRBuilderTest, ParallelSimple) { Instruction *ThenTerm, *ElseTerm; SplitBlockAndInsertIfThenElse(Cmp, CodeGenIP.getBlock()->getTerminator(), &ThenTerm, &ElseTerm); + return Error::success(); }; auto PrivCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, @@ -762,18 +798,22 @@ TEST_F(OpenMPIRBuilderTest, ParallelSimple) { return CodeGenIP; }; - auto FiniCB = [&](InsertPointTy CodeGenIP) { ++NumFinalizationPoints; }; + auto FiniCB = [&](InsertPointTy CodeGenIP) { + ++NumFinalizationPoints; + return Error::success(); + }; IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()); - IRBuilder<>::InsertPoint AfterIP = + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createParallel(Loc, AllocaIP, BodyGenCB, PrivCB, FiniCB, nullptr, nullptr, OMP_PROC_BIND_default, false); + assert(AfterIP && "unexpected error"); EXPECT_EQ(NumBodiesGenerated, 1U); EXPECT_EQ(NumPrivatizedVars, 1U); EXPECT_EQ(NumFinalizationPoints, 1U); - Builder.restoreIP(AfterIP); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -826,6 +866,7 @@ TEST_F(OpenMPIRBuilderTest, ParallelNested) { auto InnerBodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { ++NumInnerBodiesGenerated; + return Error::success(); }; auto PrivCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, @@ -841,7 +882,10 @@ TEST_F(OpenMPIRBuilderTest, ParallelNested) { return CodeGenIP; }; - auto FiniCB = [&](InsertPointTy CodeGenIP) { ++NumFinalizationPoints; }; + auto FiniCB = [&](InsertPointTy CodeGenIP) { + ++NumFinalizationPoints; + return Error::success(); + }; auto OuterBodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { ++NumOuterBodiesGenerated; @@ -849,27 +893,29 @@ TEST_F(OpenMPIRBuilderTest, ParallelNested) { BasicBlock *CGBB = CodeGenIP.getBlock(); BasicBlock *NewBB = SplitBlock(CGBB, &*CodeGenIP.getPoint()); CGBB->getTerminator()->eraseFromParent(); - ; - IRBuilder<>::InsertPoint AfterIP = OMPBuilder.createParallel( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createParallel( InsertPointTy(CGBB, CGBB->end()), AllocaIP, InnerBodyGenCB, PrivCB, FiniCB, nullptr, nullptr, OMP_PROC_BIND_default, false); + assert(AfterIP && "unexpected error"); - Builder.restoreIP(AfterIP); + Builder.restoreIP(*AfterIP); Builder.CreateBr(NewBB); + return Error::success(); }; IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()); - IRBuilder<>::InsertPoint AfterIP = + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createParallel(Loc, AllocaIP, OuterBodyGenCB, PrivCB, FiniCB, nullptr, nullptr, OMP_PROC_BIND_default, false); + assert(AfterIP && "unexpected error"); EXPECT_EQ(NumInnerBodiesGenerated, 1U); EXPECT_EQ(NumOuterBodiesGenerated, 1U); EXPECT_EQ(NumFinalizationPoints, 2U); - Builder.restoreIP(AfterIP); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -920,6 +966,7 @@ TEST_F(OpenMPIRBuilderTest, ParallelNested2Inner) { auto InnerBodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { ++NumInnerBodiesGenerated; + return Error::success(); }; auto PrivCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, @@ -935,7 +982,10 @@ TEST_F(OpenMPIRBuilderTest, ParallelNested2Inner) { return CodeGenIP; }; - auto FiniCB = [&](InsertPointTy CodeGenIP) { ++NumFinalizationPoints; }; + auto FiniCB = [&](InsertPointTy CodeGenIP) { + ++NumFinalizationPoints; + return Error::success(); + }; auto OuterBodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { ++NumOuterBodiesGenerated; @@ -948,32 +998,36 @@ TEST_F(OpenMPIRBuilderTest, ParallelNested2Inner) { NewBB1->getTerminator()->eraseFromParent(); ; - IRBuilder<>::InsertPoint AfterIP1 = OMPBuilder.createParallel( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP1 = OMPBuilder.createParallel( InsertPointTy(CGBB, CGBB->end()), AllocaIP, InnerBodyGenCB, PrivCB, FiniCB, nullptr, nullptr, OMP_PROC_BIND_default, false); + assert(AfterIP1 && "unexpected error"); - Builder.restoreIP(AfterIP1); + Builder.restoreIP(*AfterIP1); Builder.CreateBr(NewBB1); - IRBuilder<>::InsertPoint AfterIP2 = OMPBuilder.createParallel( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP2 = OMPBuilder.createParallel( InsertPointTy(NewBB1, NewBB1->end()), AllocaIP, InnerBodyGenCB, PrivCB, FiniCB, nullptr, nullptr, OMP_PROC_BIND_default, false); + assert(AfterIP2 && "unexpected error"); - Builder.restoreIP(AfterIP2); + Builder.restoreIP(*AfterIP2); Builder.CreateBr(NewBB2); + return Error::success(); }; IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()); - IRBuilder<>::InsertPoint AfterIP = + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createParallel(Loc, AllocaIP, OuterBodyGenCB, PrivCB, FiniCB, nullptr, nullptr, OMP_PROC_BIND_default, false); + assert(AfterIP && "unexpected error"); EXPECT_EQ(NumInnerBodiesGenerated, 2U); EXPECT_EQ(NumOuterBodiesGenerated, 1U); EXPECT_EQ(NumFinalizationPoints, 3U); - Builder.restoreIP(AfterIP); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -1043,6 +1097,7 @@ TEST_F(OpenMPIRBuilderTest, ParallelIfCond) { Instruction *ThenTerm, *ElseTerm; SplitBlockAndInsertIfThenElse(Cmp, &*Builder.GetInsertPoint(), &ThenTerm, &ElseTerm); + return Error::success(); }; auto PrivCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, @@ -1073,20 +1128,22 @@ TEST_F(OpenMPIRBuilderTest, ParallelIfCond) { auto FiniCB = [&](InsertPointTy CodeGenIP) { ++NumFinalizationPoints; // No destructors. + return Error::success(); }; IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()); - IRBuilder<>::InsertPoint AfterIP = + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createParallel(Loc, AllocaIP, BodyGenCB, PrivCB, FiniCB, Builder.CreateIsNotNull(F->arg_begin()), nullptr, OMP_PROC_BIND_default, false); + assert(AfterIP && "unexpected error"); EXPECT_EQ(NumBodiesGenerated, 1U); EXPECT_EQ(NumPrivatizedVars, 1U); EXPECT_EQ(NumFinalizationPoints, 1U); - Builder.restoreIP(AfterIP); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -1141,8 +1198,10 @@ TEST_F(OpenMPIRBuilderTest, ParallelCancelBarrier) { // Create three barriers, two cancel barriers but only one checked. Function *CBFn, *BFn; - Builder.restoreIP( - OMPBuilder.createBarrier(Builder.saveIP(), OMPD_parallel)); + OpenMPIRBuilder::InsertPointOrErrorTy BarrierIP1 = + OMPBuilder.createBarrier(Builder.saveIP(), OMPD_parallel); + assert(BarrierIP1 && "unexpected error"); + Builder.restoreIP(*BarrierIP1); CBFn = M->getFunction("__kmpc_cancel_barrier"); BFn = M->getFunction("__kmpc_barrier"); @@ -1153,8 +1212,10 @@ TEST_F(OpenMPIRBuilderTest, ParallelCancelBarrier) { ASSERT_EQ(CBFn->user_back()->getNumUses(), 1U); CheckedBarrier = cast<CallInst>(CBFn->user_back()); - Builder.restoreIP( - OMPBuilder.createBarrier(Builder.saveIP(), OMPD_parallel, true)); + OpenMPIRBuilder::InsertPointOrErrorTy BarrierIP2 = + OMPBuilder.createBarrier(Builder.saveIP(), OMPD_parallel, true); + assert(BarrierIP2 && "unexpected error"); + Builder.restoreIP(*BarrierIP2); CBFn = M->getFunction("__kmpc_cancel_barrier"); BFn = M->getFunction("__kmpc_barrier"); ASSERT_NE(CBFn, nullptr); @@ -1164,8 +1225,10 @@ TEST_F(OpenMPIRBuilderTest, ParallelCancelBarrier) { ASSERT_TRUE(isa<CallInst>(BFn->user_back())); ASSERT_EQ(BFn->user_back()->getNumUses(), 0U); - Builder.restoreIP(OMPBuilder.createBarrier(Builder.saveIP(), OMPD_parallel, - false, false)); + OpenMPIRBuilder::InsertPointOrErrorTy BarrierIP3 = + OMPBuilder.createBarrier(Builder.saveIP(), OMPD_parallel, false, false); + assert(BarrierIP3 && "unexpected error"); + Builder.restoreIP(*BarrierIP3); ASSERT_EQ(CBFn->getNumUses(), 2U); ASSERT_EQ(BFn->getNumUses(), 1U); ASSERT_TRUE(CBFn->user_back() != CheckedBarrier); @@ -1190,21 +1253,23 @@ TEST_F(OpenMPIRBuilderTest, ParallelCancelBarrier) { Builder.restoreIP(IP); Builder.CreateCall(FakeDestructor, {Builder.getInt32(NumFinalizationPoints)}); + return Error::success(); }; IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()); - IRBuilder<>::InsertPoint AfterIP = - OMPBuilder.createParallel(Loc, AllocaIP, BodyGenCB, PrivCB, FiniCB, - Builder.CreateIsNotNull(F->arg_begin()), - nullptr, OMP_PROC_BIND_default, true); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createParallel( + Loc, AllocaIP, BODYGENCB_WRAPPER(BodyGenCB), PrivCB, FiniCB, + Builder.CreateIsNotNull(F->arg_begin()), nullptr, OMP_PROC_BIND_default, + true); + assert(AfterIP && "unexpected error"); EXPECT_EQ(NumBodiesGenerated, 1U); EXPECT_EQ(NumPrivatizedVars, 0U); EXPECT_EQ(NumFinalizationPoints, 2U); EXPECT_EQ(FakeDestructor->getNumUses(), 2U); - Builder.restoreIP(AfterIP); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -1269,20 +1334,22 @@ TEST_F(OpenMPIRBuilderTest, ParallelForwardAsPointers) { Builder.CreateCall(TakeI32PtrFunc, I32PtrVal); Builder.CreateCall(TakeStructFunc, StructVal); Builder.CreateCall(TakeStructPtrFunc, StructPtrVal); + return Error::success(); }; auto PrivCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, Value &, Value &Inner, Value *&ReplacementValue) { ReplacementValue = &Inner; return CodeGenIP; }; - auto FiniCB = [](InsertPointTy) {}; + auto FiniCB = [](InsertPointTy) { return Error::success(); }; IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()); - IRBuilder<>::InsertPoint AfterIP = + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createParallel(Loc, AllocaIP, BodyGenCB, PrivCB, FiniCB, nullptr, nullptr, OMP_PROC_BIND_default, false); - Builder.restoreIP(AfterIP); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -1312,10 +1379,13 @@ TEST_F(OpenMPIRBuilderTest, CanonicalLoopSimple) { Instruction *ThenTerm, *ElseTerm; SplitBlockAndInsertIfThenElse(Cmp, CodeGenIP.getBlock()->getTerminator(), &ThenTerm, &ElseTerm); + return Error::success(); }; - CanonicalLoopInfo *Loop = + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop(Loc, LoopBodyGenCB, TripCount); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *Loop = *LoopResult; Builder.restoreIP(Loop->getAfterIP()); ReturnInst *RetInst = Builder.CreateRetVoid(); @@ -1367,10 +1437,14 @@ TEST_F(OpenMPIRBuilderTest, CanonicalLoopBounds) { Value *StartVal = ConstantInt::get(LCTy, Start); Value *StopVal = ConstantInt::get(LCTy, Stop); Value *StepVal = ConstantInt::get(LCTy, Step); - auto LoopBodyGenCB = [&](InsertPointTy CodeGenIP, llvm::Value *LC) {}; - CanonicalLoopInfo *Loop = + auto LoopBodyGenCB = [&](InsertPointTy CodeGenIP, llvm::Value *LC) { + return Error::success(); + }; + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop(Loc, LoopBodyGenCB, StartVal, StopVal, StepVal, IsSigned, InclusiveStop); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *Loop = *LoopResult; Loop->assertOK(); Builder.restoreIP(Loop->getAfterIP()); Value *TripCount = Loop->getTripCount(); @@ -1463,16 +1537,22 @@ TEST_F(OpenMPIRBuilderTest, CollapseNestedLoops) { Value *InnerLC) { Builder.restoreIP(InnerCodeGenIP); Call = createPrintfCall(Builder, "body i=%d j=%d\\n", {OuterLC, InnerLC}); + return Error::success(); }; - InnerLoop = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( Builder.saveIP(), InnerLoopBodyGenCB, InnerTripCount, "inner"); + assert(LoopResult && "unexpected error"); + InnerLoop = *LoopResult; Builder.restoreIP(InnerLoop->getAfterIP()); InbetweenTrail = createPrintfCall(Builder, "In-between trail i=%d\\n", {OuterLC}); + return Error::success(); }; - CanonicalLoopInfo *OuterLoop = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( OuterLoc, OuterLoopBodyGenCB, OuterTripCount, "outer"); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *OuterLoop = *LoopResult; // Finish the function. Builder.restoreIP(OuterLoop->getAfterIP()); @@ -1582,12 +1662,18 @@ TEST_F(OpenMPIRBuilderTest, TileNestedLoops) { // Add something that consumes the induction variables to the body. createPrintfCall(Builder, "i=%d j=%d\\n", {OuterLC, InnerLC}); + return Error::success(); }; - InnerLoop = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( OuterCodeGenIP, InnerLoopBodyGenCB, TripCount, "inner"); + assert(LoopResult && "unexpected error"); + InnerLoop = *LoopResult; + return Error::success(); }; - CanonicalLoopInfo *OuterLoop = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( Loc, OuterLoopBodyGenCB, TripCount, "outer"); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *OuterLoop = *LoopResult; // Finalize the function. Builder.restoreIP(OuterLoop->getAfterIP()); @@ -1682,14 +1768,20 @@ TEST_F(OpenMPIRBuilderTest, TileNestedLoopsWithBounds) { // Add something that consumes the induction variable to the body. Call = createPrintfCall(Builder, "i=%d j=%d\\n", {OuterLC, InnerLC}); + return Error::success(); }; - InnerLoop = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( OuterCodeGenIP, InnerLoopBodyGenCB, InnerStartVal, InnerStopVal, InnerStep, false, false, ComputeIP, "inner"); + assert(LoopResult && "unexpected error"); + InnerLoop = *LoopResult; + return Error::success(); }; - CanonicalLoopInfo *OuterLoop = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( Loc, OuterLoopBodyGenCB, OuterStartVal, OuterStopVal, OuterStep, false, false, ComputeIP, "outer"); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *OuterLoop = *LoopResult; // Finalize the function Builder.restoreIP(OuterLoop->getAfterIP()); @@ -1793,10 +1885,14 @@ TEST_F(OpenMPIRBuilderTest, TileSingleLoopCounts) { Value *StepVal = ConstantInt::get(LCTy, Step); // Generate a loop. - auto LoopBodyGenCB = [&](InsertPointTy CodeGenIP, llvm::Value *LC) {}; - CanonicalLoopInfo *Loop = + auto LoopBodyGenCB = [&](InsertPointTy CodeGenIP, llvm::Value *LC) { + return Error::success(); + }; + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop(Loc, LoopBodyGenCB, StartVal, StopVal, StepVal, IsSigned, InclusiveStop); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *Loop = *LoopResult; InsertPointTy AfterIP = Loop->getAfterIP(); // Tile the loop. @@ -2245,19 +2341,22 @@ TEST_F(OpenMPIRBuilderTest, StaticWorkshareLoopTarget) { Value *StartVal = ConstantInt::get(LCTy, 10); Value *StopVal = ConstantInt::get(LCTy, 52); Value *StepVal = ConstantInt::get(LCTy, 2); - auto LoopBodyGen = [&](InsertPointTy, Value *) {}; + auto LoopBodyGen = [&](InsertPointTy, Value *) { return Error::success(); }; - CanonicalLoopInfo *CLI = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( Loc, LoopBodyGen, StartVal, StopVal, StepVal, false, false); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *CLI = *LoopResult; BasicBlock *Preheader = CLI->getPreheader(); Value *TripCount = CLI->getTripCount(); Builder.SetInsertPoint(BB, BB->getFirstInsertionPt()); - IRBuilder<>::InsertPoint AfterIP = OMPBuilder.applyWorkshareLoop( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.applyWorkshareLoop( DL, CLI, AllocaIP, true, OMP_SCHEDULE_Static, nullptr, false, false, false, false, WorksharingLoopType::ForStaticLoop); - Builder.restoreIP(AfterIP); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -2306,11 +2405,15 @@ TEST_F(OpenMPIRBuilderTest, StaticWorkShareLoop) { Value *StartVal = ConstantInt::get(LCTy, 10); Value *StopVal = ConstantInt::get(LCTy, 52); Value *StepVal = ConstantInt::get(LCTy, 2); - auto LoopBodyGen = [&](InsertPointTy, llvm::Value *) {}; + auto LoopBodyGen = [&](InsertPointTy, llvm::Value *) { + return Error::success(); + }; - CanonicalLoopInfo *CLI = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( Loc, LoopBodyGen, StartVal, StopVal, StepVal, /*IsSigned=*/false, /*InclusiveStop=*/false); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *CLI = *LoopResult; BasicBlock *Preheader = CLI->getPreheader(); BasicBlock *Body = CLI->getBody(); Value *IV = CLI->getIndVar(); @@ -2319,8 +2422,9 @@ TEST_F(OpenMPIRBuilderTest, StaticWorkShareLoop) { Builder.SetInsertPoint(BB, BB->getFirstInsertionPt()); InsertPointTy AllocaIP = Builder.saveIP(); - OMPBuilder.applyWorkshareLoop(DL, CLI, AllocaIP, /*NeedsBarrier=*/true, - OMP_SCHEDULE_Static); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.applyWorkshareLoop( + DL, CLI, AllocaIP, /*NeedsBarrier=*/true, OMP_SCHEDULE_Static); + assert(AfterIP && "unexpected error"); BasicBlock *Cond = Body->getSinglePredecessor(); Instruction *Cmp = &*Cond->begin(); @@ -2412,8 +2516,9 @@ TEST_P(OpenMPIRBuilderTestWithIVBits, StaticChunkedWorkshareLoop) { Value *ChunkSize = ConstantInt::get(LCTy, 5); InsertPointTy AllocaIP{&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()}; - OMPBuilder.applyWorkshareLoop(DL, CLI, AllocaIP, /*NeedsBarrier=*/true, - OMP_SCHEDULE_Static, ChunkSize); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.applyWorkshareLoop( + DL, CLI, AllocaIP, /*NeedsBarrier=*/true, OMP_SCHEDULE_Static, ChunkSize); + assert(AfterIP && "unexpected error"); OMPBuilder.finalize(); EXPECT_FALSE(verifyModule(*M, &errs())); @@ -2500,11 +2605,15 @@ TEST_P(OpenMPIRBuilderTestWithParams, DynamicWorkShareLoop) { Value *StepVal = ConstantInt::get(LCTy, 2); Value *ChunkVal = (ChunkSize == 1) ? nullptr : ConstantInt::get(LCTy, ChunkSize); - auto LoopBodyGen = [&](InsertPointTy, llvm::Value *) {}; + auto LoopBodyGen = [&](InsertPointTy, llvm::Value *) { + return Error::success(); + }; - CanonicalLoopInfo *CLI = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( Loc, LoopBodyGen, StartVal, StopVal, StepVal, /*IsSigned=*/false, /*InclusiveStop=*/false); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *CLI = *LoopResult; Builder.SetInsertPoint(BB, BB->getFirstInsertionPt()); InsertPointTy AllocaIP = Builder.saveIP(); @@ -2517,7 +2626,7 @@ TEST_P(OpenMPIRBuilderTestWithParams, DynamicWorkShareLoop) { BasicBlock *LatchBlock = CLI->getLatch(); Value *IV = CLI->getIndVar(); - InsertPointTy EndIP = OMPBuilder.applyWorkshareLoop( + OpenMPIRBuilder::InsertPointOrErrorTy EndIP = OMPBuilder.applyWorkshareLoop( DL, CLI, AllocaIP, /*NeedsBarrier=*/true, getSchedKind(SchedType), ChunkVal, /*Simd=*/false, (SchedType & omp::OMPScheduleType::ModifierMonotonic) == @@ -2525,10 +2634,11 @@ TEST_P(OpenMPIRBuilderTestWithParams, DynamicWorkShareLoop) { (SchedType & omp::OMPScheduleType::ModifierNonmonotonic) == omp::OMPScheduleType::ModifierNonmonotonic, /*Ordered=*/false); + assert(EndIP && "unexpected error"); // The returned value should be the "after" point. - ASSERT_EQ(EndIP.getBlock(), AfterIP.getBlock()); - ASSERT_EQ(EndIP.getPoint(), AfterIP.getPoint()); + ASSERT_EQ(EndIP->getBlock(), AfterIP.getBlock()); + ASSERT_EQ(EndIP->getPoint(), AfterIP.getPoint()); auto AllocaIter = BB->begin(); ASSERT_GE(std::distance(BB->begin(), BB->end()), 4); @@ -2603,7 +2713,7 @@ TEST_P(OpenMPIRBuilderTestWithParams, DynamicWorkShareLoop) { EXPECT_EQ(NumCallsInExitBlock, 2u); // Add a termination to our block and check that it is internally consistent. - Builder.restoreIP(EndIP); + Builder.restoreIP(*EndIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); EXPECT_FALSE(verifyModule(*M, &errs())); @@ -2642,11 +2752,15 @@ TEST_F(OpenMPIRBuilderTest, DynamicWorkShareLoopOrdered) { Value *StopVal = ConstantInt::get(LCTy, 52); Value *StepVal = ConstantInt::get(LCTy, 2); Value *ChunkVal = ConstantInt::get(LCTy, ChunkSize); - auto LoopBodyGen = [&](InsertPointTy, llvm::Value *) {}; + auto LoopBodyGen = [&](InsertPointTy, llvm::Value *) { + return llvm::Error::success(); + }; - CanonicalLoopInfo *CLI = OMPBuilder.createCanonicalLoop( + Expected<CanonicalLoopInfo *> LoopResult = OMPBuilder.createCanonicalLoop( Loc, LoopBodyGen, StartVal, StopVal, StepVal, /*IsSigned=*/false, /*InclusiveStop=*/false); + assert(LoopResult && "unexpected error"); + CanonicalLoopInfo *CLI = *LoopResult; Builder.SetInsertPoint(BB, BB->getFirstInsertionPt()); InsertPointTy AllocaIP = Builder.saveIP(); @@ -2658,14 +2772,15 @@ TEST_F(OpenMPIRBuilderTest, DynamicWorkShareLoopOrdered) { BasicBlock *LatchBlock = CLI->getLatch(); Value *IV = CLI->getIndVar(); - InsertPointTy EndIP = OMPBuilder.applyWorkshareLoop( + OpenMPIRBuilder::InsertPointOrErrorTy EndIP = OMPBuilder.applyWorkshareLoop( DL, CLI, AllocaIP, /*NeedsBarrier=*/true, OMP_SCHEDULE_Static, ChunkVal, /*HasSimdModifier=*/false, /*HasMonotonicModifier=*/false, /*HasNonmonotonicModifier=*/false, /*HasOrderedClause=*/true); + assert(EndIP && "unexpected error"); // Add a termination to our block and check that it is internally consistent. - Builder.restoreIP(EndIP); + Builder.restoreIP(*EndIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); EXPECT_FALSE(verifyModule(*M, &errs())); @@ -2749,7 +2864,10 @@ TEST_F(OpenMPIRBuilderTest, MasterDirective) { EXPECT_NE(IPBB->end(), IP.getPoint()); }; - Builder.restoreIP(OMPBuilder.createMaster(Builder, BodyGenCB, FiniCB)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createMaster( + Builder, BODYGENCB_WRAPPER(BodyGenCB), FINICB_WRAPPER(FiniCB)); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Value *EntryBBTI = EntryBB->getTerminator(); EXPECT_NE(EntryBBTI, nullptr); EXPECT_TRUE(isa<BranchInst>(EntryBBTI)); @@ -2827,8 +2945,10 @@ TEST_F(OpenMPIRBuilderTest, MaskedDirective) { }; Constant *Filter = ConstantInt::get(Type::getInt32Ty(M->getContext()), 0); - Builder.restoreIP( - OMPBuilder.createMasked(Builder, BodyGenCB, FiniCB, Filter)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createMasked( + Builder, BODYGENCB_WRAPPER(BodyGenCB), FINICB_WRAPPER(FiniCB), Filter); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Value *EntryBBTI = EntryBB->getTerminator(); EXPECT_NE(EntryBBTI, nullptr); EXPECT_TRUE(isa<BranchInst>(EntryBBTI)); @@ -2893,8 +3013,11 @@ TEST_F(OpenMPIRBuilderTest, CriticalDirective) { }; BasicBlock *EntryBB = Builder.GetInsertBlock(); - Builder.restoreIP(OMPBuilder.createCritical(Builder, BodyGenCB, FiniCB, - "testCRT", nullptr)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createCritical(Builder, BODYGENCB_WRAPPER(BodyGenCB), + FINICB_WRAPPER(FiniCB), "testCRT", nullptr); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); CallInst *CriticalEntryCI = nullptr; for (auto &EI : *EntryBB) { @@ -3141,8 +3264,11 @@ TEST_F(OpenMPIRBuilderTest, OrderedDirectiveThreads) { // Test for "#omp ordered [threads]" BasicBlock *EntryBB = Builder.GetInsertBlock(); - Builder.restoreIP( - OMPBuilder.createOrderedThreadsSimd(Builder, BodyGenCB, FiniCB, true)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createOrderedThreadsSimd(Builder, BODYGENCB_WRAPPER(BodyGenCB), + FINICB_WRAPPER(FiniCB), true); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -3212,8 +3338,11 @@ TEST_F(OpenMPIRBuilderTest, OrderedDirectiveSimd) { // Test for "#omp ordered simd" BasicBlock *EntryBB = Builder.GetInsertBlock(); - Builder.restoreIP( - OMPBuilder.createOrderedThreadsSimd(Builder, BodyGenCB, FiniCB, false)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createOrderedThreadsSimd(Builder, BODYGENCB_WRAPPER(BodyGenCB), + FINICB_WRAPPER(FiniCB), false); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -3326,8 +3455,11 @@ TEST_F(OpenMPIRBuilderTest, SingleDirective) { EXPECT_NE(IPBB->end(), IP.getPoint()); }; - Builder.restoreIP( - OMPBuilder.createSingle(Builder, BodyGenCB, FiniCB, /*IsNowait*/ false)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createSingle(Builder, BODYGENCB_WRAPPER(BodyGenCB), + FINICB_WRAPPER(FiniCB), /*IsNowait*/ false); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Value *EntryBBTI = EntryBB->getTerminator(); EXPECT_NE(EntryBBTI, nullptr); EXPECT_TRUE(isa<BranchInst>(EntryBBTI)); @@ -3416,8 +3548,11 @@ TEST_F(OpenMPIRBuilderTest, SingleDirectiveNowait) { EXPECT_NE(IPBB->end(), IP.getPoint()); }; - Builder.restoreIP( - OMPBuilder.createSingle(Builder, BodyGenCB, FiniCB, /*IsNowait*/ true)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createSingle(Builder, BODYGENCB_WRAPPER(BodyGenCB), + FINICB_WRAPPER(FiniCB), /*IsNowait*/ true); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Value *EntryBBTI = EntryBB->getTerminator(); EXPECT_NE(EntryBBTI, nullptr); EXPECT_TRUE(isa<BranchInst>(EntryBBTI)); @@ -3535,9 +3670,11 @@ TEST_F(OpenMPIRBuilderTest, SingleDirectiveCopyPrivate) { EXPECT_NE(IPBB->end(), IP.getPoint()); }; - Builder.restoreIP(OMPBuilder.createSingle(Builder, BodyGenCB, FiniCB, - /*IsNowait*/ false, {CPVar}, - {CopyFunc})); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createSingle( + Builder, BODYGENCB_WRAPPER(BodyGenCB), FINICB_WRAPPER(FiniCB), + /*IsNowait*/ false, {CPVar}, {CopyFunc}); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Value *EntryBBTI = EntryBB->getTerminator(); EXPECT_NE(EntryBBTI, nullptr); EXPECT_TRUE(isa<BranchInst>(EntryBBTI)); @@ -3798,8 +3935,10 @@ TEST_F(OpenMPIRBuilderTest, OMPAtomicUpdate) { Sub = IRB.CreateSub(ConstVal, Atomic); return Sub; }; - Builder.restoreIP(OMPBuilder.createAtomicUpdate( - Builder, AllocaIP, X, Expr, AO, RMWOp, UpdateOp, IsXLHSInRHSPart)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createAtomicUpdate( + Builder, AllocaIP, X, Expr, AO, RMWOp, UpdateOp, IsXLHSInRHSPart); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); BasicBlock *ContBB = EntryBB->getSingleSuccessor(); BranchInst *ContTI = dyn_cast<BranchInst>(ContBB->getTerminator()); EXPECT_NE(ContTI, nullptr); @@ -3865,8 +4004,10 @@ TEST_F(OpenMPIRBuilderTest, OMPAtomicUpdateFloat) { Sub = IRB.CreateFSub(ConstVal, Atomic); return Sub; }; - Builder.restoreIP(OMPBuilder.createAtomicUpdate( - Builder, AllocaIP, X, Expr, AO, RMWOp, UpdateOp, IsXLHSInRHSPart)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createAtomicUpdate( + Builder, AllocaIP, X, Expr, AO, RMWOp, UpdateOp, IsXLHSInRHSPart); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); BasicBlock *ContBB = EntryBB->getSingleSuccessor(); BranchInst *ContTI = dyn_cast<BranchInst>(ContBB->getTerminator()); EXPECT_NE(ContTI, nullptr); @@ -3931,8 +4072,10 @@ TEST_F(OpenMPIRBuilderTest, OMPAtomicUpdateIntr) { Sub = IRB.CreateSub(ConstVal, Atomic); return Sub; }; - Builder.restoreIP(OMPBuilder.createAtomicUpdate( - Builder, AllocaIP, X, Expr, AO, RMWOp, UpdateOp, IsXLHSInRHSPart)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createAtomicUpdate( + Builder, AllocaIP, X, Expr, AO, RMWOp, UpdateOp, IsXLHSInRHSPart); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); BasicBlock *ContBB = EntryBB->getSingleSuccessor(); BranchInst *ContTI = dyn_cast<BranchInst>(ContBB->getTerminator()); EXPECT_NE(ContTI, nullptr); @@ -4003,9 +4146,12 @@ TEST_F(OpenMPIRBuilderTest, OMPAtomicCapture) { // integer update - not used auto UpdateOp = [&](Value *Atomic, IRBuilder<> &IRB) { return nullptr; }; - Builder.restoreIP(OMPBuilder.createAtomicCapture( - Builder, AllocaIP, X, V, Expr, AO, RMWOp, UpdateOp, UpdateExpr, - IsPostfixUpdate, IsXLHSInRHSPart)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createAtomicCapture(Builder, AllocaIP, X, V, Expr, AO, RMWOp, + UpdateOp, UpdateExpr, IsPostfixUpdate, + IsXLHSInRHSPart); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); EXPECT_EQ(EntryBB->getParent()->size(), 1U); AtomicRMWInst *ARWM = dyn_cast<AtomicRMWInst>(Init->getNextNode()); EXPECT_NE(ARWM, nullptr); @@ -4361,12 +4507,15 @@ TEST_F(OpenMPIRBuilderTest, CreateTeams) { Instruction *ThenTerm, *ElseTerm; SplitBlockAndInsertIfThenElse(Cmp, CodeGenIP.getBlock()->getTerminator(), &ThenTerm, &ElseTerm); + return Error::success(); }; OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL}); - Builder.restoreIP(OMPBuilder.createTeams( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTeams( Builder, BodyGenCB, /*NumTeamsLower=*/nullptr, /*NumTeamsUpper=*/nullptr, - /*ThreadLimit=*/nullptr, /*IfExpr=*/nullptr)); + /*ThreadLimit=*/nullptr, /*IfExpr=*/nullptr); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); @@ -4423,14 +4572,16 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithThreadLimit) { auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { Builder.restoreIP(CodeGenIP); Builder.CreateCall(FakeFunction, {}); + return Error::success(); }; // `F` has an argument - an integer, so we use that as the thread limit. - Builder.restoreIP(OMPBuilder.createTeams(/*=*/Builder, BodyGenCB, - /*NumTeamsLower=*/nullptr, - /*NumTeamsUpper=*/nullptr, - /*ThreadLimit=*/F->arg_begin(), - /*IfExpr=*/nullptr)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTeams( + /*=*/Builder, BodyGenCB, /*NumTeamsLower=*/nullptr, + /*NumTeamsUpper=*/nullptr, /*ThreadLimit=*/F->arg_begin(), + /*IfExpr=*/nullptr); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -4474,15 +4625,19 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithNumTeamsUpper) { auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { Builder.restoreIP(CodeGenIP); Builder.CreateCall(FakeFunction, {}); + return Error::success(); }; // `F` already has an integer argument, so we use that as upper bound to // `num_teams` - Builder.restoreIP(OMPBuilder.createTeams(Builder, BodyGenCB, - /*NumTeamsLower=*/nullptr, - /*NumTeamsUpper=*/F->arg_begin(), - /*ThreadLimit=*/nullptr, - /*IfExpr=*/nullptr)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createTeams(Builder, BodyGenCB, + /*NumTeamsLower=*/nullptr, + /*NumTeamsUpper=*/F->arg_begin(), + /*ThreadLimit=*/nullptr, + /*IfExpr=*/nullptr); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -4531,13 +4686,16 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithNumTeamsBoth) { auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { Builder.restoreIP(CodeGenIP); Builder.CreateCall(FakeFunction, {}); + return Error::success(); }; // `F` already has an integer argument, so we use that as upper bound to // `num_teams` - Builder.restoreIP( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTeams(Builder, BodyGenCB, NumTeamsLower, NumTeamsUpper, - /*ThreadLimit=*/nullptr, /*IfExpr=*/nullptr)); + /*ThreadLimit=*/nullptr, /*IfExpr=*/nullptr); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -4593,11 +4751,14 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithNumTeamsAndThreadLimit) { auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { Builder.restoreIP(CodeGenIP); Builder.CreateCall(FakeFunction, {}); + return Error::success(); }; OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL}); - Builder.restoreIP(OMPBuilder.createTeams( - Builder, BodyGenCB, NumTeamsLower, NumTeamsUpper, ThreadLimit, nullptr)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTeams( + Builder, BodyGenCB, NumTeamsLower, NumTeamsUpper, ThreadLimit, nullptr); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -4644,13 +4805,16 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithIfCondition) { auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { Builder.restoreIP(CodeGenIP); Builder.CreateCall(FakeFunction, {}); + return Error::success(); }; // `F` already has an integer argument, so we use that as upper bound to // `num_teams` - Builder.restoreIP(OMPBuilder.createTeams( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTeams( Builder, BodyGenCB, /*NumTeamsLower=*/nullptr, /*NumTeamsUpper=*/nullptr, - /*ThreadLimit=*/nullptr, IfExpr)); + /*ThreadLimit=*/nullptr, IfExpr); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -4707,12 +4871,15 @@ TEST_F(OpenMPIRBuilderTest, CreateTeamsWithIfConditionAndNumTeams) { auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { Builder.restoreIP(CodeGenIP); Builder.CreateCall(FakeFunction, {}); + return Error::success(); }; // `F` already has an integer argument, so we use that as upper bound to // `num_teams` - Builder.restoreIP(OMPBuilder.createTeams(Builder, BodyGenCB, NumTeamsLower, - NumTeamsUpper, ThreadLimit, IfExpr)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTeams( + Builder, BodyGenCB, NumTeamsLower, NumTeamsUpper, ThreadLimit, IfExpr); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -4937,6 +5104,7 @@ TEST_F(OpenMPIRBuilderTest, CreateReductions) { BodyIP = Builder.saveIP(); BodyAllocaIP = InnerAllocaIP; + return Error::success(); }; // Privatization for reduction creates local copies of reduction variables and @@ -4969,14 +5137,15 @@ TEST_F(OpenMPIRBuilderTest, CreateReductions) { }; // Do nothing in finalization. - auto FiniCB = [&](InsertPointTy CodeGenIP) { return CodeGenIP; }; + auto FiniCB = [&](InsertPointTy CodeGenIP) { return Error::success(); }; - InsertPointTy AfterIP = + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createParallel(Loc, OuterAllocaIP, BodyGenCB, PrivCB, FiniCB, /* IfCondition */ nullptr, /* NumThreads */ nullptr, OMP_PROC_BIND_default, /* IsCancellable */ false); - Builder.restoreIP(AfterIP); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OpenMPIRBuilder::ReductionInfo ReductionInfos[] = { {SumType, SumReduced, SumPrivatized, @@ -4989,10 +5158,12 @@ TEST_F(OpenMPIRBuilderTest, CreateReductions) { bool ReduceVariableByRef[] = {false, false}; - OMPBuilder.createReductions(BodyIP, BodyAllocaIP, ReductionInfos, - ReduceVariableByRef); + OpenMPIRBuilder::InsertPointOrErrorTy ReductionsIP = + OMPBuilder.createReductions(BodyIP, BodyAllocaIP, ReductionInfos, + ReduceVariableByRef); + assert(ReductionsIP && "unexpected error"); - Builder.restoreIP(AfterIP); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(F); @@ -5172,6 +5343,7 @@ TEST_F(OpenMPIRBuilderTest, CreateTwoReductions) { FirstBodyIP = Builder.saveIP(); FirstBodyAllocaIP = InnerAllocaIP; + return Error::success(); }; InsertPointTy SecondBodyIP, SecondBodyAllocaIP; @@ -5190,6 +5362,7 @@ TEST_F(OpenMPIRBuilderTest, CreateTwoReductions) { SecondBodyIP = Builder.saveIP(); SecondBodyAllocaIP = InnerAllocaIP; + return Error::success(); }; // Privatization for reduction creates local copies of reduction variables and @@ -5224,36 +5397,44 @@ TEST_F(OpenMPIRBuilderTest, CreateTwoReductions) { }; // Do nothing in finalization. - auto FiniCB = [&](InsertPointTy CodeGenIP) { return CodeGenIP; }; + auto FiniCB = [&](InsertPointTy CodeGenIP) { return Error::success(); }; - Builder.restoreIP( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP1 = OMPBuilder.createParallel(Loc, OuterAllocaIP, FirstBodyGenCB, PrivCB, FiniCB, /* IfCondition */ nullptr, /* NumThreads */ nullptr, OMP_PROC_BIND_default, - /* IsCancellable */ false)); - InsertPointTy AfterIP = OMPBuilder.createParallel( + /* IsCancellable */ false); + assert(AfterIP1 && "unexpected error"); + Builder.restoreIP(*AfterIP1); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP2 = OMPBuilder.createParallel( {Builder.saveIP(), DL}, OuterAllocaIP, SecondBodyGenCB, PrivCB, FiniCB, /* IfCondition */ nullptr, /* NumThreads */ nullptr, OMP_PROC_BIND_default, /* IsCancellable */ false); + assert(AfterIP2 && "unexpected error"); + Builder.restoreIP(*AfterIP2); OMPBuilder.Config.setIsGPU(false); bool ReduceVariableByRef[] = {false}; - OMPBuilder.createReductions( - FirstBodyIP, FirstBodyAllocaIP, - {{SumType, SumReduced, SumPrivatized, - /*EvaluationKind=*/OpenMPIRBuilder::EvalKind::Scalar, sumReduction, - /*ReductionGenClang=*/nullptr, sumAtomicReduction}}, - ReduceVariableByRef); - OMPBuilder.createReductions( - SecondBodyIP, SecondBodyAllocaIP, - {{XorType, XorReduced, XorPrivatized, - /*EvaluationKind=*/OpenMPIRBuilder::EvalKind::Scalar, xorReduction, - /*ReductionGenClang=*/nullptr, xorAtomicReduction}}, - ReduceVariableByRef); - - Builder.restoreIP(AfterIP); + OpenMPIRBuilder::InsertPointOrErrorTy ReductionsIP1 = + OMPBuilder.createReductions( + FirstBodyIP, FirstBodyAllocaIP, + {{SumType, SumReduced, SumPrivatized, + /*EvaluationKind=*/OpenMPIRBuilder::EvalKind::Scalar, sumReduction, + /*ReductionGenClang=*/nullptr, sumAtomicReduction}}, + ReduceVariableByRef); + assert(ReductionsIP1 && "unexpected error"); + OpenMPIRBuilder::InsertPointOrErrorTy ReductionsIP2 = + OMPBuilder.createReductions( + SecondBodyIP, SecondBodyAllocaIP, + {{XorType, XorReduced, XorPrivatized, + /*EvaluationKind=*/OpenMPIRBuilder::EvalKind::Scalar, xorReduction, + /*ReductionGenClang=*/nullptr, xorAtomicReduction}}, + ReduceVariableByRef); + assert(ReductionsIP2 && "unexpected error"); + + Builder.restoreIP(*AfterIP2); Builder.CreateRetVoid(); OMPBuilder.finalize(F); @@ -5320,8 +5501,10 @@ TEST_F(OpenMPIRBuilderTest, CreateSectionsSimple) { llvm::SmallVector<BodyGenCallbackTy, 4> SectionCBVector; llvm::SmallVector<BasicBlock *, 4> CaseBBs; - auto FiniCB = [&](InsertPointTy IP) {}; - auto SectionCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) {}; + auto FiniCB = [&](InsertPointTy IP) { return Error::success(); }; + auto SectionCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { + return Error::success(); + }; SectionCBVector.push_back(SectionCB); auto PrivCB = [](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, @@ -5329,8 +5512,10 @@ TEST_F(OpenMPIRBuilderTest, CreateSectionsSimple) { llvm::Value *&ReplVal) { return CodeGenIP; }; IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()); - Builder.restoreIP(OMPBuilder.createSections(Loc, AllocaIP, SectionCBVector, - PrivCB, FiniCB, false, false)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createSections( + Loc, AllocaIP, SectionCBVector, PrivCB, FiniCB, false, false); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); // Required at the end of the function EXPECT_NE(F->getEntryBlock().getTerminator(), nullptr); EXPECT_FALSE(verifyModule(*M, &errs())); @@ -5371,6 +5556,7 @@ TEST_F(OpenMPIRBuilderTest, CreateSections) { Value *PrivLoad = Builder.CreateLoad(F->arg_begin()->getType(), PrivAI, "local.alloca"); Builder.CreateICmpNE(F->arg_begin(), PrivLoad); + return Error::success(); }; auto PrivCB = [](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, llvm::Value &, llvm::Value &Val, llvm::Value *&ReplVal) { @@ -5383,8 +5569,11 @@ TEST_F(OpenMPIRBuilderTest, CreateSections) { IRBuilder<>::InsertPoint AllocaIP(&F->getEntryBlock(), F->getEntryBlock().getFirstInsertionPt()); - Builder.restoreIP(OMPBuilder.createSections(Loc, AllocaIP, SectionCBVector, - PrivCB, FiniCB, false, false)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createSections(Loc, AllocaIP, SectionCBVector, PrivCB, + FINICB_WRAPPER(FiniCB), false, false); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); // Required at the end of the function // Switch BB's predecessor is loop condition BB, whose successor at index 1 is @@ -5468,10 +5657,12 @@ TEST_F(OpenMPIRBuilderTest, CreateSectionsNoWait) { auto PrivCB = [](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, llvm::Value &, llvm::Value &Val, llvm::Value *&ReplVal) { return CodeGenIP; }; - auto FiniCB = [&](InsertPointTy IP) {}; + auto FiniCB = [&](InsertPointTy IP) { return Error::success(); }; - Builder.restoreIP(OMPBuilder.createSections(Loc, AllocaIP, SectionCBVector, - PrivCB, FiniCB, false, true)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createSections( + Loc, AllocaIP, SectionCBVector, PrivCB, FiniCB, false, true); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); // Required at the end of the function for (auto &Inst : instructions(*F)) { EXPECT_FALSE(isa<CallInst>(Inst) && @@ -5692,9 +5883,11 @@ TEST_F(OpenMPIRBuilderTest, TargetEnterData) { OMPBuilder.Config.setIsGPU(true); llvm::omp::RuntimeFunction RTLFunc = OMPRTL___tgt_target_data_begin_mapper; - Builder.restoreIP(OMPBuilder.createTargetData( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTargetData( Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID), - /* IfCond= */ nullptr, Info, GenMapInfoCB, &RTLFunc)); + /* IfCond= */ nullptr, Info, GenMapInfoCB, &RTLFunc); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); CallInst *TargetDataCall = dyn_cast<CallInst>(&BB->back()); EXPECT_NE(TargetDataCall, nullptr); @@ -5751,9 +5944,11 @@ TEST_F(OpenMPIRBuilderTest, TargetExitData) { OMPBuilder.Config.setIsGPU(true); llvm::omp::RuntimeFunction RTLFunc = OMPRTL___tgt_target_data_end_mapper; - Builder.restoreIP(OMPBuilder.createTargetData( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTargetData( Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID), - /* IfCond= */ nullptr, Info, GenMapInfoCB, &RTLFunc)); + /* IfCond= */ nullptr, Info, GenMapInfoCB, &RTLFunc); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); CallInst *TargetDataCall = dyn_cast<CallInst>(&BB->back()); EXPECT_NE(TargetDataCall, nullptr); @@ -5859,9 +6054,12 @@ TEST_F(OpenMPIRBuilderTest, TargetDataRegion) { return Builder.saveIP(); }; - Builder.restoreIP(OMPBuilder.createTargetData( - Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID), - /* IfCond= */ nullptr, Info, GenMapInfoCB, nullptr, BodyCB)); + OpenMPIRBuilder::InsertPointOrErrorTy TargetDataIP1 = + OMPBuilder.createTargetData( + Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID), + /* IfCond= */ nullptr, Info, GenMapInfoCB, nullptr, BodyCB); + assert(TargetDataIP1 && "unexpected error"); + Builder.restoreIP(*TargetDataIP1); CallInst *TargetDataCall = dyn_cast<CallInst>(&BB->back()); EXPECT_NE(TargetDataCall, nullptr); @@ -5884,9 +6082,12 @@ TEST_F(OpenMPIRBuilderTest, TargetDataRegion) { EXPECT_EQ(TargetDataCall, nullptr); return Builder.saveIP(); }; - Builder.restoreIP(OMPBuilder.createTargetData( - Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID), - /* IfCond= */ nullptr, Info, GenMapInfoCB, nullptr, BodyTargetCB)); + OpenMPIRBuilder::InsertPointOrErrorTy TargetDataIP2 = + OMPBuilder.createTargetData( + Loc, AllocaIP, Builder.saveIP(), Builder.getInt64(DeviceID), + /* IfCond= */ nullptr, Info, GenMapInfoCB, nullptr, BodyTargetCB); + assert(TargetDataIP2 && "unexpected error"); + Builder.restoreIP(*TargetDataIP2); EXPECT_TRUE(CheckDevicePassBodyGen); Builder.CreateRetVoid(); @@ -5981,9 +6182,11 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) { TargetRegionEntryInfo EntryInfo("func", 42, 4711, 17); OpenMPIRBuilder::LocationDescription OmpLoc({Builder.saveIP(), DL}); - Builder.restoreIP(OMPBuilder.createTarget( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTarget( OmpLoc, /*IsOffloadEntry=*/true, Builder.saveIP(), Builder.saveIP(), - EntryInfo, -1, 0, Inputs, GenMapInfoCB, BodyGenCB, SimpleArgAccessorCB)); + EntryInfo, -1, 0, Inputs, GenMapInfoCB, BodyGenCB, SimpleArgAccessorCB); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); @@ -6089,11 +6292,13 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) { TargetRegionEntryInfo EntryInfo("parent", /*DeviceID=*/1, /*FileID=*/2, /*Line=*/3, /*Count=*/0); - Builder.restoreIP( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTarget(Loc, /*IsOffloadEntry=*/true, EntryIP, EntryIP, EntryInfo, /*NumTeams=*/-1, /*NumThreads=*/0, CapturedArgs, GenMapInfoCB, - BodyGenCB, SimpleArgAccessorCB)); + BodyGenCB, SimpleArgAccessorCB); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -6238,11 +6443,13 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) { TargetRegionEntryInfo EntryInfo("parent", /*DeviceID=*/1, /*FileID=*/2, /*Line=*/3, /*Count=*/0); - Builder.restoreIP( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTarget(Loc, /*IsOffloadEntry=*/true, EntryIP, EntryIP, EntryInfo, /*NumTeams=*/-1, /*NumThreads=*/0, CapturedArgs, GenMapInfoCB, - BodyGenCB, SimpleArgAccessorCB)); + BodyGenCB, SimpleArgAccessorCB); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); Builder.CreateRetVoid(); OMPBuilder.finalize(); @@ -6354,15 +6561,17 @@ TEST_F(OpenMPIRBuilderTest, CreateTask) { Instruction *ThenTerm, *ElseTerm; SplitBlockAndInsertIfThenElse(Cmp, CodeGenIP.getBlock()->getTerminator(), &ThenTerm, &ElseTerm); + return Error::success(); }; BasicBlock *AllocaBB = Builder.GetInsertBlock(); BasicBlock *BodyBB = splitBB(Builder, /*CreateBranch=*/true, "alloca.split"); OpenMPIRBuilder::LocationDescription Loc( InsertPointTy(BodyBB, BodyBB->getFirstInsertionPt()), DL); - Builder.restoreIP(OMPBuilder.createTask( - Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), - BodyGenCB)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTask( + Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), BodyGenCB); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); @@ -6460,15 +6669,18 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskNoArgs) { F->setName("func"); IRBuilder<> Builder(BB); - auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) {}; + auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { + return Error::success(); + }; BasicBlock *AllocaBB = Builder.GetInsertBlock(); BasicBlock *BodyBB = splitBB(Builder, /*CreateBranch=*/true, "alloca.split"); OpenMPIRBuilder::LocationDescription Loc( InsertPointTy(BodyBB, BodyBB->getFirstInsertionPt()), DL); - Builder.restoreIP(OMPBuilder.createTask( - Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), - BodyGenCB)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTask( + Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), BodyGenCB); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); @@ -6490,14 +6702,18 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskUntied) { OMPBuilder.initialize(); F->setName("func"); IRBuilder<> Builder(BB); - auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) {}; + auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { + return Error::success(); + }; BasicBlock *AllocaBB = Builder.GetInsertBlock(); BasicBlock *BodyBB = splitBB(Builder, /*CreateBranch=*/true, "alloca.split"); OpenMPIRBuilder::LocationDescription Loc( InsertPointTy(BodyBB, BodyBB->getFirstInsertionPt()), DL); - Builder.restoreIP(OMPBuilder.createTask( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTask( Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), BodyGenCB, - /*Tied=*/false)); + /*Tied=*/false); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); @@ -6520,7 +6736,9 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskDepend) { OMPBuilder.initialize(); F->setName("func"); IRBuilder<> Builder(BB); - auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) {}; + auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { + return Error::success(); + }; BasicBlock *AllocaBB = Builder.GetInsertBlock(); BasicBlock *BodyBB = splitBB(Builder, /*CreateBranch=*/true, "alloca.split"); OpenMPIRBuilder::LocationDescription Loc( @@ -6532,9 +6750,11 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskDepend) { Type::getInt32Ty(M->getContext()), InDep); DDS.push_back(DDIn); } - Builder.restoreIP(OMPBuilder.createTask( + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTask( Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), BodyGenCB, - /*Tied=*/false, /*Final*/ nullptr, /*IfCondition*/ nullptr, DDS)); + /*Tied=*/false, /*Final*/ nullptr, /*IfCondition*/ nullptr, DDS); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); @@ -6594,7 +6814,9 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskFinal) { OMPBuilder.initialize(); F->setName("func"); IRBuilder<> Builder(BB); - auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) {}; + auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { + return Error::success(); + }; BasicBlock *BodyBB = splitBB(Builder, /*CreateBranch=*/true, "alloca.split"); IRBuilderBase::InsertPoint AllocaIP = Builder.saveIP(); Builder.SetInsertPoint(BodyBB); @@ -6602,8 +6824,11 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskFinal) { CmpInst::Predicate::ICMP_EQ, F->getArg(0), ConstantInt::get(Type::getInt32Ty(M->getContext()), 0U)); OpenMPIRBuilder::LocationDescription Loc(Builder.saveIP(), DL); - Builder.restoreIP(OMPBuilder.createTask(Loc, AllocaIP, BodyGenCB, - /*Tied=*/false, Final)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createTask(Loc, AllocaIP, BodyGenCB, + /*Tied=*/false, Final); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); @@ -6648,7 +6873,9 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskIfCondition) { OMPBuilder.initialize(); F->setName("func"); IRBuilder<> Builder(BB); - auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) {}; + auto BodyGenCB = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { + return Error::success(); + }; BasicBlock *BodyBB = splitBB(Builder, /*CreateBranch=*/true, "alloca.split"); IRBuilderBase::InsertPoint AllocaIP = Builder.saveIP(); Builder.SetInsertPoint(BodyBB); @@ -6656,9 +6883,11 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskIfCondition) { CmpInst::Predicate::ICMP_EQ, F->getArg(0), ConstantInt::get(Type::getInt32Ty(M->getContext()), 0U)); OpenMPIRBuilder::LocationDescription Loc(Builder.saveIP(), DL); - Builder.restoreIP(OMPBuilder.createTask(Loc, AllocaIP, BodyGenCB, - /*Tied=*/false, /*Final=*/nullptr, - IfCondition)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = + OMPBuilder.createTask(Loc, AllocaIP, BodyGenCB, + /*Tied=*/false, /*Final=*/nullptr, IfCondition); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); @@ -6742,15 +6971,17 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskgroup) { SplitBlockAndInsertIfThenElse(InternalIfCmp, CodeGenIP.getBlock()->getTerminator(), &ThenTerm, &ElseTerm); + return Error::success(); }; BasicBlock *AllocaBB = Builder.GetInsertBlock(); BasicBlock *BodyBB = splitBB(Builder, /*CreateBranch=*/true, "alloca.split"); OpenMPIRBuilder::LocationDescription Loc( InsertPointTy(BodyBB, BodyBB->getFirstInsertionPt()), DL); - Builder.restoreIP(OMPBuilder.createTaskgroup( - Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), - BodyGenCB)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTaskgroup( + Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), BodyGenCB); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); @@ -6823,9 +7054,13 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskgroupWithTasks) { Builder.CreateLoad(Alloca64->getAllocatedType(), Alloca64); Value *AddInst = Builder.CreateAdd(LoadValue, Builder.getInt64(64)); Builder.CreateStore(AddInst, Alloca64); + return Error::success(); }; OpenMPIRBuilder::LocationDescription Loc(Builder.saveIP(), DL); - Builder.restoreIP(OMPBuilder.createTask(Loc, AllocaIP, TaskBodyGenCB1)); + OpenMPIRBuilder::InsertPointOrErrorTy TaskIP1 = + OMPBuilder.createTask(Loc, AllocaIP, TaskBodyGenCB1); + assert(TaskIP1 && "unexpected error"); + Builder.restoreIP(*TaskIP1); auto TaskBodyGenCB2 = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) { Builder.restoreIP(CodeGenIP); @@ -6833,18 +7068,24 @@ TEST_F(OpenMPIRBuilderTest, CreateTaskgroupWithTasks) { Builder.CreateLoad(Alloca32->getAllocatedType(), Alloca32); Value *AddInst = Builder.CreateAdd(LoadValue, Builder.getInt32(32)); Builder.CreateStore(AddInst, Alloca32); + return Error::success(); }; OpenMPIRBuilder::LocationDescription Loc2(Builder.saveIP(), DL); - Builder.restoreIP(OMPBuilder.createTask(Loc2, AllocaIP, TaskBodyGenCB2)); + OpenMPIRBuilder::InsertPointOrErrorTy TaskIP2 = + OMPBuilder.createTask(Loc2, AllocaIP, TaskBodyGenCB2); + assert(TaskIP2 && "unexpected error"); + Builder.restoreIP(*TaskIP2); + return Error::success(); }; BasicBlock *AllocaBB = Builder.GetInsertBlock(); BasicBlock *BodyBB = splitBB(Builder, /*CreateBranch=*/true, "alloca.split"); OpenMPIRBuilder::LocationDescription Loc( InsertPointTy(BodyBB, BodyBB->getFirstInsertionPt()), DL); - Builder.restoreIP(OMPBuilder.createTaskgroup( - Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), - BodyGenCB)); + OpenMPIRBuilder::InsertPointOrErrorTy AfterIP = OMPBuilder.createTaskgroup( + Loc, InsertPointTy(AllocaBB, AllocaBB->getFirstInsertionPt()), BodyGenCB); + assert(AfterIP && "unexpected error"); + Builder.restoreIP(*AfterIP); OMPBuilder.finalize(); Builder.CreateRetVoid(); |