Bug 108329 - IEEE_SET_ROUNDING_MODE ineffective with common subexpression elimination
Summary: IEEE_SET_ROUNDING_MODE ineffective with common subexpression elimination
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 13.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on: 34678
Blocks: fortran-ieee
  Show dependency treegraph
 
Reported: 2023-01-07 12:24 UTC by Thomas Koenig
Modified: 2023-01-09 15:27 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-01-07 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Koenig 2023-01-07 12:24:26 UTC
Split from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=34678#c47 .

The test case

$ cat y.f90
module y
  implicit none
  integer, parameter :: wp = selected_real_kind(15)
contains
  subroutine foo(a,b,c)
    use ieee_arithmetic
    real(kind=wp), dimension(4), intent(out) :: a
    real(kind=wp), intent(in) :: b, c
    type (ieee_round_type), dimension(4), parameter :: mode = &
         [ieee_nearest, ieee_to_zero, ieee_up, ieee_down]
    call ieee_set_rounding_mode (mode(1))
    a(1) = b + c
    call ieee_set_rounding_mode (mode(2))
    a(2) = b + c
    call ieee_set_rounding_mode (mode(3))
    a(3) = b + c
    call ieee_set_rounding_mode (mode(4))
    a(4) = b + c
  end subroutine foo
end module y

program main
  use y
  real(kind=wp), dimension(4) :: a
  call foo(a, 0.1_wp, 0.2_wp)
  print *,a
end program main
$ gfortran -O  y.f90 && ./a.out
  0.30000000000000004       0.30000000000000004       0.30000000000000004       0.30000000000000004     
$ gfortran y.f90 && ./a.out
  0.30000000000000004       0.29999999999999999       0.30000000000000004       0.29999999999999999

shows that common subexpression removal causes the addition to be performed
only once.
Comment 1 Thomas Koenig 2023-01-07 12:33:59 UTC
As long as PR 36678 is not fixed, I see one possible solution in
putting a memory barrier after ieee_set_rounding_mode.

This is a rather big hammer, but as long as the middle-end issue
is not fixed, I do not see an alternative.
Comment 2 Thomas Koenig 2023-01-07 12:47:38 UTC
(In reply to Thomas Koenig from comment #1)
> As long as PR 36678

That should be PR 34678 .
Comment 3 Thomas Koenig 2023-01-09 15:27:14 UTC
Seems to be much more complicated than I thought, see the thrad starting at
https://gcc.gnu.org/pipermail/gcc-patches/2023-January/609532.html