Skip to content

Implement Tail Call Optimize into ReturnCall Instruction#400

Open
makachanm wants to merge 10 commits intoSamsung:mainfrom
makachanm:impl_tco
Open

Implement Tail Call Optimize into ReturnCall Instruction#400
makachanm wants to merge 10 commits intoSamsung:mainfrom
makachanm:impl_tco

Conversation

@makachanm
Copy link
Copy Markdown

I implemented a tail call optimize into WebAssembly 3's ReturnCall recursive instruction.
It currently unfinished, (the issue with type checking in the standard's spec, indirect call is not completed..) but it passes Return Call Test code.

@clover2123
Copy link
Copy Markdown
Collaborator

clover2123 commented Apr 8, 2026

Please run tools/check_tidy.py --update to format the code according to the style guidelines.

@makachanm
Copy link
Copy Markdown
Author

I applied. thanks.

Copy link
Copy Markdown
Collaborator

@zherczeg zherczeg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a good idea. I would simplify the parameter storing: the parameters are simply stored in a linear buffer, and copied by a memcpy.

Comment thread src/interpreter/Interpreter.h Outdated
Comment thread src/runtime/ExecutionState.h Outdated
Comment thread src/runtime/ExecutionState.h Outdated
Comment thread src/interpreter/ByteCode.h
@zherczeg
Copy link
Copy Markdown
Collaborator

zherczeg commented Apr 9, 2026

Parameters are stored in a linear area:
https://github.com/Samsung/walrus/blob/main/src/interpreter/Interpreter.h#L57

Then the tco buffer could be copied here without changes.

@makachanm
Copy link
Copy Markdown
Author

I'll rework parameter recover code with memcpy and Walrus's own vector implementation. Thanks for the feedback.

@makachanm
Copy link
Copy Markdown
Author

I think lack of JIT version of TCO implement causes CI failure. I'll seperate it for future works.

Comment thread src/runtime/ExecutionState.h Outdated
@makachanm
Copy link
Copy Markdown
Author

makachanm commented Apr 13, 2026

I've verified that all tests in the walrus suite are passing in the x64 local machine at interpret mode.

@clover2123
Copy link
Copy Markdown
Collaborator

There is a conflict in ByteCode.h file.
Please rebase it and push this pr again

@makachanm makachanm force-pushed the impl_tco branch 3 times, most recently from 67bc7f8 to daa44c1 Compare April 13, 2026 09:30
@makachanm makachanm marked this pull request as draft April 13, 2026 09:30
@makachanm makachanm marked this pull request as ready for review April 13, 2026 10:13
@makachanm
Copy link
Copy Markdown
Author

makachanm commented Apr 14, 2026

CI is failing when Clang compiler is used. I had no problem with GCC.
I'll fix it with reordering some parameter initialize order.

Comment thread src/interpreter/Interpreter.cpp Outdated
Comment thread src/interpreter/Interpreter.cpp Outdated
Comment thread src/interpreter/Interpreter.cpp Outdated
Comment thread src/interpreter/Interpreter.h Outdated
Comment thread src/interpreter/Interpreter.h Outdated
Comment thread src/interpreter/Interpreter.h Outdated
@clover2123
Copy link
Copy Markdown
Collaborator

Lastly, have you been able to test this patch for recursive tail calls?

@makachanm
Copy link
Copy Markdown
Author

Lastly, have you been able to test this patch for recursive tail calls?

I'll do it through recursive fibonacci test. Thanks for the mention.

@makachanm
Copy link
Copy Markdown
Author

I found fibonacci test makes call stack exhaustion. I'm analyzing what's problem with.

@makachanm
Copy link
Copy Markdown
Author

makachanm commented Apr 21, 2026

Yup it passes 10000-depth fibonacci test without problems.
I removed some edge-case code so it may cause problem in JIT, but it's seems not a huge problem.

I'll add new edge-case logic into it.

Comment thread src/interpreter/Interpreter.h
Comment thread src/parser/WASMParser.cpp Outdated
Comment thread src/interpreter/Interpreter.h
@clover2123
Copy link
Copy Markdown
Collaborator

One more, this patch doesn't consider exceptions.
What if exception occurs during the execution of tail call?

@makachanm
Copy link
Copy Markdown
Author

I found some inconsistency in parameter space. Copy to callee can be not a problem, but Copy from caller is making some corruption into buffer value.

I changed it does manually copy data one by one.

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

Successfully merging this pull request may close these issues.

3 participants