Skip to content

Commit b886d11

Browse files
committed
[WIP][flang][OpenMP] Translate OpenMP scopes when compiling for target device
1 parent 6e7e46c commit b886d11

File tree

1 file changed

+53
-9
lines changed

1 file changed

+53
-9
lines changed

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

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,13 @@ static llvm::omp::ProcBindKind getProcBindKind(omp::ClauseProcBindKind kind) {
542542
llvm_unreachable("Unknown ClauseProcBindKind kind");
543543
}
544544

545+
static void forwardArgs(LLVM::ModuleTranslation &moduleTranslation,
546+
llvm::ArrayRef<BlockArgument> blockArgs,
547+
OperandRange operands) {
548+
for (auto [arg, var] : llvm::zip_equal(blockArgs, operands))
549+
moduleTranslation.mapValue(arg, moduleTranslation.lookupValue(var));
550+
}
551+
545552
/// Helper function to map block arguments defined by ignored loop wrappers to
546553
/// LLVM values and prevent any uses of those from triggering null pointer
547554
/// dereferences.
@@ -554,18 +561,12 @@ convertIgnoredWrapper(omp::LoopWrapperInterface opInst,
554561
// Map block arguments directly to the LLVM value associated to the
555562
// corresponding operand. This is semantically equivalent to this wrapper not
556563
// being present.
557-
auto forwardArgs =
558-
[&moduleTranslation](llvm::ArrayRef<BlockArgument> blockArgs,
559-
OperandRange operands) {
560-
for (auto [arg, var] : llvm::zip_equal(blockArgs, operands))
561-
moduleTranslation.mapValue(arg, moduleTranslation.lookupValue(var));
562-
};
563-
564564
return llvm::TypeSwitch<Operation *, LogicalResult>(opInst)
565565
.Case([&](omp::SimdOp op) {
566566
auto blockArgIface = cast<omp::BlockArgOpenMPOpInterface>(*op);
567-
forwardArgs(blockArgIface.getPrivateBlockArgs(), op.getPrivateVars());
568-
forwardArgs(blockArgIface.getReductionBlockArgs(),
567+
forwardArgs(moduleTranslation, blockArgIface.getPrivateBlockArgs(),
568+
op.getPrivateVars());
569+
forwardArgs(moduleTranslation, blockArgIface.getReductionBlockArgs(),
569570
op.getReductionVars());
570571
op.emitWarning() << "simd information on composite construct discarded";
571572
return success();
@@ -5255,6 +5256,49 @@ convertTargetOpsInNest(Operation *op, llvm::IRBuilderBase &builder,
52555256
return WalkResult::interrupt();
52565257
return WalkResult::skip();
52575258
}
5259+
5260+
// Non-target ops might nest target-related ops, therefore, we
5261+
// translate them as non-OpenMP scopes. Translating them is needed by
5262+
// nested target-related ops since they might LLVM values defined in
5263+
// their parent non-target ops.
5264+
if (isa<omp::OpenMPDialect>(oper->getDialect()) &&
5265+
oper->getParentOfType<LLVM::LLVMFuncOp>() &&
5266+
!oper->getRegions().empty()) {
5267+
5268+
// TODO Handle other ops with entry block args.
5269+
if (auto wsloop = dyn_cast<omp::WsloopOp>(oper)) {
5270+
auto blockArgIface = cast<omp::BlockArgOpenMPOpInterface>(oper);
5271+
if (blockArgIface) {
5272+
forwardArgs(moduleTranslation,
5273+
blockArgIface.getPrivateBlockArgs(),
5274+
wsloop.getPrivateVars());
5275+
forwardArgs(moduleTranslation,
5276+
blockArgIface.getReductionBlockArgs(),
5277+
wsloop.getReductionVars());
5278+
}
5279+
}
5280+
5281+
if (auto loopNest = dyn_cast<omp::LoopNestOp>(oper)) {
5282+
for (auto iv : loopNest.getIVs()) {
5283+
// Create fake allocas just to maintain IR validity.
5284+
moduleTranslation.mapValue(
5285+
iv, builder.CreateAlloca(
5286+
moduleTranslation.convertType(iv.getType())));
5287+
}
5288+
}
5289+
5290+
for (Region &region : oper->getRegions()) {
5291+
auto result = convertOmpOpRegions(
5292+
region, oper->getName().getStringRef().str() + ".fake.region",
5293+
builder, moduleTranslation);
5294+
if (result.takeError())
5295+
return WalkResult::interrupt();
5296+
builder.SetInsertPoint(result.get(), result.get()->end());
5297+
}
5298+
5299+
return WalkResult::skip();
5300+
}
5301+
52585302
return WalkResult::advance();
52595303
}).wasInterrupted();
52605304
return failure(interrupted);

0 commit comments

Comments
 (0)