-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat.rkt
1073 lines (873 loc) · 32.9 KB
/
sat.rkt
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket
(require
"util.rkt"
sxml
srfi/1
srfi/13)
(provide (all-defined-out))
(define DEBUG_MODE (getenv "ACCSAT_DEBUG"))
(define (sat sxml)
((compose (update 'OMPPragma omp-updater)
(update 'ACCPragma acc-updater)) sxml))
(define (update symbol proc)
(lambda (node)
(if (not (pair? node)) node
(cons
(car node)
(cdr
(pre-post-order
;; Skip the top node
(cons '_tmp (cdr node))
`((,symbol *preorder* . ,(lambda args (proc args)))
(*text* . ,(lambda (tag content) content))
(*default* . ,(lambda args args)))))
))))
(define (acc-updater node)
(define dirname ((if-car-sxpath "string") node))
(define fn
(match dirname
[(list _ (or "PARALLEL" "PARALLEL_LOOP"))
parallel-updater]
[(list _ (or "KERNELS" "KERNELS_LOOP"))
kernels-updater]
[else sat]))
(fn node))
(define (parallel-updater node)
(define start (current-inexact-milliseconds))
(define ret ((update 'forStatement for-updater) node))
(define end (current-inexact-milliseconds))
(when DEBUG_MODE
(eprintf "[ACCSAT] Time: ~a \n" (/ (- end start) 1000)))
ret)
(define (kernels-updater node)
(parallel-updater node))
(define (omp-updater node)
(define dirname ((if-car-sxpath "string") node))
(define fn
(match dirname
[(list _ (or (regexp #rx"TARGET_TEAMS") (regexp #rx"TEAMS"))) parallel-updater]
[else sat]))
(fn node))
;;
;; 1. Get kernel code
;; { S1 = ..; S2 = ..; ... }
;;
;; 2. Make SSA without if/for/while/.. structures
;; (( S1 .. ) ( S2 .. ) ...)
;;
;; 3. Put each expression in a e-graph in Rust
;;
;; 4. Saturate; Extract with ILP to select all the expression
;; (Get the minimum of (list s0 s1 s2 ...) )
;;
;; 5. Update SSA with extended statements for common sub-expressions
;; (( E1 .. ) ( S1 ..) ( S2 ..) ...)
;;
;; 6. Update kernel code while checking corresponding statements by hashing
;; { E1 = ..; S1 = ..; S2 = ..; ... }
;;
;;
;; SSA conversion
;;
;; e.g. a[i] = x; a[i+1] = a[i]+1;
;; => (( a0 (st a a i x) ) (a1 (st a a0 (+ i 1) (+ (ld a0 i) 1))))
;;
;; for (i) { a[i] = a[i-1]; a[i*2] = a[i-1]; }
;; => (( a0 (st a (phi for-cond for-a a-init) i
;; (ld a (phi for-cond for-a a-init) (- i 1)) ))
;; ( a1 (st a a0 (* i 2) (ld a0 (- i 1)) )))
;;
;; if (k) { a += 1 } a += 1;
;; => (( a0 (+ a-init 1)) (a1 (+ (phi k a0 a-init) 1)))
;;
[define (for-updater node)
(define-values (ssa0 _) (extract-ssa node))
(define ssa1 (reduce-st-chain ssa0))
(define ssa2 (reverse (delete-unnecessary-phi ssa1)))
;; var + hash concat
(define sat-in
(map (match-lambda [(list var hash expr)
(list (format "~a__~a" var hash) expr)])
ssa2))
(define rewriter-path
(build-path
(path-only (path->complete-path (find-system-path 'run-file)))
"rewriter/target/release/rewriter"))
(define output-path (make-temporary-file))
(define-values (sp oport iport eport)
(subprocess #f #f #f rewriter-path output-path))
(displayln sat-in iport)
(close-output-port iport)
(subprocess-wait sp)
(if DEBUG_MODE
(for-each (curry eprintf "[ACCSAT] ~a \n") (port->lines eport))
(close-input-port eport))
(close-input-port oport)
(define tmp-out
(let ()
(define x (file->list output-path))
(if (null? x) '()
(car x))))
(define sat-out
;; symbol->string map except op
(let ()
(define (each x)
(list (f (~ x 0)) (f (~ x 1))))
(define (f x)
(cond [(pair? x) (cons (car x) (map f (cdr x)))]
[(symbol? x) (symbol->string x)]
[else x]))
(map each tmp-out)))
(define out-ssa
(map (match-lambda [(list id expr)
(define x (string-split id "__"))
(define v (~ x 0))
(define h (if (tmpvar? v) (~ x 1)
(string->number (~ x 1))))
`(,v ,h ,expr)])
sat-out))
(update-with-ssa node out-ssa)]
(define (update-with-ssa node env)
;; Put extensional variables and new expressions
(extend-node node env))
(define TMPTAG "_v")
(define (tmpvar? v)
;; string-prefix? in srfi-13's order
(string-prefix? TMPTAG v))
(define (remove-varhash var)
(car (string-split var "__")))
(define (cut-original-var var)
(if (not (tmpvar? var)) var
;; This remove-varhash is for variables in inner expressions (still having hash)
(remove-varhash (~a TMPTAG (car (string-split var TMPTAG))))))
(define (test-add-vars env)
;; '((varname hash_hash_.. expr) ...)
(define ext
(append-map
(match-lambda
[(list _ h1 _)
(filter-map
(match-lambda
[(list _ h2 _)
(and (number? h1) (number? h2) (< 0 h1 h2)
(list (format "~a~a_~a" TMPTAG h1 h2)
(format "~a_~a" h1 h2) "1"))])
env)])
env))
(append ext env))
(define (ssa-code->xcodeml a)
(define (rec n) (ssa-code->xcodeml n))
(define (cast n) `(castExpr (@ (type "int")) ,n))
(match a
[(? number? _)
(define s (number->string a))
(if (exact-integer? a) `(intConstant ,s) `(floatConstant ,s)) ]
[(? string? _)
(if (regexp-match "[fF]$" a)
`(floatConstant ,a)
`(Var ,(cut-original-var a)))]
[(list (or 'st 'st2 'st3 'st4 'st5 'st6) name _ args ... expr)
`(assignExpr
(arrayRef (@ (type "none")) ; type is required but not actually used
(arrayAddr ,name)
,@(map (compose cast rec) args))
,(rec expr))]
[(list (or 'ld 'ld2 'ld3 'ld4 'ld5 'ld6) name _ args ...)
`(arrayRef (@ (type "none"))
(arrayAddr ,name)
,@(map (compose cast rec) args))]
[(list 'fma a b c) `(plusExpr ,(rec a) (mulExpr ,(rec b) ,(rec c)))]
[(list 'mfma a b c) `(minusExpr ,(rec a) (mulExpr ,(rec b) ,(rec c)))]
[(list 'asgFma a b c) `(asgPlusExpr ,(rec a) (mulExpr ,(rec b) ,(rec c)))]
[(list 'asgMfma a b c) `(asgMinusExpr ,(rec a) (mulExpr ,(rec b) ,(rec c)))]
[(list 'functionCall name args ...)
(cond [(equal? name "__cast")
`(castExpr (@ (type ,(~ args 0))) ,(rec (~ args 1)))]
[(equal? name "__moe")
`(moeConstant (@ (type ,(~ args 0))) ,(~ args 1))]
[(equal? name "__memberref")
`(memberRef (@ (member ,(~ args 1)) (type ,(~ args 2)) )
,(sxml:set-attr (rec (~ args 0)) `(type ,(~ args 3)) ))]
[(equal? name "__varAddr")
`(varAddr ,(car (sxml:content (rec (~ args 0)))))]
[else
`(functionCall
(function (funcAddr ,name))
(arguments ,@(map rec args)))])]
[(list (and (or
'plusExpr 'minusExpr 'mulExpr 'divExpr 'modExpr 'condExpr
'LshiftExpr 'RshiftExpr 'bitAndExpr 'bitOrExpr 'bitXorExpr
'logEQExpr 'logNEQExpr 'logGEExpr 'logGTExpr 'logLEExpr
'logLTExpr 'logAndExpr 'logOrExpr 'unaryMinusExpr 'bitNotExpr
'asgPlusExpr 'asgMinusExpr 'asgMulExpr 'asgDivExpr)
op) args ...)
`(,op ,@(map rec args))]
))
;;
;; Put new compounds to declare extensional variables
;;
;; New variables could refer other new ones via varname of env
;; where env ::= ((varname hash_hash_.. expr) ...;
;;
;; Therefore, env must contain new variables in the order of declaration.
;;
;;
;; Also, Update expressions with new ones
;;
(define (extend-node node env)
(define extension
(filter (compose string? (curryr ~ 1)) env))
(define ext-vars (map car extension))
(define var-to-asglist
(map (lambda (x)
(cons (~ x 0)
(map string->number (string-split (~ x 1) "_"))))
extension))
(define var-to-asglistH (make-hash var-to-asglist))
(define var-to-expr (map (lambda (x) (cons (~ x 0) (~ x 2))) extension))
;; '((hash-of-compound hash-of-asg ...) ...)
;; containing negligible a (hash-of-asg . hash-of-asg) map.
;;
;; `reverse` for ascending order (from smaller to bigger scopes)
;; Nested (asg contained in several relevant compounds)
(define comp-to-asglist (reverse (decl-mapping node)))
;; Nested (asg contained in several relevants nodes)
(define node-to-asglist (decl-mapping node #t))
;; '((ext-varname . minimum-scope) ...)
(define var-to-comp
(map
(lambda (v)
(define asglist (hash-ref var-to-asglistH v))
(define s
(find (compose null? (curry lset-difference = asglist))
comp-to-asglist))
(cons v (car s)))
ext-vars))
;; '((hash-of-compound ext-varname ...) ...)
;; var contained only in the minimum compound
(define comp-to-varlist (reverse-assoc var-to-comp))
;; '((hash-of-asg ext-varname ...) ...) ; var contained in several places
(define asg-to-varlist (reverse-assoc-multi var-to-asglist))
;; '((hash-of-node ext-varname ...) ...) ; var contained in several places
(define node-to-varlist
(map
(lambda (x)
(cons
(car x)
(delete-duplicates
(append-map (lambda (a) (assoc-ref asg-to-varlist a '())) (cdr x)))))
node-to-asglist))
(define var-to-type0
(fold
(lambda (pair alist)
(assoc-adjoin alist (car pair) (cdr pair)))
'()
(append
(map (lambda (v)
(cons ((if-car-sxpath '(*text*)) v)
((if-car-sxpath '(@ type *text*)) v)))
((sxpath "//Var") node))
(map (lambda (p)
(cons ((if-car-sxpath '(plusExpr (Var 1) *text*)) p)
((if-car-sxpath '(@ type *text*)) p)))
((sxpath "//pointerRef") node))
(map (lambda (a)
(cons ((if-car-sxpath '(arrayAddr *text*)) a)
((if-car-sxpath '(@ type *text*)) a)))
((sxpath "//arrayRef") node)))))
(define var-to-type1
(append-map
(lambda (x)
(define varname (lv-name (car (sxml:content x))))
(define type (assoc-ref var-to-type0 varname))
(define ref-tmpvars
(filter-map (lambda (e)
(and (equal? (~ e 1) (eq-hash-code x))
((conjoin string? tmpvar? remove-varhash) (~ e 2))))
env))
(map (curryr cons type) ref-tmpvars))
((sxpath "//assignExpr") node)))
;; todo:
;; consider casting by storing
;; (currently no local variables are referenced by temporary variables;
;; thus, no such information is used -> partially supported now)
;; consider type difference among register reuse or separate
(define var-to-type
(let loop ([v2t (append var-to-type0 var-to-type1)] [pending (map car var-to-expr)])
(define ca (and (pair? pending) (car pending)))
(cond [(not ca) v2t]
;; Cast by storing
[(assoc-ref v2t ca)
=> (lambda (t)
(loop v2t (cdr pending)))]
[(pair? (lset-intersection
equal? (get-var-dep ca (curry assoc-ref var-to-expr))
pending))
(loop v2t (append (cdr pending) (list ca)))]
[else ;; dependency solved
(loop
(assoc-adjoin v2t ca (infer-type (assoc-ref var-to-expr ca) v2t))
(cdr pending))])))
(extend-compound node env
(make-hash comp-to-varlist)
(make-hash node-to-varlist)
(make-hash var-to-expr)
(make-hash var-to-type)))
(define (infer-type e v2t)
(define (it x) (infer-type x v2t))
(define (select-type types)
(define order '("double" "float" "long" "int"))
(or
;; pointer, struct
(any (conjoin (negate (curryr member order))
(negate (curry string-prefix? "B")) ; avoid const scalar
values) types)
;; scalar
(find (curryr member types) order)
"double"))
(match e
[(? number? _)
(if (exact-integer? e) "int" "float")]
[(? string? _)
(assoc-ref v2t (cut-original-var e))]
[(? (curryr sxml:attr 'type) type) type]
[(list 'functionCall name args ...)
(cond [(equal? name "__cast") (~ args 0)]
[(equal? name "__moe") "int"]
[(equal? name "__memberref") (~ args 2)]
[(equal? name "__varAddr") (~ args 1)]
;; todo check declaration
[else (select-type (map it args))])]
[(list (or 'ld 'ld2 'ld3 'ld4 'ld5 'ld6) name _ args ...)
(assoc-ref v2t name)]
[(list 'condExpr cond then else)
(select-type (map it (list then else)))]
[(list (and (or
'fma 'asgFma 'mfma 'asgMfma
'plusExpr 'minusExpr 'mulExpr 'divExpr 'modExpr
'LshiftExpr 'RshiftExpr 'bitAndExpr 'bitOrExpr 'bitXorExpr
'logEQExpr 'logNEQExpr 'logGEExpr 'logGTExpr 'logLEExpr
'logLTExpr 'logAndExpr 'logOrExpr 'unaryMinusExpr
'bitNotExpr
'asgPlusExpr 'asgMinusExpr 'asgMulExpr 'asgDivExpr)
op) args ...)
(select-type (map it args))]
))
(define (get-var-dep v expr-ref)
(filter-map
(conjoin string? tmpvar? remove-varhash)
(flatten (expr-ref v))))
(define (extend-compound
node env comp-to-varlist node-to-varlist var-to-expr var-to-type)
(define (rec n)
(extend-compound
n env comp-to-varlist node-to-varlist var-to-expr var-to-type))
;; Decide the order of temp vars declared in the same spots
(define (sort-vars vars)
(define var-to-dep
(map (lambda (v) (cons v (get-var-dep v (curry hash-ref var-to-expr))))
vars))
(define (previous v)
(~a TMPTAG (string-join (drop-right (string-split v TMPTAG) 1) TMPTAG)))
(let loop ([ret '()] [pending vars])
(if (null? pending) ret
(let ()
(define-values
(here there)
(partition
(lambda (x)
(and
;; Dependency among vars solved?
(null? (lset-intersection
equal? vars
(lset-difference equal? (assoc-ref var-to-dep x) ret)))
;; Not to overwrite variables still read by other vars
(not (member (previous x)
(append-map (curry assoc-ref var-to-dep)
;; srfi-1 overwrites remove
(remove (curry equal? x) pending))))))
pending))
;; Have loads first
(define-values
(loads rest)
(partition
(lambda (x)
(memq (car (hash-ref var-to-expr x))
'(ld ld2 ld3 ld4 ld5 ld6)))
here))
(define sorted-loads
(sort
loads
(lambda (a b)
(define ea (hash-ref var-to-expr a))
(define eb (hash-ref var-to-expr b))
(if (not (equal? (~ ea 1) (~ eb 1)))
(string<? (~ ea 1) (~ eb 1))
(let ()
(define ia (drop ea 3))
(define ib (drop eb 3))
(define diff
(findf (negate (curry apply equal?)) (map list ia ib)))
(define da (~ diff 0))
(define db (~ diff 1))
(if (and (number? da) (number? db)) (< da db)
(string<? da db)))
))))
(loop (append ret sorted-loads rest) there)))))
(define (build-compound nodes vars)
(define-values (decl update) ; declared and overwritten
(partition
(compose (curry = 1) length (curryr string-split TMPTAG)) vars))
;; No initialization at declaration in order to keep vars sorted
(set! update (sort-vars vars))
(define update-stmts
(map
(lambda (v)
(define orig (cut-original-var v))
(define expr (ssa-code->xcodeml (hash-ref var-to-expr v)))
`(exprStatement
,(if (member (car expr)
'(asgPlusExpr asgMinusExpr asgMulExpr asgDivExpr))
expr
`(assignExpr (Var ,orig) ,expr))))
update))
`(compoundStatement
(symbols
,@(map
(lambda (v)
`(id (@ (type ,(hash-ref var-to-type v))) (name ,v)))
decl))
(declarations
,@(map
(lambda (v)
`(varDecl
(name ,v)
#;(value ,(ssa-code->xcodeml (hash-ref var-to-expr v)) )))
decl))
(body ,@update-stmts ,@nodes)))
(define (build-body nodes ext-vars-ht)
(if (null? nodes) '()
(let ()
(define n (car nodes))
(define h (eq-hash-code n))
(define vars (hash-ref node-to-varlist h))
(define impl (filter (curry hash-update?! ext-vars-ht) vars))
(define suc (cons (rec n) (build-body (cdr nodes) ext-vars-ht)))
(if (null? impl) suc
(list (build-compound suc impl))))))
(define (list->marking-hash lst)
(define ht (make-hash))
(for-each (lambda (v) (hash-set! ht v #t)) lst)
ht)
(define (hash-mark! ht v)
(hash-set! ht v #f))
(define (hash-unmarked? ht v)
(hash-ref ht v #f))
(define (hash-update?! ht v)
(and (hash-unmarked? ht v)
(hash-mark! ht v)))
(match (and (pair? node) (sxml:name node))
['compoundStatement
(define h (eq-hash-code node))
(define ext-vars-ht (list->marking-hash (hash-ref comp-to-varlist h '())))
;; ext-vars for vars declared here
(define here
(filter
(curry hash-update?! ext-vars-ht)
(delete-duplicates
(append-map
(lambda (x) (hash-ref node-to-varlist (eq-hash-code x) '()))
((sxpath "declarations/varDecl") node)))))
(for-each (curry hash-mark! ext-vars-ht) here)
(define new-body (build-body ((sxpath "body/*") node) ext-vars-ht))
(define used '())
(define vardecl-to-vars
(map
(lambda (x)
(filter
(lambda (v)
(and (member v here) (not (member v used))
(set! used (cons v used))))
(hash-ref node-to-varlist (eq-hash-code x) '())))
((sxpath "declarations/varDecl") node)))
;; Split declarations to insert additional ones and assigns
(fold
(lambda (vars s d b)
(define r
`(compoundStatement
(symbols ,s) (declarations ,d)
(body ,b)))
(if (null? vars) r (build-compound (list r) vars)))
`(compoundStatement (symbols) (declarations) (body ,@new-body))
(reverse vardecl-to-vars)
(reverse ((sxpath "symbols/*") node))
(reverse (map rec ((sxpath "declarations/*") node))))]
[(or 'assignExpr 'varDecl
'postIncrExpr 'postDecrExpr 'preIncrExpr 'preDecrExpr
'asgPlusExpr 'asgMinusExpr 'asgMulExpr 'asgDivExpr
'asgModExpr 'asgLshiftExpr 'asgRshiftExpr 'asgBitAndExpr
'asgBitOrExpr 'asgBitXorExpr)
(define r (map rec node)) ;; todo nest asg (make compound with the current asg)
(define h (eq-hash-code node))
;; env is necessary because varlist of node-to-varlist could allow nests
(define e (find (lambda (e) (equal? (~ e 1) h)) env))
(if (not e) r
(let ()
(define out (ssa-code->xcodeml (~ e 2)))
(cond [(eq? (car out) 'assignExpr) out] ; in the case of stN
[(eq? (sxml:name node) 'varDecl)
`(varDecl (name ,(~ e 0)) (value ,out))]
[else
(if (and (eq? (car out) 'Var) (equal? (cadr out) (~ e 0)))
'(intConstant "0") ; for removed stN
`(assignExpr (Var ,(~ e 0)) ,out))]
)))]
[#f node]
[else (map rec node)]))
;; '((hash-of-compound hash-of-asg ...) ...)
(define (decl-mapping node [include-all #f] [nest #t])
(define (rec n)
(decl-mapping n include-all nest))
(match (and (pair? node) (sxml:name node))
[(or 'assignExpr 'varDecl
'postIncrExpr 'postDecrExpr 'preIncrExpr 'preDecrExpr
'asgPlusExpr 'asgMinusExpr 'asgMulExpr 'asgDivExpr
'asgModExpr 'asgLshiftExpr 'asgRshiftExpr 'asgBitAndExpr
'asgBitOrExpr 'asgBitXorExpr)
(define r (append-map rec node))
(define h (eq-hash-code node))
(cons (list h h) r)]
[#f '()]
[else
(define r (append-map rec node))
(define h (eq-hash-code node))
(if (not (or include-all (eq? (sxml:name node) 'compoundStatement))) r
(let ()
(define-values (comp asg)
(partition
(conjoin (compose (curry <= 2) length)
(compose not (curry apply =) (curryr take 2))) r))
(define n (cons h (append-map cdr asg)))
(define m (if (not nest) n (append n (append-map cdr comp))))
(cons (delete-duplicates m) (if include-all r comp))))]))
(define (reduce-st-chain env)
(map
(lambda (x)
(if (and (pair? (~ x 2)) (memq (car (~ x 2)) '(st st2 st3 st4 st5 st6)))
;; refer to original env not to reflect reduced st
(list (~ x 0) (~ x 1) (opt-dep-st (~ x 2) env))
x))
env))
(define (delete-unnecessary-phi env)
(define new-env (delete-unnecessary-phi-once env))
(if (equal? env new-env) env
(delete-unnecessary-phi new-env)))
(define (delete-unnecessary-phi-once env)
(define terms (flatten env))
(filter
(lambda (i)
(define name (format "~a__~a" (~ i 0) (~ i 1)))
(define expr (~ i 2))
(define phi (and (pair? expr) (eq? (car expr) 'phi)))
(or (not phi) (member name terms)))
env))
(define TMPHASH_COUNT 0)
(define (sat-temporary-hash)
(set! TMPHASH_COUNT (- TMPHASH_COUNT 1))
TMPHASH_COUNT)
;; env ::= '((var hash expr) ..)
(define (extract-ssa node [env '()])
(define (rec n)
(extract-ssa n env))
(define (mrec ns [env env])
(if (null? ns) (values env '())
(let*-values ([(top-env top-n) (extract-ssa (car ns) env)]
[(suc-env suc-n) (mrec (cdr ns) top-env)])
(values suc-env (cons top-n suc-n)))))
(match (sxml:name node)
[(or 'OMPPragma 'ACCPragma) (rec (~ (sxml:content node) 2))]
['varDecl
(define name ((if-car-sxpath "name/text()") node))
(define value ((if-car-sxpath "value/*") node))
(define h (eq-hash-code node))
(define-values (_ new-value) (if value (rec value) (values #f #f)))
(define new-env
(if (and name value) (cons (list name h new-value) env) env))
(values new-env #f)]
['compoundStatement
(mrec
(append
((sxpath "declarations/varDecl") node)
((sxpath "body/*") node)))]
['forStatement
(define body ((sxpath "body/*") node))
(define-values (body-env0 _) (mrec body))
(define iterators
((sxpath `(init assignExpr (* 1) ,(sxpath:name 'Var) *text*)) node))
(define diff (lset-difference equal? body-env0 env))
(define diff-vars (delete-duplicates (append iterators (map car diff))))
(define pre-phi-env
(map
(lambda (var)
(define h (sat-temporary-hash))
(define expr
`(phi ,var
,(format "for__~a_pre" (eq-hash-code node))
,(format "for__~a_cont" (eq-hash-code node))
,(rename var env)))
(list var h expr))
diff-vars))
(define body-env1 (append pre-phi-env env))
(define-values (body-env __) (mrec body body-env1))
(define cond-expr (format "for__~a" (eq-hash-code node)))
(define phi-env
(map
(lambda (var)
(define b (assoc-ref body-env var))
(define o (assoc-ref body-env1 var))
(define h (sat-temporary-hash))
(define expr
`(phi ,var ,cond-expr
,(rename var body-env) ,(rename var body-env1)))
(and expr (list var h expr)))
diff-vars))
(values (append phi-env body-env) #f)]
[(or 'doStatement 'whileStatement)
;; todo
(mrec ((sxpath "body/*") node))]
['switchStatement
;; todo
(mrec ((sxpath "body/*") node))]
['ifStatement
(define condition ((if-car-sxpath "condition/*") node))
(define then ((if-car-sxpath "then/*") node))
(define else ((if-car-sxpath "else/*") node))
(define-values (cond-env cond-expr) (rec condition))
(define-values (then-env0 _) (extract-ssa then cond-env))
(define-values (else-env0 __)
(if else (extract-ssa else cond-env) (values cond-env #f)))
;; Bound computation within conditional scopes
(define then-updated (map car (lset-difference equal? then-env0 cond-env)))
(define else-updated (map car (lset-difference equal? else-env0 cond-env)))
(define then-phi (map (lambda (x)
(list x (sat-temporary-hash)
`(phi ,x 1 ,(~ (assoc-ref cond-env x `(0 ,x))
1) ,x))) then-updated))
(define else-phi (map (lambda (x)
(list x (sat-temporary-hash)
`(phi ,x 1 ,(~ (assoc-ref cond-env x `(0 ,x))
1) ,x))) else-updated))
(define-values (then-env ___) (extract-ssa then (append then-phi cond-env)))
(define-values (else-env ____)
(if else (extract-ssa else (append else-phi cond-env)) (values cond-env #f)))
(define then-vars (map car then-env))
(define else-vars (map car else-env))
(define vars (delete-duplicates (append then-vars else-vars)))
;; phi is ignored in code generation;
;; It works just as a pointer to an e-node.
;; phi prevents invalid code sharing unless the condition allows rewrites
(define phi-env
(filter-map
(lambda (var)
(define t (assoc-ref then-env var))
(define e (assoc-ref else-env var))
(define h (sat-temporary-hash))
(define expr
(and (not (and t e (equal? t e)))
`(phi ,var ,cond-expr
,(rename var then-env)
,(rename var else-env))))
(and expr (list var h expr)))
vars))
(define diff-env (lset-difference equal? else-env then-env))
(values (append phi-env diff-env then-env) #f)]
['exprStatement
(rec (car (sxml:content node)))]
['castExpr
(define-values (new-env new-rv) (rec (car (sxml:content node))))
(values new-env
`(functionCall "__cast" ,(sxml:attr node 'type) ,new-rv))]
['moeConstant
(values env
`(functionCall "__moe" ,(sxml:attr node 'type) ,(sxml:text node)))]
['memberRef
(define rv (car (sxml:content node)))
(define-values (new-env new-rv) (rec rv))
(values new-env
`(functionCall "__memberref" ,new-rv
,(sxml:attr node 'member)
,(sxml:attr node 'type)
,(sxml:attr rv 'type)))]
[(or 'postIncrExpr 'postDecrExpr 'preIncrExpr 'preDecrExpr)
(define var (car (sxml:content node)))
(define name (lv-name var))
(define h (eq-hash-code node))
(define op
(match (sxml:name node)
[(or 'preIncrExpr 'postIncrExpr) 'plusExpr]
[(or 'preDecrExpr 'postDecrExpr) 'minusExpr]
[else #f]))
(define pre
(match (sxml:name node)
[(or 'preIncrExpr 'preDecrExpr) #t] [else #t]))
(define-values (_ orig-expr) (rec var))
(define-values (incl-env incl-expr)
(rec `(assignExpr ,var (,op ,var (intConstant "1")))))
(define new-expr (~ (assoc-ref incl-env name) 1))
;; Set a proper hash
(values (assoc-adjoin incl-env name (list h new-expr))
(if pre incl-expr orig-expr))]
[(or 'asgPlusExpr 'asgMinusExpr 'asgMulExpr 'asgDivExpr
'asgModExpr 'asgLshiftExpr 'asgRshiftExpr 'asgBitAndExpr
'asgBitOrExpr 'asgBitXorExpr)
(match-define (list var val) (sxml:content node))
(define name (lv-name var))
(define h (eq-hash-code node))
(define op
(match (sxml:name node)
['asgPlusExpr 'plusExpr]
['asgMinusExpr 'minusExpr]
['asgMulExpr 'mulExpr]
['asgDivExpr 'divExpr]
['asgModExpr 'modExpr]
['asgLshiftExpr 'LshiftExpr]
['asgRshiftExpr 'RshiftExpr]
['asgBitAndExpr 'bitAndExpr]
['asgBitOrExpr 'bitOrExpr]
['asgBitXorExpr 'bitXorExpr]))
(define-values (asg-env ret-expr)
(rec `(assignExpr ,var (,op ,var ,val))))
(define new-expr (~ (assoc-ref asg-env name) 1))
;; Set a proper hash
(values (assoc-adjoin asg-env name (list h new-expr)) ret-expr)]
['assignExpr
(match-define (list lv rv) (sxml:content node))
(define-values (renv new-rv) (rec rv))
(define name (lv-name lv))
(define h (eq-hash-code node))
(unless (member (sxml:name lv) '(Var arrayRef pointerRef))
(error (~a lv)))
(define-values (new-env new-addrs)
(match (sxml:name lv)
['arrayRef
(define addrs (cdr (sxml:content lv)))
(mrec addrs renv)]
['pointerRef
(define addrs (list (pointer-addr lv)))
(mrec addrs renv)]
[else (values renv #f)]))
(define len (and new-addrs (length new-addrs)))
(define st
(and new-addrs
(if (= len 1) 'st (string->symbol (format "st~a" len)))))
(define expr
(if (not st) new-rv
`(,st ,name ,(rename name new-env) ,@new-addrs ,new-rv)))
(values (cons (list name h expr) new-env) new-rv)]
['functionCall
(define-values (new-env expr)
(mrec (sxml:content ((if-car-sxpath "arguments") node))))
;; todo support reference arguments
(values new-env
`(functionCall ,((if-car-sxpath "function/funcAddr/text()") node)
,@expr))]
[(or 'plusExpr 'minusExpr 'mulExpr 'divExpr 'condExpr 'modExpr
'LshiftExpr 'RshiftExpr 'bitAndExpr 'bitOrExpr 'bitXorExpr
'logEQExpr 'logNEQExpr 'logGEExpr 'logGTExpr 'logLEExpr
'logLTExpr 'logAndExpr 'logOrExpr 'unaryMinusExpr 'bitNotExpr
'logNotExpr 'sizeOfExpr 'commaExpr 'commaExpr0)
(define-values (new-env expr) (mrec (sxml:content node)))
(values new-env (cons (sxml:name node) expr))]
['arrayRef
(define addrs (cdr (sxml:content node)))
(define-values (new-env new-addrs) (mrec addrs))
(define len (length addrs))
(define ld (if (= len 1) 'ld (string->symbol (format "ld~a" len))))
(define name (lv-name node))
(values env
(opt-dep-ld `(,ld ,name ,(rename name new-env) ,@new-addrs) new-env))]
['pointerRef
(define addr (pointer-addr node))
(define-values (new-env new-addr) (rec addr))
(define name (lv-name node))
(values new-env
(opt-dep-ld `(ld ,name ,(rename name new-env) ,new-addr) new-env))]
['Var
(define name (rename (lv-name node) env))
(values env name)]
['varAddr
(define name (lv-name node))
(define h (sat-temporary-hash))
(values
(cons (list name h name) env)
`(functionCall "__varAddr" ,name ,(sxml:attr node 'type) ,h))]
[(or 'intConstant 'longlongConstant 'floatConstant)
(define text (sxml:text node))
(if (regexp-match "[fF]$" text)
(values env text)
(values
env
(string->number
(regexp-replace #rx"[a-zA-Z]$" text ""))))]
[(or 'caseLabel 'defaultLabel 'breakStatement 'continueStatement