Skip to content

Commit daee9e9

Browse files
committed
update llvmir lowering and tests
1 parent 38b566f commit daee9e9

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5994,7 +5994,7 @@ static bool isTargetDeviceOp(Operation *op) {
59945994
// by taking it in as an operand, so we must always lower these in
59955995
// some manner or result in an ICE (whether they end up in a no-op
59965996
// or otherwise).
5997-
if (mlir::isa<omp::ThreadprivateOp>(op))
5997+
if (mlir::isa<omp::ThreadprivateOp, omp::GroupprivateOp>(op))
59985998
return true;
59995999

60006000
if (mlir::isa<omp::TargetAllocMemOp>(op) ||
@@ -6095,8 +6095,7 @@ convertTargetFreeMemOp(Operation &opInst, llvm::IRBuilderBase &builder,
60956095
/// Converts an OpenMP Groupprivate operation into LLVM IR.
60966096
static LogicalResult
60976097
convertOmpGroupprivate(Operation &opInst, llvm::IRBuilderBase &builder,
6098-
LLVM::ModuleTranslation &moduleTranslation) {
6099-
llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder);
6098+
LLVM::ModuleTranslation &moduleTranslation) {
61006099
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
61016100
auto groupprivateOp = cast<omp::GroupprivateOp>(opInst);
61026101

@@ -6117,12 +6116,20 @@ convertOmpGroupprivate(Operation &opInst, llvm::IRBuilderBase &builder,
61176116
addressOfOp.getGlobal(moduleTranslation.symbolTable());
61186117
llvm::GlobalValue *globalValue = moduleTranslation.lookupGlobal(global);
61196118

6120-
if (!ompBuilder->Config.isTargetDevice()) {
6121-
llvm_unreachable("NYI");
6122-
} else {
6123-
moduleTranslation.mapValue(opInst.getResult(0), globalValue);
6124-
}
6125-
6119+
// Get the size of the variable
6120+
llvm::Type *varType = globalValue->getValueType();
6121+
llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
6122+
llvm::DataLayout DL = llvmModule->getDataLayout();
6123+
uint64_t typeSize = DL.getTypeAllocSize(varType);
6124+
// Call omp_alloc_shared to allocate memory for groupprivate variable.
6125+
llvm::FunctionCallee allocSharedFn = ompBuilder->getOrCreateRuntimeFunction(
6126+
*llvmModule, llvm::omp::OMPRTL___kmpc_alloc_shared);
6127+
// Call runtime to allocate shared memory for this group
6128+
llvm::Value *groupPrivatePtr =
6129+
builder.CreateCall(allocSharedFn, {builder.getInt64(typeSize)});
6130+
groupPrivatePtr =
6131+
builder.CreateBitCast(groupPrivatePtr, globalValue->getType());
6132+
moduleTranslation.mapValue(opInst.getResult(0), groupPrivatePtr);
61266133
return success();
61276134
}
61286135

mlir/test/Target/LLVMIR/openmp-llvm.mlir

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3449,3 +3449,39 @@ llvm.func @nested_task_with_deps() {
34493449

34503450
// CHECK: ret void
34513451
// CHECK: }
3452+
3453+
// -----
3454+
3455+
// CHECK: @_QFsubEx = internal global i32 undef
3456+
3457+
// CHECK-LABEL: @omp_groupprivate
3458+
llvm.func @omp_groupprivate() {
3459+
// CHECK: [[TMP1:%.*]] = call ptr @__kmpc_alloc_shared(i64 4)
3460+
// CHECK: store i32 1, ptr [[TMP1]], align 4
3461+
3462+
// CHECK-LABEL: omp.teams.region{{.*}}
3463+
// CHECK: [[TMP2:%.*]] = call ptr @__kmpc_alloc_shared(i64 4)
3464+
// CHECK: store i32 2, ptr [[TMP2]], align 4
3465+
3466+
%0 = llvm.mlir.constant(1 : i32) : i32
3467+
%1 = llvm.mlir.constant(2 : i32) : i32
3468+
%2 = llvm.mlir.constant(3 : i32) : i32
3469+
3470+
%3 = llvm.mlir.addressof @_QFsubEx : !llvm.ptr
3471+
%4 = omp.groupprivate %3 : !llvm.ptr -> !llvm.ptr
3472+
3473+
llvm.store %0, %4 : i32, !llvm.ptr
3474+
3475+
omp.teams {
3476+
%5 = omp.groupprivate %3 : !llvm.ptr -> !llvm.ptr
3477+
llvm.store %1, %5 : i32, !llvm.ptr
3478+
omp.terminator
3479+
}
3480+
3481+
llvm.store %2, %4 : i32, !llvm.ptr
3482+
llvm.return
3483+
}
3484+
3485+
llvm.mlir.global internal @_QFsubEx() : i32
3486+
3487+
// -----

0 commit comments

Comments
 (0)