Skip to content

Introduce IR BinaryOps for unsigned division and remainder. #5171

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

Merged
merged 1 commit into from
May 22, 2025

Conversation

sjrd
Copy link
Member

@sjrd sjrd commented May 16, 2025

This allows to better mutualize their implementation with the signed divisions.

Moreover, our 3 implementation strategies (JS with RuntimeLong, JS with bigint and Wasm) have different efficient implementations of those operations. Using IR BinaryOps for them allows each backend to use the most appropriate implementation, while letting the optimizer generically manipulate their mathematical properties.

@sjrd sjrd requested a review from gzm0 May 16, 2025 13:17
@sjrd sjrd force-pushed the ir-unsigned-div-mod branch from d1c4295 to 9410652 Compare May 19, 2025 21:28
Copy link
Contributor

@gzm0 gzm0 left a comment

Choose a reason for hiding this comment

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

Just one question :)

expectedFastLinkSize = 147046,
expectedFullLinkSizeWithoutClosure = 85355,
expectedFullLinkSizeWithClosure = 21492,
expectedFastLinkSize = 148624,
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to check my understanding: This is because of (signed) integer division now being inlined more strongly? (IMO OK).

Copy link
Member Author

@sjrd sjrd May 21, 2025

Choose a reason for hiding this comment

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

Not really. The bulk of it is the unsigned div/mod methods of RuntimeLong, which are now unconditionally retained:
https://gist.github.com/sjrd/7ed63b130ce471a24480869f8f784dc6
That is a fixed cost.

GCC manages to get rid of them, but other JS optimizers would not be able to do that. Ideally, one day we should only conditionally reach the methods of RuntimeLong depending on which Long_x operators are used, but it's probably not really worth the trouble.

In a larger codebase, this is compensated by the fact that the unsigned divisions produce much shorter (and better) code now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. OK, that's acceptable. Maybe an alternative could be to emit runtime long methods as static methods, so it would be easier for JS optimizers to get rid of them. (it's not entirely clear to me how easy that would be in practice :-/).

Copy link
Member Author

Choose a reason for hiding this comment

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

It would be nice for them to be static, indeed. At the top-level we can do this by reaching the static forwarders, and forcing the module instance methods to be inlined into those. However, for divisions in particular, they are not entirely inlined. They use several non-inlined helpers. So those need to perform the LoadModule anyway, and that makes the static forwarders not so great for them.

Then again, if we actually have to call the division methods, we are in a bad place anyway, performance-wise. I'll follow up with attempts at doing that.

In the meantime, I'll wait to merge #5158 first, since they will conflict on the size checks, and I'd rather rebase this PR against #5158 than the other way around.

This allows to better mutualize their implementation with the
signed divisions.

Moreover, our 3 implementation strategies (JS with `RuntimeLong`,
JS with `bigint` and Wasm) have different efficient implementations
of those operations. Using IR BinaryOps for them allows each backend
to use the most appropriate implementation, while letting the
optimizer generically manipulate their mathematical properties.
@sjrd sjrd force-pushed the ir-unsigned-div-mod branch from 9410652 to 6d04757 Compare May 21, 2025 20:14
@sjrd sjrd merged commit 4c11983 into scala-js:main May 22, 2025
3 checks passed
@sjrd sjrd deleted the ir-unsigned-div-mod branch May 22, 2025 04:58
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.

2 participants