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

Add INLINE syntax #1290

merged 22 commits into from
Jan 28, 2025

Conversation

digikar99
Copy link
Contributor

The final PR towards adding inlining support in coalton. The earlier ones include:

This PR adds support for (inline) syntax in (i) coalton toplevel (ii) inside define-instance before methods. See the file tests/inliner-tests.lisp for a number of examples.

Particular aspects I can think of reviewing in this PR include:

  • if someone prefers the name inline to inline-p at some places
  • adding more tests / improving them

But yes, let's address any other worries or issues others spot!

Copy link
Collaborator

@macrologist macrologist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly nits

src/codegen/optimizer.lisp Outdated Show resolved Hide resolved
(_ (and (node-abstraction-p code)
(funcall heuristic code)
(or inline-p (funcall heuristic code))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, inline-p will always be non-nil at this point if this code is evaluated at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to rewrite this condition as the following?

                  (fun-env-entry (tc:lookup-function env name :no-error t))
                  (_    (and (node-abstraction-p code)
                             (or (tc:function-env-entry-inline-p fun-env-entry)
                                 (funcall heuristic code))
                             (<= (length call-stack)
                                 max-depth)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that is a good way to rewrite it so that it will work with the when-let*.
The only thing I'm unsure of is if there ever can be a case when there is an entry in the code-environment but not in the function-environment. (I can't tell easily just from looking at the code, since the code entries and function entries are added at very different places in the code.) If so, then in order to allow heuristic inlining of functions with code env entries but not function env entries, it seems like a separate let statement might be necessary, like

                  (_    (and (node-abstraction-p 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)

or if not, then your suggestion should work exactly as written, or even without the :no-error t.

Copy link
Contributor Author

@digikar99 digikar99 Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove :no-error t in my suggestion, I obtain the error "Internal coalton bug: Unknown function TREE:INCREASING-ORDER" while compiling library/ord-map.lisp. So, there indeed seem to be cases with code env entries but not function env entries.

However, I note that your suggestion works even without the :no-error t. My hypothesis is there are fun-env-entries only when code is an abstraction node.

Copy link
Contributor Author

@digikar99 digikar99 Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, in those cases, code is an application node.

                  (fun-env-entry (let ((fun-env-entry (tc:lookup-function env name :no-error t)))
                                   (unless fun-env-entry (print (type-of code)))
                                   fun-env-entry))
                  (_    (and (node-abstraction-p code)
                             (or (tc:function-env-entry-inline-p fun-env-entry)
                                 (funcall heuristic code))

With the above code, I obtain COALTON-IMPL/CODEGEN/AST:NODE-APPLICATION as print statements while compiling library/ord-map.lisp and library/seq.lisp.

I'm inclined to commit your suggestion without :no-error t to better understand internal behavior. :no-error t feels like a hack here and I think it would rather be used as such.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Committed in 849af1f

src/parser/toplevel.lisp Outdated Show resolved Hide resolved
@stylewarning stylewarning requested a review from shirok October 7, 2024 10:56
@shirok
Copy link
Collaborator

shirok commented Oct 9, 2024

This is a general comment, probably for future work. As the optimizer gets more sophisticated, there'll be more context like monomorphize-table and inline-p-table to be passed. At some point we might want to consolidate them into optimizer context, maybe?

Copy link
Collaborator

@YarinHeffes YarinHeffes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from my main change request in the parser for inlining methods, and for tests in tests/test-files/define-instance.txt, this PR looks ready to me. A follow-up PR should include documentation in docs/intro-to-coalton.md, and eventually, users should have control over variables like max-unroll and max-depth when using the attribute. These two points should not hold up this PR.

src/parser/toplevel.lisp Show resolved Hide resolved
src/parser/toplevel.lisp Show resolved Hide resolved
tests/test-files/define-type.txt Show resolved Hide resolved
tests/test-files/define.txt Show resolved Hide resolved
@digikar99 digikar99 force-pushed the syntax-inline branch 3 times, most recently from 726f748 to aea25b3 Compare January 19, 2025 00:12
Copy link
Collaborator

@YarinHeffes YarinHeffes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me overall, just a few small changes 👍

src/parser/toplevel.lisp Show resolved Hide resolved
src/parser/toplevel.lisp Outdated Show resolved Hide resolved
src/entry.lisp Show resolved Hide resolved
src/typechecker/environment.lisp Outdated Show resolved Hide resolved
src/codegen/monomorphize.lisp Show resolved Hide resolved
@stylewarning stylewarning dismissed macrologist’s stale review January 28, 2025 04:51

Lots of changes since.

@stylewarning stylewarning enabled auto-merge (rebase) January 28, 2025 04:52
@stylewarning stylewarning merged commit ef8daa5 into coalton-lang:main Jan 28, 2025
25 checks passed
@stylewarning
Copy link
Member

I didn't squash commits though I normally would, just to preserve some history. At long last! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants