This is an archive of the discontinued LLVM Phabricator instance.

[clang][WebAssembly] Fix __BIGGEST_ALIGNMENT__ under emscripten
AbandonedPublic

Authored by sbc100 on May 31 2023, 10:50 AM.

Details

Reviewers
dschuff
Summary

Follow up to https://reviews.llvm.org/D104808 and
https://reviews.llvm.org/D105749. The only place that seems to be used
is when defining __BIGGEST_ALIGNMENT__ and when choosing the alignment
for the alloca builtin. It seems that both of these should match the
alignof(max_align_t) which, for emscripten, is currently 8 bytes.

Diff Detail

Event Timeline

sbc100 created this revision.May 31 2023, 10:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 31 2023, 10:50 AM
sbc100 requested review of this revision.May 31 2023, 10:50 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 31 2023, 10:50 AM
sbc100 edited the summary of this revision. (Show Details)

I guess this is basically the C version of max_align_t so it should match.
but... this still has the potential to break things.
e.g. it will change the allocation in https://github.com/google/XNNPACK/blob/master/src/xnnpack/allocator.h#L66
ISTR that was one of the projects that had an issue with this the first time around?

I guess this is basically the C version of max_align_t so it should match.
but... this still has the potential to break things.

True, but I think it's not as likely the break things as that change the max_align_t.. which is more commonly used.

e.g. it will change the allocation in https://github.com/google/XNNPACK/blob/master/src/xnnpack/allocator.h#L66

I don't think it will change anything in that code since __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT will still hold true both before and after this change (XNN_ALLOCATION_ALIGNMENT == 4 on wasm)

ISTR that was one of the projects that had an issue with this the first time around?

I guess this is basically the C version of max_align_t so it should match.

Yes, it should match. Having __BIGGEST_ALIGNMENT__ be 16 for emscripten doesn't make any sense right now.

but... this still has the potential to break things.
e.g. it will change the allocation in https://github.com/google/XNNPACK/blob/master/src/xnnpack/allocator.h#L66
ISTR that was one of the projects that had an issue with this the first time around?

I don't think it will change anything in that code since __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT will still hold true both before and after this change (XNN_ALLOCATION_ALIGNMENT == 4 on wasm)

Right, that check causes XNN_ALLOCATION_ALIGNMENT to be ignored in favor of using clang's _builtin_alloca() which will be changed by this CL.
I seem to recall that @tlively and I spent a bunch of time with XNNpack chasing down some kind of subtle error that I suspect had to do with alignment, but maybe he remembers that better than I do.

I don't think it will change anything in that code since __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT will still hold true both before and after this change (XNN_ALLOCATION_ALIGNMENT == 4 on wasm)

Right, that check causes XNN_ALLOCATION_ALIGNMENT to be ignored in favor of using clang's _builtin_alloca() which will be changed by this CL.

I don't think it will since __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT will remain true after this change.. so this change should have no effect on that code.

I don't think it will since __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT will remain true after this change.. so this change should have no effect on that code.

I meant that when __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT (which was true before and will remain true), then XNNPack uses __builtin_alloca() as the implementation of XNN_SIMD_ALLOCA (which presumably is for allocating SIMD values). This change will reduce the alignment used by __builtin_alloca() from 16 to 8, such that (I think) it is no longer suitable for SIMD values.

Maybe this is a bug in XNNPack (they should maybe be using XNN_ALLOCATION_ALIGNMENT with a value suitable for SIMD?) but given that BIGGEST_ALIGNMENT and alloca seem to be intended for any base type (including SIMD) it wouldn't be surprising if someone else were depending on this too.

which... maybe this is just re-litigating the previous discussion, I don't know. I wonder at what point our ABI should be treating SIMD values as "normal" rather than rare.

I don't think it will since __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT will remain true after this change.. so this change should have no effect on that code.

I meant that when __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT (which was true before and will remain true), then XNNPack uses __builtin_alloca() as the implementation of XNN_SIMD_ALLOCA (which presumably is for allocating SIMD values). This change will reduce the alignment used by __builtin_alloca() from 16 to 8, such that (I think) it is no longer suitable for SIMD values.

Maybe this is a bug in XNNPack (they should maybe be using XNN_ALLOCATION_ALIGNMENT with a value suitable for SIMD?) but given that BIGGEST_ALIGNMENT and alloca seem to be intended for any base type (including SIMD) it wouldn't be surprising if someone else were depending on this too.

XNN_ALLOCATION_ALIGNMENT is 8 under webassembly, which apparently the alignment than xnnpack wants for webassemebly. Using alloca for this is find both before and after this change since both 8 and 18 as fit this requirement.

which... maybe this is just re-litigating the previous discussion, I don't know. I wonder at what point our ABI should be treating SIMD values as "normal" rather than rare.

sbc100 added a subscriber: ngzhian.May 31 2023, 3:02 PM

I don't think it will since __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT will remain true after this change.. so this change should have no effect on that code.

I meant that when __BIGGEST_ALIGNMENT__ >= XNN_ALLOCATION_ALIGNMENT (which was true before and will remain true), then XNNPack uses __builtin_alloca() as the implementation of XNN_SIMD_ALLOCA (which presumably is for allocating SIMD values). This change will reduce the alignment used by __builtin_alloca() from 16 to 8, such that (I think) it is no longer suitable for SIMD values.

Maybe this is a bug in XNNPack (they should maybe be using XNN_ALLOCATION_ALIGNMENT with a value suitable for SIMD?) but given that BIGGEST_ALIGNMENT and alloca seem to be intended for any base type (including SIMD) it wouldn't be surprising if someone else were depending on this too.

XNN_ALLOCATION_ALIGNMENT is 8 under webassembly, which apparently the alignment than xnnpack wants for webassemebly. Using alloca for this is find both before and after this change since both 8 and 18 as fit this requirement.

which... maybe this is just re-litigating the previous discussion, I don't know. I wonder at what point our ABI should be treating SIMD values as "normal" rather than rare.

If xnnpack wanted more than 8 byte alignment it should surely set XNN_ALLOCATION_ALIGNMENT to greater than 8?

Adding @tlively and @ngzhian in case that have some more background on this..

I seem to recall that @tlively and I spent a bunch of time with XNNpack chasing down some kind of subtle error that I suspect had to do with alignment, but maybe he remembers that better than I do.

Sorry, I have no recollection of this at all 😬

sbc100 added a comment.Jun 6 2023, 1:44 PM

I seem to recall that @tlively and I spent a bunch of time with XNNpack chasing down some kind of subtle error that I suspect had to do with alignment, but maybe he remembers that better than I do.

Sorry, I have no recollection of this at all 😬

As far as I can tell the only way this change could break XNNpack if XNN_ALLOCATION_ALIGNMENT = 8 is wrongly set there... as long as that is the correct value for XNN_ALLOCATION_ALIGNMENT I don't see how this change could break it. If XNN_ALLOCATION_ALIGNMENT is set wrongly this change might expose that bug.. but it seems correct to me.

Can we land this?

As far as I can tell the only way this change could break XNNpack if XNN_ALLOCATION_ALIGNMENT = 8 is wrongly set there... as long as that is the correct value for XNN_ALLOCATION_ALIGNMENT I don't see how this change could break it. If XNN_ALLOCATION_ALIGNMENT is set wrongly this change might expose that bug.. but it seems correct to me.

yeah, that's actually what my concern is. IIUC as written the code is asking for 8, but it's being masked by our value of BIGGEST_ALIGNMENT.

I suppose we should land this since I think we do want to have it match max_align_t. But it does make me wonder (again) whether our choice of ABI is correct here.
Can you also put something in the emscripten release notes about this?

As far as I can tell the only way this change could break XNNpack if XNN_ALLOCATION_ALIGNMENT = 8 is wrongly set there... as long as that is the correct value for XNN_ALLOCATION_ALIGNMENT I don't see how this change could break it. If XNN_ALLOCATION_ALIGNMENT is set wrongly this change might expose that bug.. but it seems correct to me.

yeah, that's actually what my concern is. IIUC as written the code is asking for 8, but it's being masked by our value of BIGGEST_ALIGNMENT.

I suppose we should land this since I think we do want to have it match max_align_t. But it does make me wonder (again) whether our choice of ABI is correct here.
Can you also put something in the emscripten release notes about this?

Presumably this change of change makes most sense in the emscripten ChangeLog right? We don't tend to document emscripten-specific changes in the llvm release notes do we?

Presumably this change of change makes most sense in the emscripten ChangeLog right? We don't tend to document emscripten-specific changes in the llvm release notes do we?

Yes, I think so. Also I think it's more likely to be seen by users in the emscripten changelog

OK to land this? (With a provision that I will add something to the emscripten changelog?)

After local discussion, I guess we decided to leave this as-is?

sbc100 abandoned this revision.Jun 27 2023, 1:50 PM

Yes, on further investigation is seems that there is precedent for having alignof(max_align_t) and __BIGGEST_ALIGNMENT__ be different. See https://github.com/emscripten-core/emscripten/pull/19728