Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add INLINE syntax #1290

Merged
merged 22 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3bd84c8
Add INLINE-P to FUNCTION-ENV-ENTRY in typechecker
digikar99 Oct 4, 2024
750364f
Add METHOD-INLINE-P to TY-CLASS-INSTANCE in typechecker
digikar99 Oct 4, 2024
a8b8355
Add (INLINE) syntax to parser (no effect)
digikar99 Oct 4, 2024
d3f6b4b
METHOD-INLINE-P of TY-CLASS-INSTANCE is now an ENVIRONMENT-MAP
digikar99 Oct 4, 2024
f54dfaa
Pass INLINE-P-TABLE to MONOMORPHIZE
digikar99 Oct 4, 2024
9293866
Use INLINE-P in HEURISTIC-INLINE-APPLICATIONS
digikar99 Oct 4, 2024
1fb6dfa
Some miscellaneous fixes
digikar99 Oct 4, 2024
80ae100
Add more inliner-tests
digikar99 Oct 4, 2024
772d77f
Some more miscellaneous fixes
digikar99 Oct 4, 2024
f3d3103
FUN-ENV-ENTRY can be NIL even when other conditions are satisfied
digikar99 Oct 16, 2024
0dbd815
Remove unnecessary indents from RECURSIVE-INLINE-TEST
digikar99 Nov 9, 2024
5b9da9b
Add test for duplicate inline attributes (Thanks @amorphedstar!)
digikar99 Nov 9, 2024
fb8d61a
Add more parser tests for INLINE
digikar99 Nov 13, 2024
6a4fc88
Renumber last two tests in define-type.txt to 112 and 113
digikar99 Jan 16, 2025
f801d95
Add more tests for inline (thanks @YarinHeffes!)
digikar99 Jan 17, 2025
6c810de
Use @YarinHeffes suggestions to rewrite parse-define-instance
digikar99 Jan 17, 2025
805604d
Rename inline-p to inline in parser because attr and not boolean
digikar99 Jan 18, 2025
d91e835
Use method-codegen-inline-p isntead of method-inline-p
digikar99 Jan 18, 2025
80cfbcd
Make sure the tests pass
digikar99 Jan 19, 2025
b68c4dc
Better error message for dangling (inline) inside define-instance
digikar99 Jan 21, 2025
152cbc4
In entry-point: process inline and monomorphize in the same loop
digikar99 Jan 21, 2025
018fb6c
Spacing alignment in ty-class-instance
digikar99 Jan 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/codegen/monomorphize.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,12 @@ propagate dictionaries that have been moved by the hoister."
(action (:after node-application) #'apply-candidate)
(action (:after node-direct-application) #'apply-candidate)))))

(defun monomorphize (name manager package resolve-table optimize-node env)
(defun monomorphize (name manager package resolve-table inline-p-table optimize-node env)
stylewarning marked this conversation as resolved.
Show resolved Hide resolved
(declare (type symbol name)
(type candidate-manager manager)
(type package package)
(type hash-table resolve-table)
(type hash-table inline-p-table)
(type function optimize-node)
(type tc:environment env)
(values binding-list &optional))
Expand All @@ -435,16 +436,23 @@ propagate dictionaries that have been moved by the hoister."

:for name := (compile-candidate-name candidate)
:for code := (tc:lookup-code env name)
:for function-env-entry := (tc:lookup-function env name :no-error t)
:for inline-p := (and (node-abstraction-p code)
function-env-entry
(tc:function-env-entry-inline-p function-env-entry))
:for new-code := (funcall optimize-node (compile-candidate candidate code env) env)

:for new-code_ := (progn
(candidate-selection new-code manager resolve-table package env)
(rewrite-callsites new-code manager resolve-table env))

:for new-code__ := (funcall optimize-node new-code_ env)
:for candidate-name := (candidate-manager-get manager candidate)

:do (candidate-selection new-code__ manager resolve-table package env)
:do (push (cons (candidate-manager-get manager candidate) (rewrite-callsites new-code__ manager resolve-table env))
binding-group))
:do (push (cons candidate-name (rewrite-callsites new-code__ manager resolve-table env))
binding-group)
:do (when inline-p
(setf (gethash candidate-name inline-p-table) t)))

binding-group))
17 changes: 11 additions & 6 deletions src/codegen/optimizer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

(in-package #:coalton-impl/codegen/optimizer)

(defun update-function-env (bindings env)
(defun update-function-env (bindings inline-p-table env)
(declare (type binding-list bindings)
(type tc:environment env)
(values tc:environment))
Expand All @@ -77,7 +77,8 @@
name
(tc:make-function-env-entry
:name name
:arity arity))))
:arity arity
:inline-p (gethash name inline-p-table)))))
(dolist (name toplevel-values)
(when (tc:lookup-function env name :no-error t)
(setf env (tc:unset-function env name)))))
Expand All @@ -91,9 +92,10 @@
(setf (gethash name table) (tc:function-env-entry-arity entry)))
table))

(defun optimize-bindings (bindings monomorphize-table package env)
(defun optimize-bindings (bindings monomorphize-table inline-p-table package env)
(declare (type binding-list bindings)
(type hash-table monomorphize-table)
(type hash-table inline-p-table)
(type package package)
(type tc:environment env)
(values binding-list tc:environment))
Expand Down Expand Up @@ -129,6 +131,7 @@
manager
package
resolve-table
inline-p-table
(lambda (node env)
(optimize-node node env))
env)
Expand All @@ -138,7 +141,7 @@


;; Update function env
(setf env (update-function-env bindings env))
(setf env (update-function-env bindings inline-p-table env))


(let ((function-table (make-function-table env)))
Expand Down Expand Up @@ -422,7 +425,9 @@ requires direct constructor calls."
((name (node-rator-name node))
(code (tc:lookup-code env name :no-error t))
(_ (and (node-abstraction-p code)
(funcall heuristic code)
(or (alexandria:when-let (fun-env-entry (tc:lookup-function env name :no-error t))
(tc:function-env-entry-inline-p fun-env-entry))
(funcall heuristic code))
(<= (length call-stack)
max-depth)
(<= (count name call-stack)
Expand Down Expand Up @@ -461,7 +466,7 @@ requires direct constructor calls."
NODE in the environment ENV."
(if settings:*coalton-heuristic-inlining*
(heuristic-inline-applications node env)
node))
(heuristic-inline-applications node env :heuristic (constantly nil))))

(defun inline-methods (node env)
(declare (type node node)
Expand Down
5 changes: 3 additions & 2 deletions src/codegen/program.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ Example:
(setf (gethash (car binding) offsets) offset))
:append instance-bindings))

(defun compile-translation-unit (translation-unit monomorphize-table env)
(defun compile-translation-unit (translation-unit monomorphize-table inline-p-table env)
(declare (type tc:translation-unit translation-unit)
(type hash-table monomorphize-table)
(type hash-table inline-p-table)
(type tc:environment env))

(let* ((offsets (make-hash-table))
Expand All @@ -150,7 +151,7 @@ Example:
(definition-names (mapcar #'car definitions)))

(multiple-value-bind (definitions env)
(optimize-bindings definitions monomorphize-table *package* env)
(optimize-bindings definitions monomorphize-table inline-p-table *package* env)

(let ((sccs (node-binding-sccs definitions))
(lisp-forms (tc:translation-unit-lisp-forms translation-unit)))
Expand Down
17 changes: 16 additions & 1 deletion src/entry.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@

(let ((monomorphize-table (make-hash-table :test #'eq))

(inline-p-table (make-hash-table :test #'eq))

(translation-unit
(tc:make-translation-unit
:types type-definitions
Expand All @@ -76,17 +78,30 @@
:when (parser:toplevel-define-monomorphize define)
:do (setf (gethash (parser:node-variable-name (parser:toplevel-define-name define))
monomorphize-table)
t)
:when (parser:toplevel-define-inline define)
:do (setf (gethash (parser:node-variable-name (parser:toplevel-define-name define))
inline-p-table)
t))

(loop :for declare :in (parser:program-declares program)
:when (parser:toplevel-declare-monomorphize declare)
:do (setf (gethash (parser:identifier-src-name (parser:toplevel-declare-name declare))
monomorphize-table)
t)
:when (parser:toplevel-declare-inline declare)
:do (setf (gethash (parser:identifier-src-name (parser:toplevel-declare-name declare))
YarinHeffes marked this conversation as resolved.
Show resolved Hide resolved
inline-p-table)
t))

(loop :for ty-instance :in ty-instances
:for method-codegen-inline-p := (tc:ty-class-instance-method-codegen-inline-p ty-instance)
:do (loop :for (method-codegen-sym . inline-p) :in method-codegen-inline-p
:do (when inline-p (setf (gethash method-codegen-sym inline-p-table) t))))

(analysis:analyze-translation-unit translation-unit env)

(codegen:compile-translation-unit translation-unit monomorphize-table env))))))))))
(codegen:compile-translation-unit translation-unit monomorphize-table inline-p-table env))))))))))


(defun expression-entry-point (node)
Expand Down
1 change: 1 addition & 0 deletions src/package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#:repr
#:lisp-toplevel
#:monomorphize
#:inline
#:specialize
#:unable-to-codegen)

Expand Down
6 changes: 4 additions & 2 deletions src/parser/renamer.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@
:docstring (source:docstring toplevel)
:body (rename-variables-generic% (toplevel-define-body toplevel) new-ctx)
:location (source:location toplevel)
:monomorphize (toplevel-define-monomorphize toplevel))
:monomorphize (toplevel-define-monomorphize toplevel)
:inline (toplevel-define-inline toplevel))
ctx)))

(:method ((method instance-method-definition) ctx)
Expand All @@ -490,7 +491,8 @@
:name (instance-method-definition-name method)
:params (rename-variables-generic% (instance-method-definition-params method) new-ctx)
:body (rename-variables-generic% (instance-method-definition-body method) new-ctx)
:location (source:location method))
:location (source:location method)
:inline (instance-method-definition-inline method))
ctx)))

(:method ((toplevel toplevel-define-instance) ctx)
Expand Down
Loading
Loading