Summary

Make inline files less expanded for the scrutinizer

Metadata

Description

In the following example take a look at the generated inline definition for `foo` in module `mod`. The `(fixnum? x)` has been turned into `(##core#inline ("C_fixnump") (##core#variable (x18)))`.

Now take a look at the two calls to `foo` in `main.scm`. If the original definition of `foo` was inlined at the call sites the scrutinizer could resolve the result statically in both cases. That is `(foo 42)` inlined as `(if (fixnum? 42) 'fixnum 'not-fixnum)` could be optimized to just `'fixnum`. This is something the scrutinizer already can do.

The scrutinizer does not, however, know anything about the type of `(##core#inline ("C_fixnump") (##core#variable (x18)))`, or any `##core#inline` form for that matter (AFAIK). Therefore it cannot optimize the predicate calls with the generated inline definition. You can see that the predicate calls are indeed present in the optimized expression tree of the compiled program.

 $ ./run.sh
 ::::::::::::::
 run.sh
 ::::::::::::::
 #!/usr/bin/env bash
 set -e
 set -o pipefail
 
 more run.sh src/*.scm | cat
 
 echo "### BUILD ###"
 rm -rf build
 mkdir -p build
 cd build
 csc -s -inline-global -J -O3 -oi mod.inline ../src/mod.scm -o mod.so
 csc -debug 7 -O3 -inline -consult-inline-file mod.inline ../src/main.scm -o main
 
 more *.import.scm *.inline | cat
 ./main
 ::::::::::::::
 src/main.scm
 ::::::::::::::
 (cond-expand
  (chicken-5 (import mod))
  (else (use mod)))
 (print (foo 42))
 (print (foo 'abcd))
 ::::::::::::::
 src/mod.scm
 ::::::::::::::
 (module
  mod
  (foo)
  (import scheme)
  (cond-expand
   (chicken-5 (import (chicken base)))
   (else (import chicken)))
 
  (define (foo x)
    (if (fixnum? x)
        'fixnum
        'not-fixnum))
  )
 ### BUILD ###
 [optimized]
 (lambda (k129)
   (let ((k130 (##core#lambda
                 (r131)
                 (let ((k133 (##core#lambda
                               (r134)
                               (let ((k136 (##core#lambda
                                             (r137)
                                             (let ((k139 (##core#lambda
                                                           (r140)
                                                           (let ((k142 (##core#lambda
                                                                         (r143)
                                                                         (let ((k145 (##core#lambda
                                                                                       (r146)
                                                                                       (let ((k151 (##core#lambda (r152) (r152 k129))))
                                                                                         (chicken.base#implicit-exit-handler k151)))))
                                                                           (let ((r142164 (##core#inline "C_fixnump" 'abcd)))
                                                                             (let ((r156168
                                                                                     (##core#cond r142164 'fixnum 'not-fixnum)))
                                                                               (chicken.base#print k145 r156168)))))))
                                                             (let ((r142167 (##core#inline "C_fixnump" 42)))
                                                               (let ((r160169
                                                                       (##core#cond r142167 'fixnum 'not-fixnum)))
                                                                 (chicken.base#print k142 r160169)))))))
                                               (chicken.load#load-extension
                                                 k139
                                                 'mod
                                                 '(mod#)
                                                 'require)))))
                                 (##core#callunit expand k136)))))
                   (##core#callunit eval k133)))))
     (##core#callunit library k130)))
 ::::::::::::::
 mod.import.scm
 ::::::::::::::
 ;;;; mod.import.scm - GENERATED BY CHICKEN 5.0.0rc4 -*- Scheme -*-
 
 (##sys#register-compiled-module
   'mod
   'mod
   (scheme#list)
   '((foo . mod#foo))
   (scheme#list)
   (scheme#list))
 
 ;; END OF FILE
 ::::::::::::::
 mod.inline
 ::::::::::::::
 ; GENERATED BY CHICKEN 5.0.0rc4 FROM ../src/mod.scm
 
 (mod#foo
   (##core#lambda
     (f_136 #t (k137 x18) 10)
     (let (r142)
       (##core#inline ("C_fixnump") (##core#variable (x18)))
       (##core#call
         (#t)
         (##core#variable (k137))
         (##core#cond () (##core#variable (r142)) '(fixnum) '(not-fixnum))))))
 
 ; END OF FILE
 fixnum
 not-fixnum

Changes and comments

[2018-11-09 15:21:10 UTC] sjamaan changed version from 5.0.0 to 5.0

[2019-04-20 17:24:50 UTC] megane changed status from new to closed

[2019-04-20 17:24:50 UTC] megane set resolution to invalid

[2019-04-20 17:24:50 UTC] megane wrote:

Inlining happens after scrutiny. Moreover, the inline format seems to be in CPS, which the scrutinizer couldn't handle anyway.