@@ -1080,8 +1080,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetKernel(
10801080}
10811081
10821082OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitKernelLaunch (
1083- const LocationDescription &Loc, Function *OutlinedFn, Value *OutlinedFnID,
1084- EmitFallbackCallbackTy emitTargetCallFallbackCB , TargetKernelArgs &Args,
1083+ const LocationDescription &Loc, Value *OutlinedFnID,
1084+ EmitFallbackCallbackTy EmitTargetCallFallbackCB , TargetKernelArgs &Args,
10851085 Value *DeviceID, Value *RTLoc, InsertPointTy AllocaIP) {
10861086
10871087 if (!updateToLocation (Loc))
@@ -1134,7 +1134,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitKernelLaunch(
11341134
11351135 auto CurFn = Builder.GetInsertBlock ()->getParent ();
11361136 emitBlock (OffloadFailedBlock, CurFn);
1137- Builder.restoreIP (emitTargetCallFallbackCB (Builder.saveIP ()));
1137+ Builder.restoreIP (EmitTargetCallFallbackCB (Builder.saveIP ()));
11381138 emitBranch (OffloadContBlock);
11391139 emitBlock (OffloadContBlock, CurFn, /* IsFinished=*/ true );
11401140 return Builder.saveIP ();
@@ -1736,7 +1736,7 @@ void OpenMPIRBuilder::createTaskyield(const LocationDescription &Loc) {
17361736// - All code is inserted in the entry block of the current function.
17371737static Value *emitTaskDependencies (
17381738 OpenMPIRBuilder &OMPBuilder,
1739- SmallVectorImpl<OpenMPIRBuilder::DependData> &Dependencies) {
1739+ const SmallVectorImpl<OpenMPIRBuilder::DependData> &Dependencies) {
17401740 // Early return if we have no dependencies to process
17411741 if (Dependencies.empty ())
17421742 return nullptr ;
@@ -6403,16 +6403,44 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData(
64036403 SrcLocInfo = getOrCreateIdent (SrcLocStr, SrcLocStrSize);
64046404 }
64056405
6406- Value *OffloadingArgs[] = {SrcLocInfo, DeviceID,
6407- PointerNum, RTArgs.BasePointersArray ,
6408- RTArgs.PointersArray , RTArgs.SizesArray ,
6409- RTArgs.MapTypesArray , RTArgs.MapNamesArray ,
6410- RTArgs.MappersArray };
6406+ SmallVector<llvm::Value *, 13 > OffloadingArgs = {
6407+ SrcLocInfo, DeviceID,
6408+ PointerNum, RTArgs.BasePointersArray ,
6409+ RTArgs.PointersArray , RTArgs.SizesArray ,
6410+ RTArgs.MapTypesArray , RTArgs.MapNamesArray ,
6411+ RTArgs.MappersArray };
64116412
64126413 if (IsStandAlone) {
64136414 assert (MapperFunc && " MapperFunc missing for standalone target data" );
6414- Builder.CreateCall (getOrCreateRuntimeFunctionPtr (*MapperFunc),
6415- OffloadingArgs);
6415+
6416+ auto TaskBodyCB = [&](Value *, Value *, IRBuilderBase::InsertPoint) {
6417+ if (Info.HasNoWait ) {
6418+ OffloadingArgs.append ({llvm::Constant::getNullValue (Int32),
6419+ llvm::Constant::getNullValue (VoidPtr),
6420+ llvm::Constant::getNullValue (Int32),
6421+ llvm::Constant::getNullValue (VoidPtr)});
6422+ }
6423+
6424+ Builder.CreateCall (getOrCreateRuntimeFunctionPtr (*MapperFunc),
6425+ OffloadingArgs);
6426+
6427+ if (Info.HasNoWait ) {
6428+ BasicBlock *OffloadContBlock =
6429+ BasicBlock::Create (Builder.getContext (), " omp_offload.cont" );
6430+ Function *CurFn = Builder.GetInsertBlock ()->getParent ();
6431+ emitBlock (OffloadContBlock, CurFn, /* IsFinished=*/ true );
6432+ Builder.restoreIP (Builder.saveIP ());
6433+ }
6434+ };
6435+
6436+ bool RequiresOuterTargetTask = Info.HasNoWait ;
6437+
6438+ if (!RequiresOuterTargetTask)
6439+ TaskBodyCB (/* DeviceID=*/ nullptr , /* RTLoc=*/ nullptr ,
6440+ /* TargetTaskAllocaIP=*/ {});
6441+ else
6442+ emitTargetTask (TaskBodyCB, DeviceID, SrcLocInfo, AllocaIP,
6443+ /* Dependencies=*/ {}, Info.HasNoWait );
64166444 } else {
64176445 Function *BeginMapperFunc = getOrCreateRuntimeFunctionPtr (
64186446 omp::OMPRTL___tgt_target_data_begin_mapper);
@@ -6836,13 +6864,18 @@ static void emitTargetOutlinedFunction(
68366864 OMPBuilder.emitTargetRegionFunction (EntryInfo, GenerateOutlinedFunction,
68376865 IsOffloadEntry, OutlinedFn, OutlinedFnID);
68386866}
6867+
68396868OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetTask (
6840- Function *OutlinedFn, Value *OutlinedFnID,
6841- EmitFallbackCallbackTy EmitTargetCallFallbackCB, TargetKernelArgs &Args,
6842- Value *DeviceID, Value *RTLoc, OpenMPIRBuilder::InsertPointTy AllocaIP,
6843- SmallVector<llvm::OpenMPIRBuilder::DependData> &Dependencies,
6869+ TargetTaskBodyCallbackTy TaskBodyCB, Value *DeviceID, Value *RTLoc,
6870+ OpenMPIRBuilder::InsertPointTy AllocaIP,
6871+ const SmallVector<llvm::OpenMPIRBuilder::DependData> &Dependencies,
68446872 bool HasNoWait) {
68456873
6874+ // The following explains the code-gen scenario for the `target` directive. A
6875+ // similar scneario is followed for other device-related directives (e.g.
6876+ // `target enter data`) but in similar fashion since we only need to emit task
6877+ // that encapsulates the proper runtime call.
6878+ //
68466879 // When we arrive at this function, the target region itself has been
68476880 // outlined into the function OutlinedFn.
68486881 // So at ths point, for
@@ -6950,22 +6983,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetTask(
69506983
69516984 Builder.restoreIP (TargetTaskBodyIP);
69526985
6953- if (OutlinedFnID) {
6954- // emitKernelLaunch makes the necessary runtime call to offload the kernel.
6955- // We then outline all that code into a separate function
6956- // ('kernel_launch_function' in the pseudo code above). This function is
6957- // then called by the target task proxy function (see
6958- // '@.omp_target_task_proxy_func' in the pseudo code above)
6959- // "@.omp_target_task_proxy_func' is generated by
6960- // emitTargetTaskProxyFunction.
6961- Builder.restoreIP (emitKernelLaunch (Builder, OutlinedFn, OutlinedFnID,
6962- EmitTargetCallFallbackCB, Args, DeviceID,
6963- RTLoc, TargetTaskAllocaIP));
6964- } else {
6965- // When OutlinedFnID is set to nullptr, then it's not an offloading call. In
6966- // this case, we execute the host implementation directly.
6967- Builder.restoreIP (EmitTargetCallFallbackCB (Builder.saveIP ()));
6968- }
6986+ TaskBodyCB (DeviceID, RTLoc, TargetTaskAllocaIP);
69696987
69706988 OI.ExitBB = Builder.saveIP ().getBlock ();
69716989 OI.PostOutlineCB = [this , ToBeDeleted, Dependencies, HasNoWait,
@@ -7153,18 +7171,40 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
71537171 bool HasDependencies = Dependencies.size () > 0 ;
71547172 bool RequiresOuterTargetTask = HasNoWait || HasDependencies;
71557173
7174+ OpenMPIRBuilder::TargetKernelArgs KArgs;
7175+
7176+ auto TaskBodyCB = [&](Value *DeviceID, Value *RTLoc,
7177+ IRBuilderBase::InsertPoint TargetTaskAllocaIP) {
7178+ if (OutlinedFnID) {
7179+ // emitKernelLaunch makes the necessary runtime call to offload the
7180+ // kernel. We then outline all that code into a separate function
7181+ // ('kernel_launch_function' in the pseudo code above). This function is
7182+ // then called by the target task proxy function (see
7183+ // '@.omp_target_task_proxy_func' in the pseudo code above)
7184+ // "@.omp_target_task_proxy_func' is generated by
7185+ // emitTargetTaskProxyFunction.
7186+ Builder.restoreIP (OMPBuilder.emitKernelLaunch (
7187+ Builder, OutlinedFnID, EmitTargetCallFallbackCB, KArgs, DeviceID,
7188+ RTLoc, TargetTaskAllocaIP));
7189+ } else {
7190+ // When OutlinedFnID is set to nullptr, then it's not an offloading
7191+ // call. In this case, we execute the host implementation directly.
7192+ OMPBuilder.Builder .restoreIP (
7193+ EmitTargetCallFallbackCB (OMPBuilder.Builder .saveIP ()));
7194+ }
7195+ };
7196+
71567197 // If we don't have an ID for the target region, it means an offload entry
71577198 // wasn't created. In this case we just run the host fallback directly.
71587199 if (!OutlinedFnID) {
71597200 if (RequiresOuterTargetTask) {
71607201 // Arguments that are intended to be directly forwarded to an
71617202 // emitKernelLaunch call are pased as nullptr, since OutlinedFnID=nullptr
71627203 // results in that call not being done.
7163- OpenMPIRBuilder::TargetKernelArgs KArgs;
7164- Builder.restoreIP (OMPBuilder.emitTargetTask (
7165- OutlinedFn, /* OutlinedFnID=*/ nullptr , EmitTargetCallFallbackCB, KArgs,
7166- /* DeviceID=*/ nullptr , /* RTLoc=*/ nullptr , AllocaIP, Dependencies,
7167- HasNoWait));
7204+ Builder.restoreIP (OMPBuilder.emitTargetTask (TaskBodyCB,
7205+ /* DeviceID=*/ nullptr ,
7206+ /* RTLoc=*/ nullptr , AllocaIP,
7207+ Dependencies, HasNoWait));
71687208 } else {
71697209 Builder.restoreIP (EmitTargetCallFallbackCB (Builder.saveIP ()));
71707210 }
@@ -7201,20 +7241,19 @@ emitTargetCall(OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
72017241 // TODO: Use correct DynCGGroupMem
72027242 Value *DynCGGroupMem = Builder.getInt32 (0 );
72037243
7204- OpenMPIRBuilder::TargetKernelArgs KArgs (NumTargetItems, RTArgs, NumIterations,
7205- NumTeamsC, NumThreadsC, DynCGGroupMem ,
7206- HasNoWait);
7244+ KArgs = OpenMPIRBuilder::TargetKernelArgs (
7245+ NumTargetItems, RTArgs, NumIterations, NumTeamsC, NumThreadsC,
7246+ DynCGGroupMem, HasNoWait);
72077247
72087248 // The presence of certain clauses on the target directive require the
72097249 // explicit generation of the target task.
72107250 if (RequiresOuterTargetTask) {
72117251 Builder.restoreIP (OMPBuilder.emitTargetTask (
7212- OutlinedFn, OutlinedFnID, EmitTargetCallFallbackCB, KArgs, DeviceID,
7213- RTLoc, AllocaIP, Dependencies, HasNoWait));
7252+ TaskBodyCB, DeviceID, RTLoc, AllocaIP, Dependencies, HasNoWait));
72147253 } else {
72157254 Builder.restoreIP (OMPBuilder.emitKernelLaunch (
7216- Builder, OutlinedFn, OutlinedFnID, EmitTargetCallFallbackCB, KArgs,
7217- DeviceID, RTLoc, AllocaIP));
7255+ Builder, OutlinedFnID, EmitTargetCallFallbackCB, KArgs, DeviceID, RTLoc ,
7256+ AllocaIP));
72187257 }
72197258}
72207259
0 commit comments