Commit graph

683 commits

Author SHA1 Message Date
Andrea Corallo
89d5a3a760 Enable integer range narrowing under compare and branch
* lisp/emacs-lisp/comp-cstr.el (comp-cstr-set-cmp-range)
	(comp-cstr->, comp-cstr->=, comp-cstr-<, comp-cstr-<=): New
	functions.
	* lisp/emacs-lisp/comp.el (comp-equality-fun-p)
	(comp-range-cmp-fun-p): New functions.
	(comp-collect-rhs): Use `comp-assign-op-p' in place of
	`comp-set-op-p'.
	(comp-negate-range-cmp-fun, comp-reverse-cmp-fun): New functions.
	(comp-emit-assume): Rework to be able to emit also comparision
	assumption.
	(comp-add-cond-cstrs-simple): Update for new `comp-emit-assume'.
	(comp-add-cond-cstrs-simple): Update to emit range assumption.
	(comp-fwprop-insn): Execute range assumptions.
	* test/src/comp-tests.el (comp-tests-type-spec-tests): Add tests.
2020-12-26 10:53:54 +01:00
Andrea Corallo
bd693ccea7 * Don't emit byte op-code annotations in LIMPLE to optimize for compile-time
Saves 10~15% in bootstrap time.

	* lisp/emacs-lisp/comp.el (comp-op-case): Don't emit op-code
	annotaitons.
	(comp-limplify-lap-inst, comp-add-cond-cstrs-simple)
	(comp-add-cond-cstrs, comp-tco-func): Update accordingly.
2020-12-25 09:44:30 +01:00
Andrea Corallo
2327a98319 * Constrain only mvars that are actually used
* lisp/emacs-lisp/comp.el (comp-mvar-used-p, comp-collect-mvars)
	(comp-collect-rhs): New functions.
	(comp-add-cond-cstrs-simple, comp-add-cond-cstrs): Update logic.
	(comp-add-cstrs): Call `comp-collect-rhs' before doing anything
	else.
2020-12-24 15:40:50 +01:00
Andrea Corallo
2a6c6bf324 * Use `comp-assign-op-p' into dead code elimination pass
* lisp/emacs-lisp/comp.el (comp-dead-assignments-func): Use
	`comp-assign-op-p' in place of `comp-set-op-p'.
2020-12-24 15:36:46 +01:00
Andrea Corallo
96d4c70412 * Fix logic for constraining block with multiple predecessors
* lisp/emacs-lisp/comp.el (comp-limple-lock-keywords)
	(comp-add-cond-cstrs-target-block): Logic update.
2020-12-24 15:36:46 +01:00
Andrea Corallo
538f59806c Extend cstrs pass to match `unless' like code
* lisp/emacs-lisp/comp.el (comp-emit-assume): Add assertion.
	(comp-add-new-block-between): Fix two typos.
	(comp-add-cond-cstrs-target-block): Fix typo.
	(comp-add-cond-cstrs-simple): Logic update.
	* test/src/comp-tests.el (comp-tests-type-spec-tests): Add a test.
2020-12-24 15:36:39 +01:00
Andrea Corallo
715cac119a * lisp/emacs-lisp/comp.el (comp-limplify-lap-inst): Opencode byte-not. 2020-12-24 15:36:36 +01:00
Andrea Corallo
c07c9f6bf8 Extend cstrs pass to match `when' like code
* lisp/emacs-lisp/comp.el (comp-emit-assume): Better parameter names.
	(comp-add-cond-cstrs-simple): New function.
	(comp-add-cond-cstrs): Rename assume-target -> block-target.
	(comp-add-cstrs): Call `comp-add-cond-cstrs-simple'.
	* test/src/comp-tests.el (comp-tests-type-spec-tests): Add test.
2020-12-24 15:36:09 +01:00
Andrea Corallo
4deeb2f2ee Invert basic block argument order in LIMPLE cond-jump
* lisp/emacs-lisp/comp.el (comp-emit-cond-jump)
	(comp-emit-switch, comp-emit-narg-prologue, comp-add-cond-cstrs):
	Invert basic block argument order in LIMPLE cond-jump.
	* src/comp.c (emit_limple_insn): Likewise.
2020-12-24 15:22:32 +01:00
Andrea Corallo
ffcd490cb4 Negate only values while constraining variables (bug#45376)
* lisp/emacs-lisp/comp-cstr.el (comp-cstr-value-negation): New
	function.
	* lisp/emacs-lisp/comp.el (comp-fwprop-insn): Use
	`comp-cstr-value-negation'.
	* test/src/comp-test-funcs.el (comp-test-45376-1-f): Rename.
	(comp-test-45376-2-f): New funcion.
	* test/src/comp-tests.el (bug-45376-1): Rename test.
	(bug-45376-2): Add test.
2020-12-23 16:17:58 +01:00
Andrea Corallo
c90aa68d90 * Follow cstr basic blocks to perform latch recognition
* lisp/emacs-lisp/comp.el (comp-fwprop-insn): Fix latch
	recognition.
2020-12-23 12:03:39 +01:00
Andrea Corallo
ebf8963a91 * Fix a bunch of known type specifiers
* lisp/emacs-lisp/comp.el (comp-known-type-specifiers): Fixes for:
	=, string-search, substring.
2020-12-21 20:22:03 +01:00
Andrea Corallo
5376563517 Fix `comp-add-call-cstr' and add a test
* lisp/emacs-lisp/comp.el (comp-add-call-cstr): Fix it.
	* test/src/comp-tests.el (assume-in-loop-1): New test.
	* test/src/comp-test-funcs.el (comp-test-assume-in-loop-1-f): New
	function.
2020-12-21 20:22:03 +01:00
Andrea Corallo
6f3570cd4a Fix value type inference for doubly negate constraints
* lisp/emacs-lisp/comp.el (comp-fwprop-insn): Do not propagate in
	case of double negation.
	* test/src/comp-test-funcs.el (comp-test-assume-double-neg-f):
	New function.
	* test/src/comp-tests.el (assume-double-neg): New test.
2020-12-21 20:22:03 +01:00
Andrea Corallo
3540b1f167 * Guarantee fwprop convergence and termination
* lisp/emacs-lisp/comp.el (comp-emit-call-cstr): Have new-mvar as
	LHS *and* RHS when constraining in and to ensure monotonicity and
	fwprop convergence.
	(comp-fwprop): Raise a warning for debug reasons in case fwprop
	does not converge within 100 iterations.
2020-12-21 20:22:03 +01:00
Andrea Corallo
07b75deea9 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.
2020-12-21 20:22:03 +01:00
Andrea Corallo
23791cf74d * Allow for modifying insn-cell inside `comp-loop-insn-in-block'
* lisp/emacs-lisp/comp.el (comp-loop-insn-in-block): Update.
2020-12-21 20:22:02 +01:00
Andrea Corallo
02551085c1 * Rename comp-cond-cstr into comp-add-cstrs
* lisp/emacs-lisp/comp.el (comp-add-cond-cstrs-target-mvar)
	(comp-add-cond-cstrs, comp-add-cstrs): Rename comp-cond-cstr
	-> comp-add-cstrs.
2020-12-21 20:22:02 +01:00
Andrea Corallo
d072ee9d34 * Two minors in comp.el
* lisp/emacs-lisp/comp.el (comp-known-func-cstr-h)
	(comp-ret-type-spec): Style.
2020-12-21 20:22:02 +01:00
Andrea Corallo
bad18f509d * Improve comp-fwprop pass
Wire-up comp-cstr.el routines in fwprop and constraint mvars also on
the else side of branches.

	* lisp/emacs-lisp/comp.el (comp-emit-assume)
	(comp-cond-cstr-target-mvar, comp-cond-cstr-func)
	(comp-fwprop-insn): Logic update.
	(comp-mvar-value-vld-p, comp-mvar-propagate, comp-fwprop-call):
	Handle neg slot.
2020-12-21 19:00:39 +01:00
Andrea Corallo
2a117ad3d7 * Add mvar pretty print support when dumping LIMPLE
* lisp/emacs-lisp/comp.el (comp-prettyformat-mvar)
	(comp-prettyformat-insn): New function.
	(comp-log-func): Update to use `comp-prettyformat-insn'.
	(comp-finalize-phis): Change LIMPLE phi format to ease
	`comp-prettyformat-insn' destructuring.
2020-12-21 18:23:46 +01:00
Andrea Corallo
72c1a41573 Have native compiler always preserve multibyte strings (bug#45342)
* lisp/emacs-lisp/comp.el (comp-final): Escape multibyte string
	when offloading compilation to child process.
	* test/src/comp-test-funcs.el (comp-test-45342-f): New function
	* test/src/comp-tests.el (bug-45342): New test
2020-12-20 21:21:39 +01:00
Andrea Corallo
682bd30347 * Allow for adding constraints targetting blocks with multiple predecessors
This commit remove the limitaiton we had not being able to add
constraints derived from conditional branches to basic blocks with
multiple predecessors.  When this condition is verified we add a new
dedicated basic block to hold the constraints.

	* lisp/emacs-lisp/comp.el (comp-block, comp-edge): Better slot
	type specifiers.
	(comp-block-cstr): New struct specializing `comp-block'.
	(make-comp-edge): New function.
	(comp-func): Better test function + doc for `blocks' slot.
	(comp-limple-lock-keywords): Update possible basic block names.
	(comp-emit-assume): Recive directly the block instead of its name.
	(comp-add-new-block-beetween): New function.
	(comp-cond-cstr-target-block): Logic update and use
	`comp-add-new-block-beetween'.
	(comp-cond-cstr-func): Make use of the latter.
	(comp-compute-edges): Make use of `make-comp-edge'.
2020-12-13 00:58:25 +01:00
Andrea Corallo
258eaddef8 * Rename comp-cond-rw -> comp-cond-cstr
* lisp/emacs-lisp/comp.el (comp-passes)
	(comp-cond-cstr-target-slot, comp-cond-cstr-func)
	(comp-cond-cstr): Rename pass from cond-rw to cond-cstr.
2020-12-13 00:58:25 +01:00
Andrea Corallo
be907b0ba8 * Spawn a sub-process for running GCC also in batch mode (bug#45056)
* lisp/emacs-lisp/comp.el (comp-async-compilation): New variable.
	(comp-final): Always run the C side of the compilation as a
	sub-process unless during bootstrap or async compilation.
	(comp-run-async-workers): Set `comp-async-compilation'.
2020-12-07 16:43:01 +01:00
Andrea Corallo
21104e6808 Fix comp-mvar-symbol-p' and comp-mvar-cons-p' (bug#44968)
* lisp/emacs-lisp/comp.el (comp-mvar-symbol-p): As all slots into
	a `comp-cstr' are in or fix this logic.
	(comp-mvar-cons-p): Likewise.
	* test/src/comp-tests.el (bug-44968): New testcase.
	* test/src/comp-test-funcs.el (comp-test-44968-f): New test
	function.
2020-12-01 00:09:46 +01:00
Andrea Corallo
23c082638e Add comp-cstr.el and comp-cstr-tests.el
As the constraint logic of the compiler is not trivial and largely
independent from the rest of the code move it into comp-cstr.el to
ease separation and maintainability.

This commit improve the conversion type
specifier -> constraint for generality.

Lastly this should help with bootstrap time as comp.el compilation
unit is slimmed down.

	* lisp/emacs-lisp/comp-cstr.el: New file.
	(comp--typeof-types, comp--all-builtin-types): Move from comp.el.
	(comp-cstr, comp-cstr-f): Same + rename.
	(comp-cstr-ctxt): New struct.
	(comp-supertypes, comp-common-supertype-2)
	(comp-common-supertype, comp-subtype-p, comp-union-typesets)
	(comp-range-1+, comp-range-1-, comp-range-<, comp-range-union)
	(comp-range-intersection): Move from comp.el.
	(comp-cstr-union-no-range, comp-cstr-union): Move from comp.el and
	rename.
	(comp-cstr-union-make): New function.
	(comp-type-spec-to-cstr, comp-cstr-to-type-spec): Move from
	comp.el, rename it and rework it.

	* lisp/emacs-lisp/comp.el (comp-known-func-cstr-h): Rework.
	(comp-ctxt): Remove two fields and include `comp-cstr-ctxt'.
	(comp-mvar, comp-fwprop-call): Update for `comp-cstr' being
	renamed.
	(comp-fwprop-insn): Use `comp-cstr-union-no-range' or
	`comp-cstr-union'.
	(comp-ret-type-spec): Use `comp-cstr-union' and rework.

	* test/lisp/emacs-lisp/comp-cstr-tests.el: New file.
	(comp-cstr-test-ts, comp-cstr-typespec-test): New functions.
	(comp-cstr-typespec-tests-alist): New defconst to generate tests
	on.
	(comp-cstr-generate-tests): New macro.

	* test/src/comp-tests.el (comp-tests-type-spec-tests): Update.
	(ret-type-spec): Initialize constraint context.
2020-11-26 22:02:30 +01:00
Andrea Corallo
7a8370ed0f * Add SELECTOR parameter to `native-compile-async' (bug#44813)
* lisp/emacs-lisp/comp.el (native-compile-async-skip-p): New function
ripping out logic from `native--compile-async' and accounting for
SELECTOR.
(native--compile-async): Add SELECTOR parameter, make use of
`native-compile-async-skip-p' and move it with other private
functions.
(native-compile-async): Add SELECTOR parameter.
2020-11-23 20:26:00 +01:00
Andrea Corallo
6104ab0f35 * Rename two native compiler customize
* lisp/emacs-lisp/comp.el (comp-deferred-compilation-deny-list):
Rename from `comp-deferred-compilation-black-list'.
* lisp/emacs-lisp/comp.el (native--compile-async): Update to use
`comp-deferred-compilation-deny-list'.
(comp-bootstrap-deny-list): Rename.
(batch-native-compile): Update to use `comp-bootstrap-deny-list'.
2020-11-23 20:25:00 +01:00
Stefan Kangas
6781cd670d Make load argument of native-compile-async internal
* lisp/emacs-lisp/comp.el (native--compile-async): New defun extracted
from native-compile-async.
(native-compile-async): Remove load argument and use above new defun.
* src/comp.c (maybe_defer_native_compilation): Use above new
defun.  (Bug#44676)
2020-11-21 04:22:59 +01:00
Stefan Kangas
c60355582a * lisp/emacs-lisp/comp.el (native-compile-async): Doc fix. 2020-11-20 21:06:09 +01:00
Stefan Kangas
a79365acaf compile-async: Don't error out on deferred compilation after load
* lisp/emacs-lisp/comp.el (native-compile-async): Update
comp-files-queue when deferred compilation is requested.  (Bug#44676)
2020-11-20 20:55:09 +01:00
Stefan Kangas
cf436db285 ; Fix trivial typos 2020-11-20 20:35:44 +01:00
Stefan Kangas
cee6401c13 Various doc fixes for comp.el and comp.c
* lisp/emacs-lisp/comp.el: Remove redundant :group args.
(comp-async-cu-done-hook, comp-async-all-done-hook)
(comp-async-env-modifier-form, comp-dry-run)
(comp-ensure-native-compiler, comp-func-ret-typeset)
(comp-func-ret-range, comp-limple-lock-keywords)
(comp-make-curr-block):
* src/comp.c (Fcomp_el_to_eln_filename, Fcomp__init_ctxt)
(Fcomp_native_driver_options_effective_p)
(Fcomp__compile_ctxt_to_file, Fcomp_libgccjit_version)
(Fcomp__register_lambda, Fcomp__register_subr)
(Fcomp__late_register_subr, Fnative_elisp_load, syms_of_comp): Doc fixes.
2020-11-16 16:06:47 +01:00
Andrea Corallo
898f929215 Fix nativecomp cond-rw pass
* lisp/emacs-lisp/comp.el (comp-mvar-symbol-p): Improve it.
	(comp-cond-rw-func): Fix logic for multiple predecessor on target
	block.
	* test/src/comp-tests.el (comp-test-cond-rw-1): New test.
	* test/src/comp-test-funcs.el (comp-test-cond-rw-1-1-f)
	(comp-test-cond-rw-1-2-f): New functions.
2020-11-16 15:32:52 +01:00
Andrea Corallo
54f2e9c06d * Improve `comp-fwprop-call'
* lisp/emacs-lisp/comp.el (comp-function-call-maybe-fold):
	Document return value.
	(comp-fwprop-call): Simplify and improve.
2020-11-15 21:16:23 +01:00
Andrea Corallo
2f8d0fca88 * Add more type specifiers
* lisp/emacs-lisp/comp.el (comp-known-type-specifiers): Add more
	pure functions.
2020-11-15 18:51:06 +01:00
Andrea Corallo
f702426780 Add `comp-constraint-to-type-spec' and better handle boolean type spec
* lisp/emacs-lisp/comp.el (comp-constraint-to-type-spec): New
	function splitting out code from comp-ret-type-spec + better
	handle boolean type specifier.
	(comp-ret-type-spec): Rework to leverage
	`comp-constraint-to-type-spec'.
	* test/src/comp-tests.el (comp-tests-type-spec-tests): Add a
	testcase.
2020-11-14 22:06:31 +01:00
Andrea Corallo
bcecdedcb7 Handle correctly quoting in *Native-compile-Log* buffer
* lisp/emacs-lisp/comp.el (comp-log): Add `quoted' parameter and
	pass it to `comp-log-to-buffer'.
	(comp-log-to-buffer): Add `quoted' parameter and leverage `prin1'
	or `princ' accordingly.
2020-11-14 22:06:31 +01:00
Andrea Corallo
22da28cf66 * Split logic into comp-fwprop-call and improve it
* lisp/emacs-lisp/comp.el (comp-func-ret-valset)
	(comp-fwprop-call): New functions.
	(comp-fwprop-insn): Remove code duplicaiton and call
	`comp-fwprop-call'.
2020-11-14 22:06:31 +01:00
Andrea Corallo
aced2cf6ac * Add a number of type specifiers for pure function
* lisp/emacs-lisp/comp.el (comp-known-type-specifiers): Add 60
	pure function type specifiers.
2020-11-14 22:06:31 +01:00
Andrea Corallo
a467fa5c49 Characterize functions in terms of type specifiers
* lisp/emacs-lisp/comp.el (comp-known-type-specifiers): New const
	in place of `comp-known-ret-types' and `comp-known-ret-ranges'.
	(comp-constraint): New struct to separate the constraint side of
	an mvar.
	(comp-constraint-f): Analogous for functions.
	(comp-mvar): Rework and include `comp-constraint'.
	(comp-type-spec-to-constraint): New function.
	(comp-known-constraints-h): New const.
	(comp-func-ret-typeset, comp-func-ret-range): Rework.
	(comp-fwprop-insn): Fix.
	* test/src/comp-tests.el (destructure-type-spec): New testcase.
2020-11-14 22:06:31 +01:00
Andrea Corallo
c412d396b0 * lisp/emacs-lisp/comp.el (comp-mvar-value-vld-p): Fix logic. 2020-11-12 23:58:15 +01:00
Andrea Corallo
a37cc95e21 * Memoize `comp-common-supertype'
* lisp/emacs-lisp/comp.el (comp-ctxt): Add `common-supertype-mem'
	slot.
	(comp-common-supertype): Memoize.
2020-11-12 23:57:43 +01:00
Andrea Corallo
6f10e0f09f * Rework comp-ret-type-spec' in terms of comp-phi'
* lisp/emacs-lisp/comp.el (comp-ret-type-spec): Use `comp-func'
	not to duplicate logic plus add null type specifier support and
	some comments.
2020-11-12 23:55:49 +01:00
Andrea Corallo
c4749cebeb * Move phi function code into dedicated function and improve it
* lisp/emacs-lisp/comp.el (comp-phi): New function moving logic
	from `comp-fwprop-insn'.
2020-11-12 23:53:26 +01:00
Andrea Corallo
93a80a4fae * Add nativecomp derived return type specifier computation support
* lisp/emacs-lisp/comp.el (comp-post-pass-hooks): Nit.
	(comp-func): Add `ret-type-specifier' slot.
	(comp-ret-type-spec): New function.
	(comp-final): Call `comp-ret-type-spec'.
2020-11-12 00:55:37 +01:00
Andrea Corallo
6b7c257e0b * Unline some functions to optimize bootstrap time
* lisp/emacs-lisp/comp.el (comp-mvar-value-vld-p)
	(comp-mvar-value, comp-mvar-fixnum-p, comp-set-op-p)
	(comp-assign-op-p, comp-call-op-p, comp-type-hint-p)
	(comp-func-ret-typeset, comp-function-pure-p)
	(comp-alloc-class-to-container, comp-lex-byte-func-p)
	(comp-lap-eob-p, comp-lap-fall-through-p, comp-emit)
	(comp-emit-set-call, comp-mvar-propagate)
	(comp-function-foldable-p, comp-function-call-maybe-fold)
	(comp-trampoline-filename): Uninline functions.
2020-11-12 00:55:37 +01:00
Andrea Corallo
00b4e0a9bb * Fix limple-mode for new type and range limple semantic
* lisp/emacs-lisp/comp.el (comp-limple-branches, comp-limple-ops):
	New variables.
	(comp-limple-lock-keywords): Update value.
2020-11-12 00:55:36 +01:00
Andrea Corallo
e96cd4e82c Add initial nativecomp typeset and range propagation support
This commit add an initial support for a better type propagation and
integer range propagation.

Each mvar can be now characterized by a set of types, a set of values
and an integral range.

	* lisp/emacs-lisp/comp.el (comp-known-ret-types): Store into
	typeset and remove fixnum.
	(comp-known-ret-ranges, comp-type-predicates): New variables.
	(comp-ctxt): Remove supertype-memoize slot and add
	union-typesets-mem.
	(comp-mvar): Remove const-vld, constant, type slots. Add typeset,
	valset, range slots.
	(comp-mvar-value-vld-p, comp-mvar-value, comp-mvar-fixnum-p)
	(comp-mvar-symbol-p, comp-mvar-cons-p)
	(comp-mvar-type-hint-match-p, comp-func-ret-typeset)
	(comp-func-ret-range): New functions.
	(make-comp-mvar, make-comp-ssa-mvar): Update logic.
	(comp--typeof-types): New variable.
	(comp-supertypes, comp-common-supertype): Logic update.
	(comp-subtype-p, comp-union-typesets, comp-range-1+)
	(comp-range-1-, comp-range-<, comp-range-union)
	(comp-range-intersection): New functions.
	(comp-fwprop-prologue, comp-mvar-propagate)
	(comp-function-foldable-p, comp-function-call-maybe-fold)
	(comp-fwprop-insn, comp-call-optim-func, comp-finalize-relocs):
	Logic update.

	* src/comp.c (emit_mvar_rval, emit_call_with_type_hint)
	(emit_call2_with_type_hint): Logic update.

	* lisp/emacs-lisp/cl-preloaded.el (cl--typeof-types): Undo the add
	of fixnum and bignum as unnecessary.

	* test/src/comp-tests.el
	(comp-tests-mentioned-p-1, comp-tests-cond-rw-checker-val)
	(comp-tests-cond-rw-checker-type, cond-rw-1, cond-rw-2)
	(cond-rw-3, cond-rw-4, cond-rw-5): Update for new type interface.
	(range-simple-union, range-simple-intersection): New integer range
	tests.
	(union-types): New union type test.
2020-11-12 00:55:36 +01:00