Skip to content

Commit 8bb2787

Browse files
tblahergawy
authored andcommitted
[mlir][OpenMP][NFC] break out priv var init into helper (llvm#125303)
1 parent 688ec60 commit 8bb2787

File tree

1 file changed

+52
-34
lines changed

1 file changed

+52
-34
lines changed

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

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,6 +1512,53 @@ findAssociatedValue(Value privateVar, llvm::IRBuilderBase &builder,
15121512
return moduleTranslation.lookupValue(privateVar);
15131513
}
15141514

1515+
/// Initialize a single (first)private variable. You probably want to use
1516+
/// allocateAndInitPrivateVars instead of this.
1517+
static llvm::Error
1518+
initPrivateVar(llvm::IRBuilderBase &builder,
1519+
LLVM::ModuleTranslation &moduleTranslation,
1520+
omp::PrivateClauseOp &privDecl, Value mlirPrivVar,
1521+
BlockArgument &blockArg, llvm::Value *llvmPrivateVar,
1522+
llvm::SmallVectorImpl<llvm::Value *> &llvmPrivateVars,
1523+
llvm::BasicBlock *privInitBlock,
1524+
llvm::DenseMap<Value, Value> *mappedPrivateVars = nullptr) {
1525+
Region &initRegion = privDecl.getInitRegion();
1526+
if (initRegion.empty()) {
1527+
moduleTranslation.mapValue(blockArg, llvmPrivateVar);
1528+
llvmPrivateVars.push_back(llvmPrivateVar);
1529+
return llvm::Error::success();
1530+
}
1531+
1532+
// map initialization region block arguments
1533+
llvm::Value *nonPrivateVar = findAssociatedValue(
1534+
mlirPrivVar, builder, moduleTranslation, mappedPrivateVars);
1535+
assert(nonPrivateVar);
1536+
moduleTranslation.mapValue(privDecl.getInitMoldArg(), nonPrivateVar);
1537+
moduleTranslation.mapValue(privDecl.getInitPrivateArg(), llvmPrivateVar);
1538+
1539+
// in-place convert the private initialization region
1540+
SmallVector<llvm::Value *, 1> phis;
1541+
builder.SetInsertPoint(privInitBlock->getTerminator());
1542+
if (failed(inlineConvertOmpRegions(initRegion, "omp.private.init", builder,
1543+
moduleTranslation, &phis)))
1544+
return llvm::createStringError(
1545+
"failed to inline `init` region of `omp.private`");
1546+
1547+
assert(phis.size() == 1 && "expected one allocation to be yielded");
1548+
1549+
// prefer the value yielded from the init region to the allocated private
1550+
// variable in case the region is operating on arguments by-value (e.g.
1551+
// Fortran character boxes).
1552+
moduleTranslation.mapValue(blockArg, phis[0]);
1553+
llvmPrivateVars.push_back(phis[0]);
1554+
1555+
// clear init region block argument mapping in case it needs to be
1556+
// re-created with a different source for another use of the same
1557+
// reduction decl
1558+
moduleTranslation.forgetMapping(initRegion);
1559+
return llvm::Error::success();
1560+
}
1561+
15151562
/// Allocate and initialize delayed private variables. Returns the basic block
15161563
/// which comes after all of these allocations. llvm::Value * for each of these
15171564
/// private variables are populated in llvmPrivateVars.
@@ -1552,40 +1599,11 @@ static llvm::Expected<llvm::BasicBlock *> allocateAndInitPrivateVars(
15521599
llvm::Value *llvmPrivateVar = builder.CreateAlloca(
15531600
llvmAllocType, /*ArraySize=*/nullptr, "omp.private.alloc");
15541601

1555-
Region &initRegion = privDecl.getInitRegion();
1556-
if (initRegion.empty()) {
1557-
moduleTranslation.mapValue(blockArg, llvmPrivateVar);
1558-
llvmPrivateVars.push_back(llvmPrivateVar);
1559-
continue;
1560-
}
1561-
1562-
// map initialization region block arguments
1563-
llvm::Value *nonPrivateVar = findAssociatedValue(
1564-
mlirPrivVar, builder, moduleTranslation, mappedPrivateVars);
1565-
assert(nonPrivateVar);
1566-
moduleTranslation.mapValue(privDecl.getInitMoldArg(), nonPrivateVar);
1567-
moduleTranslation.mapValue(privDecl.getInitPrivateArg(), llvmPrivateVar);
1568-
1569-
// in-place convert the private initialization region
1570-
SmallVector<llvm::Value *, 1> phis;
1571-
builder.SetInsertPoint(privInitBlock->getTerminator());
1572-
if (failed(inlineConvertOmpRegions(initRegion, "omp.private.init", builder,
1573-
moduleTranslation, &phis)))
1574-
return llvm::createStringError(
1575-
"failed to inline `init` region of `omp.private`");
1576-
1577-
assert(phis.size() == 1 && "expected one allocation to be yielded");
1578-
1579-
// prefer the value yielded from the init region to the allocated private
1580-
// variable in case the region is operating on arguments by-value (e.g.
1581-
// Fortran character boxes).
1582-
moduleTranslation.mapValue(blockArg, phis[0]);
1583-
llvmPrivateVars.push_back(phis[0]);
1584-
1585-
// clear init region block argument mapping in case it needs to be
1586-
// re-created with a different source for another use of the same
1587-
// reduction decl
1588-
moduleTranslation.forgetMapping(initRegion);
1602+
llvm::Error err = initPrivateVar(
1603+
builder, moduleTranslation, privDecl, mlirPrivVar, blockArg,
1604+
llvmPrivateVar, llvmPrivateVars, privInitBlock, mappedPrivateVars);
1605+
if (err)
1606+
return err;
15891607
}
15901608
return afterAllocas;
15911609
}

0 commit comments

Comments
 (0)