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
Possible constructor inlining improvements #9432
Comments
Here's what each filter pass would do to the example main function: Input code for mainvar fib = new Fib();
var take = new Take(fib, 10);
take.count--;
take.from.next(); apply inline constructorsvar fib : Fib = new Fib();
var take_from : Iterator<Int> = fib; // Take gets inlined
var take_count : Int = 0;
take_count--;
take_from.next(); apply type tighteningvar fib : Fib = new Fib();
var take_from : Fib = fib; // Iterator<Int> changes to Fib
var take_count : Int = 0;
take_count--;
take_from.next(); // take_from.next is now typed as Fib.next field apply function inline passvar fib : Fib = new Fib();
var take_from : Fib = fib;
var take_count : Int = 0;
take_count--;
var r = take_from.a + take_from.b; // take_from.next() gets inlined
take_from.a = take_from.b;
take_from.b = r;
r; apply inline constructor pass 2var fib_a : Int = 0; // new Fib() gets inlined
var fib_b : Int = 1;
var take_count : Int = 0;
take_count--;
var r = fib_a + fib_b;
fib_a = fib_b;
fib_b = r;
r; tighten and inline make no changes and loop ends. |
I guess if this "type tightening" is implemented, it'll also fix #3147? I can't say about the technical part, but I'm sure it would be a welcome addition \o/ |
Some more thoughts: Type tightening might be a lot easier if restricted to variables that are assigned only once. Inline constructors already impose the restriction that object-aliasing variables can only be assigned once or else inlining is cancelled for the aliased object. So I think a restricted type tightening might work well in most cases too. |
Although the approach was different than what I suggested here #9599 achieves the goals in this issue so I'll close it. |
Goal
The goal is to make this compile to code with 0 allocations:
For this I would like these constructors to inline properly:
But currently only take iterator is inlined, fib is not.
Why it currently doesn't work
The inline constructors filter receives code with functions already inlined, but logically, before the constructors are inlined the call to Take's from.next() couldn't have been inlined.
For the main function the inline constructors filter receives this:
The constructor inliner output is then:
Proposal on how to make this work
I propose implementing a new filter which "Tightens" the types of local variables and then inlines method calls from the tightened local variables.
With "Tightening" the types I mean changing the local variable types to be as specific as possible.
For example in the output of the constructor inliner above, the variable take_from would be "tightened" from
Iterator<Int>
toFib
.The type of expressions that depend on this variable would also get updated.
This filter would inline the take_from.next() call since it is now Fib.next() instead of Iteartor.next()
With this filter implemented the inline constructors filter can be altered so that if it inlined at least one constructor then the output goes through the tighten_and_inline filter, and then if the tighten_and_inline filter reports that it managed to inline a new function then the inline constructors filter is run again on that output. The loop ends when one of the filters failed to do any new inlining.
Possibly undesired side effects
Disable inlining tricks break
Casting to a less specific type as a trick to disable inlining would not work with this filter.
Performance
Each of the .method there cause one extra pass of the constructor inliner, 4 passes total...
Might get slow.
The good news is that if you have multiple for loops:
It's still only 4 passes.
Why this pitch
I'm interested in implementing this feature but Haxe compiler ocaml code doesn't roll of the tongue for me.
Perhaps the idea is bad and can be rejected early, would save me time.
If the idea seems good I'd humbly request a few tips on implementing the type "tightening" part of the filter, for which at the moment I have no clue of how to implement (I haven't spent much time trying to figure it out either, I want to see if this gets rejected first). Perhaps something like this already exists somewhere in the haxe compiler code.
The text was updated successfully, but these errors were encountered: