Skip to content

Commit c7754a2

Browse files
committed
Fortran: Cray pointer comparison wrongly optimized away [PR106692]
PR fortran/106692 gcc/fortran/ChangeLog: * trans-expr.cc (gfc_conv_expr_op): Inhibit excessive optimization of Cray pointers by treating them as volatile in comparisons. gcc/testsuite/ChangeLog: * gfortran.dg/cray_pointers_13.f90: New test.
1 parent 75da7a6 commit c7754a2

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

gcc/fortran/trans-expr.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4150,6 +4150,19 @@ gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
41504150

41514151
if (lop)
41524152
{
4153+
// Inhibit overeager optimization of Cray pointer comparisons (PR106692).
4154+
if (expr->value.op.op1->expr_type == EXPR_VARIABLE
4155+
&& expr->value.op.op1->ts.type == BT_INTEGER
4156+
&& expr->value.op.op1->symtree
4157+
&& expr->value.op.op1->symtree->n.sym->attr.cray_pointer)
4158+
TREE_THIS_VOLATILE (lse.expr) = 1;
4159+
4160+
if (expr->value.op.op2->expr_type == EXPR_VARIABLE
4161+
&& expr->value.op.op2->ts.type == BT_INTEGER
4162+
&& expr->value.op.op2->symtree
4163+
&& expr->value.op.op2->symtree->n.sym->attr.cray_pointer)
4164+
TREE_THIS_VOLATILE (rse.expr) = 1;
4165+
41534166
/* The result of logical ops is always logical_type_node. */
41544167
tmp = fold_build2_loc (input_location, code, logical_type_node,
41554168
lse.expr, rse.expr);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
! { dg-do run }
2+
! { dg-additional-options "-fcray-pointer" }
3+
!
4+
! PR fortran/106692 - Cray pointer comparison wrongly optimized away
5+
!
6+
! Contributed by Marek Polacek
7+
8+
program test
9+
call test_cray()
10+
call test_cray2()
11+
end
12+
13+
subroutine test_cray()
14+
pointer(ptrzz1 , zz1)
15+
ptrzz1=0
16+
if (ptrzz1 .ne. 0) then
17+
print *, "test_cray: ptrzz1=", ptrzz1
18+
stop 1
19+
else
20+
call shape_cray(zz1)
21+
end if
22+
end
23+
24+
subroutine shape_cray(zz1)
25+
pointer(ptrzz , zz)
26+
ptrzz=loc(zz1)
27+
if (ptrzz .ne. 0) then
28+
print *, "shape_cray: ptrzz=", ptrzz
29+
stop 3
30+
end if
31+
end
32+
33+
subroutine test_cray2()
34+
pointer(ptrzz1 , zz1)
35+
ptrzz1=0
36+
if (0 == ptrzz1) then
37+
call shape_cray2(zz1)
38+
else
39+
print *, "test_cray2: ptrzz1=", ptrzz1
40+
stop 2
41+
end if
42+
end
43+
44+
subroutine shape_cray2(zz1)
45+
pointer(ptrzz , zz)
46+
ptrzz=loc(zz1)
47+
if (.not. (0 == ptrzz)) then
48+
print *, "shape_cray2: ptrzz=", ptrzz
49+
stop 4
50+
end if
51+
end

0 commit comments

Comments
 (0)