Where to attach backend builtin decl?

FX fxcoudert@gmail.com
Fri Feb 29 15:55:00 GMT 2008


> The new decl has the same name
> as the symbol "spu_add_int", and is non-builtin function.

Yes. Calling builtins from Fortran code is not something we currently
support, because it was never needed before. What I think you should
do is: in gfc_get_extern_function_decl(), right before the "if
(sym->attr.intrinsic)", add a new block to take care of your
intrinsics:

  if (sym->attr.is_bind_c == 1 && strncmp (sym->binding_label,
"__builtin_", 10))
    {
      /* When a procedure is BIND(C) and its binding label starts with
"__builtin_",
          it is translated into a call to the builtin of the same name.  */
      sym->backend_decl = find_builtin_by_name (sym->binding_label);
      if (sym->backend_decl)
        return sym->backend_decl;
    }

You need to write a find_builtin_by_name() function, which takes the
name and finds the builtin decl that goes with it. As I said before,
there probably is code that can be leveraged from the C front-end to
do that, but I don't know it. You may ask on the gcc@ list where
people are more likely to know that.

> If I replace the decl with its backend builtin decl, which has name
> "__builtin_spu_add_1",later the function is not expanded.

That is weird (and slightly worrying). Can you show me the tree dump
for the function call, where you have manually replaced the decl by
the builtin decl?

> I think the problem here is that the symbol being handled is the function
> name "spu_add_int", not its binding label "__builtin_spu_add_1".

I don't think that's it. The name is indeed "spu_add_1" but the
assembler name is "__builtin_spu_add_1", as is demonstrated by:

$ cat u.f90
module spu_intrinsics
implicit none

  interface
    function spu_add_int(a, b) result(c) bind(c, name="__builtin_spu_add_1")
    use, intrinsic :: iso_c_binding
    integer(c_int), intent(in) :: a(4), b(4)
    integer(c_int) :: c(4)
    end function spu_add_int
  end interface
end module spu_intrinsics

program test
  use spu_intrinsics
  use, intrinsic :: iso_c_binding

  integer(c_int) :: a(4), b(4), c(4)
  c = spu_add_int(a,b)
  print *, c
end
$ gfortran u.f90 -fdump-tree-original
/tmp/ccyRSVrf.o: In function `MAIN__':
u.f90:(.text+0x57): undefined reference to `__builtin_spu_add_1'
collect2: ld returned 1 exit status
$ grep spu_add u.f90.003t.original
    spu_add_int (&parm.1, D.922, D.924);

-- 
FX Coudert
http://www.homepages.ucl.ac.uk/~uccafco/



More information about the Fortran mailing list