This is an archive of the discontinued LLVM Phabricator instance.

[ADT] llvm::bit_cast - use __builtin_bit_cast if available
ClosedPublic

Authored by RKSimon on Jan 22 2023, 7:52 AM.

Details

Summary

If the compiler supports __builtin_bit_cast we should try to use it instead of std::memcpy (and avoid including the cstring header).

Diff Detail

Event Timeline

RKSimon created this revision.Jan 22 2023, 7:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 22 2023, 7:52 AM
RKSimon requested review of this revision.Jan 22 2023, 7:52 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 22 2023, 7:52 AM
This revision is now accepted and ready to land.Jan 22 2023, 9:30 AM
kazu accepted this revision.Jan 22 2023, 9:32 AM
This revision was landed with ongoing or failed builds.Jan 22 2023, 10:21 AM
This revision was automatically updated to reflect the committed changes.

If the compiler supports __builtin_bit_cast we should try to use it instead of std::memcpy (and avoid including the cstring header).

Why? Maintaining non-portable code has some cost, if we can get similar/sufficient/the same performance without special casing, that seems better. Do we not get adequate performance with std::memcpy? Is the header especially expensive to include?

If the compiler supports __builtin_bit_cast we should try to use it instead of std::memcpy (and avoid including the cstring header).

Why? Maintaining non-portable code has some cost, if we can get similar/sufficient/the same performance without special casing, that seems better. Do we not get adequate performance with std::memcpy? Is the header especially expensive to include?

No strong preference tbh

barannikov88 added a comment.EditedJan 24 2023, 9:23 AM

Why? Maintaining non-portable code has some cost, if we can get similar/sufficient/the same performance without special casing, that seems better. Do we not get adequate performance with std::memcpy? Is the header especially expensive to include?

Half a point is that __builtin_bit_cast can be used in constant expressions. "Half", because llvm::bit_cast is not constexpr :)

If the compiler supports __builtin_bit_cast we should try to use it instead of std::memcpy (and avoid including the cstring header).

Why? Maintaining non-portable code has some cost, if we can get similar/sufficient/the same performance without special casing, that seems better. Do we not get adequate performance with std::memcpy? Is the header especially expensive to include?

No strong preference tbh

Did something motivate proposing this change?

If the compiler supports __builtin_bit_cast we should try to use it instead of std::memcpy (and avoid including the cstring header).

Why? Maintaining non-portable code has some cost, if we can get similar/sufficient/the same performance without special casing, that seems better. Do we not get adequate performance with std::memcpy? Is the header especially expensive to include?

No strong preference tbh

Did something motivate proposing this change?

This was part of a general attempt to get our bit.h as close to c++ <bit> as possible - especially when I realised that all latest supported compilers already implement '__builtin_bit_cast' : https://godbolt.org/z/YWM4GcceE

If the compiler supports __builtin_bit_cast we should try to use it instead of std::memcpy (and avoid including the cstring header).

Why? Maintaining non-portable code has some cost, if we can get similar/sufficient/the same performance without special casing, that seems better. Do we not get adequate performance with std::memcpy? Is the header especially expensive to include?

No strong preference tbh

Did something motivate proposing this change?

This was part of a general attempt to get our bit.h as close to c++ <bit> as possible - especially when I realised that all latest supported compilers already implement '__builtin_bit_cast' : https://godbolt.org/z/YWM4GcceE

I'd say unless there's some significant gain - maintaining #ifdef'd non-portable(to all supported compilers) codepaths isn't worth the maintenance burden. But I don't feel strongly enough to insist on a revert - up to you.

mib added a subscriber: mib.Feb 21 2023, 11:25 AM

Hey @RKSimon, it seems this patch is cause lldb's macOS ASAN bot to fail. Could you take a look ?

/Users/buildslave/jenkins/workspace/lldb-cmake-sanitized/llvm-project/llvm/include/llvm/ADT/bit.h:55:3: error: non-void function 'bit_cast' should return a value [-Wreturn-type]
  return __builtin_bit_cast(To, from);
  ^

https://green.lab.llvm.org/green/view/LLDB/job/lldb-cmake-sanitized/3475/console