[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
feature/native-comp 07b75de 10/19: Enhance type inference constraining f
From: |
Andrea Corallo |
Subject: |
feature/native-comp 07b75de 10/19: Enhance type inference constraining function arguments |
Date: |
Mon, 21 Dec 2020 14:52:39 -0500 (EST) |
branch: feature/native-comp
commit 07b75deea9febd2cb6fd4d3467e909df341e96fb
Author: Andrea Corallo <akrl@sdf.org>
Commit: Andrea Corallo <akrl@sdf.org>
Enhance type inference constraining function arguments
* lisp/emacs-lisp/comp.el: Add some commentary.
(comp-cond-cstrs-target-mvar): Rename and update docstring.
(comp-add-cond-cstrs): Update to use
`comp-cond-cstrs-target-mvar'.
(comp-emit-call-cstr, comp-lambda-list-gen, comp-add-call-cstr):
New functions.
(comp-add-cstrs): Call `comp-add-call-cstr'.
* test/src/comp-tests.el (comp-tests-type-spec-tests): Update two
type specifier tests.
---
lisp/emacs-lisp/comp.el | 80 +++++++++++++++++++++++++++++++++++++++++++------
test/src/comp-tests.el | 4 +--
2 files changed, 73 insertions(+), 11 deletions(-)
diff --git a/lisp/emacs-lisp/comp.el b/lisp/emacs-lisp/comp.el
index e8db238..6f1ef26 100644
--- a/lisp/emacs-lisp/comp.el
+++ b/lisp/emacs-lisp/comp.el
@@ -1868,7 +1868,19 @@ into the C code forwarding the compilation unit."
(comp-add-func-to-ctxt (comp-limplify-top-level t))))
-;;; conditional branches rewrite pass specific code.
+;;; add-cstrs pass specific code.
+
+;; This pass is responsible for adding constraints, these are
+;; generated from:
+;;
+;; - Conditional branches: each branch taken or non taken can be used
+;; in the CFG to infer infomations on the tested variables.
+;;
+;; - Function calls: function calls to function assumed to be not
+;; redefinable can be used to add constrains on the function
+;; arguments. Ex: if we execute successfully (= x y) we know that
+;; afterwards both x and y must satisfy the (or number marker)
+;; type specifier.
(defun comp-emit-assume (target rhs bb negated)
"Emit an assume for mvar TARGET being RHS.
@@ -1907,10 +1919,10 @@ The assume is emitted at the beginning of the block BB."
(cl-return (puthash bb-symbol new-bb (comp-func-blocks comp-func)))
finally (cl-assert nil)))
-(defun comp-add-cond-cstrs-target-mvar (mvar exit-insn bb)
- "Given MVAR search in BB what we'll use as assume target.
-Keep on searching till EXIT-INSN is encountered.
-Return the corresponding rhs mvar."
+;; Cheap substitute to a copy propagation pass...
+(defun comp-cond-cstrs-target-mvar (mvar exit-insn bb)
+ "Given MVAR search in BB the original mvar MVAR got assigned from.
+Keep on searching till EXIT-INSN is encountered."
(cl-flet ((targetp (x)
;; Ret t if x is an mvar and target the correct slot number.
(and (comp-mvar-p x)
@@ -1955,10 +1967,8 @@ TARGET-BB-SYM is the symbol name of the target block."
(comment ,_comment-str)
(cond-jump ,cond ,(pred comp-mvar-p) . ,blocks))
(cl-loop
- with target-mvar1 = (comp-add-cond-cstrs-target-mvar op1 (car
insns-seq)
- b)
- with target-mvar2 = (comp-add-cond-cstrs-target-mvar op2 (car
insns-seq)
- b)
+ with target-mvar1 = (comp-cond-cstrs-target-mvar op1 (car insns-seq) b)
+ with target-mvar2 = (comp-cond-cstrs-target-mvar op2 (car insns-seq) b)
for branch-target-cell on blocks
for branch-target = (car branch-target-cell)
for assume-target = (comp-add-cond-cstrs-target-block b branch-target)
@@ -1970,6 +1980,57 @@ TARGET-BB-SYM is the symbol name of the target block."
do (comp-emit-assume target-mvar2 op1 assume-target negated)
finally (cl-return-from in-the-basic-block)))))))
+(defun comp-emit-call-cstr (mvar call-cell cstr)
+ "Emit a constraint CSTR for MVAR after CALL-CELL."
+ (let ((next-cell (cdr call-cell))
+ (new-cell `((assume ,(make-comp-mvar :slot (comp-mvar-slot mvar))
+ (and ,mvar ,cstr)))))
+ (setf (cdr call-cell) new-cell
+ (cdr new-cell) next-cell
+ (comp-func-ssa-status comp-func) 'dirty)))
+
+(defun comp-lambda-list-gen (lambda-list)
+ "Return a generator to iterate over LAMBDA-LIST."
+ (lambda ()
+ (cl-case (car lambda-list)
+ (&optional
+ (setf lambda-list (cdr lambda-list))
+ (prog1
+ (car lambda-list)
+ (setf lambda-list (cdr lambda-list))))
+ (&rest
+ (cadr lambda-list))
+ (t
+ (prog1
+ (car lambda-list)
+ (setf lambda-list (cdr lambda-list)))))))
+
+(defun comp-add-call-cstr ()
+ "Add args assumptions for each function of which the type specifier is
known."
+ (cl-loop
+ for bb being each hash-value of (comp-func-blocks comp-func)
+ do
+ (comp-loop-insn-in-block bb
+ (when-let ((match
+ (pcase insn
+ (`(set ,lhs (,(pred comp-call-op-p) ,f . ,args))
+ (when-let ((cstr-f (gethash f comp-known-func-cstr-h)))
+ (cl-values cstr-f lhs args)))
+ (`(,(pred comp-call-op-p) ,f . ,args)
+ (when-let ((cstr-f (gethash f comp-known-func-cstr-h)))
+ (cl-values cstr-f nil args))))))
+ (cl-multiple-value-bind (cstr-f lhs args) match
+ (cl-loop
+ for arg in args
+ for gen = (comp-lambda-list-gen (comp-cstr-f-args cstr-f))
+ for cstr = (funcall gen)
+ for target = (comp-cond-cstrs-target-mvar arg insn bb)
+ when (and target
+ (or (null lhs)
+ (not (eql (comp-mvar-slot lhs)
+ (comp-mvar-slot target)))))
+ do (comp-emit-call-cstr target insn-cell cstr)))))))
+
(defun comp-add-cstrs (_)
"Rewrite conditional branches adding appropriate 'assume' insns.
This is introducing and placing 'assume' insns in use by fwprop
@@ -1984,6 +2045,7 @@ blocks."
(not (comp-func-has-non-local f)))
(let ((comp-func f))
(comp-add-cond-cstrs)
+ (comp-add-call-cstr)
(comp-log-func comp-func 3))))
(comp-ctxt-funcs-h comp-ctxt)))
diff --git a/test/src/comp-tests.el b/test/src/comp-tests.el
index 4ea8dbb..a3e887b 100644
--- a/test/src/comp-tests.el
+++ b/test/src/comp-tests.el
@@ -872,14 +872,14 @@ Return a list of results."
(if (= x 3)
'foo
x))
- (or (member foo) (integer * 2) (integer 4 *)))
+ (or (member foo) marker number))
;; 13
((defun comp-tests-ret-type-spec-8-4-f (x y)
(if (= x y)
x
'foo))
- t)
+ (or (member foo) marker number))
;; 14
((defun comp-tests-ret-type-spec-9-1-f (x)
- feature/native-comp updated (5b10a03 -> 9676e4d), Andrea Corallo, 2020/12/21
- feature/native-comp 2a117ad 01/19: * Add mvar pretty print support when dumping LIMPLE, Andrea Corallo, 2020/12/21
- feature/native-comp bad18f5 02/19: * Improve comp-fwprop pass, Andrea Corallo, 2020/12/21
- feature/native-comp a0c0daf 03/19: * Fix a number of type specifier simplification tests, Andrea Corallo, 2020/12/21
- feature/native-comp 34c1d75 05/19: * Enumerate and split type specifier tests in comp-tests.el to ease debug, Andrea Corallo, 2020/12/21
- feature/native-comp 7074988 06/19: * Add a type specifier test to comp-cstr-tests.el, Andrea Corallo, 2020/12/21
- feature/native-comp 0255108 08/19: * Rename comp-cond-cstr into comp-add-cstrs, Andrea Corallo, 2020/12/21
- feature/native-comp 48d43f5 04/19: * Improve constraint simplification logic in comp-cstr.el, Andrea Corallo, 2020/12/21
- feature/native-comp 07b75de 10/19: Enhance type inference constraining function arguments,
Andrea Corallo <=
- feature/native-comp e0f20da 14/19: Simplify correctly (or (integer 1 1) (not (integer 1 1))) as t, Andrea Corallo, 2020/12/21
- feature/native-comp 6f3570c 16/19: Fix value type inference for doubly negate constraints, Andrea Corallo, 2020/12/21
- feature/native-comp d072ee9 07/19: * Two minors in comp.el, Andrea Corallo, 2020/12/21
- feature/native-comp 23791cf 09/19: * Allow for modifying insn-cell inside `comp-loop-insn-in-block', Andrea Corallo, 2020/12/21
- feature/native-comp 9bbe6ea 15/19: Fix native compiler tests when they are bytecompiled, Andrea Corallo, 2020/12/21
- feature/native-comp 8e816b0 11/19: Symplify type specifier (not t) as nil, Andrea Corallo, 2020/12/21
- feature/native-comp c70c080 12/19: * Allow for overlapping src and dst in cstr set operations, Andrea Corallo, 2020/12/21
- feature/native-comp 5376563 17/19: Fix `comp-add-call-cstr' and add a test, Andrea Corallo, 2020/12/21
- feature/native-comp ebf8963 18/19: * Fix a bunch of known type specifiers, Andrea Corallo, 2020/12/21
- feature/native-comp 9676e4d 19/19: * Fix a test in auth-source-tests.el, Andrea Corallo, 2020/12/21