-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[OpenMP][OMPIRBuilder] Support parallel in Generic kernels #150926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
skatrak
wants to merge
1
commit into
users/skatrak/flang-generic-04-parallel-args
Choose a base branch
from
users/skatrak/flang-generic-05-parallel-wrapper
base: users/skatrak/flang-generic-04-parallel-args
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1323,6 +1323,86 @@ Error OpenMPIRBuilder::emitCancelationCheckImpl( | |||||
return Error::success(); | ||||||
} | ||||||
|
||||||
// Create wrapper function used to gather the outlined function's argument | ||||||
// structure from a shared buffer and to forward them to it when running in | ||||||
// Generic mode. | ||||||
// | ||||||
// The outlined function is expected to receive 2 integer arguments followed by | ||||||
// an optional pointer argument to an argument structure holding the rest. | ||||||
static Function *createTargetParallelWrapper(OpenMPIRBuilder *OMPIRBuilder, | ||||||
Function &OutlinedFn) { | ||||||
size_t NumArgs = OutlinedFn.arg_size(); | ||||||
assert((NumArgs == 2 || NumArgs == 3) && | ||||||
"expected a 2-3 argument parallel outlined function"); | ||||||
bool UseArgStruct = NumArgs == 3; | ||||||
|
||||||
IRBuilder<> &Builder = OMPIRBuilder->Builder; | ||||||
IRBuilder<>::InsertPointGuard IPG(Builder); | ||||||
auto *FnTy = FunctionType::get(Builder.getVoidTy(), | ||||||
{Builder.getInt16Ty(), Builder.getInt32Ty()}, | ||||||
/*isVarArg=*/false); | ||||||
auto *WrapperFn = | ||||||
Function::Create(FnTy, GlobalValue::InternalLinkage, | ||||||
OutlinedFn.getName() + ".wrapper", OMPIRBuilder->M); | ||||||
|
||||||
WrapperFn->addParamAttr(0, Attribute::NoUndef); | ||||||
WrapperFn->addParamAttr(0, Attribute::ZExt); | ||||||
WrapperFn->addParamAttr(1, Attribute::NoUndef); | ||||||
|
||||||
BasicBlock *EntryBB = | ||||||
BasicBlock::Create(OMPIRBuilder->M.getContext(), "entry", WrapperFn); | ||||||
Builder.SetInsertPoint(EntryBB); | ||||||
|
||||||
// Allocation. | ||||||
Value *AddrAlloca = Builder.CreateAlloca(Builder.getInt32Ty(), | ||||||
/*ArraySize=*/nullptr, "addr"); | ||||||
AddrAlloca = Builder.CreatePointerBitCastOrAddrSpaceCast( | ||||||
AddrAlloca, Builder.getPtrTy(/*AddrSpace=*/0), | ||||||
AddrAlloca->getName() + ".ascast"); | ||||||
|
||||||
Value *ZeroAlloca = Builder.CreateAlloca(Builder.getInt32Ty(), | ||||||
/*ArraySize=*/nullptr, "zero"); | ||||||
ZeroAlloca = Builder.CreatePointerBitCastOrAddrSpaceCast( | ||||||
ZeroAlloca, Builder.getPtrTy(/*AddrSpace=*/0), | ||||||
ZeroAlloca->getName() + ".ascast"); | ||||||
|
||||||
Value *ArgsAlloca = nullptr; | ||||||
if (UseArgStruct) { | ||||||
ArgsAlloca = Builder.CreateAlloca(Builder.getPtrTy(), | ||||||
/*ArraySize=*/nullptr, "global_args"); | ||||||
ArgsAlloca = Builder.CreatePointerBitCastOrAddrSpaceCast( | ||||||
ArgsAlloca, Builder.getPtrTy(/*AddrSpace=*/0), | ||||||
ArgsAlloca->getName() + ".ascast"); | ||||||
} | ||||||
|
||||||
// Initialization. | ||||||
Builder.CreateStore(WrapperFn->getArg(1), AddrAlloca); | ||||||
Builder.CreateStore(Builder.getInt32(0), ZeroAlloca); | ||||||
if (UseArgStruct) { | ||||||
Builder.CreateCall( | ||||||
OMPIRBuilder->getOrCreateRuntimeFunctionPtr( | ||||||
llvm::omp::RuntimeFunction::OMPRTL___kmpc_get_shared_variables), | ||||||
{ArgsAlloca}); | ||||||
} | ||||||
|
||||||
SmallVector<Value *, 3> Args{AddrAlloca, ZeroAlloca}; | ||||||
|
||||||
// Load structArg from global_args. | ||||||
if (UseArgStruct) { | ||||||
Value *StructArg = Builder.CreateLoad(Builder.getPtrTy(), ArgsAlloca); | ||||||
StructArg = Builder.CreateInBoundsGEP(Builder.getPtrTy(), StructArg, | ||||||
{Builder.getInt64(0)}); | ||||||
StructArg = Builder.CreateLoad(Builder.getPtrTy(), StructArg, "structArg"); | ||||||
Args.push_back(StructArg); | ||||||
} | ||||||
|
||||||
// Call the outlined function holding the parallel body. | ||||||
Builder.CreateCall(&OutlinedFn, Args); | ||||||
Builder.CreateRetVoid(); | ||||||
|
||||||
return WrapperFn; | ||||||
} | ||||||
|
||||||
// Callback used to create OpenMP runtime calls to support | ||||||
// omp parallel clause for the device. | ||||||
// We need to use this callback to replace call to the OutlinedFn in OuterFn | ||||||
|
@@ -1332,6 +1412,10 @@ static void targetParallelCallback( | |||||
BasicBlock *OuterAllocaBB, Value *Ident, Value *IfCondition, | ||||||
Value *NumThreads, Instruction *PrivTID, AllocaInst *PrivTIDAddr, | ||||||
Value *ThreadID, const SmallVector<Instruction *, 4> &ToBeDeleted) { | ||||||
assert(OutlinedFn.arg_size() >= 2 && | ||||||
"Expected at least tid and bounded tid as arguments"); | ||||||
unsigned NumCapturedVars = OutlinedFn.arg_size() - /* tid & bounded tid */ 2; | ||||||
|
||||||
// Add some known attributes. | ||||||
IRBuilder<> &Builder = OMPIRBuilder->Builder; | ||||||
OutlinedFn.addParamAttr(0, Attribute::NoAlias); | ||||||
|
@@ -1340,17 +1424,12 @@ static void targetParallelCallback( | |||||
OutlinedFn.addParamAttr(1, Attribute::NoUndef); | ||||||
OutlinedFn.addFnAttr(Attribute::NoUnwind); | ||||||
|
||||||
assert(OutlinedFn.arg_size() >= 2 && | ||||||
"Expected at least tid and bounded tid as arguments"); | ||||||
unsigned NumCapturedVars = OutlinedFn.arg_size() - /* tid & bounded tid */ 2; | ||||||
|
||||||
CallInst *CI = cast<CallInst>(OutlinedFn.user_back()); | ||||||
assert(CI && "Expected call instruction to outlined function"); | ||||||
CI->getParent()->setName("omp_parallel"); | ||||||
|
||||||
Builder.SetInsertPoint(CI); | ||||||
Type *PtrTy = OMPIRBuilder->VoidPtr; | ||||||
Value *NullPtrValue = Constant::getNullValue(PtrTy); | ||||||
|
||||||
// Add alloca for kernel args | ||||||
OpenMPIRBuilder ::InsertPointTy CurrentIP = Builder.saveIP(); | ||||||
|
@@ -1376,6 +1455,15 @@ static void targetParallelCallback( | |||||
IfCondition ? Builder.CreateSExtOrTrunc(IfCondition, OMPIRBuilder->Int32) | ||||||
: Builder.getInt32(1); | ||||||
|
||||||
// If this is not a Generic kernel, we can skip generating the wrapper. | ||||||
std::optional<omp::OMPTgtExecModeFlags> ExecMode = | ||||||
getTargetKernelExecMode(*OuterFn); | ||||||
Value *WrapperFn; | ||||||
if (ExecMode && *ExecMode & OMP_TGT_EXEC_MODE_GENERIC) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
[suggestion] |
||||||
WrapperFn = createTargetParallelWrapper(OMPIRBuilder, OutlinedFn); | ||||||
else | ||||||
WrapperFn = Constant::getNullValue(PtrTy); | ||||||
|
||||||
// Build kmpc_parallel_51 call | ||||||
Value *Parallel51CallArgs[] = { | ||||||
/* identifier*/ Ident, | ||||||
|
@@ -1384,7 +1472,7 @@ static void targetParallelCallback( | |||||
/* number of threads */ NumThreads ? NumThreads : Builder.getInt32(-1), | ||||||
/* Proc bind */ Builder.getInt32(-1), | ||||||
/* outlined function */ &OutlinedFn, | ||||||
/* wrapper function */ NullPtrValue, | ||||||
/* wrapper function */ WrapperFn, | ||||||
/* arguments of the outlined funciton*/ Args, | ||||||
/* number of arguments */ Builder.getInt64(NumCapturedVars)}; | ||||||
|
||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider doxygen
///
coments