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

More function inlining #470

Open
therontarigo opened this issue Nov 6, 2024 · 4 comments
Open

More function inlining #470

therontarigo opened this issue Nov 6, 2024 · 4 comments

Comments

@therontarigo
Copy link
Contributor

therontarigo commented Nov 6, 2024

It should (in some circumstances) be possible to inline a function that follows these rules:

  • Is of void type, or ends in an unconditional return statement.
  • Contains no other return statements.
@eldritchconundrum
Copy link
Collaborator

Inlining is not that simple. There are many potential issues to tackle.

  • are the arguments lvalues, are they assigned in the inlined function, should they be copied to new variable declarations?
  • variable shadowing (parameters, locals, and caller's)
  • in/out/inout parameters
  • evaluation order of expressions with side-effects, e.g. when passing arguments
  • is the function called in the middle of an expression?
  • does it save code size to inline functions called multiple times?

@laurentlb
Copy link
Owner

A difficulty is to handle variables correctly, even with shadowing (renaming might be required).

If the function return value is used, it can be difficult to inline it (and do side-effects in the correct order).

int f() {
  for (...) { ... }
  return ...;
} 

void main() {
  int a = g() + f();
}

To inline f() here, you might need to introduce new local variables and split the line.

@therontarigo
Copy link
Contributor Author

I reopened #469 , as it is the only one with a chance of being easy to implement.

@therontarigo
Copy link
Contributor Author

I was thinking that it is an unnecessary restriction in the case that the function is called in a simple assignment statement:
float x = func(...);
However, considering more carefully what the resulting minified code would be, there's no case for that where return-by-inout wouldn't work just as well. So that's one more reason to abandon this and focus on #469 .

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

No branches or pull requests

3 participants