llvm / llvm/llvm-project

[flang][safe-trampoline] Compiler fails with unimplemented `fir.emboxproc` codegen for procedure pointers

Open
#223,705 0 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

`-fsafe-trampoline` fixes the executable-stack trampoline failure for internal procedures on hardened macOS systems, but it causes Flang to fail compiling a valid program that declares and assigns an ordinary typed procedure pointer.

This prevents projects such as MAPL from enabling `-fsafe-trampoline` as a general workaround: MAPL contains normal procedure-pointer declarations that then fail during code generation, even in sources that do not pass internal procedures as callback arguments.

## Reproducer

The below code (named `flang_safe_trampoline_procedure_pointer_reproducer.F90` on disk) declares a module-level procedure pointer, then assigns a procedure to it. It does not require MPI, external libraries, or any nonstandard extensions.

```fortran
module safe_trampoline_procedure_pointer_reproducer_mod
implicit none

abstract interface
subroutine callback()
end subroutine callback
end interface

procedure(callback), pointer :: current_callback => null()

contains

subroutine set_callback(new_callback)
procedure(callback) :: new_callback

current_callback => new_callback
end subroutine set_callback

end module safe_trampoline_procedure_pointer_reproducer_mod

program flang_safe_trampoline_procedure_pointer_reproducer
use safe_trampoline_procedure_pointer_reproducer_mod, only: set_callback
implicit none

call set_callback(no_op)

contains

subroutine no_op()
end subroutine no_op

end program flang_safe_trampoline_procedure_pointer_reproducer
```

## Commands and results

The source compiles and runs normally without `-fsafe-trampoline`:

```sh
❯ flang-23 ./flang_safe_trampoline_procedure_pointer_reproducer.F90
❯ echo $?
0
```

Adding `-fsafe-trampoline` causes a compiler failure:

```sh
❯ flang-23 -fsafe-trampoline ./flang_safe_trampoline_procedure_pointer_reproducer.F90
loc("/Users/mathomp4/Flang-Reproducers/flang_safe_trampoline_procedure_pointer_reproducer.F90":9:36): error: flang/lib/Optimizer/CodeGen/CodeGen.cpp:2786: not yet implemented: fir.emboxproc codegen
LLVM ERROR: aborting
❯ echo $?
1
```

## Environment

- Homebrew `flang-23` 23.1.1
- Target: `arm64-apple-darwin25.6.0`
- macOS 26.x on Apple Silicon

## Context

This appears distinct from the internal-procedure callback runtime failure tracked by #182813. The W^X-compliant trampoline-pool support from #183108 fixes that runtime failure when `-fsafe-trampoline` is enabled.

However, the safe-trampoline compilation path currently does not support the ordinary legal procedure-pointer construct in this reproducer. As a result, the option cannot yet be applied broadly to a project that needs the runtime trampoline fix.

## Expected behavior

The source should compile successfully with `-fsafe-trampoline`, with the same behavior as compilation without the option.

## Disclosure

GPT-5.6 Terra assisted with preparing this issue text and the minimal reproducer. The commands and results above were run and verified by the reporter.

Contributor guide

Open the contributing guide

Research direction

Start with the reproducer flang_safe_trampoline_procedure_pointer_reproducer.F90 and run it with -fsafe-trampoline to reproduce the failure. Inspect flang/lib/Optimizer/CodeGen/CodeGen.cpp at the reported fir.emboxproc codegen location and trace the safe-trampoline procedure-pointer path. Done means the reproducer compiles successfully with the flag and behaves like the build without it.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.