fp-floatcontrol-pragma.cpp
5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// RUN: %clang_cc1 -DEXCEPT=1 -fcxx-exceptions -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck -check-prefix=CHECK-NS %s
// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -verify -DFENV_ON=1 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple %itanium_abi_triple -O3 -emit-llvm -o - %s | FileCheck -check-prefix=CHECK-O3 %s
// Verify float_control(precise, off) enables fast math flags on fp operations.
float fp_precise_1(float a, float b, float c) {
// CHECK-O3: _Z12fp_precise_1fff
// CHECK-O3: %[[M:.+]] = fmul fast float{{.*}}
// CHECK-O3: fadd fast float %[[M]], %c
#pragma float_control(precise, off)
return a * b + c;
}
// Is float_control state cleared on exiting compound statements?
float fp_precise_2(float a, float b, float c) {
// CHECK-O3: _Z12fp_precise_2fff
// CHECK-O3: %[[M:.+]] = fmul float{{.*}}
// CHECK-O3: fadd float %[[M]], %c
{
#pragma float_control(precise, off)
}
return a * b + c;
}
// Does float_control survive template instantiation?
class Foo {};
Foo operator+(Foo, Foo);
template <typename T>
T template_muladd(T a, T b, T c) {
#pragma float_control(precise, off)
return a * b + c;
}
float fp_precise_3(float a, float b, float c) {
// CHECK-O3: _Z12fp_precise_3fff
// CHECK-O3: %[[M:.+]] = fmul fast float{{.*}}
// CHECK-O3: fadd fast float %[[M]], %c
return template_muladd<float>(a, b, c);
}
template <typename T>
class fp_precise_4 {
float method(float a, float b, float c) {
#pragma float_control(precise, off)
return a * b + c;
}
};
template class fp_precise_4<int>;
// CHECK-O3: _ZN12fp_precise_4IiE6methodEfff
// CHECK-O3: %[[M:.+]] = fmul fast float{{.*}}
// CHECK-O3: fadd fast float %[[M]], %c
// Check file-scoped float_control
#pragma float_control(push)
#pragma float_control(precise, off)
float fp_precise_5(float a, float b, float c) {
// CHECK-O3: _Z12fp_precise_5fff
// CHECK-O3: %[[M:.+]] = fmul fast float{{.*}}
// CHECK-O3: fadd fast float %[[M]], %c
return a * b + c;
}
#pragma float_control(pop)
float fff(float x, float y) {
// CHECK-LABEL: define float @_Z3fffff{{.*}}
// CHECK: entry
#pragma float_control(except, on)
float z;
z = z * z;
//CHECK: llvm.experimental.constrained.fmul{{.*}}
{
z = x * y;
//CHECK: llvm.experimental.constrained.fmul{{.*}}
}
{
// This pragma has no effect since if there are any fp intrin in the
// function then all the operations need to be fp intrin
#pragma float_control(except, off)
z = z + x * y;
//CHECK: llvm.experimental.constrained.fmul{{.*}}
}
z = z * z;
//CHECK: llvm.experimental.constrained.fmul{{.*}}
return z;
}
float check_precise(float x, float y) {
// CHECK-LABEL: define float @_Z13check_preciseff{{.*}}
float z;
{
#pragma float_control(precise, on)
z = x * y + z;
//CHECK: llvm.fmuladd{{.*}}
}
{
#pragma float_control(precise, off)
z = x * y + z;
//CHECK: fmul fast float
//CHECK: fadd fast float
}
return z;
}
float fma_test2(float a, float b, float c) {
// CHECK-LABEL define float @_Z9fma_test2fff{{.*}}
#pragma float_control(precise, off)
float x = a * b + c;
//CHECK: fmuladd
return x;
}
float fma_test1(float a, float b, float c) {
// CHECK-LABEL define float @_Z9fma_test1fff{{.*}}
#pragma float_control(precise, on)
float x = a * b + c;
//CHECK: fmuladd
return x;
}
#pragma float_control(push)
#pragma float_control(precise, on)
struct Distance {};
Distance operator+(Distance, Distance);
template <class T>
T add(T lhs, T rhs) {
#pragma float_control(except, on)
return lhs + rhs;
}
#pragma float_control(pop)
float test_OperatorCall() {
return add(1.0f, 2.0f);
//CHECK: llvm.experimental.constrained.fadd{{.*}}fpexcept.strict
}
// CHECK-LABEL define float {{.*}}test_OperatorCall{{.*}}
#if FENV_ON
// expected-warning@+1{{pragma STDC FENV_ACCESS ON is not supported, ignoring pragma}}
#pragma STDC FENV_ACCESS ON
#endif
// CHECK-LABEL: define {{.*}}callt{{.*}}
void callt() {
volatile float z;
z = z * z;
//CHECK: = fmul float
}
#if EXCEPT
namespace ns {
// Check that pragma float_control can appear in namespace.
#pragma float_control(except, on, push)
float exc_on(double x, float zero) {
// CHECK-NS: define {{.*}}exc_on{{.*}}
{} try {
x = 1.0 / zero; /* division by zero, the result unused */
//CHECK-NS: llvm.experimental.constrained.fdiv{{.*}}
} catch (...) {}
return zero;
}
}
// Check pragma is still effective after namespace closes
float exc_still_on(double x, float zero) {
// CHECK-NS: define {{.*}}exc_still_on{{.*}}
{} try {
x = 1.0 / zero; /* division by zero, the result unused */
//CHECK-NS: llvm.experimental.constrained.fdiv{{.*}}
} catch (...) {}
return zero;
}
#pragma float_control(pop)
float exc_off(double x, float zero) {
// CHECK-NS: define {{.*}}exc_off{{.*}}
{} try {
x = 1.0 / zero; /* division by zero, the result unused */
//CHECK-NS: fdiv double
} catch (...) {}
return zero;
}
namespace fc_template_namespace {
#pragma float_control(except, on, push)
template <class T>
T exc_on(double x, T zero) {
// CHECK-NS: define {{.*}}fc_template_namespace{{.*}}
{} try {
x = 1.0 / zero; /* division by zero, the result unused */
//CHECK-NS: llvm.experimental.constrained.fdiv{{.*}}
} catch (...) {}
return zero;
}
}
#pragma float_control(pop)
float xx(double x, float z) {
return fc_template_namespace::exc_on<float>(x, z);
}
#endif // EXCEPT