Skip to content

Commit e2fc25d

Browse files
authored
Merge branch 'main' into codex/output-chrome-/tracing-json-trace
2 parents 32b48f8 + 55fd399 commit e2fc25d

File tree

7 files changed

+98
-104
lines changed

7 files changed

+98
-104
lines changed

src/config.rkt

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
#lang racket
22

3+
(require racket/hash)
4+
35
(provide (all-defined-out))
46

57
;;; Flags
68

7-
(define all-flags
8-
#hash([precision . (double fallback)]
9-
[setup . (simplify search)]
10-
[localize . (costs errors)]
11-
[generate . (rr taylor simplify better-rr proofs egglog evaluate)]
12-
[reduce . (regimes avg-error binary-search branch-expressions simplify)]
13-
[rules
14-
. (arithmetic polynomials
15-
fractions
16-
exponents
17-
trigonometry
18-
hyperbolic
19-
numerics
20-
special
21-
bools
22-
branches)]
23-
[dump . (egg rival egglog trace)]))
24-
259
(define default-flags
2610
#hash([precision . ()]
2711
[setup . (search)]
@@ -31,22 +15,20 @@
3115
[rules . (arithmetic polynomials fractions exponents trigonometry hyperbolic)]
3216
[dump . ()]))
3317

18+
(define deprecated-flags
19+
#hash([precision . (double fallback)]
20+
[setup . (simplify)]
21+
[localize . (costs errors)]
22+
[generate . (better-rr simplify)]
23+
[reduce . (avg-error simplify)]
24+
[rules . (numerics special bools branches)]))
25+
26+
(define debug-flags #hash([generate . (egglog)] [dump . (egg rival egglog trace)]))
27+
28+
(define all-flags (hash-union default-flags deprecated-flags debug-flags #:combine set-union))
29+
3430
(define (flag-deprecated? category flag)
35-
(match* (category flag)
36-
[('precision 'double) #t]
37-
[('precision 'fallback) #t]
38-
[('setup 'simplify) #t]
39-
[('generate 'better-rr) #t]
40-
[('generate 'simplify) #t]
41-
[('reduce 'simplify) #t]
42-
[('reduce 'avg-error) #t]
43-
[('localize 'costs) #t]
44-
[('localize 'errors) #t]
45-
[('rules 'numerics) #t]
46-
[('rules 'special) #t]
47-
[('rules 'bools) #t]
48-
[('rules 'branches) #t]
49-
[(_ _) #f]))
31+
(set-member? (dict-ref deprecated-flags category '()) flag))
5032

5133
; `hash-copy` returns a mutable hash, which makes `dict-update` invalid
5234
(define *flags* (make-parameter (make-immutable-hash (hash->list default-flags))))

src/core/egg-herbie.rkt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,44 @@
503503
(cons egg-rule ffi-rule)))))
504504
(for-each sow egg&ffi-rules))))
505505

506+
;; Rules from impl to spec (fixed for a particular platform)
507+
(define/reset *lifting-rules* (make-hash))
508+
509+
;; Rules from spec to impl (fixed for a particular platform)
510+
(define/reset *lowering-rules* (make-hash))
511+
512+
;; Synthesizes the LHS and RHS of lifting/lowering rules.
513+
(define (impl->rule-parts impl)
514+
(define vars (impl-info impl 'vars))
515+
(define spec (impl-info impl 'spec))
516+
(values vars spec (cons impl vars)))
517+
518+
;; Synthesizes lifting rules for a platform platform.
519+
(define (platform-lifting-rules [pform (*active-platform*)])
520+
(define impls (platform-impls pform))
521+
(for/list ([impl (in-list impls)])
522+
(hash-ref! (*lifting-rules*)
523+
(cons impl pform)
524+
(lambda ()
525+
(define name (sym-append 'lift- impl))
526+
(define-values (vars spec-expr impl-expr) (impl->rule-parts impl))
527+
(rule name impl-expr spec-expr '(lifting))))))
528+
529+
;; Synthesizes lowering rules for a given platform.
530+
(define (platform-lowering-rules [pform (*active-platform*)])
531+
(define impls (platform-impls pform))
532+
(append* (for/list ([impl (in-list impls)])
533+
(hash-ref! (*lowering-rules*)
534+
(cons impl pform)
535+
(lambda ()
536+
(define name (sym-append 'lower- impl))
537+
(define-values (vars spec-expr impl-expr) (impl->rule-parts impl))
538+
(list (rule name spec-expr impl-expr '(lowering))
539+
(rule (sym-append 'lower-unsound- impl)
540+
(add-unsound spec-expr)
541+
impl-expr
542+
'(lowering))))))))
543+
506544
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
507545
;; Racket egraph
508546
;;

src/syntax/generators.rkt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"types.rkt")
99

1010
(provide from-rival
11+
from-ffi
1112
from-libm
1213
from-bigfloat
1314
define-generator
@@ -32,32 +33,36 @@
3233
(first exs)
3334
fail)))
3435

35-
; ----------------------- LIBM GENERATOR ----------------------------
36+
; ----------------------- FFI GENERATOR -----------------------------
3637

3738
;; Looks up a function `name` with type signature `itype -> ... -> otype`
38-
;; in the system libm and returns the FFI function or `#f` if
39-
;; the procedure cannot be found.
39+
;; in the given FFI library and returns the function or `#f` if it
40+
;; cannot be found.
4041
;; ```
41-
;; (make-libm (<name> <itype> ... <otype))
42+
;; (make-ffi <lib> (<name> <itype> ... <otype>))
4243
;; ```
43-
(define (make-libm name itypes otype)
44+
(define (make-ffi lib name itypes otype)
4445
; Repr matching
4546
(define (repr->ffi repr)
4647
(match (representation-name repr)
4748
['binary64 _double]
4849
['binary32 _float]
4950
['integer _int]
5051
[else (raise-syntax-error 'repr->type "unknown type" repr)]))
51-
(get-ffi-obj name #f (_cprocedure (map repr->ffi itypes) (repr->ffi otype)) (const #f)))
52+
(get-ffi-obj name lib (_cprocedure (map repr->ffi itypes) (repr->ffi otype)) (const #f)))
5253

53-
(define-generator ((from-libm name) spec ctx)
54+
(define-generator ((from-ffi lib name) spec ctx)
5455
(let ([itypes (context-var-reprs ctx)]
5556
[otype (context-repr ctx)])
56-
(define fl (make-libm name itypes otype))
57+
(define fl (make-ffi lib name itypes otype))
5758
(unless fl
58-
(error 'libm-generator "Could not find libm implementation of `~a ~a ~a`" otype name itypes))
59+
(error 'ffi-generator "Could not find FFI implementation of `~a ~a ~a`" otype name itypes))
5960
fl))
6061

62+
(define libm-lib (ffi-lib #f))
63+
(define (from-libm name)
64+
(from-ffi libm-lib name))
65+
6166
; ----------------------- BIGFLOAT GENERATOR ------------------------
6267

6368
(define (repr->bf x type)

src/syntax/platform-language.rkt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@
244244
; Update table
245245
(hash-set! impls (operator-impl-name impl) impl))
246246

247+
(define (validate-platform! platform)
248+
(when (empty? (platform-implementations platform))
249+
(raise-herbie-error "Platform contains no operations"))
250+
(for ([(name impl) (in-hash (platform-implementations platform))])
251+
(define ctx (operator-impl-ctx impl))
252+
(for ([repr (in-list (cons (context-repr ctx) (context-var-reprs ctx)))])
253+
(unless (equal? (hash-ref (platform-representations platform) (representation-name repr) #f)
254+
repr)
255+
(raise-herbie-error "Representation ~a not defined" (representation-name repr))))))
256+
247257
(define-syntax (platform-register-implementations! stx)
248258
(syntax-case stx ()
249259
[(_ platform ([name ([var : repr] ...) otype spec fl fpcore cost] ...))

src/syntax/platform.rkt

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
(require "../utils/common.rkt"
55
"../utils/errors.rkt"
66
"../config.rkt"
7-
"../core/rules.rkt"
87
"matcher.rkt"
98
"types.rkt"
109
"syntax.rkt"
@@ -29,10 +28,7 @@
2928
(fprintf port "#<platform>"))])
3029

3130
(provide *active-platform*
32-
platform-lifting-rules
33-
platform-lowering-rules
3431
platform-copy
35-
validate-platform!
3632
repr-exists?
3733
get-representation
3834
impl-exists?
@@ -68,16 +64,6 @@
6864
(define impls (make-hash))
6965
(create-platform reprs impls repr-costs))
7066

71-
(define (validate-platform! platform)
72-
(when (empty? (platform-implementations platform))
73-
(raise-herbie-error "Platform contains no operations"))
74-
(for ([(name impl) (in-hash (platform-implementations platform))])
75-
(define ctx (operator-impl-ctx impl))
76-
(for ([repr (in-list (cons (context-repr ctx) (context-var-reprs ctx)))])
77-
(unless (equal? (hash-ref (platform-representations platform) (representation-name repr) #f)
78-
repr)
79-
(raise-herbie-error "Representation ~a not defined" (representation-name repr))))))
80-
8167
;; Returns the representation associated with `name`
8268
;; attempts to generate the repr if not initially found
8369
(define (get-representation name)
@@ -181,44 +167,6 @@
181167
(define itypes (impl-info impl 'itype))
182168
(apply cost-proc (map loop args itypes))]))))
183169

184-
;; Rules from impl to spec (fixed for a particular platform)
185-
(define/reset *lifting-rules* (make-hash))
186-
187-
;; Rules from spec to impl (fixed for a particular platform)
188-
(define/reset *lowering-rules* (make-hash))
189-
190-
;; Synthesizes the LHS and RHS of lifting/lowering rules.
191-
(define (impl->rule-parts impl)
192-
(define vars (impl-info impl 'vars))
193-
(define spec (impl-info impl 'spec))
194-
(values vars spec (cons impl vars)))
195-
196-
;; Synthesizes lifting rules for a platform platform.
197-
(define (platform-lifting-rules [pform (*active-platform*)])
198-
(define impls (platform-impls pform))
199-
(for/list ([impl (in-list impls)])
200-
(hash-ref! (*lifting-rules*)
201-
(cons impl pform)
202-
(lambda ()
203-
(define name (sym-append 'lift- impl))
204-
(define-values (vars spec-expr impl-expr) (impl->rule-parts impl))
205-
(rule name impl-expr spec-expr '(lifting))))))
206-
207-
;; Synthesizes lowering rules for a given platform.
208-
(define (platform-lowering-rules [pform (*active-platform*)])
209-
(define impls (platform-impls pform))
210-
(append* (for/list ([impl (in-list impls)])
211-
(hash-ref! (*lowering-rules*)
212-
(cons impl pform)
213-
(lambda ()
214-
(define name (sym-append 'lower- impl))
215-
(define-values (vars spec-expr impl-expr) (impl->rule-parts impl))
216-
(list (rule name spec-expr impl-expr '(lowering))
217-
(rule (sym-append 'lower-unsound- impl)
218-
(add-unsound spec-expr)
219-
impl-expr
220-
'(lowering))))))))
221-
222170
;; Extracts the `fpcore` field of an operator implementation
223171
;; as a property dictionary and expression.
224172
(define (impl->fpcore impl)

www/doc/2.3/platforms.html

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,22 @@ <h3>Math Library Functions</h3>
214214
<pre>(define-operation (fabs.f32 [x &lt;binary32&gt;]) &lt;binary32&gt;
215215
#:spec (fabs x) #:impl (from-libm 'fabsf) #:cost 0.125)</pre>
216216

217-
<p>The <code>from-libm</code> uses dynamic linking to
218-
load <code>libm</code>, extracts the symbol passed
219-
to <code>from-libm</code>, and then uses the operation's signature
220-
to call into the dynamically-linked library from Racket. Must make
221-
sure to pass the correct symbol name; for single-precision
222-
functions, make sure to add the "<code>f</code>" suffix.</p>
217+
<p>The <code>from-libm</code> helper uses dynamic linking to load
218+
<code>libm</code>, extracts the symbol passed to
219+
<code>from-libm</code>, and then uses the operation's signature to
220+
call into the dynamically-linked library from Racket. Be sure to
221+
pass the correct symbol name; for single-precision functions, add the
222+
"<code>f</code>" suffix.</p>
223+
224+
<p>For other libraries, open them with
225+
<code>ffi-lib</code> and use <code>from-ffi</code>, which
226+
generalizes <code>from-libm</code>. For example, to load functions
227+
from the GNU Scientific Library:</p>
228+
229+
<pre>(require ffi/unsafe)
230+
(define libgsl (ffi-lib "gsl"))
231+
(define-operation (cos.gsl [x &lt;binary64&gt;]) &lt;binary64&gt;
232+
#:spec (cos x) #:impl (from-ffi libgsl 'gsl_sf_cos) #:cost 0.125)</pre>
223233

224234
<h3>Numeric Variations</h3>
225235

www/doc/2.3/plugins.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ <h2>Defining representations</h2>
114114
<h2>Defining Generators</h2>
115115

116116
<p><dfn>Generators</dfn> are helper functions for generating
117-
implementations in <code>define-operation</code>. For
118-
example, <a href="platforms.html"><code>from-libm</code>
119-
and <code>from-rival</code></a> are generators.</p>
117+
implementations in <code>define-operation</code>. For example,
118+
<a href="platforms.html"><code>from-ffi</code></a> is a
119+
generator.</p>
120120

121121
<p>To define a generator, use <code>define-generator</code>:</p>
122122

@@ -128,7 +128,8 @@ <h2>Defining Generators</h2>
128128
<p>Here, <code>from-<var>foo</var></code> is the name of your
129129
generator, and <var>args</var> are any additional arguments the
130130
generator takes. For example, <code>from-libm</code> takes one
131-
argument, the symbol name.</p>
131+
argument, the symbol name, while <code>from-ffi</code> takes an open
132+
library and a symbol name.</p>
132133

133134
<p>Then, inside the <var>body</var>, you can use those arguments as
134135
well as <code>spec</code> and <code>ctx</code>, to construct an

0 commit comments

Comments
 (0)