Skip to content

Commit

Permalink
Use clang pragmas to enable features rather than depend on compiler s…
Browse files Browse the repository at this point in the history
…ettings
  • Loading branch information
jedisct1 committed Apr 27, 2024
1 parent 8dc537d commit ee40b36
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 228 deletions.
174 changes: 3 additions & 171 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -208,129 +208,7 @@ pub fn build(b: *std.Build) !void {

initLibConfig(target, lib);

const MFlags = enum {
sse2,
ssse3,
sse41,
avx,
avx2,
avx512f,
aes,
pclmul,
rdrnd,
crypto,

fn f(flag: @This()) Target.Cpu.Feature.Set.Index {
return switch (flag) {
.sse2 => @intFromEnum(Target.x86.Feature.sse2),
.ssse3 => @intFromEnum(Target.x86.Feature.ssse3),
.sse41 => @intFromEnum(Target.x86.Feature.sse4_1),
.avx => @intFromEnum(Target.x86.Feature.avx),
.avx2 => @intFromEnum(Target.x86.Feature.avx2),
.avx512f => @intFromEnum(Target.x86.Feature.avx512f),
.aes => @intFromEnum(Target.x86.Feature.aes),
.pclmul => @intFromEnum(Target.x86.Feature.pclmul),
.rdrnd => @intFromEnum(Target.x86.Feature.rdrnd),
.crypto => @intFromEnum(Target.aarch64.Feature.crypto),
};
}
};

const MLib = struct {
name: []const u8,
count: usize,
sources: []const []const u8,
flags: []const MFlags,
arches: []const std.Target.Cpu.Arch,
lib: *Compile = undefined,
};

var mlibs: [8]MLib = .{
.{
.name = "armcrypto",
.count = 3,
.sources = &.{
"crypto_aead/aegis128l/aegis128l_armcrypto.c",
"crypto_aead/aegis256/aegis256_armcrypto.c",
"crypto_aead/aes256gcm/armcrypto/aead_aes256gcm_armcrypto.c",
},
.flags = &.{.aes},
.arches = &.{ .aarch64, .aarch64_be, .aarch64_32 },
},

.{
.name = "sse2",
.count = 1,
.sources = &.{
"crypto_stream/salsa20/xmm6int/salsa20_xmm6int-sse2.c",
},
.flags = &.{.sse2},
.arches = &.{ .x86_64, .x86 },
},
.{
.name = "ssse3",
.count = 3,
.sources = &.{
"crypto_generichash/blake2b/ref/blake2b-compress-ssse3.c",
"crypto_pwhash/argon2/argon2-fill-block-ssse3.c",
"crypto_stream/chacha20/dolbeau/chacha20_dolbeau-ssse3.c",
},
.flags = &.{ .sse2, .ssse3 },
.arches = &.{ .x86_64, .x86 },
},
.{
.name = "sse41",
.count = 1,
.sources = &.{
"crypto_generichash/blake2b/ref/blake2b-compress-sse41.c",
},
.flags = &.{ .sse2, .ssse3, .sse41 },
.arches = &.{ .x86_64, .x86 },
},
.{
.name = "avx2",
.count = 4,
.sources = &.{
"crypto_generichash/blake2b/ref/blake2b-compress-avx2.c",
"crypto_pwhash/argon2/argon2-fill-block-avx2.c",
"crypto_stream/chacha20/dolbeau/chacha20_dolbeau-avx2.c",
"crypto_stream/salsa20/xmm6int/salsa20_xmm6int-avx2.c",
},
.flags = &.{ .sse2, .ssse3, .sse41, .avx, .avx2 },
.arches = &.{.x86_64},
},
.{
.name = "avx512f",
.count = 1,
.sources = &.{
"crypto_pwhash/argon2/argon2-fill-block-avx512f.c",
},
.flags = &.{ .sse2, .ssse3, .sse41, .avx, .avx2, .avx512f },
.arches = &.{.x86_64},
},
.{
.name = "aesni",
.count = 3,
.sources = &.{
"crypto_aead/aegis128l/aegis128l_aesni.c",
"crypto_aead/aegis256/aegis256_aesni.c",
"crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c",
},
.flags = &.{ .avx, .aes, .pclmul },
.arches = &.{.x86_64},
},
.{
.name = "mrdrnd",
.count = 1,
.sources = &.{
"randombytes/internal/randombytes_internal_random.c",
},
.flags = &.{.rdrnd},
.arches = &.{ .x86_64, .x86 },
},
};

const base_flags = &.{
const flags = &.{
"-fvisibility=hidden",
"-fno-strict-aliasing",
"-fno-strict-overflow",
Expand All @@ -340,61 +218,15 @@ pub fn build(b: *std.Build) !void {

const allocator = heap.page_allocator;

// compile CPU-specific library code
for (&mlibs) |*mlib| {
var target2 = target;
for (mlib.arches) |arch| {
if (target.result.cpu.arch == arch) {
for (mlib.flags) |flag| {
target2.query.cpu_features_add.addFeature(flag.f());
}
break;
}
}

mlib.lib = b.addStaticLibrary(.{
.name = mlib.name,
.target = target2,
.optimize = optimize,
});
const elib = mlib.lib;
initLibConfig(target, elib);

var flags = std.ArrayList([]const u8).init(allocator);
defer flags.deinit();
try flags.appendSlice(base_flags);

for (mlib.sources) |path| {
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, path });
elib.addCSourceFiles(.{
.files = &.{full_path},
.flags = flags.items,
});
}
lib.linkLibrary(elib);
}

// compile generic library code
var walker = try src_dir.walk(allocator);
files: while (try walker.next()) |entry| {
var flags = std.ArrayList([]const u8).init(allocator);
defer flags.deinit();
try flags.appendSlice(base_flags);

while (try walker.next()) |entry| {
const name = entry.basename;

if (mem.endsWith(u8, name, ".c")) {
for (mlibs) |mlib| {
for (mlib.sources) |path| {
if (mem.eql(u8, entry.path, path)) continue :files;
}
}

const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path });

lib.addCSourceFiles(.{
.files = &.{full_path},
.flags = flags.items,
.flags = flags,
});
} else if (mem.endsWith(u8, name, ".S")) {
const full_path = try fmt.allocPrint(allocator, "{s}/{s}", .{ src_path, entry.path });
Expand Down
12 changes: 9 additions & 3 deletions src/libsodium/crypto_aead/aes256gcm/aesni/aead_aes256gcm_aesni.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

#if defined(HAVE_TMMINTRIN_H) && defined(HAVE_WMMINTRIN_H)

#ifdef __GNUC__
#pragma GCC target("avx,aes,pclmul")
#endif
# ifdef __clang__
# pragma clang attribute push(__attribute__((target("aes,avx,pclmul"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("aes,avx,pclmul")
# endif

#if !defined(_MSC_VER) || _MSC_VER < 1800
#define __vectorcall
Expand Down Expand Up @@ -1006,4 +1008,8 @@ crypto_aead_aes256gcm_is_available(void)
return sodium_runtime_has_pclmul() & sodium_runtime_has_aesni() & sodium_runtime_has_avx();
}

#ifdef __clang__
# pragma clang attribute pop
#endif

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
#if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \
defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)

# ifdef __GNUC__
# pragma GCC target("sse2")
# pragma GCC target("ssse3")
# pragma GCC target("sse4.1")
# pragma GCC target("avx2")
# ifdef __clang__
# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("sse2,ssse3,sse4.1,avx2")
# endif

# include <emmintrin.h>
Expand Down Expand Up @@ -46,4 +45,8 @@ blake2b_compress_avx2(blake2b_state *S, const uint8_t block[BLAKE2B_BLOCKBYTES])
return 0;
}

# ifdef __clang__
# pragma clang attribute pop
# endif

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && \
defined(HAVE_SMMINTRIN_H)

# ifdef __GNUC__
# pragma GCC target("sse2")
# pragma GCC target("ssse3")
# pragma GCC target("sse4.1")
# ifdef __clang__
# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("sse2,ssse3,sse4.1")
# endif

# include <emmintrin.h>
Expand Down Expand Up @@ -84,4 +84,8 @@ blake2b_compress_sse41(blake2b_state *S,
return 0;
}

# ifdef __clang__
# pragma clang attribute pop
# endif

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)

# ifdef __GNUC__
# pragma GCC target("sse2")
# pragma GCC target("ssse3")
# ifdef __clang__
# pragma clang attribute push(__attribute__((target("sse2,ssse3"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("sse2,ssse3")
# endif

# include <emmintrin.h>
Expand Down Expand Up @@ -87,4 +88,8 @@ blake2b_compress_ssse3(blake2b_state *S,
return 0;
}

# ifdef __clang__
# pragma clang attribute pop
# endif

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

#if defined(HAVE_TI_MODE) && defined(HAVE_EMMINTRIN_H)

# ifdef __GNUC__
# ifdef __clang__
# pragma clang attribute push(__attribute__((target("sse2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("sse2")
# endif

Expand Down Expand Up @@ -946,4 +948,8 @@ struct crypto_onetimeauth_poly1305_implementation
SODIUM_C99(.onetimeauth_final =) crypto_onetimeauth_poly1305_sse2_final
};

#ifdef __clang__
# pragma clang attribute pop
#endif

#endif
14 changes: 9 additions & 5 deletions src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx2.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
#if defined(HAVE_AVX2INTRIN_H) && defined(HAVE_EMMINTRIN_H) && \
defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)

# ifdef __GNUC__
# pragma GCC target("sse2")
# pragma GCC target("ssse3")
# pragma GCC target("sse4.1")
# pragma GCC target("avx2")
# ifdef __clang__
# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1,avx2"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("sse2,ssse3,sse4.1,avx2")
# endif

# ifdef _MSC_VER
Expand Down Expand Up @@ -236,4 +235,9 @@ argon2_fill_segment_avx2(const argon2_instance_t *instance,
}
}
}

#ifdef __clang__
# pragma clang attribute pop
#endif

#endif
15 changes: 9 additions & 6 deletions src/libsodium/crypto_pwhash/argon2/argon2-fill-block-avx512f.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
#if defined(HAVE_AVX512FINTRIN_H) && defined(HAVE_AVX2INTRIN_H) && \
defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H) && defined(HAVE_SMMINTRIN_H)

# ifdef __GNUC__
# pragma GCC target("sse2")
# pragma GCC target("ssse3")
# pragma GCC target("sse4.1")
# pragma GCC target("avx2")
# pragma GCC target("avx512f")
# ifdef __clang__
# pragma clang attribute push(__attribute__((target("sse2,ssse3,sse4.1,avx2,avx512f"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("sse2,ssse3,sse4.1,avx2,avx512f")
# endif

# ifdef _MSC_VER
Expand Down Expand Up @@ -241,4 +239,9 @@ argon2_fill_segment_avx512f(const argon2_instance_t *instance,
}
}
}

#ifdef __clang__
# pragma clang attribute pop
#endif

#endif
12 changes: 9 additions & 3 deletions src/libsodium/crypto_pwhash/argon2/argon2-fill-block-ssse3.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@

#if defined(HAVE_EMMINTRIN_H) && defined(HAVE_TMMINTRIN_H)

# ifdef __GNUC__
# pragma GCC target("sse2")
# pragma GCC target("ssse3")
# ifdef __clang__
# pragma clang attribute push(__attribute__((target("sse2,ssse3"))), apply_to = function)
# elif defined(__GNUC__)
# pragma GCC target("sse2,ssse3")
# endif

# ifdef _MSC_VER
Expand Down Expand Up @@ -235,4 +236,9 @@ argon2_fill_segment_ssse3(const argon2_instance_t *instance,
}
}
}

#ifdef __clang__
# pragma clang attribute pop
#endif

#endif
Loading

0 comments on commit ee40b36

Please sign in to comment.