SICL icon indicating copy to clipboard operation
SICL copied to clipboard

cleavir-kildall-type-inference passes cleavir environments to CL:TYPE and CL:SUBTYPEP

Open 3b opened this issue 6 years ago • 0 comments

possibly not using things correctly yet, but with following code:

;;; define enough of an env to build ast/hir

(defclass env (sicl-simple-environment:simple-environment) ())
(defmethod cleavir-compilation-policy:compute-policy-quality
    (name optimize (env env))
  ;; avoid errors about CLEAVIR-ESCAPE:TRUST-DYNAMIC-EXTENT and CLEAVIR-KILDALL-TYPE-INFERENCE:INSERT-TYPE-CHECKS
  1)

(defmethod cleavir-env:function-info ((env env) name)
  (or (call-next-method)
      (when (symbolp name)
        (cond
          ((special-operator-p name)
           (make-instance 'cleavir-env:special-operator-info
                          :name name))
          ((eq (symbol-package name) (find-package 'cleavir-primop))
           (make-instance 'cleavir-env:special-operator-info
                          :name name))
          ((member name '(error))
           (make-instance 'cleavir-env:global-function-info
                          :name name))))))

(defmethod cleavir-env:has-extended-char-p ((env env))
  nil)
(defmethod cleavir-env:float-types ((env env))
  '(single-float double-float))
(defmethod cleavir-env:upgraded-array-element-types ((env env))
  '(single-float double-float character (unsigned-byte 8) (unsigned-byte 32)))
(defmethod cleavir-env:upgraded-complex-part-types ((env env))
  '(single-float double-float))

;;; try to compile something
(let* ((cleavir-generate-ast:*compiler* 'cl:compile)
       (sys t)
       (env (make-instance 'env))
       (ast (cleavir-generate-ast:generate-ast
             '#'(lambda (object)
                  (declare (type cons object))
                  (if (cleavir-primop:typeq object cons)
                      (cleavir-primop:car object)
                      (if (cleavir-primop:typeq object null)
                          nil
                          (error 'type-error :datum object
                                             :expected-type '(or cons null)))))
             env sys))
       (hir (cleavir-ast-to-hir:compile-toplevel-unhoisted ast)))
  (cleavir-kildall-type-inference:infer-types hir env :prune t)
  hir)

I get a type error :

The value
  #<ENV {1002EDB663}>
is not of type
  (OR SB-C::ABSTRACT-LEXENV NULL)
when binding SB-KERNEL::ENVIRONMENT

from the call to cl:subtypep in specifier->ltype. Similarly, object-ltype passes the cleavir environment to cl:typep.

As a temporary workaround I seem to get reasonable results if I don't pass the environment to the CL functions, though I assume that won't work for real code (leaking float types from host system if nothing else). I couldn't find anything that looked like a 'correct' solution though.

3b avatar Aug 06 '18 18:08 3b