-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[Flang][MLIR][OpenMP] Add explicit shared memory (de-)allocation ops #161862
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-08-allocmem-refactor
Choose a base branch
from
users/skatrak/flang-generic-09-sharedmem-ops
base: users/skatrak/flang-generic-08-allocmem-refactor
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
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
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
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
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
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 |
---|---|---|
|
@@ -6104,11 +6104,9 @@ static bool isTargetDeviceOp(Operation *op) { | |
// by taking it in as an operand, so we must always lower these in | ||
// some manner or result in an ICE (whether they end up in a no-op | ||
// or otherwise). | ||
if (mlir::isa<omp::ThreadprivateOp>(op)) | ||
return true; | ||
|
||
if (mlir::isa<omp::TargetAllocMemOp>(op) || | ||
mlir::isa<omp::TargetFreeMemOp>(op)) | ||
if (mlir::isa<omp::ThreadprivateOp, omp::TargetAllocMemOp, | ||
omp::TargetFreeMemOp, omp::AllocSharedMemOp, | ||
omp::FreeSharedMemOp>(op)) | ||
return true; | ||
|
||
if (auto parentFn = op->getParentOfType<LLVM::LLVMFuncOp>()) | ||
|
@@ -6135,6 +6133,21 @@ static llvm::Function *getOmpTargetAlloc(llvm::IRBuilderBase &builder, | |
return func; | ||
} | ||
|
||
static llvm::Value * | ||
getAllocationSize(llvm::IRBuilderBase &builder, | ||
LLVM::ModuleTranslation &moduleTranslation, Type allocatedTy, | ||
OperandRange typeparams, OperandRange shape) { | ||
llvm::DataLayout dataLayout = | ||
moduleTranslation.getLLVMModule()->getDataLayout(); | ||
llvm::Type *llvmHeapTy = moduleTranslation.convertType(allocatedTy); | ||
llvm::TypeSize typeSize = dataLayout.getTypeStoreSize(llvmHeapTy); | ||
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. getTypeAllocSize() would be better to use here since it considers any alignment aswell. |
||
llvm::Value *allocSize = builder.getInt64(typeSize.getFixedValue()); | ||
for (auto typeParam : typeparams) | ||
allocSize = | ||
builder.CreateMul(allocSize, moduleTranslation.lookupValue(typeParam)); | ||
return allocSize; | ||
} | ||
|
||
static LogicalResult | ||
convertTargetAllocMemOp(Operation &opInst, llvm::IRBuilderBase &builder, | ||
LLVM::ModuleTranslation &moduleTranslation) { | ||
|
@@ -6149,14 +6162,9 @@ convertTargetAllocMemOp(Operation &opInst, llvm::IRBuilderBase &builder, | |
mlir::Value deviceNum = allocMemOp.getDevice(); | ||
llvm::Value *llvmDeviceNum = moduleTranslation.lookupValue(deviceNum); | ||
// Get the allocation size. | ||
llvm::DataLayout dataLayout = llvmModule->getDataLayout(); | ||
mlir::Type heapTy = allocMemOp.getAllocatedType(); | ||
llvm::Type *llvmHeapTy = moduleTranslation.convertType(heapTy); | ||
llvm::TypeSize typeSize = dataLayout.getTypeStoreSize(llvmHeapTy); | ||
llvm::Value *allocSize = builder.getInt64(typeSize.getFixedValue()); | ||
for (auto typeParam : allocMemOp.getTypeparams()) | ||
allocSize = | ||
builder.CreateMul(allocSize, moduleTranslation.lookupValue(typeParam)); | ||
llvm::Value *allocSize = getAllocationSize( | ||
builder, moduleTranslation, allocMemOp.getAllocatedType(), | ||
allocMemOp.getTypeparams(), allocMemOp.getShape()); | ||
// Create call to "omp_target_alloc" with the args as translated llvm values. | ||
llvm::CallInst *call = | ||
builder.CreateCall(ompTargetAllocFunc, {allocSize, llvmDeviceNum}); | ||
|
@@ -6167,6 +6175,19 @@ convertTargetAllocMemOp(Operation &opInst, llvm::IRBuilderBase &builder, | |
return success(); | ||
} | ||
|
||
static LogicalResult | ||
convertAllocSharedMemOp(omp::AllocSharedMemOp allocMemOp, | ||
llvm::IRBuilderBase &builder, | ||
LLVM::ModuleTranslation &moduleTranslation) { | ||
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder(); | ||
llvm::Value *size = getAllocationSize( | ||
builder, moduleTranslation, allocMemOp.getAllocatedType(), | ||
allocMemOp.getTypeparams(), allocMemOp.getShape()); | ||
moduleTranslation.mapValue(allocMemOp.getResult(), | ||
ompBuilder->createOMPAllocShared(builder, size)); | ||
return success(); | ||
} | ||
|
||
static llvm::Function *getOmpTargetFree(llvm::IRBuilderBase &builder, | ||
llvm::Module *llvmModule) { | ||
llvm::Type *ptrTy = builder.getPtrTy(0); | ||
|
@@ -6202,6 +6223,21 @@ convertTargetFreeMemOp(Operation &opInst, llvm::IRBuilderBase &builder, | |
return success(); | ||
} | ||
|
||
static LogicalResult | ||
convertFreeSharedMemOp(omp::FreeSharedMemOp freeMemOp, | ||
llvm::IRBuilderBase &builder, | ||
LLVM::ModuleTranslation &moduleTranslation) { | ||
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder(); | ||
auto allocMemOp = | ||
freeMemOp.getHeapref().getDefiningOp<omp::AllocSharedMemOp>(); | ||
llvm::Value *size = getAllocationSize( | ||
builder, moduleTranslation, allocMemOp.getAllocatedType(), | ||
allocMemOp.getTypeparams(), allocMemOp.getShape()); | ||
ompBuilder->createOMPFreeShared( | ||
builder, moduleTranslation.lookupValue(freeMemOp.getHeapref()), size); | ||
return success(); | ||
} | ||
|
||
/// Given an OpenMP MLIR operation, create the corresponding LLVM IR (including | ||
/// OpenMP runtime calls). | ||
static LogicalResult | ||
|
@@ -6382,6 +6418,12 @@ convertHostOrTargetOperation(Operation *op, llvm::IRBuilderBase &builder, | |
.Case([&](omp::TargetFreeMemOp) { | ||
return convertTargetFreeMemOp(*op, builder, moduleTranslation); | ||
}) | ||
.Case([&](omp::AllocSharedMemOp op) { | ||
return convertAllocSharedMemOp(op, builder, moduleTranslation); | ||
}) | ||
.Case([&](omp::FreeSharedMemOp op) { | ||
return convertFreeSharedMemOp(op, builder, moduleTranslation); | ||
}) | ||
.Default([&](Operation *inst) { | ||
return inst->emitError() | ||
<< "not yet implemented: " << inst->getName(); | ||
|
Oops, something went wrong.
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.
The concept of alloc/free might not need such detailed explanation