diff --git a/flang/test/Lower/OpenMP/target-data-if-false.f90 b/flang/test/Lower/OpenMP/target-data-if-false.f90 new file mode 100644 index 0000000000000..ac0f675ddc806 --- /dev/null +++ b/flang/test/Lower/OpenMP/target-data-if-false.f90 @@ -0,0 +1,25 @@ +!RUN: %flang_fc1 -emit-llvm -fopenmp %openmp_flags -fopenmp-version=52 %s -o - | FileCheck %s + +!Check that this doesn't crash. + +!CHECK-LABEL: define void @f00_() +!CHECK: call i1 @_FortranAioOutputDerivedType + +subroutine f00 + use iso_c_binding + type(c_ptr) :: x + +!$omp target data use_device_ptr(x) if(.false.) + print *, x +!$omp end target data +end + +!CHECK-LABEL: define void @f01_() +!CHECK: call i1 @_FortranAioOutputInteger32 +subroutine f01 + integer :: x + +!$omp target data use_device_addr(x) if(.false.) + print *, x +!$omp end target data +end diff --git a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp index 90ce06a0345c0..37e9f3816407a 100644 --- a/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp @@ -4518,6 +4518,8 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder, using BodyGenTy = llvm::OpenMPIRBuilder::BodyGenTy; auto bodyGenCB = [&](InsertPointTy codeGenIP, BodyGenTy bodyGenType) -> llvm::OpenMPIRBuilder::InsertPointOrErrorTy { + // We must always restoreIP regardless of doing anything the caller + // does not restore it, leading to incorrect (no) branch generation. builder.restoreIP(codeGenIP); assert(isa(op) && "BodyGen requested for non TargetDataOp"); @@ -4549,9 +4551,18 @@ convertOmpTargetData(Operation *op, llvm::IRBuilderBase &builder, } break; case BodyGenTy::DupNoPriv: - // We must always restoreIP regardless of doing anything the caller - // does not restore it, leading to incorrect (no) branch generation. - builder.restoreIP(codeGenIP); + if (info.DevicePtrInfoMap.empty()) { + // For host device we still need to do the mapping for codegen, + // otherwise it may try to lookup a missing value. + if (!ompBuilder->Config.IsTargetDevice.value_or(false)) { + mapUseDevice(llvm::OpenMPIRBuilder::DeviceInfoTy::Address, + blockArgIface.getUseDeviceAddrBlockArgs(), + useDeviceAddrVars, mapData); + mapUseDevice(llvm::OpenMPIRBuilder::DeviceInfoTy::Pointer, + blockArgIface.getUseDevicePtrBlockArgs(), + useDevicePtrVars, mapData); + } + } break; case BodyGenTy::NoPriv: // If device info is available then region has already been generated