llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[OpenMP][Flang] LowerWorkDistribute emits 'omp.target_data', triggering translation error

Open mooxiu opened this issue 9 months ago • 7 comments

Summary

In OpenMPToLLVMIRTranslation, operations like omp.target_data are supposed to have already been lowered and not included. (related PR: https://github.com/llvm/llvm-project/pull/137201)

However, in LowerWorkdistribute, omp.target_data operation could be created again, the above assertion would fail.

Reproduce:

program test
  implicit none

  integer, parameter :: N = 42
  double precision, dimension(:), allocatable :: x

  allocate(x(N))
  x = 0

  !$omp target data map(tofrom:x)
  !$omp target teams workdistribute
  x = x
  !$omp end target teams workdistribute
  !$omp end target data

  deallocate(x)
end program test

Error Message

warning: OpenMP support for version 60 in flang is still incomplete
warning: OpenMP support for version 60 in flang is still incomplete
loc("{FilePath}.f90":11:9): error: 'omp.target_data' op unsupported host op found in device
loc("{FilePath}.f90":11:9): error: LLVM Translation failed for operation: omp.target_data
error: failed to create the LLVM module

Environment

LLVM Version: Latest main (commit: f0a4a91f1b49d77fe28d71363eed02dd8b9003e2)
Target: NVPTX / OpenMP Offloading
Host: x86 Linux

mooxiu avatar Feb 09 '26 10:02 mooxiu

Except for following 2 conditions check, there are no other logic conflict with it (commented them and can compile and run).

  • https://github.com/llvm/llvm-project/blob/964651ad515b61df56ce51cdc9978b2bcc8d0a7b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp#L7094-L7098
  • https://github.com/llvm/llvm-project/blob/964651ad515b61df56ce51cdc9978b2bcc8d0a7b/mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp#L5488-L5489

mooxiu avatar Feb 09 '26 11:02 mooxiu

@llvm/issue-subscribers-flang-ir

Author: Muyao Xiao (mooxiu)

## Summary

In OpenMPToLLVMIRTranslation, operations like omp.target_data are supposed to have already been lowered and not included. (related PR: https://github.com/llvm/llvm-project/pull/137201)

However, in LowerWorkdistribute, omp.target_data operation could be created again, the above assertion would fail.

Reproduce:

program test
  implicit none

  integer, parameter :: N = 42
  double precision, dimension(:), allocatable :: x

  allocate(x(N))
  x = 0

  !$omp target data map(tofrom:x)
  !$omp target teams workdistribute
  x = x
  !$omp end target teams workdistribute
  !$omp end target data

  deallocate(x)
end program test

Error Message

warning: OpenMP support for version 60 in flang is still incomplete
warning: OpenMP support for version 60 in flang is still incomplete
loc("{FilePath}.f90":11:9): error: 'omp.target_data' op unsupported host op found in device
loc("{FilePath}.f90":11:9): error: LLVM Translation failed for operation: omp.target_data
error: failed to create the LLVM module

Environment

LLVM Version: Latest main (commit: f0a4a91f1b49d77fe28d71363eed02dd8b9003e2)
Target: NVPTX / OpenMP Offloading
Host: x86 Linux

llvmbot avatar Feb 09 '26 14:02 llvmbot

Thank you @mooxiu for bringing attention to this problem. We could, as you suggest, stop checking for unexpected host ops when translating target device OpenMP MLIR modules. However, the whole point of the early filtering was to ensure we didn't get any such operations because lowering them, though generally fine (the generated LLVM IR for these would get removed as long as it remained in the same function), sometimes can interfere and cause compilation issues (e.g. if they create new functions).

In my opinion, we can either:

  1. Fix the ordering between these passes so that the FunctionFilteringPass runs after LowerWorkdistribute.
  2. Split function filtering and host op filtering into separate passes and only run the second after LowerWorkdistribute.
  3. Make changes to LowerWorkdistribute so that it takes into account whether it's doing codegen for host or device and skip creating host-only ops for a target device.

I've tracked the call chain to get to createLowerWorkdistribute and createFunctionFilteringPass, and indeed the latter runs first (see the calls to beginSourceFile and FrontendAction::execute here). Here are the call chains:

  • FrontendAction::beginSourceFile -> CodeGenAction::beginSourceFileAction -> createOpenMPFIRPassPipeline -> createFunctionFilteringPass.
  • FrontendAction::execute -> CodeGenAction::executeAction -> CodeGenAction::lowerHLFIRToFIR -> createHLFIRToFIRPassPipeline -> createLowerWorkdistribute

If LowerWorkdistribute doesn't have to run that late, maybe a simple solution would be to move it before FunctionFiltering, but I don't know whether it has a dependency on some particular pass(es) to run earlier. @skc7 might be able to comment on the feasibility of that. It might also be possible to move FunctionFiltering later, but at this stage perhaps other passes are already built expecting that one to have completed, so it might cause problems. And I'd generally try avoiding option 3, since doing that kind of thing might increase the complexity of passes over time.

So, if moving LowerWorkdistribute to run significantly earlier is possible, that would be my first choice. Otherwise, option 2 above makes more sense to me than dealing with a much later FunctionFiltering or updating the logic in LowerWorkdistribute. Let me know if you (or anyone else) have any thoughts on that.

skatrak avatar Feb 09 '26 15:02 skatrak

Hi @skatrak thanks for your investigating and suggestions!

By saying commented out those checks I was just trying to provide some diagnostic information, I acknowledge those checks are necessary and serve a purpose.

I agree your solutions sound structurally clearer. I am happy to take a look at the feasibility of moving LowerWorkDistribute earlier (Option 1). However, since I am relatively new to the Flang pass pipeline architecture, I'm not sure yet if there are hard dependencies preventing this. I will investigate a bit.

mooxiu avatar Feb 10 '26 01:02 mooxiu

Hi @mooxiu . Apologies, I missed this issue when it was filed.

LowerWorkdistribute operates on fir.do_loop unordered ops that are only produced by HLFIR lowering passes (BufferizeHLFIR, ConvertHLFIRToFIR) which run in CreateHLFIRToFIRPipeline. FunctionFilteringPass executes before any HLFIR lowering.

Moving LowerWorkdistribute prior to FunctionFilteringPass would mean the FIR ops it needs don't exist yet. So, option 1 might not be feasible in the current state of LowerWorkdistribute pass.

Thanks @skatrak for detailed analysis and suggestions. Option 2 looks like approach that can be pursued to fix this.

skc7 avatar Feb 24 '26 05:02 skc7

Thank you @skc7 for sharing details on LowerWorkdistribute, it does look like splitting function filtering from host op filtering and delaying the latter makes the most sense.

At the moment, that part of the pass is quite dependent on how things are represented in HLFIR, so it will take some tweaking to get it to properly work in a lower level representation. I'll add it to my TODO list, but it'll take me a bit of time to get to it, so anyone else is welcome to give it a try if they are able to do so earlier. Just post a message here if you start working on it to avoid duplicated work.

skatrak avatar Feb 24 '26 14:02 skatrak

I'm currently taking a look at this, will post a PR or an update here once I make significant progress.

skatrak avatar Mar 04 '26 14:03 skatrak