Page 1 of 1

Fortran and /name=lower on X86 gives wrong results at run time

Posted: Thu Nov 16, 2023 9:17 am
by joukj
Hi I get this:

Code: Select all

rumba-jj) f90/vers
VSI Fortran x86-64 X8.5-007 (GEM 50XBD) on OpenVMS x86_64 V9.2-1
rumba-jj) ty test.f90
program test
  real aap
  integer ceil
  aap =11.0
  write(*,*) ceil( aap / 8.0 )
end
rumba-jj) ty ceil.f90
      integer function ceil( realvar )
!
      ceil = int( realvar )
      if ( float( ceil ) .ne. realvar ) then
        if ( realvar .gt. 0.0 ) then
          ceil = ceil + 1
        else
          ceil = ceil - 1
        end if
      end if
!
      return
      end
rumba-jj) f90 test.f90
rumba-jj) f90 ceil.f90
rumba-jj) link test,ceil
rumba-jj) run test
          2
rumba-jj) f90/name=lower test.f90
rumba-jj) f90/name=lower ceil.f90
rumba-jj) link test,ceil
rumba-jj) run test
          0
rumba-jj) f90/name=lower/noop test.f90
rumba-jj) f90/name=lower/noop ceil.f90
rumba-jj) link test,ceil
rumba-jj) run test
 1068498945

Obvious the right result is 2 but I get 2 different wrong results if compiled wih /name=lower.

Jouk

Re: Fortran and /name=lower on X86 gives wrong results at run time

Posted: Thu Nov 16, 2023 8:17 pm
by arne_v
It looks like the "weird results" comes from calling math$ceil_t instead of your ceil.

The code works fine if ceil is renamed to myceil.

It must be some sort of bug that lowercasing causes it to switch from your ceil to math$ceil.

Re: Fortran and /name=lower on X86 gives wrong results at run time

Posted: Fri Nov 17, 2023 2:39 am
by joukj
Thanks

It means that now all C-functions can be called directly from Fortran? I just tested a few and all seem to work:

Code: Select all

rumba-jj) ty test.f90
program test
  real*8 aap
  real*8 , external :: ceil
  integer strlen
  integer*8 getpid
  character*8 str
  str = '12345' // char(0)
  aap =11.7
  write(*,*) ceil( %val(aap / 8.0 ))
  write(*,*) getpid()
  write(*,*) strlen( %ref( str ) )
end
  17-NOV-2023 08:36:43
rumba-jj) f90/name=lower test
rumba-jj) link test
rumba-jj) run test
  2.00000000000000     
            538969400
          5

Re: Fortran and /name=lower on X86 gives wrong results at run time

Posted: Fri Nov 17, 2023 8:54 am
by arne_v
It has always been possible to call a C function from Fortran, but it seems like C functions has become Fortran functions now - since the Fortran compiler adds prefix and suffix to the name. If they are lowercase that is.

It could be a nice feature I guess.

But:
1) it should be documented
2) there should be a warning/error when compiling a conflicting name
3) I believe that it should be done for both lowercase and uppercase

Looking forward to hear VSI explain what they have done and what they want to do.

Re: Fortran and /name=lower on X86 gives wrong results at run time

Posted: Fri Nov 17, 2023 12:47 pm
by jreagan
We'll discuss it and document it better. The trouble here is that LLVM sometimes wants to insert a call to ceil as part of a code transformation and needed to know the name of the routine. The internal table had "ceil" in it and we changed it to "math$ceil_t". I think the code in LLVM decided that ALL calls to ceil need to be mapped to math$ceil_t.

Re: Fortran and /name=lower on X86 gives wrong results at run time

Posted: Mon Nov 20, 2023 3:28 am
by joukj
I solved the problem by putting ceil in a module. It was in a library anyway and I was transforming all the entries to accept different arguments (in theis caese al*4,*8&*16) anyway.

calling the crl-routines directly from fortran without a wrapper may be a nice feature to have.

Jouk

Re: Fortran and /name=lower on X86 gives wrong results at run time

Posted: Mon Jan 29, 2024 3:02 am
by joukj
Solved in the latest Fortran compiler.