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

Hook for custom AST/IR rewriting #2950

Open
japgolly opened this issue May 15, 2017 · 7 comments
Open

Hook for custom AST/IR rewriting #2950

japgolly opened this issue May 15, 2017 · 7 comments
Labels

Comments

@japgolly
Copy link
Contributor

@japgolly japgolly commented May 15, 2017

What do you think about the idea of exposing some kind of hook so that advanced users can add custom rewrite and optimisation rules/transformations to their builds?

@sjrd
Copy link
Member

@sjrd sjrd commented May 15, 2017

We've been wanting to do that for a while, actually. So-called "linker plugins", similar to scalac plugins. We'll probably work on this sometime post-1.0.0, though, based on an even simpler Tools API.

What would help correctly designing such an interface would be to have concrete use cases, though. So if you have some, feel free to post them as comments herein.

@sjrd sjrd added the enhancement label May 15, 2017
@andreaTP
Copy link
Contributor

@andreaTP andreaTP commented May 15, 2017

I have a concrete use case where I manipulate IR before packing it
https://github.com/akka-js/akka.js/blob/master/project/IrPatcherPlugin.scala

basically what I do is to have a separate subproject that implements patches to be applied on the master IR.
This works, but actually has got a number of limitations...

@sjrd
Copy link
Member

@sjrd sjrd commented May 15, 2017

@andreaTP That use case is actually quite different from what I associate this issue to. In your case you patch IR at compile-time, before putting it in .jars. This does not interact with the linker in any way, so it doesn't really fit in a "linker plugin". IMO the way this use case is currently implemented is pretty much already as good as it gets. You could of course make it a linker plugin, but then you'd have to impose that linker plugin to all the users of your library, whereas right now it only needs to be applied in your own build, without users noticing anything.

Linker plugins would transform the IR at link-time, when we have the IR of the whole program. Probably before linking (raw IR) or after linking but before the optimizer kicks in (linked IR).

Allowing custom transformations after the optimizer is too dangerous currently, because the validity of what the optimizer outputs is only specified by "it goes through the Emitter somehow, with appropriate semantics". Everything before the optimizer is much better specified.

@andreaTP
Copy link
Contributor

@andreaTP andreaTP commented May 15, 2017

I do not read anywhere "linker-plugin" in the original proposal,
it talks about:

custom rewrite and optimisation rules/transformations

I agree that my transformations aren't checked, and that's why I'm proposing it as a use-case.
Anyhow I really see a point in applying trasformations before putting in jar because it affect only the original codebase and doesn't force users to be aware of dirty tricks.

@japgolly
Copy link
Contributor Author

@japgolly japgolly commented May 22, 2017

I'm sorry. I forget what I had in mind when I raised this. (I've been sick in bed for 5 days; fever stole my ideas, let's blame that!)

All I can think of now is rewriting Option to be js.UndefOr which I admit, doesn't seem likely.

@japgolly
Copy link
Contributor Author

@japgolly japgolly commented May 22, 2017

Here's a cool abstract idea: I'd like to be able to define my own optimisations or AST customisation before writing JS. There are a number of optimisations that apply to pure code that you can't make universally in Scala because of a lack of guarantee, or analysis of purity, which means ambiguous semantics always need be preserved. It might be a cool idea to grow a library of optimisations safe for pure code and say "these work great so long as you follow these common FP rules, they aren't for everyone".

@sjrd
Copy link
Member

@sjrd sjrd commented May 22, 2017

That would be hard to write, because you'll have a hard time deciding in which part of the codebase you can apply your optimisations. For example the implementation of the collections is definitely not pure, so you would have to avoid rewriting those.

I think this would be mostly useful for domain-specific optimizations. If your library has some properties that allows optimizing certain method calls, you could use such a plugin to do so. But my hunch is that you would need to make sure that these optimizations are always correct, the only requirement being that your library semantics themselves are what you think they are.

@gzm0 gzm0 added this to the Post-v1.0.0 milestone May 22, 2019
@gzm0 gzm0 removed this from the Post-v1.0.0 milestone Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.