Commit graph

715 commits

Author SHA1 Message Date
Andrea Corallo
7622740e29 * Introduce a new pass ipa-pure
Add a simple pass to infer pure functions not explicitly declared as
such.  Use this information only during compilation (speed 3) to
optimize out function calls whe possible.
2020-07-09 16:22:48 +01:00
Andrea Corallo
5688739c5b * Add `comp-call-op-p'
* lisp/emacs-lisp/comp.el (comp-call-op-p): New predicate.
	(comp-limple-insn-call-p): Make use of.
2020-07-09 16:22:48 +01:00
Andrea Corallo
3db6ace804 * Define `comp-symbol-func-to-fun'
* lisp/emacs-lisp/comp.el (comp-symbol-func-to-fun): New function.
	(comp-func-in-unit): Make use of the `comp-symbol-func-to-fun'.
2020-07-09 16:22:48 +01:00
Andrea Corallo
0b81044e7e * Clean-up some const folding logic and add `comp-function-pure-p'
* lisp/emacs-lisp/comp.el (comp-function-pure-p): New predicate.
	(comp-function-call-maybe-remove): Update to use the
	`comp-function-pure-p'.
2020-07-08 17:26:51 +01:00
Andrea Corallo
2593bbee51 * Relax constant folding rules
* lisp/emacs-lisp/comp.el (comp-function-optimizable-p): No need to
check for operands or result to be fixnums.
2020-07-04 15:55:48 +01:00
Andrea Corallo
b0f683ec16 * Fix missing tail recursion elimination
* lisp/emacs-lisp/comp.el (comp-tco-func): Fix tail recursion
	elimination given now functions in LIMPLE are expressed with
	the C name.
2020-07-02 22:55:42 +02:00
Andrea Corallo
8f81859497 Rework `comp-c-func-name' arguments
* lisp/emacs-lisp/comp.el (comp-c-func-name): Add FIRST argument
	to ignore the compiler context and return the first name.

	* lisp/emacs-lisp/disass.el (disassemble-internal): Update the
	`comp-c-func-name' call.
2020-07-02 22:39:39 +02:00
Andrea Corallo
b67e156041 * Add to possibility to write per pass specific tests
* lisp/emacs-lisp/comp.el (comp-post-pass-hooks): New special
	variable.
	(native-compile): Run what is registered in
	`comp-post-pass-hooks'.
2020-07-02 22:39:32 +02:00
Andrea Corallo
4681f33071 Fix lambda-list relocation class
Lambda-lists must stay in the same relocation class of the object
referenced by code to respect uninterned symbols.

	* lisp/emacs-lisp/comp.el (comp-prepare-args-for-top-level): Break
	the original function in a generic specializing for
	dynamic/lexical functions.  When allocating the lambda-list for
	dynamic functions do that in the default relocation class.
	(comp-emit-for-top-level): Make use of the new
	`comp-prepare-args-for-top-level'.
	(comp-emit-lambda-for-top-level): Likewise.
2020-06-30 21:30:35 +02:00
Andrea Corallo
0ce4bf3ede * Do not skip native compilation for leim subfolder during boostrap
* lisp/emacs-lisp/comp.el (comp-bootstrap-black-list): Remove
	"^leim/".
2020-06-28 21:23:45 +01:00
Andrea Corallo
cfb871add4 * Handle correctly pure delaration specifier.
* lisp/emacs-lisp/comp.el (comp-func): New slot 'pure'.
	(comp-spill-decl-spec): New function.
	(comp-spill-speed): Rework to use the later.
	(comp-spill-lap-function, comp-intern-func-in-ctxt): Spill pure
	decl value.
	(comp-function-optimizable-p): Check in the compiler env too if
	pure.
2020-06-22 00:13:43 +02:00
Andrea Corallo
decced8337 Allow per function speed declaration
* src/comp.c (COMP_SPEED): Rename.
	(comp_t): Add 'func_speed' field.
	(emit_mvar_lval, compile_function): Update for per function speed.
	(Fcomp__compile_ctxt_to_file): COMP_SPEED renamed.

	* lisp/emacs-lisp/comp.el (comp-speed): Doc update.
	(comp-func): New 'speed' slot.
	(comp-spill-speed): New function.
	(comp-spill-lap-function, comp-intern-func-in-ctxt): Fill 'speed'
	slot.
	(comp-spill-lap-function): Gate -1 speed functions for native
	compilation and emit bytecode instead.
	(comp-spill-lap): Close over `byte-to-native-plist-environment'.
	(comp-latch-make-fill): Update for per function speed.
	(comp-limplify-top-level): Fill speed.
	(comp-propagate1, comp-call-optim-form-call, comp-call-optim)
	(comp-dead-code, comp-tco, comp-remove-type-hints): Update for per
	function speed.
2020-06-22 00:05:29 +02:00
Andrea Corallo
29b2a08c36 Execute top level forms in the right lex/dyn scope.
* lisp/emacs-lisp/bytecomp.el (byte-to-native-top-level): Add
	'lexical' slot.
	(byte-compile-output-file-form): Update for new slot.
	(byte-compile-file-form-defmumble): Capture scope.

	* lisp/emacs-lisp/comp.el (comp-emit-for-top-level): Specify
	execution scope.
2020-06-22 00:03:23 +02:00
Andrea Corallo
c37b5446d1 Add native compiler dynamic scope support
Add an initial implementation to support dynamic scope.  Arg
parsing/binding it's done using the existing code in use for
bytecode (no ad-hoc code is synthetized for that).

	* src/lisp.h (struct Lisp_Subr): Add lambda_list field.
	(SUBR_NATIVE_COMPILED_DYNP): New inliner.

	* src/alloc.c (mark_object): Update for Add lambda_list field.

	* src/eval.c (eval_sub, Ffuncall, funcall_lambda): Handle native
	compiled dynamic scope

	* src/comp.c (declare_lex_function): Rename from declare_function
	and rework.
	(declare_function): New function.
	(make_subr): Handle daynamic scope

	* src/pdumper.c (dump_subr): Update for lambda_list field.

	* lisp/emacs-lisp/comp.el (comp-func): Remove args slot.
	(comp-func-l, comp-func-d): New classes deriving from `comp-func'.
	(comp-spill-lap-function): Rework.
	(comp-prepare-args-for-top-level): New function.
	(comp-emit-for-top-level, comp-emit-lambda-for-top-level): Make
	use of `comp-prepare-args-for-top-level'.
	(comp-limplify-top-level): Use `comp-func-l'.
	(comp-limplify-function): Emit arg prologue only for dynamic
	scoped functions.
	(comp-call-optim-form-call): Use `comp-func-l'.
	(comp-call-optim, comp-tco): Do not optimize dynamic scoped code.
2020-06-19 09:04:49 +02:00
Andrea Corallo
34ed9d2498 * Introduce latches
Define a new kind of basic block 'latch' to close over loops.  Its
purpose is for now to emit calls to `comp-maybe-gc-or-quit' but in
future will be usefull for the loop optimizer to exploit unboxes.

	* lisp/emacs-lisp/comp.el (comp-block): New base class.
	(comp-block-lap): New class for LAP derived basic blocks.
	(comp-latch): New class.
	(comp-bb-maybe-add, comp-make-curr-block, comp-emit-handler)
	(comp-emit-switch, comp-emit-switch, comp-limplify-top-level)
	(comp-addr-to-bb-name, comp-limplify-block)
	(comp-limplify-function): Update logic for new bb objects
	arrangment.
	(comp-latch-make-fill): New function.
	(comp-emit-uncond-jump, comp-emit-cond-jump): Update to emit
	latches.
	(comp-new-block-sym): Add a postfix paramenter.
2020-06-13 16:37:14 +02:00
Andrea Corallo
3d3737b90a * Move final log after containers has been finalized
* lisp/emacs-lisp/comp.el (comp-final): Remove function log.
	(comp-compile-ctxt-to-file): Add function log.
2020-06-08 21:51:24 +01:00
Andrea Corallo
fbf4882a8b * Rename comp-function-optimizable -> comp-function-optimizable-p
* lisp/emacs-lisp/comp.el (comp-function-optimizable): Rename into
	'comp-function-optimizable-p'.
	(comp-function-call-maybe-remove): Use the new name.
2020-06-07 19:20:04 +02:00
Andrea Corallo
88ccee4083 * Fix comp-call-optim-form-call for null `callee'
* lisp/emacs-lisp/comp.el (comp-call-optim-form-call): Guard
	agains null `calle'.
2020-06-07 19:20:04 +02:00
Andrea Corallo
47a6fbd382 * Improve propagate pass
As function folding can generate 'setimm' insns handle them in the
`comp-propagate-insn'.

	* lisp/emacs-lisp/comp.el (comp-propagate-insn): Handle 'setimm'
	insn.
2020-06-07 16:59:23 +02:00
Andrea Corallo
a58fef9f63 * Optimize optimizable variables
* lisp/emacs-lisp/comp.el (comp-symbol-values-optimizable): New
	defconst.
	(comp-function-call-maybe-remove): New logic to to remove
	unnecessary `symbol-value' calls.
2020-06-07 10:36:58 +02:00
Andrea Corallo
489a79de96 * Mitigate possible speed 3 miss-optimization
Do not perform trampoline optimization at speed 3 on function if their
name is not unique inside the compilation unit.  Note that the
function can still be redefined in any other way therefore this is a
mitigation.

	* lisp/emacs-lisp/comp.el (comp-func-unique-in-cu-p): New
	predicate.
	(comp-call-optim-form-call): Perform trampoline optimization
	for named functions only if they are unique within the current
	compilation unit.
2020-06-06 22:03:11 +01:00
Andrea Corallo
dcfcbb14f5 * Allow for optimizing anonymous lambdas in call-optim
* lisp/emacs-lisp/comp.el (comp-func-in-unit): New function.
	(comp-call-optim-form-call): Update logic for optimizing
	anonymous lambdas.
2020-06-06 22:03:11 +01:00
Andrea Corallo
e8ab017b6d Change 'direct-call' 'direct-callref' LIMPLE ops sematinc
Is cleaner to have the function c-name as first argument of
'direct-call' 'direct-callref'.  This is preparatory to anonymous
lambdas optimization.

	* lisp/emacs-lisp/comp.el (comp-propagate-insn): Use c-name when
	gathering the comp-func definition for direct calls.
	(comp-call-optim-form-call): Add put c-name as first argument of
	direct-call direct-callref when optimizing.

	* src/comp.c (emit_call): Update logic for having c-name as
	first arg of direct calls.
	(emit_call_ref): Rename 'subr_sym' into 'func'.
2020-06-06 22:03:11 +01:00
Andrea Corallo
e4e6bb7fdd * Introduce `comp-loop-insn-in-block'
* lisp/emacs-lisp/comp.el (comp-loop-insn-in-block): New macro.
	(comp-call-optim-func, comp-dead-assignments-func)
	(comp-remove-type-hints-func): Use `comp-loop-insn-in-block'.
2020-06-03 22:06:26 +01:00
Andrea Corallo
f28b1780c6 * Split type hint pass from dead code removal pass into dedicated one.
Given SSA prop overwrite mvar type slot we clean-up the compiler type
hints as last.

* lisp/emacs-lisp/comp.el (comp-passes): Add comp-remove-type-hints.
(comp-remove-type-hints-func): Code move.
(comp-dead-code): Do not call `comp-remove-type-hints-func'.
(comp-remove-type-hints): Add as new pass.
2020-05-25 22:10:42 +01:00
Andrea Corallo
0bba0e367b Fix GNU style
* src/comp.h: Fix GNU style.

* src/comp.c (Fcomp__compile_ctxt_to_file): Likewise.

* lisp/emacs-lisp/comp.el (comp--replace-output-file): Likewise.

* src/pdumper.c (dump_do_dump_relocation): Likewise.
2020-05-24 21:59:25 +01:00
Nicolás Bértolo
1b809f378f Improve handling of native compilation units still in use in Windows
When closing emacs will inspect all directories from which it loaded
native compilation units. If it finds a ".eln.old" file it will try to
delete it, if it fails that means that another Emacs instance is using it.

When compiling a file we rename the file that was in the output path
in case it has been loaded into another Emacs instance.

When deleting a package we move any ".eln" or ".eln.old" files in the
package folder that we can't delete to `package-user-dir`. Emacs will
check that directory when closing and delete them.

* lisp/emacs-lisp/comp.el (comp--replace-output-file): Function called
from C code to finish the compilation process. It performs renaming of
the old file if necessary.
* lisp/emacs-lisp/package.el (package--delete-directory): Function to
delete a package directory. It moves native compilation units that it
can't delete to `package-user-dir'.
* src/alloc.c (cleanup_vector): Call dispose_comp_unit().
  (garbage_collect): Call finish_delayed_disposal_of_comp_units().
* src/comp.c: Restore the signal mask using unwind-protect. Store
loaded native compilation units in a hash table for disposal on
close. Store filenames of native compilation units GC'd in a linked
list to finish their disposal when the GC is over.
(clean_comp_unit_directory): Delete all *.eln.old files in a
directory.
(clean_package_user_dir_of_old_comp_units): Delete all *.eln.old files
in `package-user-dir'.
(dispose_all_remaining_comp_units): Dispose of native compilation
units that are still loaded.
(dispose_comp_unit): Close handle and cleanup directory or arrange for
later cleanup if DELAY is true.
(finish_delayed_disposal_of_comp_units): Dispose of native compilation
units that were GC'd.
(register_native_comp_unit): Register native compilation unit for
disposal when Emacs closes.
* src/comp.h: Introduce cfile member in Lisp_Native_Comp_Unit.
Add declarations of functions that: clean directories of unused native
compilation units, handle disposal of native compilation units.
* src/emacs.c (kill-emacs): Dispose all remaining compilation units
right right before calling exit().
* src/eval.c (internal_condition_case_3, internal_condition_case_4):
Add functions.
* src/lisp.h (internal_condition_case_3, internal_condition_case_4):
Add functions.
* src/pdumper.c (dump_do_dump_relocation): Set cfile to a copy of the
Lisp string specifying the file path.
2020-05-25 09:42:10 +01:00
Nicolás Bértolo
d59607b685 * Windows: Use NUMBER_OF_PROCESSORS environment variable.
* lisp/emacs-lisp/comp.el (comp-effective-async-max-jobs): Use
NUMBER_OF_PROCESSORS environment variable if system is Windows NT,
"nproc" if it is in PATH or a default of 1.
2020-05-23 09:36:52 +01:00
Nicolás Bértolo
60b326ef11 * Workaround the 32768 chars command line limit in Windows.
* lisp/emacs-lisp/comp.el (comp-run-async-workers): Pass the
compilation commands through a temporary file that is loaded by the
child process. This is also done all other operating systems, even
those that support long command lines. It should not be a problem
since libgccjit uses temporary files too.
2020-05-23 09:36:52 +01:00
Andrea Corallo
f5ba60defb * lisp/emacs-lisp/comp.el (comp-num-cpus): Fix definition.
Introduced by 2aec16ab75.
2020-05-19 08:43:38 +01:00
Andrea Corallo
2aec16ab75 * Pacify with the byte-compiler
* lisp/emacs-lisp/comp.el (comp-num-cpus): New special variable.
	(comp-effective-async-max-jobs): Make use of `comp-num-cpus'.
	(comp-call-optim-form-call): Remove unnecessary parameter.
	(comp-call-optim-func): Reflect `comp-call-optim-form-call'
	parameter removal.
2020-05-18 21:04:36 +01:00
Andrea Corallo
2ac6194585 * Add new customize `comp-async-env-modifier-form' (Bug#40838)
* lisp/emacs-lisp/comp.el (comp-async-env-modifier-form): New
	customize.
	(comp-run-async-workers): Make use of `comp-async-env-modifier-form'.
2020-05-18 21:04:36 +01:00
Andrea Corallo
6d850b50c5 * Make the Evil happy (Bug#41374)
* lisp/emacs-lisp/comp.el (comp-never-optimize-functions):
	Blacklist all primitives advised by evil-mode from trampoline
	optimization.
	(comp-call-optim-form-call): Prevent trampoline optimization for
	recursive calls at speed 2 to respect elisp original semantic.
2020-05-18 19:19:05 +01:00
Andrea Corallo
d6f6353cfd * Do not refuse to compile if a dynamic lambda is encountered
* lisp/emacs-lisp/comp.el (comp-lex-byte-func-p): New subst.
	(comp-intern-func-in-ctxt): Do not crash if we still encounter a
	non lexical scoped lambda.
2020-05-15 20:06:49 +01:00
Andrea Corallo
9a64585c12 * Allow for logging async compilation command line
* lisp/emacs-lisp/comp.el (comp-run-async-workers): When non zero
	verbose log async compilation command line invocation.
2020-05-15 20:06:49 +01:00
Andrea Corallo
e351a12216 Sanity check on lambdas fixups
* src/pdumper.c (dump_do_dump_relocation): While fixing up lambda
	relocation verify placeholder coherency.

	* src/comp.c (syms_of_comp): Define symbol 'lambda-fixup'.

	* lisp/emacs-lisp/comp.el (comp-finalize-container): Leave a
	lambda-fixup as placeholder in the relocation as a sanity check.
2020-05-15 20:06:49 +01:00
Andrea Corallo
2b064c780c * Fix speed 2 bootstrap
(comp-call-optim-func): Do nothing if the function name is
	unknown.
2020-05-15 20:06:49 +01:00
Andrea Corallo
44b0ce6e38 Add anonymous lambdas reload mechanism
* src/pdumper.c (dump_do_dump_relocation): Initialize
	'lambda_gc_guard' while resurrecting.
	(dump_do_dump_relocation): Revive lambdas and fixup them.

	* src/comp.h (struct Lisp_Native_Comp_Unit): Define new
	'lambda_gc_guard' 'lambda_c_name_idx_h' 'data_imp_relocs'
	'loaded_once' fields.

	* src/comp.c (load_comp_unit): Use compilaiton unit 'loaded_once'
	field.
	(make_subr, Fcomp__register_lambda): New functions.
	(Fcomp__register_subr): Make use of 'make_subr'.
	(Fnative_elisp_load): Indent.
	(Fnative_elisp_load): Initialize 'lambda_gc_guard'
	'lambda_c_name_idx_h' fields.
	(syms_of_comp): Add Scomp__register_lambda.

	* lisp/emacs-lisp/comp.el (comp-ctxt): Change
	'byte-func-to-func-h' hash key test.
	(comp-ctxt): Add 'lambda-fixups-h' slot.
	(comp-emit-lambda-for-top-level): New function.
	(comp-finalize-relocs): Never emit lambdas in pure space.
	(comp-finalize-relocs): Fixup relocation indexes.
2020-05-15 20:06:49 +01:00
Andrea Corallo
2ee2fb5a86 * Prune now unnecessary byte-code objects
* lisp/emacs-lisp/comp.el (comp-finalize-container): Prune
	byte-code that was lambdas.
	(comp-compile-ctxt-to-file): Remove fixme.
2020-05-14 21:50:32 +01:00
Andrea Corallo
c12831a6b6 * Rework comp-spill-lap-function
* lisp/emacs-lisp/comp.el (comp-spill-lap-function): Move code
	from to comp-intern-func-in-ctxt.
	(comp-intern-func-in-ctxt): New function, this guard
	in case byte-to-native-lambda-byte-func is nil.
2020-05-14 21:50:32 +01:00
Andrea Corallo
a335f7eeac Update spill LAP machinery and compile anonymous lambdas
* lisp/emacs-lisp/comp.el (comp-spill-lap-function): Make use of
	byte-to-native-lambdas-h and update for 'byte-to-native-func-def'.
	(comp-spill-lap-function): Rework logic to retrive LAP using
	'byte-to-native-lambdas-h'.
	(comp-emit-for-top-level): Update for 'byte-to-native-function'.

	* lisp/emacs-lisp/bytecomp.el: Add commentary on new spill LAP
	mechanism.
	(byte-to-native-lambda, byte-to-native-func-def): New structures.
	(byte-to-native-top-level): Indent.
	(byte-to-native-lambdas-h): Update doc.
	(byte-compile-lapcode): Add a 'byte-to-native-lambda' instance
	into byte-to-native-lambdas-h instead of just LAP.
	(byte-compile-file-form-defmumble): Store into
	'byte-to-native-func-def' only the byte compiled function, the LAP
	will be retrived through 'byte-to-native-lambdas-h'.
	(byte-compile-lambda): Return the byte compiled function.
2020-05-14 21:50:31 +01:00
Andrea Corallo
9bc0a7c408 * Fix `comp-deferred-compilation-black-list' effectiveness
* lisp/emacs-lisp/comp.el (native-compile-async): Fix logic for
	'comp-deferred-compilation-black-list' effectiveness.
2020-05-10 09:34:21 +01:00
Andrea Corallo
40f655e050 * Add 'comp-deferred-compilation-black-list' customize
* lisp/emacs-lisp/comp.el (comp-deferred-compilation-black-list):
	New customize.
	(native-compile-async): Make use of
	'comp-deferred-compilation-black-list'.
2020-05-09 14:06:43 +01:00
Andrea Corallo
cf105f6044 * Fix bug#41112
* lisp/emacs-lisp/comp.el (comp-jump-table-optimizable): New
	function.
	(comp-emit-switch): Make use of 'comp-jump-table-optimizable'.
2020-05-07 10:23:10 +01:00
Andrea Corallo
f8df3320b1 * Add native compilation unit black list
* lisp/emacs-lisp/comp.el (comp-bootstrap-black-list): New customize.
	(batch-native-compile): Rework to make use of
	'comp-bootstrap-black-list'.
	(batch-byte-native-compile-for-bootstrap): Add assertion to make
	logic assumption explicit.
2020-05-06 20:11:47 +01:00
Andrea Corallo
1ec7499e59 * Add a warning for missing write privilege
* lisp/emacs-lisp/comp.el (native-compile-async): Check for write
	privilege and raise a warning in case.
2020-05-03 13:37:38 +01:00
Andrea Corallo
8d37220190 * Introduce `comp-output-directory'
* lisp/emacs-lisp/comp.el (comp-output-directory): New function.
	(comp-output-base-filename): Use `comp-output-directory'.
2020-05-03 14:35:50 +01:00
Andrea Corallo
02e3ffad6d * Fix async compilation non respecting `comp-always-compile' nil value.
* lisp/emacs-lisp/comp.el (comp-run-async-workers): Fix missing
	`comp-output-filename' usage.
2020-04-29 21:25:02 +01:00
Andrea Corallo
f8b254d195 Rework spill LAP mechanism in preparation of compiling lambdas.
* lisp/emacs-lisp/comp.el (comp-spill-lap-function): No need anymore
	to have `byte-native-compiling' bound to free-func.
	(comp-spill-lap-function): Make use of `byte-to-native-lap-h' and
	clean-up.
	(comp-spill-lap-function): Likewise.

	* lisp/emacs-lisp/bytecomp.el (byte-to-native-function): Add lap slot.
	(byte-to-native-lap): Rename into byte-to-native-lap-h.
	(byte-compile-lapcode): Spill lap after having int assembled and
	store it into `byte-to-native-lap-h'.
	(byte-compile-not-top-level): Remove.
	(byte-compile-file-form-defmumble): Fill directly lap slot.
	(byte-compile-lambda): Remove `byte-compile-not-top-level'.
	(byte-compile-out-toplevel): Restore original code.
	(byte-compile-form): Remove `byte-compile-not-top-level'.
	(byte-compile-function-form): Likewise.
	(byte-compile-flush-pending): No need anymore to set
	`byte-compile-current-form' so restore orignal code.
2020-04-26 22:17:37 +01:00
Andrea Corallo
bb4cf13c47 Convert before final function doc hash into a vector.
* lisp/emacs-lisp/comp.el (comp-finalize-relocs): Convert doc hash
	table into vector befor final.
	(comp-emit-for-top-level): Rename `comp-ctxt-doc-index-h' ->
	`comp-ctxt-function-docs'.
	(comp-ctxt): Likewise.

	* src/comp.c (native_function_doc): Update logic for documentation
	being a vector.
	(emit_ctxt_code): Update for 'comp-ctxt-doc-index-h' slot rename.

	* src/comp.h (struct Lisp_Native_Comp_Unit): Rename 'data_fdoc_h'
	into data_fdoc_v.

	* src/pdumper.c (dump_native_comp_unit): Likewise.
2020-04-26 10:10:17 +01:00