This is an archive of the discontinued LLVM Phabricator instance.

[lldb] fix build issue on MSVC because of missing byte-swap builtins
ClosedPublic

Authored by ashay-github on Apr 17 2023, 9:38 AM.

Details

Summary

The __builtin_bswap{32,64}() builtins (introduced in commit e07a421d)
are missing from MSVC, which causes build errors when compiling LLDB on
Windows (tested with MSVC 19.34.31943.0). This patch replaces the
builtins with either MSVC's _byteswap_u{long,64}() or the original
builtins, or the bswap_{32,64}() functions from byteswap.h, depending
on which ones are available.

Diff Detail

Event Timeline

ashay-github created this revision.Apr 17 2023, 9:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 17 2023, 9:38 AM
ashay-github requested review of this revision.Apr 17 2023, 9:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 17 2023, 9:38 AM
bulbazord requested changes to this revision.Apr 17 2023, 11:14 AM

Instead of not using __builtin_bswap{32,64} entirely, we should check if they're available.

You can use the clang feature-checking macro __has_builtin (and if it doesn't exist you can define it). So you could amend this patch like so:

#if !defined(__has_builtin)
#define __has_builtin(x) 0
#endif

#if __has_builtin(__builtin_bswap32)
#define bswap_32(x) __builtin_bswap32(x)
#elif _MSC_VER
#define bswap_32(x) _byteswap_ulong(x)
#else
#include <byteswap.h>
#endif
This revision now requires changes to proceed.Apr 17 2023, 11:14 AM

Updated to check for builtins and use them, if available.

ashay-github edited the summary of this revision. (Show Details)Apr 17 2023, 11:57 AM
This revision is now accepted and ready to land.Apr 17 2023, 12:36 PM

Thanks for the fix!

Made me realise I should have looked harder for an llvm utility in the first place, and it turns out llvm::byteswap exists. So I've changed this to use that in https://github.com/llvm/llvm-project/commit/9984cfc86ed6d5c1558d8dd82d82448e50d704ad.

By the way, did this come up on a bot? Just wondering if I ignored an email, apologies if so.

The llvm::byteswap() fix is much nicer. Thanks for the change!

No, this build error didn't show up on a bot (at least as far as I can tell).