Skip to content

Stage 2 llvm backend, improve code gen for return values #9581

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

Open
lerno opened this issue Aug 18, 2021 · 3 comments
Open

Stage 2 llvm backend, improve code gen for return values #9581

lerno opened this issue Aug 18, 2021 · 3 comments
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. optimization
Milestone

Comments

@lerno
Copy link

lerno commented Aug 18, 2021

Currently the ABI for passing optional and error parameters prevent full optimization by LLVM.

  1. Sub optimal code generation with full optimization when using catch |...|
    https://godbolt.org/z/xqTKWKW37

  2. Optimal code generation with full optimization when performing essentially the same but using catch 1
    https://godbolt.org/z/PWe4GPb3c

For Optional, both variants produce sub optimal code:
https://godbolt.org/z/qGavK9aWK

Note that Swift has a similar strategy (passing a struct), but manages to get better optimization:
https://godbolt.org/z/4xa5Y85Gx

@lerno
Copy link
Author

lerno commented Aug 18, 2021

It is possible to follow the Swift implementation to get good optimization.

Another method would be to return the error as the normal return value and have the normal return out to be passed by ref. E.g. fn square(n:i32) !i32 is lowered to fn square(...) i16

@Vexu Vexu added backend-llvm The LLVM backend outputs an LLVM IR Module. optimization enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Aug 18, 2021
@Vexu Vexu added this to the 0.10.0 milestone Aug 18, 2021
@lerno
Copy link
Author

lerno commented Aug 18, 2021

A related, possible enhancement which will allow dynamic linking is the following way to create an error code:

For every error code:

  1. Create a unique global
  2. Use the (constant) address to this global as the value of the error code.

The (dynamic) C linker will ensure that all libraries use the same address, which ensures uniqueness. This is also understood by C code. This would make the error code pointer sized, but if the error is passed as a return value, then this would usually be exactly register sized and consequently equally cheap as using a i16.

@andrewrk
Copy link
Member

Related: #561

@andrewrk andrewrk modified the milestones: 0.10.0, 1.0.0 Aug 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. enhancement Solving this issue will likely involve adding new logic or components to the codebase. frontend Tokenization, parsing, AstGen, Sema, and Liveness. optimization
Projects
None yet
Development

No branches or pull requests

3 participants