[flang] [llvm] [flang] Support FLUSH as an intrinsic subroutine (PR #165942)
Miguel Saldivar via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 16 22:10:09 PST 2025
Saldivarcher wrote:
> gfortran doesn't support it. classic-flang (armflang 24.04) does support it.
>
> llvm-flang with your patch does not prevent the usage of a dynamically optional argument. I built your branch and tried it and it generates
>
> ```
> func.func @_QPflush_unit(%arg0: !fir.ref<i32> {fir.bindc_name = "unit", fir.optional}) {
> %0 = fir.dummy_scope : !fir.dscope
> %1:2 = hlfir.declare %arg0 dummy_scope %0 {fortran_attrs = #fir.var_attrs<optional>, uniq_name = "_QFflush_unitEunit"} : (!fir.ref<i32>, !fir.dscope) -> (!fir.ref<i32>, !fir.ref<i32>)
> %2 = fir.is_present %1#0 : (!fir.ref<i32>) -> i1
> %3 = fir.if %2 -> (i32) {
> %4 = fir.load %1#0 : !fir.ref<i32>
> fir.result %4 : i32
> } else {
> %c0_i32 = arith.constant 0 : i32
> fir.result %c0_i32 : i32
> }
> fir.call @_FortranAFlush(%3) fastmath<contract> : (i32) -> ()
> return
> }
> func.func private @_FortranAFlush(i32) attributes {fir.runtime}
> ```
>
> From this we can see that whilst the dynamic optional is handled, it defaults to 0. I think you will need -1 for how you defined flush.
>
> I don't mind if you choose to support this or not. If you do support it, you need to make sure the default value is -1 and have a test for the lowering of a dynamically optional argument. If you choose not to support this, please make it clear in the documentation and add a test to make sure that this is correctly diagnosed by the compiler (if you just remove `handleDynamicOptional`, there could be segfaults at runtime because the program will unconditionally try to load the argument): e.g.
>
> ```
> func.func @_QPflush_unit(%arg0: !fir.ref<i32> {fir.bindc_name = "unit", fir.optional}) {
> %0 = fir.dummy_scope : !fir.dscope
> %1:2 = hlfir.declare %arg0 dummy_scope %0 {fortran_attrs = #fir.var_attrs<optional>, uniq_name = "_QFflush_unitEunit"} : (!fir.ref<i32>, !fir.dscope) -> (!fir.ref<i32>, !fir.ref<i32>)
> %2 = fir.load %1#0 : !fir.ref<i32>
> fir.call @_FortranAFlush(%2) fastmath<contract> : (i32) -> ()
> return
> }
> func.func private @_FortranAFlush(i32) attributes {fir.runtime}
> ```
Ended up adding support for it! I also added the testcase to the `flush.f90` test. Let me know how that looks.
Thanks for the review btw, I appreciate it :pray:
https://github.com/llvm/llvm-project/pull/165942
More information about the llvm-commits
mailing list