Calling varlist C routine from Fortran (corrupted stack?)

Dear all

We are using pgf90 5.2-2 on dual Opteron 64bit-mode on SuSe 9.1 Professional.

Commnad line: “pgf90 -g …”

In our application ( we have f90 routing calling C routine with varlists arguments. For example we have on an f90 routine the following call:

       call CCTK_Reduce(ierr, cctkGH, -1, Reduction_Handle, &
             1, 107, rho_max, 1, VarIndex)

to the following C routine:

void CCTK_FCALL cctk_reduce_
     (int *fortranreturn,
      const cGH **GH,
      const int *proc,
      const int *operation_handle,
      const int *num_out_vals,
      const int *type_out_vals,
      void *out_vals,
      const int *num_in_fields,
      ... );

Now in the calling the argument got messed up and the stack got currupted. Infact this C routine in turns call:

int CCTK_Reduce(const cGH *GH,
                int proc,
                int operation_handle,
                int num_out_vals,
                int type_out_vals,
                void *out_vals,
                int num_in_fields, ...);

and after some “C” subcall a memory exeception is generated. The debuging session show that in the stack no trace of the Fortarn interface function “cctk_reduce_” is shown.

Rerunning the debugging session a traceing by hand the calling flow everything go well but “cctk_reduce_” receive a bad sequence of argument.

There are workaround for this problem ?

Thanky you for any suggestions

This is a problem that only occurs on Opterons when using Fortran to C with varargs. When you compile a C program on Opteron using varargs, the EAX register is expected to contain the number of XMM registers that will be used for the varargs. When you call a C function with varargs from Fortran the EAX register is not set since its not needed in Fortran. Since EAX contains garbage, the C varargs becomes corrupt.

To work around this problem is to use the x flag “-Mx,125,0x200” which will set EAX to zero before every call. Note that we generally do not release x flags to the public since they are for the compiler’s internal use, new, or experimental. So the caveat to using this flag is that it has not been properly QA’d and is subject to change. However, I believe in this case we plan to give it a name in a future release.


Please correct me if I’m wrong, but I’m assuming that the flag -Mvarargs is the name for -Mx,125,0x200

PGI 17


No - -Mvarargs is the same for all CPUs. A special switch is needed
for Opterons, which adds time but corrects the issue on all CPUs.
This is the one to zero out a register on the cpu.

Needed for Opteron, unnecessary on other cpus - but still correct everywhere.