optimize-gcc-options.scm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. (define bench "")
  2. (define pop-size 25)
  3. (define goal <)
  4. (define (evaluate options)
  5. (let ((p (open-process (list path: "./evaluate"
  6. arguments: (list bench options)))))
  7. (let ((x (read p)))
  8. (close-port p)
  9. (let ((t (cadr x)))
  10. (display ";") (pp (list t options))
  11. t))))
  12. (define options '(
  13. ("" " -fno-merge-constants")
  14. ("" " -fno-defer-pop")
  15. ("" " -fno-thread-jumps")
  16. ("" " -fno-guess-branch-probability")
  17. ("" " -fno-cprop-registers")
  18. ("" " -fno-if-conversion")
  19. ("" " -fno-if-conversion2")
  20. ("" " -fno-delayed-branch")
  21. ("" " -fno-loop-optimize" " -floop-optimize2")
  22. ("" " -ftree-ccp")
  23. ("" " -ftree-dce")
  24. ("" " -ftree-dominator-opts")
  25. ("" " -ftree-dse")
  26. ("" " -ftree-ter")
  27. ("" " -ftree-lrs")
  28. ("" " -ftree-sra")
  29. ("" " -ftree-copyrename")
  30. ("" " -ftree-fre")
  31. ("" " -ftree-ch")
  32. ("" " -fmerge-constants")
  33. ("" " -fcrossjumping")
  34. ("" " -foptimize-sibling-calls")
  35. ("" " -fcse-follow-jumps")
  36. ("" " -fcse-skip-blocks")
  37. ("" " -fgcse")
  38. ("" " -fexpensive-optimizations")
  39. ("" " -fstrength-reduce")
  40. ("" " -frerun-cse-after-loop")
  41. ("" " -frerun-loop-opt")
  42. ("" " -fcaller-saves")
  43. ("" " -fforce-mem" " -fforce-addr")
  44. ("" " -fpeephole2")
  45. ("" " -fschedule-insns")
  46. ("" " -fschedule-insns2")
  47. ("" " -fregmove")
  48. ;;;(" -fno-strict-aliasing" " -fstrict-aliasing")
  49. ("" " -fdelete-null-pointer-checks")
  50. ("" " -freorder-blocks")
  51. ("" " -fthread-jumps")
  52. ("" " -fgcse-lm")
  53. ("" " -fsched-interblock")
  54. ("" " -fsched-spec")
  55. ("" " -freorder-blocks")
  56. ("" " -freorder-functions")
  57. ("" " -funit-at-a-time")
  58. ("" " -falign-functions")
  59. ("" " -falign-jumps")
  60. ("" " -falign-loops")
  61. ("" " -falign-labels")
  62. ("" " -ftree-pre")
  63. ("" " -finline-functions")
  64. ("" " -funswitch-loops")
  65. ("" " -fgcse-after-reload")
  66. ;;;("" " -fomit-frame-pointer" " -momit-leaf-frame-pointer")
  67. ("" " -ffloat-store")
  68. ("" " -fprefetch-loop-arrays")
  69. ("" " -fno-inline")
  70. ("" " -fpeel-loops")
  71. ("" " -ftracer")
  72. ("" " -funroll-loops" " -funroll-all-loops")
  73. ("" " -fbranch-target-load-optimize" " -fbranch-target-load-optimize2")
  74. ("" " -fmodulo-sched")
  75. ("" " -fno-function-cse")
  76. ("" " -fgcse-sm")
  77. ("" " -fgcse-las")
  78. ("" " -freschedule-modulo-scheduled-loops")
  79. ("" " -ftree-loop-im")
  80. ("" " -ftree-loop-ivcanon")
  81. ("" " -fivopts")
  82. ("" " -ftree-vectorize")
  83. ("" " -fvariable-expansion-in-unroller")
  84. ))
  85. (define (sort-list l <?)
  86. (define (mergesort l)
  87. (define (merge l1 l2)
  88. (cond ((null? l1) l2)
  89. ((null? l2) l1)
  90. (else
  91. (let ((e1 (car l1)) (e2 (car l2)))
  92. (if (<? e1 e2)
  93. (cons e1 (merge (cdr l1) l2))
  94. (cons e2 (merge l1 (cdr l2))))))))
  95. (define (split l)
  96. (if (or (null? l) (null? (cdr l)))
  97. l
  98. (cons (car l) (split (cddr l)))))
  99. (if (or (null? l) (null? (cdr l)))
  100. l
  101. (let* ((l1 (mergesort (split l)))
  102. (l2 (mergesort (split (cdr l)))))
  103. (merge l1 l2))))
  104. (mergesort l))
  105. (define (iota n)
  106. (let loop ((i (- n 1)) (lst '()))
  107. (if (>= i 0)
  108. (loop (- i 1) (cons i lst))
  109. lst)))
  110. (define (random-options)
  111. (map (lambda (x) (list-ref x (random-integer (length x))))
  112. options))
  113. (define (options->string options)
  114. (apply string-append options))
  115. (define (cross options1 options2)
  116. (map (lambda (o1 o2 o)
  117. (cond ((= 0 (random-integer 35))
  118. (list-ref o (random-integer (length o))))
  119. ((= 0 (random-integer 2))
  120. o1)
  121. (else
  122. o2)))
  123. options1
  124. options2
  125. options))
  126. (define (evaluated options)
  127. (let* ((t (evaluate (options->string options)))
  128. (x (cons t options)))
  129. (if (< t (car best))
  130. (set! best x))
  131. x))
  132. (define (value evaluated-options)
  133. (car evaluated-options))
  134. (define (opts evaluated-options)
  135. (cdr evaluated-options))
  136. (define (order evaluated-options)
  137. (sort-list evaluated-options
  138. (lambda (x y) (goal (value x) (value y)))))
  139. (define (select)
  140. (let* ((n (quotient (* pop-size 2) 3))
  141. (x (apply + (map (lambda (x) (random-integer 2))
  142. (iota (* 2 n)))))
  143. (i (abs (- x n))))
  144. i))
  145. (define (new-generation pop)
  146. (order
  147. (map (lambda (x) (evaluated x))
  148. (map (lambda (_)
  149. (let ((x (select)))
  150. (let loop ()
  151. (let ((y (select)))
  152. (if (= x y)
  153. (loop)
  154. (cross (opts (list-ref pop x))
  155. (opts (list-ref pop y))))))))
  156. pop))))
  157. (define best '())
  158. (define (optimize)
  159. (set! best (list 99999999999))
  160. (let ((initial-pop
  161. (order
  162. (map (lambda (i) (evaluated (random-options)))
  163. (iota pop-size)))))
  164. (let loop ((i 0) (pop initial-pop))
  165. ; (display ";")(write (list i (map value pop)))(newline)
  166. (if (< i 10)
  167. (loop (+ i 1)
  168. (new-generation pop))
  169. best))))
  170. (for-each
  171. (lambda (b)
  172. (set! bench b)
  173. (pretty-print (list b (optimize))))
  174. '("lattice"
  175. "ray"
  176. "boyer"
  177. "diviter"
  178. "puzzle"
  179. "takl"
  180. "fft"
  181. "conform"
  182. "graphs"
  183. "simplex"))