Nvfortran 26.9: ICE "Bad ilm operand 0" from a use-after-realloc (`convert_subscript_in_expr`): it comes and goes with unrelated code changes

Hello,

We hit an internal compiler error in nvfortran 26.9 that appeared and disappeared with unrelated edits to our code. One extra unused local variable made it go away, and so did compiling the modules as one file. Other edits turned it into unexpected storage type, a fort1 segfault, or a fort1 that hung. Running fort1 under valgrind showed the cause, and with that we could write a small reproducer.

Environment: NVIDIA HPC SDK 26.9 (nvfortran 26.9-0 64-bit target on x86-64 Linux -tp cascadelake), Ubuntu 26.04.1 LTS.

Reproducer (ice_mre.f90; gfortran 15.2, ifx 2026.1 and LLVM Flang (20.1.8 and trunk, tested on Compiler Explorer) compile it)

subroutine s(a,b,m)
  implicit none
  integer, intent(in ) :: a(3),b
  integer, intent(out) :: m
  ! this call only fills the front end's argument table (86 arguments)
  call pad(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, &
           0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, &
           0,0,0,0,0,0)
  m = maxval(abs(a-spread(b,1,3)))
end subroutine s
$ nvfortran -O0 -c ice_mre.f90
ILM FILE line 216: Bad ilm operand 0
NVFORTRAN-F-0000-Internal compiler error. Errors in ILM file       1  (ice_mre.f90: 10)

The result is the same with -O2 and with -fast -acc. Only the number of arguments of pad (K) decides whether it fails. For K from 0 to 300, K = 85, 86, 186 and 188 give this ICE, and K = 187 kills fort1 with signal 6. All other values compile. In other versions the failing values are probably different.

Cause

$ valgrind .../tools/fort1 ice_mre.f90 ...
Invalid write of size 4
   at 0x742139: convert_subscript_in_expr(int)
   by 0x75A98C: inline_reduction_f90(int, int, int, bool*)
   by 0x74F452: rewrite_sub_ast(int, int)
 Address 0x4f73698 is 792 bytes inside a block of size 800 free'd
   at 0x485307F: realloc
   by 0x4C1A9D: sccrelal(char*, unsigned long)
   by 0x4128D6: mk_argt(int)
   by 0x7420BA: convert_subscript_in_expr(int)
   by 0x741D44: convert_subscript_in_expr(int)
   by 0x742138: convert_subscript_in_expr(int)
   by 0x75A98C: inline_reduction_f90(int, int, int, bool*)

The same code is in the open-source classic Flang front end (tools/flang1/flang1exe/func.c, convert_subscript_in_expr, case A_INTR):

newargt = mk_argt(nargs);
for (i = 0; i < nargs; ++i) {
  ARGT_ARG(newargt, i) = convert_subscript_in_expr(ARGT_ARG(argt, i));
}

ARGT_ARG indexes astb.argt.stg_base. The recursive call on the right-hand side reaches mk_argt again (here for spread), and mk_argt may realloc the table. C does not specify whether the address on the left is computed before or after that call. In this build it is computed before, so the result is stored into the freed block. The new node keeps an unset argument, presumably the 0 of “Bad ilm operand 0”, and the stray store can corrupt the heap. That explains why the symptom depends on unrelated code: any change that shifts the table’s fill level moves the reallocation. Storing the result in a local variable first, then into the table, should fix it. The A_FUNC case just below has the same pattern.

In our tests the failure needs a reduction (maxval, all) in an assignment. Its argument must contain an elemental intrinsic (abs) applied to an expression with a transformational intrinsic (spread, transpose). abs(spread(...)) alone, or nested elemental intrinsics only, never failed.

The message is the same as in the full-TMPDIR case, but this is a different bug: it is deterministic, with plenty of free disk space.

LLVM Flang has its own front end, which does not share this code. As long as nvfortran uses the classic front end, though, any procedure with such a reduction can hit the bug, whenever the table happens to grow at that point.

Workaround: compute the argument of the reduction into a temporary array first (t = a-spread(b,1,3); m = maxval(abs(t))). This compiled for every K we tried.

Thank you in advance!
Pedro

Written with the help of Claude Code.

Thanks for the report Pedro!

I added TPR #39148 and sent it to engineering.

Since classic flang is the open sourced version of PGI, same base as our current nvfortran, I included Claude’s analysis of where the bug might be. I’m not sure it’s applicable, but very possible.

-Mat