Skip to content

Commit 3a71884

Browse files
authored
[flang][OpenMP] Map device pointers on host device as well (#145562)
Given a TARGET DATA construct with USE_DEVICE_PTR(x) and IF(FALSE), the compiler will crash if `x` was used in the body. The cause of the crash is that the MLIR->LLVM codegen tries to look up the translated value of x, but one had not been mapped. Given an IF clause, the translation will generate an if-then-else construct, with the "else" block corresponding to the false condition, i.e. the host device playing the role of the target device. In that block, still process the USE_DEVICE_ADDR/USE_DEVICE_PTR clauses, which will cause the translation mappings to be created. Fixes #145558
1 parent 3413aa8 commit 3a71884

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
!RUN: %flang_fc1 -emit-llvm -fopenmp %openmp_flags -fopenmp-version=52 %s -o - | FileCheck %s
2+
3+
!Check that this doesn't crash.
4+
5+
!CHECK-LABEL: define void @f00_()
6+
!CHECK: call i1 @_FortranAioOutputDerivedType
7+
8+
subroutine f00
9+
use iso_c_binding
10+
type(c_ptr) :: x
11+
12+
!$omp target data use_device_ptr(x) if(.false.)
13+
print *, x
14+
!$omp end target data
15+
end
16+
17+
!CHECK-LABEL: define void @f01_()
18+
!CHECK: call i1 @_FortranAioOutputInteger32
19+
subroutine f01
20+
integer :: x
21+
22+
!$omp target data use_device_addr(x) if(.false.)
23+
print *, x
24+
!$omp end target data
25+
end

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4518,6 +4518,8 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
45184518
using BodyGenTy = llvm::OpenMPIRBuilder::BodyGenTy;
45194519
auto bodyGenCB = [&](InsertPointTy codeGenIP, BodyGenTy bodyGenType)
45204520
-> llvm::OpenMPIRBuilder::InsertPointOrErrorTy {
4521+
// We must always restoreIP regardless of doing anything the caller
4522+
// does not restore it, leading to incorrect (no) branch generation.
45214523
builder.restoreIP(codeGenIP);
45224524
assert(isa<omp::TargetDataOp>(op) &&
45234525
"BodyGen requested for non TargetDataOp");
@@ -4549,9 +4551,18 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder,
45494551
}
45504552
break;
45514553
case BodyGenTy::DupNoPriv:
4552-
// We must always restoreIP regardless of doing anything the caller
4553-
// does not restore it, leading to incorrect (no) branch generation.
4554-
builder.restoreIP(codeGenIP);
4554+
if (info.DevicePtrInfoMap.empty()) {
4555+
// For host device we still need to do the mapping for codegen,
4556+
// otherwise it may try to lookup a missing value.
4557+
if (!ompBuilder->Config.IsTargetDevice.value_or(false)) {
4558+
mapUseDevice(llvm::OpenMPIRBuilder::DeviceInfoTy::Address,
4559+
blockArgIface.getUseDeviceAddrBlockArgs(),
4560+
useDeviceAddrVars, mapData);
4561+
mapUseDevice(llvm::OpenMPIRBuilder::DeviceInfoTy::Pointer,
4562+
blockArgIface.getUseDevicePtrBlockArgs(),
4563+
useDevicePtrVars, mapData);
4564+
}
4565+
}
45554566
break;
45564567
case BodyGenTy::NoPriv:
45574568
// If device info is available then region has already been generated

0 commit comments

Comments
 (0)