Skip to content

[flang][OpenMP] Skip runtime mapping with no offload targets #144534

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 5 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 30 additions & 0 deletions flang/test/Lower/ignore-target-data.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
!RUN: %flang_fc1 -emit-llvm -fopenmp %s -o - | FileCheck %s --check-prefix=NORT
!RUN: %flang_fc1 -emit-llvm -fopenmp %s -o - | FileCheck %s --check-prefix=LLVM

!Make sure that there are no calls to the mapper.
!NORT-NOT: call{{.*}}__tgt_target_data_begin_mapper
!NORT-NOT: call{{.*}}__tgt_target_data_end_mapper

!Make sure we generate the body
!LLVM: define internal void @_QFPf(ptr %[[A0:[0-9]+]], ptr %[[A1:[0-9]+]]) {
!LLVM: %[[V0:[0-9]+]] = load i32, ptr %[[A0]], align 4
!LLVM: %[[V1:[0-9]+]] = load i32, ptr %[[A1]], align 4
!LLVM: %[[V2:[0-9]+]] = add i32 %[[V0]], %[[V1]]
!LLVM: store i32 %[[V2]], ptr %[[A0]], align 4
!LLVM: ret void
!LLVM: }


program test

call f(1, 2)

contains

subroutine f(x, y)
integer :: x, y
!$omp target data map(tofrom: x, y)
x = x + y
!$omp end target data
end subroutine
end
Original file line number Diff line number Diff line change
Expand Up @@ -4378,6 +4378,9 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
llvm::OpenMPIRBuilder *ompBuilder = moduleTranslation.getOpenMPBuilder();
llvm::OpenMPIRBuilder::TargetDataInfo info(/*RequiresDevicePointerInfo=*/true,
/*SeparateBeginEndCalls=*/true);
bool isTargetDevice = ompBuilder->Config.isTargetDevice();
bool isOffloadEntry =
isTargetDevice || !ompBuilder->Config.TargetTriples.empty();

LogicalResult result =
llvm::TypeSwitch<Operation *, LogicalResult>(op)
Expand All @@ -4402,6 +4405,8 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
.Case([&](omp::TargetEnterDataOp enterDataOp) -> LogicalResult {
if (failed(checkImplementationStatus(*enterDataOp)))
return failure();
if (!isOffloadEntry)
return success();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think it would be more readable if we checked this condition and exited early before this TypeSwitch, rather than checking on each case and then later after it was executed.

Also, shouldn't we still process the body of omp.target_data? That code still should run on the host, AFAIU.

if (!isOffloadEntry && !isa<omp::TargetDataOp>(op))
  return success();

LogicalResult result = llvm::TypeSwitch...;

if (failed(result))
  return failure();

using InsertPointTy = ...


if (auto ifVar = enterDataOp.getIfExpr())
ifCond = moduleTranslation.lookupValue(ifVar);
Expand All @@ -4422,6 +4427,8 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
.Case([&](omp::TargetExitDataOp exitDataOp) -> LogicalResult {
if (failed(checkImplementationStatus(*exitDataOp)))
return failure();
if (!isOffloadEntry)
return success();

if (auto ifVar = exitDataOp.getIfExpr())
ifCond = moduleTranslation.lookupValue(ifVar);
Expand All @@ -4442,6 +4449,8 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
.Case([&](omp::TargetUpdateOp updateDataOp) -> LogicalResult {
if (failed(checkImplementationStatus(*updateDataOp)))
return failure();
if (!isOffloadEntry)
return success();

if (auto ifVar = updateDataOp.getIfExpr())
ifCond = moduleTranslation.lookupValue(ifVar);
Expand All @@ -4467,6 +4476,10 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,

if (failed(result))
return failure();
if (!isOffloadEntry) {
// Pretend we have IF(false) if we're not doing offload.
ifCond = builder.getFalse();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just reduce the changes to this? At the moment, the isOffloadEntry condition is checked in multiple places, which adds complexity, but overriding the if condition would be enough to get the wanted behavior (let me know if I'm missing anything there). In my opinion, overhead shouldn't be a concern, since the early returns above only skip a few constant time cheap operations.

If overhead is still a concern for you, I think the other suggestion I made would achieve that while keeping this logic contained and make things easier to follow.

Suggested change
if (!isOffloadEntry) {
// Pretend we have IF(false) if we're not doing offload.
ifCond = builder.getFalse();
}
// Pretend we have IF(false) if we're not doing offload.
if (!isOffloadEntry)
ifCond = builder.getFalse();


using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
MapInfoData mapData;
Expand Down
Loading
Loading