This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt][atomics] Add `restrict` modifier to pointer arguments of `__atomic_exchange_c`
Needs ReviewPublic

Authored by lkail on Jun 15 2021, 7:50 PM.

Details

Summary

Add constraint that ptr, val and old should not alias each other.

Diff Detail

Event Timeline

lkail created this revision.Jun 15 2021, 7:50 PM
lkail requested review of this revision.Jun 15 2021, 7:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2021, 7:50 PM
Herald added a subscriber: Restricted Project. · View Herald Transcript
lkail abandoned this revision.Jun 29 2021, 11:46 PM

I don't believe a compiler can ever generate code that hits this code path. The public function is:

c
C atomic_exchange_explicit( volatile A* obj, C desired, memory_order order );

The compiler lowers this by allocating space for the return value (*old) and passing desired by reference. The right thing to do is probably document that the two parameters must not alias (I'd be happy with sticking restrict on all three pointer arguments, actually, and documenting that).

I really don't like the xor magic in the alternative path because it breaks any pointer provenance. If the target is any kind of CHERI CPU, it will generate invalid machine code. If we are in an LTO build and end up inlining this function then we're destroying any alias info that we might otherwise have. If we actually need that path, I'd much rather that we copied everything via a temporary and used intptr_t for any chunks that are sized and aligned like an intptr_t.

lkail updated this revision to Diff 355811.EditedJul 1 2021, 2:58 AM
lkail retitled this revision from [compiler-rt][atomics][RFC] Enable __atomic_exchange handling case when `val == old` to [compiler-rt][atomics] Add `restrict` modifier to pointer arguments.
lkail edited the summary of this revision. (Show Details)

Thanks @theraven 's detailed explanation. Updated per his suggestion.

lkail retitled this revision from [compiler-rt][atomics] Add `restrict` modifier to pointer arguments to [compiler-rt][atomics] Add `restrict` modifier to pointer arguments of `__atomic_exchange_c`.Jul 1 2021, 3:01 AM
ldionne resigned from this revision.Sep 22 2021, 8:26 AM