Skip to content

Commit 8269c40

Browse files
authored
[mlir][OpenMP][NFC] delayed privatisation cleanup (#115298)
Upstreaming some code cleanups ahead of supporting delayed task execution. - Make allocatePrivateVars not need to be a template (it will need to operate separately on firstprivate and private variables for delayed task execution so it can't index into lists of all variables in the operation). - Use llvm::SmallVectorImpl for function arguments - collectPrivatizationDecls already reserves size for privateDecls so we don't need to do that in callers - Use llvm::zip_equal instead of C-style array indexing
1 parent b358f21 commit 8269c40

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,13 +1254,13 @@ static LogicalResult allocAndInitializeReductionVars(
12541254
/// Allocate delayed private variables. Returns the basic block which comes
12551255
/// after all of these allocations. llvm::Value * for each of these private
12561256
/// variables are populated in llvmPrivateVars.
1257-
template <class OP>
12581257
static llvm::Expected<llvm::BasicBlock *>
1259-
allocatePrivateVars(OP opInst, llvm::IRBuilderBase &builder,
1258+
allocatePrivateVars(llvm::IRBuilderBase &builder,
12601259
LLVM::ModuleTranslation &moduleTranslation,
12611260
MutableArrayRef<BlockArgument> privateBlockArgs,
12621261
MutableArrayRef<omp::PrivateClauseOp> privateDecls,
1263-
llvm::SmallVector<llvm::Value *> &llvmPrivateVars,
1262+
MutableArrayRef<mlir::Value> mlirPrivateVars,
1263+
llvm::SmallVectorImpl<llvm::Value *> &llvmPrivateVars,
12641264
const llvm::OpenMPIRBuilder::InsertPointTy &allocaIP) {
12651265
// Allocate private vars
12661266
llvm::BranchInst *allocaTerminator =
@@ -1285,19 +1285,18 @@ allocatePrivateVars(OP opInst, llvm::IRBuilderBase &builder,
12851285
llvm::BasicBlock *privAllocBlock = nullptr;
12861286
if (!privateBlockArgs.empty())
12871287
privAllocBlock = splitBB(builder, true, "omp.private.latealloc");
1288-
for (unsigned i = 0; i < privateBlockArgs.size(); ++i) {
1289-
Region &allocRegion = privateDecls[i].getAllocRegion();
1288+
for (auto [privDecl, mlirPrivVar, blockArg] :
1289+
llvm::zip_equal(privateDecls, mlirPrivateVars, privateBlockArgs)) {
1290+
Region &allocRegion = privDecl.getAllocRegion();
12901291

12911292
// map allocation region block argument
1292-
llvm::Value *nonPrivateVar =
1293-
moduleTranslation.lookupValue(opInst.getPrivateVars()[i]);
1293+
llvm::Value *nonPrivateVar = moduleTranslation.lookupValue(mlirPrivVar);
12941294
assert(nonPrivateVar);
1295-
moduleTranslation.mapValue(privateDecls[i].getAllocMoldArg(),
1296-
nonPrivateVar);
1295+
moduleTranslation.mapValue(privDecl.getAllocMoldArg(), nonPrivateVar);
12971296

12981297
// in-place convert the private allocation region
12991298
SmallVector<llvm::Value *, 1> phis;
1300-
if (privateDecls[i].getAllocMoldArg().getUses().empty()) {
1299+
if (privDecl.getAllocMoldArg().getUses().empty()) {
13011300
// TODO this should use
13021301
// allocaIP.getBlock()->getFirstNonPHIOrDbgOrAlloca() so it goes before
13031302
// the code for fetching the thread id. Not doing this for now to avoid
@@ -1313,7 +1312,7 @@ allocatePrivateVars(OP opInst, llvm::IRBuilderBase &builder,
13131312

13141313
assert(phis.size() == 1 && "expected one allocation to be yielded");
13151314

1316-
moduleTranslation.mapValue(privateBlockArgs[i], phis[0]);
1315+
moduleTranslation.mapValue(blockArg, phis[0]);
13171316
llvmPrivateVars.push_back(phis[0]);
13181317

13191318
// clear alloc region block argument mapping in case it needs to be
@@ -1561,11 +1560,14 @@ convertOmpTaskOp(omp::TaskOp taskOp, llvm::IRBuilderBase &builder,
15611560
// Collect delayed privatisation declarations
15621561
MutableArrayRef<BlockArgument> privateBlockArgs =
15631562
cast<omp::BlockArgOpenMPOpInterface>(*taskOp).getPrivateBlockArgs();
1563+
SmallVector<mlir::Value> mlirPrivateVars;
15641564
SmallVector<llvm::Value *> llvmPrivateVars;
15651565
SmallVector<omp::PrivateClauseOp> privateDecls;
1566+
mlirPrivateVars.reserve(privateBlockArgs.size());
15661567
llvmPrivateVars.reserve(privateBlockArgs.size());
1567-
privateDecls.reserve(privateBlockArgs.size());
15681568
collectPrivatizationDecls(taskOp, privateDecls);
1569+
for (mlir::Value privateVar : taskOp.getPrivateVars())
1570+
mlirPrivateVars.push_back(privateVar);
15691571

15701572
auto bodyCB = [&](InsertPointTy allocaIP,
15711573
InsertPointTy codegenIP) -> llvm::Error {
@@ -1575,8 +1577,8 @@ convertOmpTaskOp(omp::TaskOp taskOp, llvm::IRBuilderBase &builder,
15751577
moduleTranslation, allocaIP);
15761578

15771579
llvm::Expected<llvm::BasicBlock *> afterAllocas = allocatePrivateVars(
1578-
taskOp, builder, moduleTranslation, privateBlockArgs, privateDecls,
1579-
llvmPrivateVars, allocaIP);
1580+
builder, moduleTranslation, privateBlockArgs, privateDecls,
1581+
mlirPrivateVars, llvmPrivateVars, allocaIP);
15801582
if (handleError(afterAllocas, *taskOp).failed())
15811583
return llvm::make_error<PreviouslyReportedError>();
15821584

@@ -1879,11 +1881,14 @@ convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder,
18791881
// Collect delayed privatization declarations
18801882
MutableArrayRef<BlockArgument> privateBlockArgs =
18811883
cast<omp::BlockArgOpenMPOpInterface>(*opInst).getPrivateBlockArgs();
1884+
SmallVector<mlir::Value> mlirPrivateVars;
18821885
SmallVector<llvm::Value *> llvmPrivateVars;
18831886
SmallVector<omp::PrivateClauseOp> privateDecls;
1887+
mlirPrivateVars.reserve(privateBlockArgs.size());
18841888
llvmPrivateVars.reserve(privateBlockArgs.size());
1885-
privateDecls.reserve(privateBlockArgs.size());
18861889
collectPrivatizationDecls(opInst, privateDecls);
1890+
for (mlir::Value privateVar : opInst.getPrivateVars())
1891+
mlirPrivateVars.push_back(privateVar);
18871892

18881893
// Collect reduction declarations
18891894
SmallVector<omp::DeclareReductionOp> reductionDecls;
@@ -1895,8 +1900,8 @@ convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder,
18951900
auto bodyGenCB = [&](InsertPointTy allocaIP,
18961901
InsertPointTy codeGenIP) -> llvm::Error {
18971902
llvm::Expected<llvm::BasicBlock *> afterAllocas = allocatePrivateVars(
1898-
opInst, builder, moduleTranslation, privateBlockArgs, privateDecls,
1899-
llvmPrivateVars, allocaIP);
1903+
builder, moduleTranslation, privateBlockArgs, privateDecls,
1904+
mlirPrivateVars, llvmPrivateVars, allocaIP);
19001905
if (handleError(afterAllocas, *opInst).failed())
19011906
return llvm::make_error<PreviouslyReportedError>();
19021907

0 commit comments

Comments
 (0)