Skip to content

Commit 38b566f

Browse files
committed
[OpenMP][mlir] Add Groupprivate op in omp dialect.
1 parent 36cfdeb commit 38b566f

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

mlir/include/mlir/Dialect/OpenMP/OpenMPOps.td

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,4 +2224,31 @@ def WorkdistributeOp : OpenMP_Op<"workdistribute"> {
22242224
let assemblyFormat = "$region attr-dict";
22252225
}
22262226

2227+
//===----------------------------------------------------------------------===//
2228+
// [6.0] groupprivate Directive
2229+
//===----------------------------------------------------------------------===//
2230+
2231+
def GroupprivateOp : OpenMP_Op<"groupprivate",
2232+
[AllTypesMatch<["sym_addr", "gp_addr"]>]> {
2233+
let summary = "groupprivate directive";
2234+
let description = [{
2235+
The groupprivate directive specifies that variables are replicated, with
2236+
each group having its own copy.
2237+
2238+
This operation takes in the address of a symbol that represents the original
2239+
variable and returns the address of its groupprivate copy. All occurrences of
2240+
groupprivate variables in a parallel region should use the groupprivate copy
2241+
returned by this operation.
2242+
2243+
The `sym_addr` refers to the address of the symbol, which is a pointer to
2244+
the original variable.
2245+
}];
2246+
2247+
let arguments = (ins OpenMP_PointerLikeType:$sym_addr);
2248+
let results = (outs OpenMP_PointerLikeType:$gp_addr);
2249+
let assemblyFormat = [{
2250+
$sym_addr `:` type($sym_addr) `->` type($gp_addr) attr-dict
2251+
}];
2252+
}
2253+
22272254
#endif // OPENMP_OPS

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6092,6 +6092,40 @@ convertTargetFreeMemOp(Operation &opInst, llvm::IRBuilderBase &builder,
60926092
return success();
60936093
}
60946094

6095+
/// Converts an OpenMP Groupprivate operation into LLVM IR.
6096+
static LogicalResult
6097+
convertOmpGroupprivate(Operation &opInst, llvm::IRBuilderBase &builder,
6098+
LLVM::ModuleTranslation &moduleTranslation) {
6099+
llvm::OpenMPIRBuilder::LocationDescription ompLoc(builder);
6100+
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
6101+
auto groupprivateOp = cast<omp::GroupprivateOp>(opInst);
6102+
6103+
if (failed(checkImplementationStatus(opInst)))
6104+
return failure();
6105+
6106+
Value symAddr = groupprivateOp.getSymAddr();
6107+
auto *symOp = symAddr.getDefiningOp();
6108+
6109+
if (auto asCast = dyn_cast<LLVM::AddrSpaceCastOp>(symOp))
6110+
symOp = asCast.getOperand().getDefiningOp();
6111+
6112+
if (!isa<LLVM::AddressOfOp>(symOp))
6113+
return opInst.emitError("Addressing symbol not found");
6114+
LLVM::AddressOfOp addressOfOp = dyn_cast<LLVM::AddressOfOp>(symOp);
6115+
6116+
LLVM::GlobalOp global =
6117+
addressOfOp.getGlobal(moduleTranslation.symbolTable());
6118+
llvm::GlobalValue *globalValue = moduleTranslation.lookupGlobal(global);
6119+
6120+
if (!ompBuilder->Config.isTargetDevice()) {
6121+
llvm_unreachable("NYI");
6122+
} else {
6123+
moduleTranslation.mapValue(opInst.getResult(0), globalValue);
6124+
}
6125+
6126+
return success();
6127+
}
6128+
60956129
/// Given an OpenMP MLIR operation, create the corresponding LLVM IR (including
60966130
/// OpenMP runtime calls).
60976131
static LogicalResult
@@ -6275,6 +6309,9 @@ convertHostOrTargetOperation(Operation *op, llvm::IRBuilderBase &builder,
62756309
.Case([&](omp::TargetFreeMemOp) {
62766310
return convertTargetFreeMemOp(*op, builder, moduleTranslation);
62776311
})
6312+
.Case([&](omp::GroupprivateOp) {
6313+
return convertOmpGroupprivate(*op, builder, moduleTranslation);
6314+
})
62786315
.Default([&](Operation *inst) {
62796316
return inst->emitError()
62806317
<< "not yet implemented: " << inst->getName();

mlir/test/Dialect/OpenMP/ops.mlir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3321,3 +3321,22 @@ func.func @omp_workdistribute() {
33213321
}
33223322
return
33233323
}
3324+
3325+
llvm.mlir.global internal @_QFgpEx() : i32
3326+
3327+
func.func @omp_groupprivate() {
3328+
%0 = arith.constant 1 : i32
3329+
%1 = arith.constant 2 : i32
3330+
// CHECK: [[ARG0:%.*]] = llvm.mlir.addressof @_QFgpEx : !llvm.ptr
3331+
%global_addr = llvm.mlir.addressof @_QFgpEx : !llvm.ptr
3332+
omp.teams {
3333+
// CHECK: {{.*}} = omp.groupprivate [[ARG0]] : !llvm.ptr -> !llvm.ptr
3334+
%group_private_addr_in_teams = omp.groupprivate %global_addr : !llvm.ptr -> !llvm.ptr
3335+
llvm.store %0, %group_private_addr_in_teams : i32, !llvm.ptr
3336+
omp.terminator
3337+
}
3338+
// CHECK: {{.*}} = omp.groupprivate [[ARG0]] : !llvm.ptr -> !llvm.ptr
3339+
%group_private_addr_after_teams = omp.groupprivate %global_addr : !llvm.ptr -> !llvm.ptr
3340+
llvm.store %1, %group_private_addr_after_teams : i32, !llvm.ptr
3341+
return
3342+
}

0 commit comments

Comments
 (0)