emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: HAVE_FAST_UNALIGNED_ACCESS


From: Sam James
Subject: Re: HAVE_FAST_UNALIGNED_ACCESS
Date: Thu, 30 Mar 2023 12:09:41 +0100
User-agent: mu4e 1.8.14; emacs 29.0.60

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Robert Pluim <rpluim@gmail.com>
>> Cc: Po Lu <luangruo@yahoo.com>
>> Date: Thu, 30 Mar 2023 11:34:42 +0200
>> 
>> Fstring_lessp has:
>> 
>> /* Check whether the platform allows access to unaligned addresses for
>>    size_t integers without trapping or undue penalty (a few cycles is OK).
>> 
>>    This whitelist is incomplete but since it is only used to improve
>>    performance, omitting cases is safe.  */
>> #if defined __x86_64__|| defined __amd64__   \
>>     || defined __i386__ || defined __i386    \
>>     || defined __arm64__ || defined __aarch64__      \
>>     || defined __powerpc__ || defined __powerpc      \
>>     || defined __ppc__ || defined __ppc              \
>>     || defined __s390__ || defined __s390x__
>> #define HAVE_FAST_UNALIGNED_ACCESS 1
>> #else
>> #define HAVE_FAST_UNALIGNED_ACCESS 0
>> #endif
>> 
>> but even if unaligned access is normally permitted by a machine, it is
>> still undefined behavior to dereference an unaligned pointer.
>
> This is incorrect.  There's nothing undefined about x86 unaligned
> accesses.  C standards can regard this as UB, but we are using
> machine-specific knowledge here (and Emacs cannot be built with a
> strict adherence to C standards anyway).

Things can still go wrong on x86, particularly with SIMD:
https://pzemtsov.github.io/2016/11/06/bug-story-alignment-on-x86.html.

>
>> Instead, HAVE_FAST_UNALIGNED_ACCESS and UNALIGNED_LOAD_SIZE should be
>> removed and memcpy used instead:
>> 
>>   word_t a, c;
>> 
>>   memcpy (&a, w1 + b / ws, sizeof a);
>>   memcpy (&c, w2 + b / ws, sizeof c);
>> 
>> doing so will make the compiler itself generate the right sequence of
>> instructions for performing unaligned accesses, normally with only a few
>> cycles penalty.
>
> We don't want that penalty here, that's all.
>
>> I would like to install such a change on emacs-29.
>
> No, please don't.
>
>> Emacs currently crashes when built with various compilers performing
>> pointer alignment checks.
>
> Details, please.  Which compilers, on what platforms, for what target
> architectures, etc.  Unconditionally removing the fast copy there is a
> non-starter.

I imagine they're referring to UBSAN. Emacs may want to add an
annotation where HAVE_FAST_UNALIGNED_ACCESS is used.

But for modern compilers, they will indeed DTRT anyway when they
see memcpy.

See https://github.com/WayneD/rsync/pull/428 for an example.

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]