llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.59k stars 11.81k forks source link

[flang] iso_c_binding compilation error: Lowering to LLVM IR failed #68314

Open Try2Code opened 1 year ago

Try2Code commented 1 year ago

The following program:

module parsing_large_iso_c_mod
  use iso_c_binding
  implicit none
  private

  interface
    integer(c_size_t) function lib_strlen(charPtr) bind(c, name = "strlen")
      import c_size_t, c_ptr
      type(c_ptr), value :: charPtr
    end function lib_strlen

    subroutine lib_free(ptr) bind(c, name = "free")
      import c_ptr
      type(c_ptr), value, intent(in) :: ptr
    end subroutine lib_free
  end interface

contains

  function vlistCopyVarName(vlistId_dummy, varId_dummy) result(f_result)
    character(kind = c_char), dimension(:), pointer :: f_result
    integer(c_int), value :: vlistId_dummy
    integer(c_int), value :: varId_dummy
    type(c_ptr) :: cString
    integer :: rv_shape(1)
    character(kind = c_char), dimension(:), pointer :: temp
! original implementation, kept for reference, might influence the AST
!   interface
!     function lib_vlistCopyVarName(vlistId_dummy, varId_dummy) bind(c, name =&
!     & 'vlistCopyVarName') result(c_result)
!       import c_int, c_ptr
!       type(c_ptr) :: c_result
!       integer(c_int), value :: vlistId_dummy
!       integer(c_int), value :: varId_dummy
!     end function lib_vlistCopyVarName
!   end interface
!   cString = lib_vlistCopyVarName(vlistId_dummy, varId_dummy)
    if(c_associated(cString)) then
      rv_shape(1) = int(lib_strlen(cString))
#ifdef FULL
      call c_f_pointer(cString, temp, rv_shape)
      allocate(f_result(rv_shape(1)))
      f_result = temp
#endif
      call lib_free(cString)
    else
      f_result => null()
    end if
  end function vlistCopyVarName

end module

can be compiled using

flang-new -cpp -c file.f90 -o file.o

but fails with

flang-new -cpp -DFULL -c file.f90 -o file.o

Compiler version:

flang-new version 18.0.0 (https://github.com/llvm/llvm-project 274ba2c910676cd34658fad33d3931c676b64e41)

Full compilation works with gfortran-11.2, aocc-4.1, nag-7.1

Can some provide hint how to work around this? thx in advance

hakostra commented 1 month ago

I tried your code and this seems to work now, with the define directive both set and unset. I tested with a recent build from the main Github branch:

$ flang-new --version
flang-new version 20.0.0git (https://github.com/llvm/llvm-project.git a9006bffa994d5afe9ad0b661b69d655658ab5e8)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/llvm/llvm-project/install/bin
llvmbot commented 1 month ago

@llvm/issue-subscribers-flang-ir

Author: Brian Spilner (Try2Code)

The following program: ```fortran module parsing_large_iso_c_mod use iso_c_binding implicit none private interface integer(c_size_t) function lib_strlen(charPtr) bind(c, name = "strlen") import c_size_t, c_ptr type(c_ptr), value :: charPtr end function lib_strlen subroutine lib_free(ptr) bind(c, name = "free") import c_ptr type(c_ptr), value, intent(in) :: ptr end subroutine lib_free end interface contains function vlistCopyVarName(vlistId_dummy, varId_dummy) result(f_result) character(kind = c_char), dimension(:), pointer :: f_result integer(c_int), value :: vlistId_dummy integer(c_int), value :: varId_dummy type(c_ptr) :: cString integer :: rv_shape(1) character(kind = c_char), dimension(:), pointer :: temp ! original implementation, kept for reference, might influence the AST ! interface ! function lib_vlistCopyVarName(vlistId_dummy, varId_dummy) bind(c, name =& ! & 'vlistCopyVarName') result(c_result) ! import c_int, c_ptr ! type(c_ptr) :: c_result ! integer(c_int), value :: vlistId_dummy ! integer(c_int), value :: varId_dummy ! end function lib_vlistCopyVarName ! end interface ! cString = lib_vlistCopyVarName(vlistId_dummy, varId_dummy) if(c_associated(cString)) then rv_shape(1) = int(lib_strlen(cString)) #ifdef FULL call c_f_pointer(cString, temp, rv_shape) allocate(f_result(rv_shape(1))) f_result = temp #endif call lib_free(cString) else f_result => null() end if end function vlistCopyVarName end module ``` can be compiled using ```shell flang-new -cpp -c file.f90 -o file.o ``` but fails with ```shell flang-new -cpp -DFULL -c file.f90 -o file.o ``` Compiler version: ```shell flang-new version 18.0.0 (https://github.com/llvm/llvm-project 274ba2c910676cd34658fad33d3931c676b64e41) ``` Full compilation works with gfortran-11.2, aocc-4.1, nag-7.1 Can some provide hint how to work around this? thx in advance
clementval commented 1 month ago

That commit is back in october 2023. Flang is moving fast and lots of bugs and been fixed since then. As @hakostra mentioned, you should try with a newer version of flang.