Skip to content

Commit fd29a1a

Browse files
jonatanlindennv-cmeerw
authored andcommitted
Evaluate each operand of floating-point <=> exactly once. [GH #520]
1 parent cd21a3e commit fd29a1a

5 files changed

Lines changed: 116 additions & 9 deletions

File tree

‎src/lower_il.c‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16328,6 +16328,7 @@ Floating-point types require additional run-time checking for NaN (see below).
1632816328
an_expr_node_ptr op2 = op1->next;
1632916329
an_expr_node_ptr temp1, temp2, one, zero, minus_one, cmp, unordered, eptr;
1633016330
a_boolean op1_has_side_effects, op2_has_side_effects;
16331+
a_boolean temp1_used, temp2_used;
1633116332
a_type_kind type_kind = expr->variant.operation.type_kind;
1633216333

1633316334
one = spaceship_result_constant_expr(1, expr->type);
@@ -16360,15 +16361,24 @@ Floating-point types require additional run-time checking for NaN (see below).
1636016361
minus_one = spaceship_result_constant_expr(-1, expr->type);
1636116362
op1_has_side_effects = node_has_side_effects(op1, (a_boolean *)NULL);
1636216363
op2_has_side_effects = node_has_side_effects(op2, (a_boolean *)NULL);
16363-
temp1 = make_reusable_copy(op1, op1_has_side_effects);
16364-
temp2 = make_reusable_copy(op2, op2_has_side_effects);
16364+
temp1 = make_reusable_copy_full(op1, op1_has_side_effects, &temp1_used,
16365+
/*treat_as_potential_prvalue=*/FALSE);
16366+
temp2 = make_reusable_copy_full(op2, op2_has_side_effects, &temp2_used,
16367+
/*treat_as_potential_prvalue=*/FALSE);
1636516368
if (type_kind_is_simple_float_like(type_kind)) {
1636616369
/* If either operand is a NaN, then return
16367-
std::partial_ordering::unordered:
16368-
(__isnan(temp1 = x) || __isnan(temp2 = y)) ? RT(unordered) :
16370+
std::partial_ordering::unordered. Each operand is evaluated
16371+
exactly once, and the NaN test and the comparisons use the same
16372+
operand values:
16373+
((temp1 = x), (temp2 = y), __isnan(temp1) | __isnan(temp2))
16374+
? RT(unordered) :
1636916375
temp1 < temp2 ? RT(-1) :
1637016376
temp2 < temp1 ? RT(1)
1637116377
: RT(0)
16378+
An operand that needs a temporary is assigned to it before the NaN
16379+
test. An operand that needs no temporary is used directly, and its
16380+
assignment to temp1 or temp2 is omitted; it is read only by the NaN
16381+
test and the comparisons.
1637216382
*/
1637316383
/* Note that the value of std::partial_ordering::unordered can vary
1637416384
based on the implementation. Look for its definition in the headers
@@ -16386,6 +16396,22 @@ Floating-point types require additional run-time checking for NaN (see below).
1638616396
check_assertion(!ovflo);
1638716397
unordered = spaceship_result_constant_expr((int)unordered_val,
1638816398
expr->type);
16399+
/* Make "__isnan(temp1) | __isnan(temp2)". */
16400+
eptr = make_isnan_call(make_reusable_copy(temp1,
16401+
/*vars_can_change=*/FALSE));
16402+
eptr->next = make_isnan_call(make_reusable_copy(temp2,
16403+
/*vars_can_change=*/FALSE));
16404+
eptr = make_operator_node(eok_or, integer_type(ik_int), eptr);
16405+
/* Evaluate the operands that were copied to temporaries before the
16406+
NaN test. */
16407+
op1->next = NULL;
16408+
op2->next = NULL;
16409+
if (temp2_used) {
16410+
eptr = make_comma_node(op2, eptr);
16411+
} /* if */
16412+
if (temp1_used) {
16413+
eptr = make_comma_node(op1, eptr);
16414+
} /* if */
1638916415
temp2->next = temp1;
1639016416
cmp = make_operator_node(eok_lt, integer_type(ik_int), temp2);
1639116417
cmp->next = one;
@@ -16398,11 +16424,6 @@ Floating-point types require additional run-time checking for NaN (see below).
1639816424
cmp = make_operator_node(eok_lt, integer_type(ik_int), temp1);
1639916425
cmp->next = minus_one;
1640016426
cmp = make_operator_node(eok_question, expr->type, cmp);
16401-
op2->next = NULL;
16402-
eptr = make_isnan_call(op2);
16403-
op1->next = NULL;
16404-
eptr->next = make_isnan_call(op1);
16405-
eptr = make_operator_node(eok_lor, integer_type(ik_int), eptr);
1640616427
unordered->next = cmp;
1640716428
eptr->next = unordered;
1640816429
set_node_operator(expr, eok_question, expr->type, expr->is_lvalue, eptr);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//allowed_words: NaN
2+
[GH #520]
3+
Lowering of <=> and the evaluation of its operands
4+
5+
For floating-point operands, the lowering of <=> for the C-generating back
6+
end tested the right operand for a NaN before evaluating the left operand,
7+
using a short-circuit "||". When the right operand was a NaN, the left
8+
operand was therefore not evaluated, and its side effects were lost. Also,
9+
an operand without side effects could be read by the NaN test before the
10+
other operand was evaluated, and by the comparisons after it. If the other
11+
operand changed it, the result could then be "equivalent" for a NaN. For
12+
example, with --c++20:
13+
14+
#include <compare>
15+
16+
float x(), y();
17+
auto f() { return x() <=> y(); } // Previously x() was not called when
18+
// y() returned a NaN.
19+
20+
float g, h(); // h() modifies g.
21+
auto k() { return h() <=> g; } // Previously could be "equivalent"
22+
// when h() stores a NaN into g.
23+
24+
Each operand is now evaluated exactly once, and the NaN test and the
25+
comparisons use the same operand values.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eccp -DTEST_NUMBER=1 --c++20 --g++ Test_name.c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./a.out
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//type:rp
2+
//options_all:--c++20 --g++
3+
4+
// Lowering of <=> must evaluate both operands exactly once, and must compare
5+
// the same operand values in the NaN test and in the ordering tests.
6+
7+
#include <compare>
8+
9+
float nan_value() { return __builtin_nanf(""); }
10+
11+
int calls;
12+
float one() { ++calls; return 1.0f; }
13+
14+
float g;
15+
float set_g_to_nan() { g = nan_value(); return 0.0f; }
16+
17+
int main() {
18+
int failures = 0;
19+
20+
// The left operand is evaluated when the right operand is a NaN.
21+
calls = 0;
22+
if ((one() <=> nan_value()) != std::partial_ordering::unordered) {
23+
failures |= 1;
24+
} // if
25+
if (calls != 1) failures |= 2;
26+
27+
// The right operand is evaluated when the left operand is a NaN.
28+
calls = 0;
29+
if ((nan_value() <=> one()) != std::partial_ordering::unordered) {
30+
failures |= 4;
31+
} // if
32+
if (calls != 1) failures |= 8;
33+
34+
// g is read either before set_g_to_nan() (less) or after it (unordered),
35+
// never both.
36+
g = 1.0f;
37+
std::partial_ordering r = set_g_to_nan() <=> g;
38+
if (r != std::partial_ordering::less &&
39+
r != std::partial_ordering::unordered) {
40+
failures |= 16;
41+
} // if
42+
g = 1.0f;
43+
r = g <=> set_g_to_nan();
44+
if (r != std::partial_ordering::greater &&
45+
r != std::partial_ordering::unordered) {
46+
failures |= 32;
47+
} // if
48+
49+
// Finite operands give less, equivalent, and greater, both with operands
50+
// that are copied to temporaries (one()) and with one that is not (g).
51+
calls = 0;
52+
g = 2.0f;
53+
if ((one() <=> g) != std::partial_ordering::less) failures |= 64;
54+
if ((one() <=> one()) != std::partial_ordering::equivalent) failures |= 64;
55+
if ((g <=> one()) != std::partial_ordering::greater) failures |= 64;
56+
if (calls != 4) failures |= 128;
57+
58+
return failures;
59+
}

0 commit comments

Comments
 (0)