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

AOT support #228

Open
stevefan1999-personal opened this issue Jan 12, 2024 · 9 comments
Open

AOT support #228

stevefan1999-personal opened this issue Jan 12, 2024 · 9 comments

Comments

@stevefan1999-personal
Copy link

I wonder if we can use the emitted code statically, so we can save the JIT code into an object file for caching (for example). This means the codegen should not emit any position-dependent code (I think it should be PIC by default) and do not reference any runtime-dependent address as well. Is it possible for SLJIT to do this?

(Out of context but as a query) Far as I can see the stackless in SLJIT means there aren't any push/pop operation directly on the stack pointer, and you have to maintain an external stack created on a heap right? How does that translate in terms of assembly?

@zherczeg
Copy link
Owner

All code is PIC except branches / calls.

  • Calls to other pic code must use indirect calls. This is slower than normal calls.
  • The code must not be too big. This is especially complicated on MIPS. Branches only have 16 bit offset, so +-128K is the maximum range. There is a J instruction, but the target address must be in the same 256MB region, which is not exactly PIC friendly. For larger branches, the absolute address is encoded in the instruction stream.

You can allocate a stack area for local data for every function. You need to manage your variables manually there (hence the sljit name comes). See the last argument of sljit_emit_enter. The SLJIT_MEM1(SLJIT_SP) is the base address of the local data.

@nacho00112
Copy link

nacho00112 commented Feb 2, 2024

So I actually can use the stack (without heap) but have to manage its usage manually?
I've confused with this theme.

@zherczeg
Copy link
Owner

zherczeg commented Feb 2, 2024

Yes. SLJIT_MEM1(SLJIT_SP), offset is the way to read/write the stack. The stack is just a linear memory area.

@nacho00112
Copy link

This guy seems to have made a stackful version of the project
https://github.com/GregorR/sfjit
what do you think about it?

@zherczeg
Copy link
Owner

zherczeg commented Feb 4, 2024

It seems the author added alloca support, but still no local management. Btw building a higher level generic compiler based on sljit is possible. It can manage locals. E.g. the WebAssembly compilers based on sljit do this.
https://github.com/partic2/pwart
https://github.com/Samsung/walrus

@zherczeg
Copy link
Owner

zherczeg commented Feb 4, 2024

Back to the topic of AOT, I have an idea. A serialize/de-serialize interface could save/restore the internal representation of the compiler. The loaded code could be compiled with generate code. This is not as fast as loading PIC, but the compiler can optimize branches, and still much faster than a full compilation.

@zherczeg
Copy link
Owner

zherczeg commented Feb 6, 2024

I have added serialization to the code:
https://github.com/zherczeg/sljit/blob/master/sljit_src/sljitLir.h#L2288

Since this is a new feature, there might be bugs in the code. With this interface the majority of the compilation can be skipped without giving up performance.

@stevefan1999-personal
Copy link
Author

I have added serialization to the code: https://github.com/zherczeg/sljit/blob/master/sljit_src/sljitLir.h#L2288

Since this is a new feature, there might be bugs in the code. With this interface the majority of the compilation can be skipped without giving up performance.

Is that also backward/forward compatible? I might consider saving the AOT results and cache to local filesystem (similar to GAC)

@zherczeg
Copy link
Owner

It has a version number, so you can detect if your cache is not valid anymore, and can recompile the code:
https://github.com/zherczeg/sljit/blob/master/sljit_src/sljitSerialize.c#L46

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