This is an archive of the discontinued LLVM Phabricator instance.

[clang][emscripten] Reduce alignof long double from 16 to 8 bytes
ClosedPublic

Authored by sbc100 on Jun 23 2021, 12:10 PM.

Details

Summary

This means max_align_t is 8 bytes which also sets the alignment
malloc. Since this is technically and ABI breaking change we have
limited to just the emscripten OS target. It is also relatively low
import breakage since it will only effect the alignment of struct that
contai long doubles (extremely rare I imagine).

Emscripten's malloc implementation already use 8 byte alignment
(dlmalloc uses and alignment of 2*sizeof(void*) == 8 rather than
checking max_align_t) so will not be effected by this change. By
bringing the ABI in line with the current malloc code this will fix
several issue we have seen in the wild.

See: https://github.com/emscripten-core/emscripten/pull/14456

Diff Detail

Event Timeline

sbc100 created this revision.Jun 23 2021, 12:10 PM
sbc100 requested review of this revision.Jun 23 2021, 12:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 23 2021, 12:10 PM
sbc100 added a reviewer: sunfish.

@sunfish what do you think about doing this for all WebAssembly targets?

sbc100 edited the summary of this revision. (Show Details)Jun 23 2021, 12:12 PM
rsmith added a subscriber: rsmith.Jun 23 2021, 12:39 PM
rsmith added inline comments.
clang/lib/Basic/Targets/OSTargets.h
954–956

Do we still intend to unify Emscripten's ABI with wasm32-unknown-unknown or wasm32-wasi eventually? This is talking a step away from that.

One of the assumptions behind this is that it would be ok for malloc to be 16-byte aligned anyway, because SIMD use cases benefit from being able to call malloc and get a buffer aligned for SIMD. Do we have more information on how much this matters in practice?

Another observation is that part of the reason for long double being 128-bit is to leave room for the possibility of 128-bit floats being added to wasm some day, and on x86-64, aarch64, and powerpc which all have 128-bit long double on Linux, the alignment is 16 bytes.

Do we still intend to unify Emscripten's ABI with wasm32-unknown-unknown or wasm32-wasi eventually? This is talking a step away from that.

I definitely think we should unify them as much as possible. Ideally we would all make this change.

The motivation for the change is that right now we are losing either some correctness or some performance. Either is a burden, and a possible future float128 in wasm doesn't seem strong enough of a justification to me - should wasm add float128, we can consider a new ABI at that point. Does that sound reasonable?

One of the assumptions behind this is that it would be ok for malloc to be 16-byte aligned anyway, because SIMD use cases benefit from being able to call malloc and get a buffer aligned for SIMD. Do we have more information on how much this matters in practice?

I think the discussions converged on it being ok with the spec, but perhaps a problem in practice, but portable SIMD code seems smart enough to avoid the issue. I don't think I've seen a bug report mentioning SIMD, but I may have missed one.

Do we still intend to unify Emscripten's ABI with wasm32-unknown-unknown or wasm32-wasi eventually? This is talking a step away from that.

I definitely think we should unify them as much as possible. Ideally we would all make this change.

The motivation for the change is that right now we are losing either some correctness or some performance. Either is a burden, and a possible future float128 in wasm doesn't seem strong enough of a justification to me - should wasm add float128, we can consider a new ABI at that point. Does that sound reasonable?

One of the assumptions behind this is that it would be ok for malloc to be 16-byte aligned anyway, because SIMD use cases benefit from being able to call malloc and get a buffer aligned for SIMD. Do we have more information on how much this matters in practice?

I think the discussions converged on it being ok with the spec, but perhaps a problem in practice, but portable SIMD code seems smart enough to avoid the issue. I don't think I've seen a bug report mentioning SIMD, but I may have missed one.

Indeed, we have had reports of issues with the under-alignment of values returns from malloc, but I don't think any of them have been related to SIMD, only the expectation that malloc honors max_align_t.

kripken accepted this revision.Jul 2 2021, 10:46 AM

I believe we have all agreed that this is the right path forward, and so this can land?

Doing this change now in emscripten does not prevent us from changing the emscripten ABI again later, as the ABI is unstable (which is why we can make this change itself). We definitely should in the future consider unifying the emscripten triple with WASI, but for now, this change will be a strict improvement in emscripten.

This revision is now accepted and ready to land.Jul 2 2021, 10:46 AM
This revision was landed with ongoing or failed builds.Jul 2 2021, 11:06 AM
This revision was automatically updated to reflect the committed changes.