Skip to content

Commit 9ad4ebd

Browse files
authored
[mlir][OpenMP][NFC] break out priv var init into helper (#125303)
1 parent 5d738b2 commit 9ad4ebd

File tree

1 file changed

+52
-34
lines changed

1 file changed

+52
-34
lines changed

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

+52-34
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,53 @@ findAssociatedValue(Value privateVar, llvm::IRBuilderBase &builder,
13291329
return moduleTranslation.lookupValue(privateVar);
13301330
}
13311331

1332+
/// Initialize a single (first)private variable. You probably want to use
1333+
/// allocateAndInitPrivateVars instead of this.
1334+
static llvm::Error
1335+
initPrivateVar(llvm::IRBuilderBase &builder,
1336+
LLVM::ModuleTranslation &moduleTranslation,
1337+
omp::PrivateClauseOp &privDecl, Value mlirPrivVar,
1338+
BlockArgument &blockArg, llvm::Value *llvmPrivateVar,
1339+
llvm::SmallVectorImpl<llvm::Value *> &llvmPrivateVars,
1340+
llvm::BasicBlock *privInitBlock,
1341+
llvm::DenseMap<Value, Value> *mappedPrivateVars = nullptr) {
1342+
Region &initRegion = privDecl.getInitRegion();
1343+
if (initRegion.empty()) {
1344+
moduleTranslation.mapValue(blockArg, llvmPrivateVar);
1345+
llvmPrivateVars.push_back(llvmPrivateVar);
1346+
return llvm::Error::success();
1347+
}
1348+
1349+
// map initialization region block arguments
1350+
llvm::Value *nonPrivateVar = findAssociatedValue(
1351+
mlirPrivVar, builder, moduleTranslation, mappedPrivateVars);
1352+
assert(nonPrivateVar);
1353+
moduleTranslation.mapValue(privDecl.getInitMoldArg(), nonPrivateVar);
1354+
moduleTranslation.mapValue(privDecl.getInitPrivateArg(), llvmPrivateVar);
1355+
1356+
// in-place convert the private initialization region
1357+
SmallVector<llvm::Value *, 1> phis;
1358+
builder.SetInsertPoint(privInitBlock->getTerminator());
1359+
if (failed(inlineConvertOmpRegions(initRegion, "omp.private.init", builder,
1360+
moduleTranslation, &phis)))
1361+
return llvm::createStringError(
1362+
"failed to inline `init` region of `omp.private`");
1363+
1364+
assert(phis.size() == 1 && "expected one allocation to be yielded");
1365+
1366+
// prefer the value yielded from the init region to the allocated private
1367+
// variable in case the region is operating on arguments by-value (e.g.
1368+
// Fortran character boxes).
1369+
moduleTranslation.mapValue(blockArg, phis[0]);
1370+
llvmPrivateVars.push_back(phis[0]);
1371+
1372+
// clear init region block argument mapping in case it needs to be
1373+
// re-created with a different source for another use of the same
1374+
// reduction decl
1375+
moduleTranslation.forgetMapping(initRegion);
1376+
return llvm::Error::success();
1377+
}
1378+
13321379
/// Allocate and initialize delayed private variables. Returns the basic block
13331380
/// which comes after all of these allocations. llvm::Value * for each of these
13341381
/// private variables are populated in llvmPrivateVars.
@@ -1368,40 +1415,11 @@ static llvm::Expected<llvm::BasicBlock *> allocateAndInitPrivateVars(
13681415
llvm::Value *llvmPrivateVar = builder.CreateAlloca(
13691416
llvmAllocType, /*ArraySize=*/nullptr, "omp.private.alloc");
13701417

1371-
Region &initRegion = privDecl.getInitRegion();
1372-
if (initRegion.empty()) {
1373-
moduleTranslation.mapValue(blockArg, llvmPrivateVar);
1374-
llvmPrivateVars.push_back(llvmPrivateVar);
1375-
continue;
1376-
}
1377-
1378-
// map initialization region block arguments
1379-
llvm::Value *nonPrivateVar = findAssociatedValue(
1380-
mlirPrivVar, builder, moduleTranslation, mappedPrivateVars);
1381-
assert(nonPrivateVar);
1382-
moduleTranslation.mapValue(privDecl.getInitMoldArg(), nonPrivateVar);
1383-
moduleTranslation.mapValue(privDecl.getInitPrivateArg(), llvmPrivateVar);
1384-
1385-
// in-place convert the private initialization region
1386-
SmallVector<llvm::Value *, 1> phis;
1387-
builder.SetInsertPoint(privInitBlock->getTerminator());
1388-
if (failed(inlineConvertOmpRegions(initRegion, "omp.private.init", builder,
1389-
moduleTranslation, &phis)))
1390-
return llvm::createStringError(
1391-
"failed to inline `init` region of `omp.private`");
1392-
1393-
assert(phis.size() == 1 && "expected one allocation to be yielded");
1394-
1395-
// prefer the value yielded from the init region to the allocated private
1396-
// variable in case the region is operating on arguments by-value (e.g.
1397-
// Fortran character boxes).
1398-
moduleTranslation.mapValue(blockArg, phis[0]);
1399-
llvmPrivateVars.push_back(phis[0]);
1400-
1401-
// clear init region block argument mapping in case it needs to be
1402-
// re-created with a different source for another use of the same
1403-
// reduction decl
1404-
moduleTranslation.forgetMapping(initRegion);
1418+
llvm::Error err = initPrivateVar(
1419+
builder, moduleTranslation, privDecl, mlirPrivVar, blockArg,
1420+
llvmPrivateVar, llvmPrivateVars, privInitBlock, mappedPrivateVars);
1421+
if (err)
1422+
return err;
14051423
}
14061424
return afterAllocas;
14071425
}

0 commit comments

Comments
 (0)