Skip to content

Commit

Permalink
Fixes for building with clang-cl on Windows (#104)
Browse files Browse the repository at this point in the history
* Avoid unused-variable warning on Windows

When compiling with clang-cl:

.../ir_emit.c(275,8): error: unused variable 'handle' [-Werror,-Wunused-variable]
  275 |         void *handle = NULL;
      |               ^~~~~~

* Avoid redefining __ORDER_LITTLE_ENDIAN__ if already defined

clang-cl defines this on Windows, resulting in an error.

.../ir.h(32,10): error: '__ORDER_LITTLE_ENDIAN__' macro redefined [-Werror,-Wmacro-redefined]
   32 | # define __ORDER_LITTLE_ENDIAN__ 1
      |          ^
<built-in>(39,9): note: previous definition is here
   39 | #define __ORDER_LITTLE_ENDIAN__ 1234

* In ir_ntzl, always use the 64 bit version on Windows

When compiling on Windows with clang-cl, __has_builtin(__builtin_ctzl)
will evaluate to true, but a long on Windows is only 32 bits, resulting
in an incorrect implementation. Move the _WIN64 block ahead of the
general block so that it gets the 64 bit version.

Fixes #101.
  • Loading branch information
sgraham authored Jan 21, 2025
1 parent 745006f commit 04022a5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ extern "C" {
# endif
/* Only supported is little endian for any arch on Windows,
so just fake the same for all. */
# define __ORDER_LITTLE_ENDIAN__ 1
# ifndef __ORDER_LITTLE_ENDIAN__
# define __ORDER_LITTLE_ENDIAN__ 1
# endif
# define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
# ifndef __has_builtin
# define __has_builtin(arg) (0)
Expand Down
2 changes: 1 addition & 1 deletion ir_emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ static bool ir_is_same_mem_var(const ir_ctx *ctx, ir_ref r1, int32_t offset)

void *ir_resolve_sym_name(const char *name)
{
void *handle = NULL;
void *addr;

#ifndef _WIN32
void *handle = NULL;
# ifdef RTLD_DEFAULT
handle = RTLD_DEFAULT;
# endif
Expand Down
9 changes: 6 additions & 3 deletions ir_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,10 @@ IR_ALWAYS_INLINE uint32_t ir_ntz(uint32_t num)
/* Number of trailing zero bits (0x01 -> 0; 0x40 -> 6; 0x00 -> LEN) */
IR_ALWAYS_INLINE uint32_t ir_ntzl(uint64_t num)
{
#if (defined(__GNUC__) || __has_builtin(__builtin_ctzl))
return __builtin_ctzl(num);
#elif defined(_WIN64)
// Note that the _WIN64 case should come before __has_builtin() below so that
// clang-cl on Windows will use the uint64_t version, not the "long" uint32_t
// version.
#if defined(_WIN64)
unsigned long index;

if (!_BitScanForward64(&index, num)) {
Expand All @@ -148,6 +149,8 @@ IR_ALWAYS_INLINE uint32_t ir_ntzl(uint64_t num)
}

return (uint32_t) index;
#elif (defined(__GNUC__) || __has_builtin(__builtin_ctzl))
return __builtin_ctzl(num);
#else
uint32_t n;

Expand Down

0 comments on commit 04022a5

Please sign in to comment.