Skip to content

[Memory64] Made clock_t 64-bit compatible #15500

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
Nov 15, 2021

Conversation

aardappel
Copy link
Collaborator

No description provided.

@aardappel aardappel requested a review from kripken November 11, 2021 22:51
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

Adding a test would be good too.

src/library.js Outdated
@@ -453,7 +453,8 @@ LibraryManager.library = {
clock__sig: 'i',
clock: function() {
if (_clock.start === undefined) _clock.start = Date.now();
return ((Date.now() - _clock.start) * ({{{ cDefine('CLOCKS_PER_SEC') }}} / 1000))|0;
const t = ((Date.now() - _clock.start) * ({{{ cDefine('CLOCKS_PER_SEC') }}} / 1000))|0;
return {{{ to64('t') }}};
},
Copy link
Member

Choose a reason for hiding this comment

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

clock() returns clock_t I think. Does musl define that differently in 64-bit? That surprises me a little as usually that stuff is fixed (say ino_t is always 64-bit).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All I know is it ended up as an i64 in the module.
Seems we have typedef long clock_t;

Copy link
Member

Choose a reason for hiding this comment

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

Is that a typedef in musl code, or in emscripten code?

I ask because I wouldn't suggest changing musl code, but if it's in an emscripten header then maybe the simple change is to just make it always 32-bit.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks like its in musl:

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @sbc100 !

Reading C docs, I don't see a requirement for the type of clock_t. So I think we are free to change that. In practice we call Date.now() which returns a result in ms. So 32 bits are likely enough for such a value. That suggests we can use int32_t for wasm64 as well.

I think that would be best. Otherwise we'd have an odd difference between wasm32 and wasm64 builds, potentially a confusing and difficult to debug one. We have not gotten any bug reports about the precision of clock_t to my knowledge, so I think it should be good enough for wasm64 too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We already have use of long all over the place, but ok, I'll try changing the type instead.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not opposed to long in principle - where we want wasm32 and wasm64 to diverge it sgtm - I'm just saying I think it isn't appropriate here specifically since we don't have a good reason to diverge the two targets. Do you disagree with that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, it's all the same to me. I already changed the type and reverted the JS change.

@kripken
Copy link
Member

kripken commented Nov 11, 2021

Another approach to solving this might be to use clock.c from musl. Looks like that calls __clock_gettime which we have defined already, so it might just work. If this works, it would be nice as it would move more code into C which is consistent with #15151

@aardappel
Copy link
Collaborator Author

Presumably this function is already under test in wasm32, it just doesn't show up because we don't have wasm64 tests enabled/finished.

Fully agree we should move as much code to Wasm as possible, though probably separate from this PR.

@aardappel aardappel force-pushed the clock branch 2 times, most recently from 630725e to c2a73fc Compare November 15, 2021 21:08
Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

lgtm % comments.

Maybe change the title to of the PR to reflect the change to clock_t rather than clock()

@aardappel aardappel changed the title [Memory64] Made clock() 64-bit compatible [Memory64] Made clock_t 64-bit compatible Nov 15, 2021
@aardappel aardappel merged commit e5c241c into emscripten-core:main Nov 15, 2021
mmarczell-graphisoft pushed a commit to GRAPHISOFT/emscripten that referenced this pull request Jan 5, 2022
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