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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//===- fprt/mpfr - MPFR wrappers ---------------------------------------===//
//
// Enzyme Project
//
// Part of the Enzyme Project, under the Apache License v2.0 with LLVM
// Exceptions. See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// If using this code in an academic setting, please cite the following:
// @incollection{enzymeNeurips,
// title = {Instead of Rewriting Foreign Code for Machine Learning,
// Automatically Synthesize Fast Gradients},
// author = {Moses, William S. and Churavy, Valentin},
// booktitle = {Advances in Neural Information Processing Systems 33},
// year = {2020},
// note = {To appear in},
// }
//
//===----------------------------------------------------------------------===//
//
// This file contains easy to use wrappers around MPFR functions.
//
//===----------------------------------------------------------------------===//
#ifndef __ENZYME_RUNTIME_ENZYME_MPFR__
#define __ENZYME_RUNTIME_ENZYME_MPFR__
#include <mpfr.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
// TODO TODO TODO
// TODO TODO TODO
// TODO TODO TODO
// TODO TODO TODO
// TODO TODO TODO
// I dont think we intercept comparisons - we most definitely should.
// TODO TODO TODO
// TODO TODO TODO
// TODO TODO TODO
// TODO TODO TODO
// TODO TODO TODO
// TODO s
//
// (for MPFR ver. 2.1)
//
// We need to set the range of the allowed exponent using `mpfr_set_emin` and
// `mpfr_set_emax`. (This means we can also play with whether the range is
// centered around 0 (1?) or somewhere else)
//
// (also these need to be mutex'ed as the exponent change is global in mpfr and
// not float-specific) ... (mpfr seems to have thread safe mode - check if it is
// enabled or if it is enabled by default)
//
// For that we need to do this check:
// If the user changes the exponent range, it is her/his responsibility to
// check that all current floating-point variables are in the new allowed
// range (for example using mpfr_check_range), otherwise the subsequent
// behavior will be undefined, in the sense of the ISO C standard.
//
// MPFR docs state the following:
// Note: Overflow handling is still experimental and currently implemented
// partially. If an overflow occurs internally at the wrong place, anything
// can happen (crash, wrong results, etc).
//
// Which we would like to avoid somehow.
//
// MPFR also has this limitation that we need to address for accurate
// simulation:
// [...] subnormal numbers are not implemented.
//
// TODO maybe take debug info as parameter - then we can emit warnings or tie
// operations to source location
//
// TODO we need to provide f32 versions, and also instrument the
// truncation/expansion between f32/f64/etc
#define __ENZYME_MPFR_ATTRIBUTES __attribute__((weak))
#define __ENZYME_MPFR_ORIGINAL_ATTRIBUTES __attribute__((weak))
#define __ENZYME_MPFR_DEFAULT_ROUNDING_MODE GMP_RNDN
static bool __enzyme_fprt_is_mem_mode(int64_t mode) { return mode & 0b0001; }
static bool __enzyme_fprt_is_op_mode(int64_t mode) { return mode & 0b0010; }
typedef struct {
mpfr_t v;
} __enzyme_fp;
static double __enzyme_fprt_ptr_to_double(__enzyme_fp *p) {
return *((double *)(&p));
}
static __enzyme_fp *__enzyme_fprt_double_to_ptr(double d) {
return *((__enzyme_fp **)(&d));
}
__ENZYME_MPFR_ATTRIBUTES
double __enzyme_fprt_64_52_get(double _a, int64_t exponent, int64_t significand,
int64_t mode) {
__enzyme_fp *a = __enzyme_fprt_double_to_ptr(_a);
return mpfr_get_d(a->v, __ENZYME_MPFR_DEFAULT_ROUNDING_MODE);
}
__ENZYME_MPFR_ATTRIBUTES
double __enzyme_fprt_64_52_new(double _a, int64_t exponent, int64_t significand,
int64_t mode) {
__enzyme_fp *a = (__enzyme_fp *)malloc(sizeof(__enzyme_fp));
mpfr_init2(a->v, significand);
mpfr_set_d(a->v, _a, __ENZYME_MPFR_DEFAULT_ROUNDING_MODE);
return __enzyme_fprt_ptr_to_double(a);
}
__ENZYME_MPFR_ATTRIBUTES
double __enzyme_fprt_64_52_const(double _a, int64_t exponent,
int64_t significand, int64_t mode) {
// TODO This should really be called only once for an appearance in the code,
// currently it is called every time a flop uses a constant.
return __enzyme_fprt_64_52_new(_a, exponent, significand, mode);
}
__ENZYME_MPFR_ATTRIBUTES
__enzyme_fp *__enzyme_fprt_64_52_new_intermediate(int64_t exponent,
int64_t significand,
int64_t mode) {
__enzyme_fp *a = (__enzyme_fp *)malloc(sizeof(__enzyme_fp));
mpfr_init2(a->v, significand);
return a;
}
__ENZYME_MPFR_ATTRIBUTES
void __enzyme_fprt_64_52_delete(double a, int64_t exponent, int64_t significand,
int64_t mode) {
free(__enzyme_fprt_double_to_ptr(a));
}
#define __ENZYME_MPFR_SINGOP(OP_TYPE, LLVM_OP_NAME, MPFR_FUNC_NAME, FROM_TYPE, \
RET, MPFR_GET, ARG1, MPFR_SET_ARG1, \
ROUNDING_MODE) \
__ENZYME_MPFR_ATTRIBUTES \
RET __enzyme_fprt_##FROM_TYPE##_##OP_TYPE##_##LLVM_OP_NAME( \
ARG1 a, int64_t exponent, int64_t significand, int64_t mode) { \
if (__enzyme_fprt_is_op_mode(mode)) { \
mpfr_t ma, mc; \
mpfr_init2(ma, significand); \
mpfr_init2(mc, significand); \
mpfr_set_##MPFR_SET_ARG1(ma, a, ROUNDING_MODE); \
mpfr_##MPFR_FUNC_NAME(mc, ma, ROUNDING_MODE); \
RET c = mpfr_get_##MPFR_GET(mc, ROUNDING_MODE); \
mpfr_clear(ma); \
mpfr_clear(mc); \
return c; \
} else if (__enzyme_fprt_is_mem_mode(mode)) { \
__enzyme_fp *ma = __enzyme_fprt_double_to_ptr(a); \
__enzyme_fp *mc = \
__enzyme_fprt_64_52_new_intermediate(exponent, significand, mode); \
mpfr_##MPFR_FUNC_NAME(mc->v, ma->v, ROUNDING_MODE); \
return __enzyme_fprt_ptr_to_double(mc); \
} else { \
abort(); \
} \
}
// TODO this is a bit sketchy if the user cast their float to int before calling
// this. We need to detect these patterns
#define __ENZYME_MPFR_BIN_INT(OP_TYPE, LLVM_OP_NAME, MPFR_FUNC_NAME, \
FROM_TYPE, RET, MPFR_GET, ARG1, MPFR_SET_ARG1, \
ARG2, ROUNDING_MODE) \
__ENZYME_MPFR_ATTRIBUTES \
RET __enzyme_fprt_##FROM_TYPE##_##OP_TYPE##_##LLVM_OP_NAME( \
ARG1 a, ARG2 b, int64_t exponent, int64_t significand, int64_t mode) { \
if (__enzyme_fprt_is_op_mode(mode)) { \
mpfr_t ma, mc; \
mpfr_init2(ma, significand); \
mpfr_init2(mc, significand); \
mpfr_set_##MPFR_SET_ARG1(ma, a, ROUNDING_MODE); \
mpfr_##MPFR_FUNC_NAME(mc, ma, b, ROUNDING_MODE); \
RET c = mpfr_get_##MPFR_GET(mc, ROUNDING_MODE); \
mpfr_clear(ma); \
mpfr_clear(mc); \
return c; \
} else if (__enzyme_fprt_is_mem_mode(mode)) { \
__enzyme_fp *ma = __enzyme_fprt_double_to_ptr(a); \
__enzyme_fp *mc = \
__enzyme_fprt_64_52_new_intermediate(exponent, significand, mode); \
mpfr_##MPFR_FUNC_NAME(mc->v, ma->v, b, ROUNDING_MODE); \
return __enzyme_fprt_ptr_to_double(mc); \
} else { \
abort(); \
} \
}
#define __ENZYME_MPFR_BIN(OP_TYPE, LLVM_OP_NAME, MPFR_FUNC_NAME, FROM_TYPE, \
RET, MPFR_GET, ARG1, MPFR_SET_ARG1, ARG2, \
MPFR_SET_ARG2, ROUNDING_MODE) \
__ENZYME_MPFR_ATTRIBUTES \
RET __enzyme_fprt_##FROM_TYPE##_##OP_TYPE##_##LLVM_OP_NAME( \
ARG1 a, ARG2 b, int64_t exponent, int64_t significand, int64_t mode) { \
if (__enzyme_fprt_is_op_mode(mode)) { \
mpfr_t ma, mb, mc; \
mpfr_init2(ma, significand); \
mpfr_init2(mb, significand); \
mpfr_init2(mc, significand); \
mpfr_set_##MPFR_SET_ARG1(ma, a, ROUNDING_MODE); \
mpfr_set_##MPFR_SET_ARG2(mb, b, ROUNDING_MODE); \
mpfr_##MPFR_FUNC_NAME(mc, ma, mb, ROUNDING_MODE); \
RET c = mpfr_get_##MPFR_GET(mc, ROUNDING_MODE); \
mpfr_clear(ma); \
mpfr_clear(mb); \
mpfr_clear(mc); \
return c; \
} else if (__enzyme_fprt_is_mem_mode(mode)) { \
__enzyme_fp *ma = __enzyme_fprt_double_to_ptr(a); \
__enzyme_fp *mb = __enzyme_fprt_double_to_ptr(b); \
__enzyme_fp *mc = \
__enzyme_fprt_64_52_new_intermediate(exponent, significand, mode); \
mpfr_##MPFR_FUNC_NAME(mc->v, ma->v, mb->v, ROUNDING_MODE); \
return __enzyme_fprt_ptr_to_double(mc); \
} else { \
abort(); \
} \
}
#define __ENZYME_MPFR_FMULADD(LLVM_OP_NAME, FROM_TYPE, TYPE, MPFR_TYPE, \
LLVM_TYPE, ROUNDING_MODE) \
__ENZYME_MPFR_ATTRIBUTES \
TYPE __enzyme_fprt_##FROM_TYPE##_intr_##LLVM_OP_NAME##_##LLVM_TYPE( \
TYPE a, TYPE b, TYPE c, int64_t exponent, int64_t significand, \
int64_t mode) { \
if (__enzyme_fprt_is_op_mode(mode)) { \
mpfr_t ma, mb, mc, mmul, madd; \
mpfr_init2(ma, significand); \
mpfr_init2(mb, significand); \
mpfr_init2(mc, significand); \
mpfr_init2(mmul, significand); \
mpfr_init2(madd, significand); \
mpfr_set_##MPFR_TYPE(ma, a, ROUNDING_MODE); \
mpfr_set_##MPFR_TYPE(mb, b, ROUNDING_MODE); \
mpfr_set_##MPFR_TYPE(mc, c, ROUNDING_MODE); \
mpfr_mul(mmul, ma, mb, ROUNDING_MODE); \
mpfr_add(madd, mmul, mc, ROUNDING_MODE); \
TYPE res = mpfr_get_##MPFR_TYPE(madd, ROUNDING_MODE); \
mpfr_clear(ma); \
mpfr_clear(mb); \
mpfr_clear(mc); \
mpfr_clear(mmul); \
mpfr_clear(madd); \
return res; \
} else if (__enzyme_fprt_is_mem_mode(mode)) { \
__enzyme_fp *ma = __enzyme_fprt_double_to_ptr(a); \
__enzyme_fp *mb = __enzyme_fprt_double_to_ptr(b); \
__enzyme_fp *mc = __enzyme_fprt_double_to_ptr(c); \
double mmul = __enzyme_fprt_##FROM_TYPE##_binop_fmul( \
__enzyme_fprt_ptr_to_double(ma), __enzyme_fprt_ptr_to_double(mb), \
exponent, significand, mode); \
double madd = __enzyme_fprt_##FROM_TYPE##_binop_fadd( \
mmul, __enzyme_fprt_ptr_to_double(mc), exponent, significand, mode); \
return madd; \
} else { \
abort(); \
} \
}
// TODO This does not currently make distinctions between ordered/unordered.
#define __ENZYME_MPFR_FCMP_IMPL(NAME, ORDERED, CMP, FROM_TYPE, TYPE, MPFR_GET, \
ROUNDING_MODE) \
__ENZYME_MPFR_ATTRIBUTES \
bool __enzyme_fprt_##FROM_TYPE##_fcmp_##NAME( \
TYPE a, TYPE b, int64_t exponent, int64_t significand, int64_t mode) { \
if (__enzyme_fprt_is_op_mode(mode)) { \
mpfr_t ma, mb; \
mpfr_init2(ma, significand); \
mpfr_init2(mb, significand); \
mpfr_set_##MPFR_GET(ma, a, ROUNDING_MODE); \
mpfr_set_##MPFR_GET(mb, b, ROUNDING_MODE); \
int ret = mpfr_cmp(ma, mb); \
mpfr_clear(ma); \
mpfr_clear(mb); \
return ret CMP; \
} else if (__enzyme_fprt_is_mem_mode(mode)) { \
__enzyme_fp *ma = __enzyme_fprt_double_to_ptr(a); \
__enzyme_fp *mb = __enzyme_fprt_double_to_ptr(b); \
int ret = mpfr_cmp(ma->v, mb->v); \
return ret CMP; \
} else { \
abort(); \
} \
}
__ENZYME_MPFR_ORIGINAL_ATTRIBUTES
bool __enzyme_fprt_original_64_52_intr_llvm_is_fpclass_f64(double a,
int32_t tests);
__ENZYME_MPFR_ATTRIBUTES bool
__enzyme_fprt_64_52_intr_llvm_is_fpclass_f64(double a, int32_t tests) {
return __enzyme_fprt_original_64_52_intr_llvm_is_fpclass_f64(a, tests);
}
#include "flops.def"
#ifdef __cplusplus
}
#endif
#endif // #ifndef __ENZYME_RUNTIME_ENZYME_MPFR__