[flang-commits] [flang] [Flang]: Lowering reference to functions that return a procedure pointer (PR #78194)

Daniel Chen via flang-commits flang-commits at lists.llvm.org
Sat Jan 27 08:35:21 PST 2024


================
@@ -1425,9 +1425,14 @@ class HlfirBuilder {
   }
 
   hlfir::EntityWithAttributes gen(const Fortran::evaluate::ProcedureRef &expr) {
-    TODO(
-        getLoc(),
-        "lowering function references that return procedure pointers to HLFIR");
+    fir::FirOpBuilder &builder = getBuilder();
+    Fortran::evaluate::ProcedureDesignator proc{expr.proc()};
+    auto procTy{Fortran::lower::translateSignature(proc, getConverter())};
+    mlir::Type resType = fir::BoxProcType::get(builder.getContext(), procTy);
----------------
DanielCChen wrote:

@jeanPerier Thanks for the review! Sorry for the delayed response. 
The `resType` as is here is `!fir.boxproc<(!fir.ref<i32>) -> !fir.boxproc<(!fir.ref<i32>) -> f32>>` for a function that returns a procedure pointer that has an interface of `real foo(integer)`
The test case I used for generating the code is
```
  interface
    real function func(x)
      integer :: x
    end
  end interface
  real :: res
  procedure(func), pointer :: arg0
  arg0=>reffunc(5)
contains
  function reffunc(arg1) result(pp)
    integer :: arg1
    procedure(func), pointer :: pp
    pp => func
  end
end
```

The FIR looks like:
```
...
%3:2 = hlfir.declare %0 {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QFEarg0"} : (!fir.ref<!fir.boxproc<(!fir.ref<i32>) -> f32>>) -> (!fir.ref<!fir.boxproc<(!fir.ref<i32>) -> f32>>, !fir.ref<!fir.boxproc<(!fir.ref<i32>) -> f32>>)
...
%7 = fir.call @_QFPreffunc(%6#1) fastmath<contract> : (!fir.ref<i32>) -> !fir.boxproc<(!fir.ref<i32>) -> f32> fir.store %7 to %3#0 : !fir.ref<!fir.boxproc<(!fir.ref<i32>) -> f32>>
```



https://github.com/llvm/llvm-project/pull/78194


More information about the flang-commits mailing list