[Mlir-commits] [mlir] [OpenMP][mlir] Add Groupprivate op in omp dialect. (PR #162704)
Kareem Ergawy
llvmlistbot at llvm.org
Thu Nov 27 22:28:03 PST 2025
================
@@ -6128,6 +6143,75 @@ convertTargetFreeMemOp(Operation &opInst, llvm::IRBuilderBase &builder,
return success();
}
+/// Converts an OpenMP Groupprivate operation into LLVM IR.
+static LogicalResult
+convertOmpGroupprivate(Operation &opInst, llvm::IRBuilderBase &builder,
+ LLVM::ModuleTranslation &moduleTranslation) {
+ llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
+ auto groupprivateOp = cast<omp::GroupprivateOp>(opInst);
+
+ if (failed(checkImplementationStatus(opInst)))
+ return failure();
+
+ bool isTargetDevice = ompBuilder->Config.isTargetDevice();
+ auto deviceType = groupprivateOp.getDeviceType();
+
+ // skip allocation based on device_type
+ bool shouldAllocate = true;
+ if (deviceType.has_value()) {
+ switch (*deviceType) {
+ case mlir::omp::DeclareTargetDeviceType::host:
+ // Only allocate on host
+ shouldAllocate = !isTargetDevice;
+ break;
+ case mlir::omp::DeclareTargetDeviceType::nohost:
+ // Only allocate on device
+ shouldAllocate = isTargetDevice;
+ break;
+ case mlir::omp::DeclareTargetDeviceType::any:
+ // Allocate on both
+ shouldAllocate = true;
+ break;
+ }
+ }
+
+ Value symAddr = groupprivateOp.getSymAddr();
+ Operation *symOp = symAddr.getDefiningOp();
+
+ LLVM::GlobalOp global =
+ getGlobalFromSymbol(symOp, moduleTranslation, &opInst);
+ if (!global)
+ return failure();
+
+ llvm::GlobalValue *globalValue = moduleTranslation.lookupGlobal(global);
+ llvm::Value *resultPtr;
+
+ if (shouldAllocate) {
+ // Get the size of the variable
+ llvm::Type *varType = globalValue->getValueType();
+ llvm::Module *llvmModule = moduleTranslation.getLLVMModule();
+ llvm::DataLayout DL = llvmModule->getDataLayout();
+ uint64_t typeSize = DL.getTypeAllocSize(varType);
+ // Call omp_alloc_shared to allocate memory for groupprivate variable.
+ llvm::FunctionCallee allocSharedFn = ompBuilder->getOrCreateRuntimeFunction(
+ *llvmModule, llvm::omp::OMPRTL___kmpc_alloc_shared);
+ // Call runtime to allocate shared memory for this group
+ resultPtr = builder.CreateCall(allocSharedFn, {builder.getInt64(typeSize)});
+ } else {
+ // Use original global address when not allocating group-private storage
+ resultPtr = moduleTranslation.lookupValue(symAddr);
+ if (!resultPtr)
+ resultPtr = globalValue;
+ }
+
+ llvm::Type *ptrTy = builder.getPtrTy();
+ if (resultPtr->getType() != ptrTy)
+ resultPtr = builder.CreateBitCast(resultPtr, ptrTy);
----------------
ergawy wrote:
Just for my understanding, why do we need the bitcast here?
https://github.com/llvm/llvm-project/pull/162704
More information about the Mlir-commits
mailing list