Issue 133260
Summary flang does not add implicit SAVE attribute to program scope variable
Labels miscompilation, flang
Assignees
Reporter DavidSpickett
    Split out of https://github.com/llvm/llvm-project/issues/60219.

The following program:
```
program emptyif
   logical c
   ! This should implcitly be:
   ! logical, save :: c
   ! but Flang does not add this, which leaves c in an undefined state.

   if (c) then
      c = .true.
   elseif (c) then
      c = .true.
   end if
end program
```
Does not compile correctly with Flang, but does with GFortran: https://godbolt.org/z/GsPrMev18

Flang should be treating `c` as if it had the `SAVE` attribute. It does not, so `main` is now UB which means it calls _FortranAProgramStart, but then just falls off the end of main into whatever is next.

See https://j3-fortran.org/doc/year/18/18-007r1.pdf 8.5.16 SAVE attribute point 4.

> A variable, common block, or procedure pointer declared in the scoping unit of a main program, module, or
> submodule implicitly has the SAVE attribute, which may be confirmed by explicit specification. If a common block
> has the SAVE attribute in any other kind of scoping unit, it shall have the SAVE attribute in every scoping unit that > is not of a
> main program, module, or submodule.

Adding "save" to `c` in the source code works around the problem.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to