Skip to content

[MLIR][OpenMP] Extend omp.private materialization support: dealloc #90841

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

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1500,19 +1500,6 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel(
};
}

// Adjust the finalization stack, verify the adjustment, and call the
// finalize function a last time to finalize values between the pre-fini
// block and the exit block if we left the parallel "the normal way".
auto FiniInfo = FinalizationStack.pop_back_val();
(void)FiniInfo;
assert(FiniInfo.DK == OMPD_parallel &&
"Unexpected finalization stack state!");

Instruction *PRegPreFiniTI = PRegPreFiniBB->getTerminator();

InsertPointTy PreFiniIP(PRegPreFiniBB, PRegPreFiniTI->getIterator());
FiniCB(PreFiniIP);

OI.OuterAllocaBB = OuterAllocaBlock;
OI.EntryBB = PRegEntryBB;
OI.ExitBB = PRegExitBB;
Expand Down Expand Up @@ -1637,6 +1624,19 @@ IRBuilder<>::InsertPoint OpenMPIRBuilder::createParallel(
dbgs() << " PBR: " << BB->getName() << "\n";
});

// Adjust the finalization stack, verify the adjustment, and call the
// finalize function a last time to finalize values between the pre-fini
// block and the exit block if we left the parallel "the normal way".
auto FiniInfo = FinalizationStack.pop_back_val();
(void)FiniInfo;
assert(FiniInfo.DK == OMPD_parallel &&
"Unexpected finalization stack state!");

Instruction *PRegPreFiniTI = PRegPreFiniBB->getTerminator();

InsertPointTy PreFiniIP(PRegPreFiniBB, PRegPreFiniTI->getIterator());
FiniCB(PreFiniIP);

// Register the outlined info.
addOutlineInfo(std::move(OI));

Expand Down
79 changes: 59 additions & 20 deletions mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "llvm/Transforms/Utils/ModuleUtils.h"

#include <any>
#include <iterator>
#include <optional>
#include <utility>

Expand Down Expand Up @@ -878,36 +879,40 @@ static void collectReductionInfo(
}

/// handling of DeclareReductionOp's cleanup region
static LogicalResult inlineReductionCleanup(
llvm::SmallVectorImpl<omp::DeclareReductionOp> &reductionDecls,
llvm::ArrayRef<llvm::Value *> privateReductionVariables,
LLVM::ModuleTranslation &moduleTranslation, llvm::IRBuilderBase &builder) {
for (auto [i, reductionDecl] : llvm::enumerate(reductionDecls)) {
Region &cleanupRegion = reductionDecl.getCleanupRegion();
if (cleanupRegion.empty())
static LogicalResult
inlineOmpRegionCleanup(llvm::SmallVectorImpl<Region *> &cleanupRegions,
llvm::ArrayRef<llvm::Value *> privateVariables,
LLVM::ModuleTranslation &moduleTranslation,
llvm::IRBuilderBase &builder, StringRef regionName,
bool shouldLoadCleanupRegionArg = true) {
for (auto [i, cleanupRegion] : llvm::enumerate(cleanupRegions)) {
if (cleanupRegion->empty())
continue;

// map the argument to the cleanup region
Block &entry = cleanupRegion.front();
Block &entry = cleanupRegion->front();

llvm::Instruction *potentialTerminator =
builder.GetInsertBlock()->empty() ? nullptr
: &builder.GetInsertBlock()->back();
if (potentialTerminator && potentialTerminator->isTerminator())
builder.SetInsertPoint(potentialTerminator);
llvm::Value *reductionVar = builder.CreateLoad(
moduleTranslation.convertType(entry.getArgument(0).getType()),
privateReductionVariables[i]);
llvm::Value *prviateVarValue =
shouldLoadCleanupRegionArg
? builder.CreateLoad(
moduleTranslation.convertType(entry.getArgument(0).getType()),
privateVariables[i])
: privateVariables[i];

moduleTranslation.mapValue(entry.getArgument(0), reductionVar);
moduleTranslation.mapValue(entry.getArgument(0), prviateVarValue);

if (failed(inlineConvertOmpRegions(cleanupRegion, "omp.reduction.cleanup",
builder, moduleTranslation)))
if (failed(inlineConvertOmpRegions(*cleanupRegion, regionName, builder,
moduleTranslation)))
return failure();

// clear block argument mapping in case it needs to be re-created with a
// different source for another use of the same reduction decl
moduleTranslation.forgetMapping(cleanupRegion);
moduleTranslation.forgetMapping(*cleanupRegion);
}
return success();
}
Expand Down Expand Up @@ -1110,8 +1115,14 @@ convertOmpWsloop(Operation &opInst, llvm::IRBuilderBase &builder,
builder.restoreIP(nextInsertionPoint);

// after the workshare loop, deallocate private reduction variables
return inlineReductionCleanup(reductionDecls, privateReductionVariables,
moduleTranslation, builder);
SmallVector<Region *> reductionRegions;
llvm::transform(reductionDecls, std::back_inserter(reductionRegions),
[](omp::DeclareReductionOp reductionDecl) {
return &reductionDecl.getCleanupRegion();
});
return inlineOmpRegionCleanup(reductionRegions, privateReductionVariables,
moduleTranslation, builder,
"omp.reduction.cleanup");
}

/// A RAII class that on construction replaces the region arguments of the
Expand Down Expand Up @@ -1267,6 +1278,9 @@ convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder,
}
};

SmallVector<omp::PrivateClauseOp> privatizerClones;
SmallVector<llvm::Value *> privateVariables;

// TODO: Perform appropriate actions according to the data-sharing
// attribute (shared, private, firstprivate, ...) of variables.
// Currently shared and private are supported.
Expand Down Expand Up @@ -1356,12 +1370,17 @@ convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder,
opInst.emitError("failed to inline `alloc` region of an `omp.private` "
"op in the parallel region");
bodyGenStatus = failure();
privatizerClone.erase();
} else {
assert(yieldedValues.size() == 1);
replacementValue = yieldedValues.front();

// Keep the LLVM replacement value and the op clone in case we need to
// emit cleanup (i.e. deallocation) logic.
privateVariables.push_back(replacementValue);
privatizerClones.push_back(privatizerClone);
}

privatizerClone.erase();
builder.restoreIP(oldIP);
}

Expand All @@ -1376,8 +1395,25 @@ convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder,

// if the reduction has a cleanup region, inline it here to finalize the
// reduction variables
if (failed(inlineReductionCleanup(reductionDecls, privateReductionVariables,
moduleTranslation, builder)))
SmallVector<Region *> reductionCleanupRegions;
llvm::transform(reductionDecls, std::back_inserter(reductionCleanupRegions),
[](omp::DeclareReductionOp reductionDecl) {
return &reductionDecl.getCleanupRegion();
});
if (failed(inlineOmpRegionCleanup(
reductionCleanupRegions, privateReductionVariables,
moduleTranslation, builder, "omp.reduction.cleanup")))
bodyGenStatus = failure();

SmallVector<Region *> privateCleanupRegions;
llvm::transform(privatizerClones, std::back_inserter(privateCleanupRegions),
[](omp::PrivateClauseOp privatizer) {
return &privatizer.getDeallocRegion();
});

if (failed(inlineOmpRegionCleanup(
privateCleanupRegions, privateVariables, moduleTranslation, builder,
"omp.private.dealloc", /*shouldLoadCleanupRegionArg=*/false)))
bodyGenStatus = failure();

builder.restoreIP(oldIP);
Expand All @@ -1403,6 +1439,9 @@ convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder,
ompBuilder->createParallel(ompLoc, allocaIP, bodyGenCB, privCB, finiCB,
ifCond, numThreads, pbKind, isCancellable));

for (mlir::omp::PrivateClauseOp privatizerClone : privatizerClones)
privatizerClone.erase();

return bodyGenStatus;
}

Expand Down
53 changes: 53 additions & 0 deletions mlir/test/Target/LLVMIR/openmp-omp.private-dealloc.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// RUN: mlir-translate -mlir-to-llvmir -split-input-file %s | FileCheck %s

llvm.func @free(!llvm.ptr)

llvm.func @parallel_op_dealloc(%arg0: !llvm.ptr) {
omp.parallel private(@x.privatizer %arg0 -> %arg2 : !llvm.ptr) {
%0 = llvm.load %arg2 : !llvm.ptr -> f32
omp.terminator
}
llvm.return
}

omp.private {type = firstprivate} @x.privatizer : !llvm.ptr alloc {
^bb0(%arg0: !llvm.ptr):
%c1 = llvm.mlir.constant(1 : i32) : i32
%0 = llvm.alloca %c1 x f32 : (i32) -> !llvm.ptr
omp.yield(%0 : !llvm.ptr)
} copy {
^bb0(%arg0: !llvm.ptr, %arg1: !llvm.ptr):
%0 = llvm.load %arg0 : !llvm.ptr -> f32
llvm.store %0, %arg1 : f32, !llvm.ptr
omp.yield(%arg1 : !llvm.ptr)
} dealloc {
^bb0(%arg0: !llvm.ptr):
%0 = llvm.ptrtoint %arg0 : !llvm.ptr to i64
%c0 = llvm.mlir.constant(0 : i64) : i64
%1 = llvm.icmp "ne" %0, %c0 : i64
llvm.cond_br %1, ^bb1, ^bb2

^bb1:
llvm.call @free(%arg0) : (!llvm.ptr) -> ()
llvm.br ^bb2

^bb2:
omp.yield
}

// CHECK-LABEL: define internal void @parallel_op_dealloc..omp_par
// CHECK: %[[LOCAL_ALLOC:.*]] = alloca float, align 4

// CHECK: omp.par.pre_finalize:
// CHECK: br label %[[DEALLOC_REG_START:.*]]

// CHECK: [[DEALLOC_REG_START]]:
// CHECK: %[[LOCAL_ALLOC_CONV:.*]] = ptrtoint ptr %[[LOCAL_ALLOC]] to i64
// CHECK: %[[COND:.*]] = icmp ne i64 %[[LOCAL_ALLOC_CONV]], 0
// CHECK: br i1 %[[COND]], label %[[DEALLOC_REG_BB1:.*]], label %[[DEALLOC_REG_BB2:.*]]

// CHECK: [[DEALLOC_REG_BB2]]:

// CHECK: [[DEALLOC_REG_BB1]]:
// CHECK-NEXT: call void @free(ptr %[[LOCAL_ALLOC]])
// CHECK-NEXT: br label %[[DEALLOC_REG_BB2]]