aboutsummaryrefslogtreecommitdiff
path: root/flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp
diff options
context:
space:
mode:
authorTom Eccles <tom.eccles@arm.com>2025-01-31 09:35:26 +0000
committerGitHub <noreply@github.com>2025-01-31 09:35:26 +0000
commitaeaafce4644788963d9823067e46b50266af4f05 (patch)
treedff5c7fa73a26ae956c56869e5317422d6b00e3a /flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp
parent2bffa5bf7abd4fc7b84f14d029a97c49f4b61130 (diff)
downloadllvm-aeaafce4644788963d9823067e46b50266af4f05.zip
llvm-aeaafce4644788963d9823067e46b50266af4f05.tar.gz
llvm-aeaafce4644788963d9823067e46b50266af4f05.tar.bz2
[mlir][OpenMP][flang] make private variable allocation implicit in omp.private (#124019)
The intention of this work is to give MLIR->LLVMIR conversion freedom to control how the private variable is allocated so that it can be allocated on the stack in ordinary cases or as part of a structure used to give closure context for tasks which might outlive the current stack frame. See RFC: https://discourse.llvm.org/t/rfc-openmp-supporting-delayed-task-execution-with-firstprivate-variables/83084 For example, a privatizer for an integer used to look like ```mlir omp.private {type = private} @x.privatizer : !fir.ref<i32> alloc { ^bb0(%arg0: !fir.ref<i32>): %0 = ... allocate proper memory for the private clone ... omp.yield(%0 : !fir.ref<i32>) } ``` After this change, allocation become implicit in the operation: ```mlir omp.private {type = private} @x.privatizer : i32 ``` For more complex types that require initialization after allocation, an init region can be used: ``` mlir omp.private {type = private} @x.privatizer : !some.type init { ^bb0(%arg0: !some.pointer<!some.type>, %arg1: !some.pointer<!some.type>): // initialize %arg1, using %arg0 as a mold for allocations omp.yield(%arg1 : !some.pointer<!some.type>) } dealloc { ^bb0(%arg0: !some.pointer<!some.type>): ... deallocate memory allocated by the init region ... omp.yield } ``` This patch lays the groundwork for delayed task execution but is not enough on its own. After this patch all gfortran tests which previously passed still pass. There are the following changes to the Fujitsu test suite: - 0380_0009 and 0435_0009 are fixed - 0688_0041 now fails at runtime. This patch is testing firstprivate variables with tasks. Previously we got lucky with the undefined behavior and won the race. After these changes we no longer get lucky. This patch lays the groundwork for a proper fix for this issue. In flang the lowering re-uses the existing lowering used for reduction init and dealloc regions. In flang, before this patch we hit a TODO with the same wording when generating the copy region for firstprivate polymorphic variables. After this patch the box-like fir.class is passed by reference into the copy region, leading to a different path that didn't hit that old TODO but the generated code still didn't work so I added a new TODO in DataSharingProcessor.
Diffstat (limited to 'flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp')
-rw-r--r--flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp b/flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp
index da13ed6..37f1c9f 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGenOpenMP.cpp
@@ -90,9 +90,45 @@ struct MapInfoOpConversion
return mlir::success();
}
};
+
+// FIR op specific conversion for PrivateClauseOp that overwrites the default
+// OpenMP Dialect lowering, this allows FIR-aware lowering of types, required
+// for boxes because the OpenMP dialect conversion doesn't know anything about
+// FIR types.
+struct PrivateClauseOpConversion
+ : public OpenMPFIROpConversion<mlir::omp::PrivateClauseOp> {
+ using OpenMPFIROpConversion::OpenMPFIROpConversion;
+
+ llvm::LogicalResult
+ matchAndRewrite(mlir::omp::PrivateClauseOp curOp, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const override {
+ const fir::LLVMTypeConverter &converter = lowerTy();
+ mlir::Type convertedAllocType;
+ if (auto box = mlir::dyn_cast<fir::BaseBoxType>(curOp.getType())) {
+ // In LLVM codegen fir.box<> == fir.ref<fir.box<>> == llvm.ptr
+ // Here we really do want the actual structure
+ if (box.isAssumedRank())
+ TODO(curOp->getLoc(), "Privatize an assumed rank array");
+ unsigned rank = 0;
+ if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(
+ fir::unwrapRefType(box.getEleTy())))
+ rank = seqTy.getShape().size();
+ convertedAllocType = converter.convertBoxTypeAsStruct(box, rank);
+ } else {
+ convertedAllocType = converter.convertType(adaptor.getType());
+ }
+ if (!convertedAllocType)
+ return mlir::failure();
+ rewriter.startOpModification(curOp);
+ curOp.setType(convertedAllocType);
+ rewriter.finalizeOpModification(curOp);
+ return mlir::success();
+ }
+};
} // namespace
void fir::populateOpenMPFIRToLLVMConversionPatterns(
const LLVMTypeConverter &converter, mlir::RewritePatternSet &patterns) {
patterns.add<MapInfoOpConversion>(converter);
+ patterns.add<PrivateClauseOpConversion>(converter);
}