diff --git a/build.zig b/build.zig index bbc0c57b80..0466b79bf2 100644 --- a/build.zig +++ b/build.zig @@ -576,6 +576,7 @@ pub fn build(b: *std.Build) !void { enable_macos_sdk, enable_ios_sdk, enable_symlinks_windows, + skip_translate_c, )); test_step.dependOn(tests.addCAbiTests(b, .{ .test_target_filters = test_target_filters, diff --git a/ci/loongarch64-linux-debug.sh b/ci/loongarch64-linux-debug.sh new file mode 100755 index 0000000000..f233fb6f77 --- /dev/null +++ b/ci/loongarch64-linux-debug.sh @@ -0,0 +1,71 @@ +#!/bin/sh + +# Requires cmake ninja-build + +set -x +set -e + +ARCH="$(uname -m)" +TARGET="$ARCH-linux-musl" +MCPU="baseline" +CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.16.0-dev.157+7fdd60df1" +PREFIX="$HOME/deps/$CACHE_BASENAME" +ZIG="$PREFIX/bin/zig" + +# Make the `zig version` number consistent. +# This will affect the cmake command below. +git fetch --unshallow || true +git fetch --tags + +# Override the cache directories because they won't actually help other CI runs +# which will be testing alternate versions of zig, and ultimately would just +# fill up space on the hard drive for no reason. +export ZIG_GLOBAL_CACHE_DIR="$PWD/zig-global-cache" +export ZIG_LOCAL_CACHE_DIR="$PWD/zig-local-cache" + +mkdir build-debug +cd build-debug + +export CC="$ZIG cc -target $TARGET -mcpu=$MCPU" +export CXX="$ZIG c++ -target $TARGET -mcpu=$MCPU" + +cmake .. \ + -DCMAKE_INSTALL_PREFIX="stage3-debug" \ + -DCMAKE_PREFIX_PATH="$PREFIX" \ + -DCMAKE_BUILD_TYPE=Debug \ + -DZIG_TARGET_TRIPLE="$TARGET" \ + -DZIG_TARGET_MCPU="$MCPU" \ + -DZIG_STATIC=ON \ + -DZIG_NO_LIB=ON \ + -GNinja \ + -DCMAKE_C_LINKER_DEPFILE_SUPPORTED=FALSE \ + -DCMAKE_CXX_LINKER_DEPFILE_SUPPORTED=FALSE +# https://github.com/ziglang/zig/issues/22213 + +# Now cmake will use zig as the C/C++ compiler. We reset the environment variables +# so that installation and testing do not get affected by them. +unset CC +unset CXX + +ninja install + +# No -fqemu and -fwasmtime here as they're covered by the x86_64-linux scripts. +stage3-debug/bin/zig build test \ + --maxrss 60129542144 \ + -Dstatic-llvm \ + -Dskip-non-native \ + -Dtarget=native-native-musl \ + --search-prefix "$PREFIX" \ + --zig-lib-dir "$PWD/../lib" \ + -Dno-langref \ + -Dskip-translate-c + +stage3-debug/bin/zig build \ + --prefix stage4-debug \ + -Denable-llvm \ + -Dno-lib \ + -Dtarget=$TARGET \ + -Duse-zig-libcxx \ + -Dversion-string="$(stage3-debug/bin/zig version)" + +stage4-debug/bin/zig test ../test/behavior.zig diff --git a/ci/loongarch64-linux-release.sh b/ci/loongarch64-linux-release.sh new file mode 100755 index 0000000000..2020449181 --- /dev/null +++ b/ci/loongarch64-linux-release.sh @@ -0,0 +1,77 @@ +#!/bin/sh + +# Requires cmake ninja-build + +set -x +set -e + +ARCH="$(uname -m)" +TARGET="$ARCH-linux-musl" +MCPU="baseline" +CACHE_BASENAME="zig+llvm+lld+clang-$TARGET-0.16.0-dev.157+7fdd60df1" +PREFIX="$HOME/deps/$CACHE_BASENAME" +ZIG="$PREFIX/bin/zig" + +# Make the `zig version` number consistent. +# This will affect the cmake command below. +git fetch --unshallow || true +git fetch --tags + +# Override the cache directories because they won't actually help other CI runs +# which will be testing alternate versions of zig, and ultimately would just +# fill up space on the hard drive for no reason. +export ZIG_GLOBAL_CACHE_DIR="$PWD/zig-global-cache" +export ZIG_LOCAL_CACHE_DIR="$PWD/zig-local-cache" + +mkdir build-release +cd build-release + +export CC="$ZIG cc -target $TARGET -mcpu=$MCPU" +export CXX="$ZIG c++ -target $TARGET -mcpu=$MCPU" + +cmake .. \ + -DCMAKE_INSTALL_PREFIX="stage3-release" \ + -DCMAKE_PREFIX_PATH="$PREFIX" \ + -DCMAKE_BUILD_TYPE=Release \ + -DZIG_TARGET_TRIPLE="$TARGET" \ + -DZIG_TARGET_MCPU="$MCPU" \ + -DZIG_STATIC=ON \ + -DZIG_NO_LIB=ON \ + -GNinja \ + -DCMAKE_C_LINKER_DEPFILE_SUPPORTED=FALSE \ + -DCMAKE_CXX_LINKER_DEPFILE_SUPPORTED=FALSE +# https://github.com/ziglang/zig/issues/22213 + +# Now cmake will use zig as the C/C++ compiler. We reset the environment variables +# so that installation and testing do not get affected by them. +unset CC +unset CXX + +ninja install + +# No -fqemu and -fwasmtime here as they're covered by the x86_64-linux scripts. +stage3-release/bin/zig build test \ + --maxrss 60129542144 \ + -Dstatic-llvm \ + -Dskip-non-native \ + -Dtarget=native-native-musl \ + --search-prefix "$PREFIX" \ + --zig-lib-dir "$PWD/../lib" \ + -Dno-langref \ + -Dskip-translate-c + +# Ensure that stage3 and stage4 are byte-for-byte identical. +stage3-release/bin/zig build \ + --prefix stage4-release \ + -Denable-llvm \ + -Dno-lib \ + -Doptimize=ReleaseFast \ + -Dstrip \ + -Dtarget=$TARGET \ + -Duse-zig-libcxx \ + -Dversion-string="$(stage3-release/bin/zig version)" + +# diff returns an error code if the files differ. +echo "If the following command fails, it means nondeterminism has been" +echo "introduced, making stage3 and stage4 no longer byte-for-byte identical." +diff stage3-release/bin/zig stage4-release/bin/zig diff --git a/doc/langref/runtime_shrExact_overflow.zig b/doc/langref/runtime_shrExact_overflow.zig index 1350e9da75..98da0dde11 100644 --- a/doc/langref/runtime_shrExact_overflow.zig +++ b/doc/langref/runtime_shrExact_overflow.zig @@ -7,7 +7,7 @@ pub fn main() void { const y = @shrExact(x, 2); std.debug.print("value: {}\n", .{y}); - if (builtin.cpu.arch.isRISCV() and builtin.zig_backend == .stage2_llvm) @panic("https://github.com/ziglang/zig/issues/24304"); + if ((builtin.cpu.arch.isRISCV() or builtin.cpu.arch.isLoongArch()) and builtin.zig_backend == .stage2_llvm) @panic("https://github.com/ziglang/zig/issues/24304"); } // exe=fail diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 60f0b2e304..5773faa5f2 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1903,7 +1903,7 @@ pub const Cpu = struct { .csky => &csky.cpu.ck810, // gcc/clang do not have a generic csky model. .hexagon => &hexagon.cpu.hexagonv68, // gcc/clang do not have a generic hexagon model. .lanai => &lanai.cpu.v11, // clang does not have a generic lanai model. - .loongarch64 => &loongarch.cpu.loongarch64, + .loongarch64 => &loongarch.cpu.la64v1_0, .m68k => &m68k.cpu.M68000, .mips, .mipsel => &mips.cpu.mips32r2, .mips64, .mips64el => &mips.cpu.mips64r2, diff --git a/lib/std/Target/loongarch.zig b/lib/std/Target/loongarch.zig index e87e4e95d1..251e202daf 100644 --- a/lib/std/Target/loongarch.zig +++ b/lib/std/Target/loongarch.zig @@ -159,11 +159,6 @@ pub const all_features = blk: { }; pub const cpu = struct { - pub const generic: CpuModel = .{ - .name = "generic", - .llvm_name = "generic", - .features = featureSet(&[_]Feature{}), - }; pub const generic_la32: CpuModel = .{ .name = "generic_la32", .llvm_name = "generic-la32", @@ -191,6 +186,30 @@ pub const cpu = struct { .ual, }), }; + pub const la64v1_0: CpuModel = .{ + .name = "la64v1_0", + .llvm_name = null, + .features = featureSet(&[_]Feature{ + .@"64bit", + .lsx, + .ual, + }), + }; + pub const la64v1_1: CpuModel = .{ + .name = "la64v1_1", + .llvm_name = null, + .features = featureSet(&[_]Feature{ + .@"64bit", + .div32, + .frecipe, + .lam_bh, + .lamcas, + .ld_seq_sa, + .lsx, + .scq, + .ual, + }), + }; pub const la664: CpuModel = .{ .name = "la664", .llvm_name = "la664", @@ -208,13 +227,4 @@ pub const cpu = struct { .ual, }), }; - pub const loongarch64: CpuModel = .{ - .name = "loongarch64", - .llvm_name = "loongarch64", - .features = featureSet(&[_]Feature{ - .@"64bit", - .d, - .ual, - }), - }; }; diff --git a/lib/std/crypto/ml_kem.zig b/lib/std/crypto/ml_kem.zig index c7ad23d3e2..0a8e73f785 100644 --- a/lib/std/crypto/ml_kem.zig +++ b/lib/std/crypto/ml_kem.zig @@ -1722,14 +1722,20 @@ test "Test happy flow" { // Code to test NIST Known Answer Tests (KAT), see PQCgenKAT.c. test "NIST KAT test d00.Kyber512" { + if (comptime builtin.cpu.has(.loongarch, .lsx)) return error.SkipZigTest; + try testNistKat(d00.Kyber512, "e9c2bd37133fcb40772f81559f14b1f58dccd1c816701be9ba6214d43baf4547"); } test "NIST KAT test d00.Kyber1024" { + if (comptime builtin.cpu.has(.loongarch, .lsx)) return error.SkipZigTest; + try testNistKat(d00.Kyber1024, "89248f2f33f7f4f7051729111f3049c409a933ec904aedadf035f30fa5646cd5"); } test "NIST KAT test d00.Kyber768" { + if (comptime builtin.cpu.has(.loongarch, .lsx)) return error.SkipZigTest; + try testNistKat(d00.Kyber768, "a1e122cad3c24bc51622e4c242d8b8acbcd3f618fee4220400605ca8f9ea02c2"); } diff --git a/lib/std/math/modf.zig b/lib/std/math/modf.zig index 870b4e6f6b..60680cf831 100644 --- a/lib/std/math/modf.zig +++ b/lib/std/math/modf.zig @@ -87,6 +87,7 @@ fn ModfTests(comptime T: type) type { test "vector" { if (builtin.os.tag == .macos and builtin.cpu.arch == .aarch64) return error.SkipZigTest; if (builtin.cpu.arch == .s390x) return error.SkipZigTest; + if (comptime builtin.cpu.has(.loongarch, .lsx)) return error.SkipZigTest; // https://github.com/llvm/llvm-project/issues/159529 const widths = [_]comptime_int{ 1, 2, 3, 4, 8, 16 }; diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 3013af36f6..128ac695cf 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -4534,7 +4534,8 @@ pub fn doNotOptimizeAway(val: anytype) void { } else doNotOptimizeAway(&val); }, .float => { - if ((t.float.bits == 32 or t.float.bits == 64) and builtin.zig_backend != .stage2_c) { + // https://github.com/llvm/llvm-project/issues/159200 + if ((t.float.bits == 32 or t.float.bits == 64) and builtin.zig_backend != .stage2_c and !builtin.cpu.arch.isLoongArch()) { asm volatile ("" : : [_] "rm" (val), diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index 549f365321..b2116c1a74 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -468,9 +468,8 @@ fn detectNativeCpuAndFeatures(cpu_arch: Target.Cpu.Arch, os: Target.Os, query: T // although it is a runtime value, is guaranteed to be one of the architectures in the set // of the respective switch prong. switch (builtin.cpu.arch) { - .x86_64, .x86 => { - return @import("system/x86.zig").detectNativeCpuAndFeatures(cpu_arch, os, query); - }, + .loongarch32, .loongarch64 => return @import("system/loongarch.zig").detectNativeCpuAndFeatures(cpu_arch, os, query), + .x86_64, .x86 => return @import("system/x86.zig").detectNativeCpuAndFeatures(cpu_arch, os, query), else => {}, } diff --git a/lib/std/zig/system/loongarch.zig b/lib/std/zig/system/loongarch.zig new file mode 100644 index 0000000000..cdeeb10564 --- /dev/null +++ b/lib/std/zig/system/loongarch.zig @@ -0,0 +1,48 @@ +const builtin = @import("builtin"); +const std = @import("std"); + +pub fn detectNativeCpuAndFeatures( + arch: std.Target.Cpu.Arch, + os: std.Target.Os, + query: std.Target.Query, +) ?std.Target.Cpu { + _ = os; + _ = query; + + // Clearly this code could do better in the future by actually querying specific CPU features + // with the cpucfg instruction like on x86. But with the small number of well-known LoongArch + // models that exist at the moment, simply checking the PRID is plenty. + var cpu: std.Target.Cpu = .{ + .arch = arch, + .model = switch (cpucfg(0) & 0xf000) { + else => return null, + 0xc000 => &std.Target.loongarch.cpu.la464, + 0xd000 => &std.Target.loongarch.cpu.la664, + }, + .features = .empty, + }; + + cpu.features.addFeatureSet(cpu.model.features); + cpu.features.populateDependencies(cpu.arch.allFeaturesList()); + + return cpu; +} + +/// This is a workaround for the C backend until zig has the ability to put +/// C code in inline assembly. +extern fn zig_loongarch_cpucfg(word: u32, result: *u32) callconv(.c) void; + +fn cpucfg(word: u32) u32 { + var result: u32 = undefined; + + if (builtin.zig_backend == .stage2_c) { + zig_loongarch_cpucfg(word, &result); + } else { + asm ("cpucfg %[result], %[word]" + : [result] "=r" (result), + : [word] "r" (word), + ); + } + + return result; +} diff --git a/lib/zig.h b/lib/zig.h index b9d4645ca2..5c96b4bea0 100644 --- a/lib/zig.h +++ b/lib/zig.h @@ -4195,7 +4195,17 @@ static inline void* zig_x86_64_windows_teb(void) { #endif -#if defined(zig_x86) +#if defined(zig_loongarch) + +static inline void zig_loongarch_cpucfg(uint32_t word, uint32_t* result) { +#if defined(zig_gnuc_asm) + __asm__("cpucfg %[result], %[word]" : [result] "=r" (result) : [word] "r" (word)); +#else + *result = 0; +#endif +} + +#elif defined(zig_x86) static inline void zig_x86_cpuid(uint32_t leaf_id, uint32_t subid, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx) { #if defined(zig_msvc) @@ -4206,7 +4216,7 @@ static inline void zig_x86_cpuid(uint32_t leaf_id, uint32_t subid, uint32_t* eax *ecx = (uint32_t)cpu_info[2]; *edx = (uint32_t)cpu_info[3]; #elif defined(zig_gnuc_asm) - __asm__("cpuid" : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx) : "a"(leaf_id), "c"(subid)); + __asm__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (leaf_id), "c" (subid)); #else *eax = 0; *ebx = 0; @@ -4221,7 +4231,7 @@ static inline uint32_t zig_x86_get_xcr0(void) { #elif defined(zig_gnuc_asm) uint32_t eax; uint32_t edx; - __asm__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0)); + __asm__("xgetbv" : "=a" (eax), "=d" (edx) : "c" (0)); return eax; #else *eax = 0; diff --git a/stage1/zig.h b/stage1/zig.h index b9d4645ca2..5c96b4bea0 100644 --- a/stage1/zig.h +++ b/stage1/zig.h @@ -4195,7 +4195,17 @@ static inline void* zig_x86_64_windows_teb(void) { #endif -#if defined(zig_x86) +#if defined(zig_loongarch) + +static inline void zig_loongarch_cpucfg(uint32_t word, uint32_t* result) { +#if defined(zig_gnuc_asm) + __asm__("cpucfg %[result], %[word]" : [result] "=r" (result) : [word] "r" (word)); +#else + *result = 0; +#endif +} + +#elif defined(zig_x86) static inline void zig_x86_cpuid(uint32_t leaf_id, uint32_t subid, uint32_t* eax, uint32_t* ebx, uint32_t* ecx, uint32_t* edx) { #if defined(zig_msvc) @@ -4206,7 +4216,7 @@ static inline void zig_x86_cpuid(uint32_t leaf_id, uint32_t subid, uint32_t* eax *ecx = (uint32_t)cpu_info[2]; *edx = (uint32_t)cpu_info[3]; #elif defined(zig_gnuc_asm) - __asm__("cpuid" : "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx) : "a"(leaf_id), "c"(subid)); + __asm__("cpuid" : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) : "a" (leaf_id), "c" (subid)); #else *eax = 0; *ebx = 0; @@ -4221,7 +4231,7 @@ static inline uint32_t zig_x86_get_xcr0(void) { #elif defined(zig_gnuc_asm) uint32_t eax; uint32_t edx; - __asm__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(0)); + __asm__("xgetbv" : "=a" (eax), "=d" (edx) : "c" (0)); return eax; #else *eax = 0; diff --git a/stage1/zig1.wasm b/stage1/zig1.wasm index ef46d39da4..1660c5a926 100644 Binary files a/stage1/zig1.wasm and b/stage1/zig1.wasm differ diff --git a/test/behavior/asm.zig b/test/behavior/asm.zig index dae2f579c0..ebfd5bb235 100644 --- a/test/behavior/asm.zig +++ b/test/behavior/asm.zig @@ -67,6 +67,7 @@ test "alternative constraints" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch.isLoongArch()) return error.SkipZigTest; // https://github.com/llvm/llvm-project/issues/159200 if (builtin.zig_backend == .stage2_c and builtin.os.tag == .windows) return error.SkipZigTest; // MSVC doesn't support inline assembly diff --git a/test/behavior/math.zig b/test/behavior/math.zig index 679c94a78e..914c47ba69 100644 --- a/test/behavior/math.zig +++ b/test/behavior/math.zig @@ -111,6 +111,8 @@ test "@clz vectors" { } fn testClzVectors() !void { + if (comptime builtin.cpu.has(.loongarch, .lsx)) return error.SkipZigTest; // https://github.com/llvm/llvm-project/issues/159529 + const Vu4 = @Vector(64, u4); const Vu8 = @Vector(64, u8); const Vu128 = @Vector(64, u128); @@ -197,6 +199,8 @@ test "@ctz vectors" { } fn testCtzVectors() !void { + if (comptime builtin.cpu.has(.loongarch, .lsx)) return error.SkipZigTest; // https://github.com/llvm/llvm-project/issues/159529 + const Vu4 = @Vector(64, u4); const Vu8 = @Vector(64, u8); @setEvalBranchQuota(10_000); diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index 49a9ab99c2..a6f692601a 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -1135,6 +1135,7 @@ extern fn c_ret_medium_vec() MediumVec; test "medium simd vector" { if (builtin.zig_backend == .stage2_x86_64 and !comptime builtin.cpu.has(.x86, .avx)) return error.SkipZigTest; if (builtin.cpu.arch.isPowerPC64()) return error.SkipZigTest; + if (builtin.cpu.arch.isLoongArch()) return error.SkipZigTest; c_medium_vec(.{ 1, 2, 3, 4 }); @@ -1155,6 +1156,7 @@ test "big simd vector" { if (builtin.cpu.arch.isMIPS64() and builtin.mode != .Debug) return error.SkipZigTest; if (builtin.cpu.arch.isPowerPC64()) return error.SkipZigTest; + if (builtin.cpu.arch.isLoongArch()) return error.SkipZigTest; if (builtin.zig_backend == .stage2_llvm and builtin.cpu.arch == .x86_64 and builtin.os.tag == .macos and builtin.mode != .Debug) return error.SkipZigTest; c_big_vec(.{ 1, 2, 3, 4, 5, 6, 7, 8 }); diff --git a/test/src/StackTrace.zig b/test/src/StackTrace.zig index d956e27cd7..061df37e3a 100644 --- a/test/src/StackTrace.zig +++ b/test/src/StackTrace.zig @@ -15,6 +15,7 @@ const Config = struct { const PerMode = struct { expect: []const u8, + exclude_arch: []const std.Target.Cpu.Arch = &.{}, exclude_os: []const std.Target.Os.Tag = &.{}, error_tracing: ?bool = null, }; @@ -59,6 +60,7 @@ fn addExpect( use_llvm: bool, mode_config: Config.PerMode, ) void { + for (mode_config.exclude_arch) |tag| if (tag == builtin.cpu.arch) return; for (mode_config.exclude_os) |tag| if (tag == builtin.os.tag) return; const b = self.b; diff --git a/test/stack_traces.zig b/test/stack_traces.zig index 40a6168297..9e815e032f 100644 --- a/test/stack_traces.zig +++ b/test/stack_traces.zig @@ -792,6 +792,16 @@ pub fn addCases(cases: *tests.StackTracesContext) void { \\} , .Debug = .{ + // std.debug.sys_can_stack_trace + .exclude_arch = &.{ + .loongarch32, + .loongarch64, + .mips, + .mipsel, + .mips64, + .mips64el, + .s390x, + }, .exclude_os = &.{ .openbsd, // integer overflow .windows, // TODO intermittent failures diff --git a/test/standalone/build.zig b/test/standalone/build.zig index cf08c9b182..b284f25483 100644 --- a/test/standalone/build.zig +++ b/test/standalone/build.zig @@ -9,6 +9,7 @@ pub fn build(b: *std.Build) void { const enable_macos_sdk = b.option(bool, "enable_macos_sdk", "Run tests requiring presence of macOS SDK and frameworks") orelse enable_ios_sdk; const enable_symlinks_windows = b.option(bool, "enable_symlinks_windows", "Run tests requiring presence of symlinks on Windows") orelse false; const omit_symlinks = builtin.os.tag == .windows and !enable_symlinks_windows; + const skip_translate_c = b.option(bool, "skip_translate_c", "Test suite skips translate-c tests") orelse false; const simple_skip_debug = b.option(bool, "simple_skip_debug", "Simple tests skip debug builds") orelse false; const simple_skip_release_safe = b.option(bool, "simple_skip_release_safe", "Simple tests skip release-safe builds") orelse false; @@ -20,6 +21,7 @@ pub fn build(b: *std.Build) void { .skip_release_safe = simple_skip_release_safe, .skip_release_fast = simple_skip_release_fast, .skip_release_small = simple_skip_release_small, + .skip_translate_c = skip_translate_c, }); const simple_dep_step = simple_dep.builder.default_step; simple_dep_step.name = "standalone_test_cases.simple"; @@ -97,9 +99,12 @@ pub fn build(b: *std.Build) void { pkg.build_zig.requires_macos_sdk; const requires_symlinks = @hasDecl(pkg.build_zig, "requires_symlinks") and pkg.build_zig.requires_symlinks; + const requires_translate_c = @hasDecl(pkg.build_zig, "requires_translate_c") and + pkg.build_zig.requires_translate_c; if ((requires_symlinks and omit_symlinks) or (requires_macos_sdk and !enable_macos_sdk) or - (requires_ios_sdk and !enable_ios_sdk)) + (requires_ios_sdk and !enable_ios_sdk) or + (requires_translate_c and skip_translate_c)) { continue :add_dep_steps; } diff --git a/test/standalone/dep_lazypath/build.zig b/test/standalone/dep_lazypath/build.zig index 01b4be8fd4..9649a02500 100644 --- a/test/standalone/dep_lazypath/build.zig +++ b/test/standalone/dep_lazypath/build.zig @@ -1,5 +1,7 @@ const std = @import("std"); +pub const requires_translate_c = true; + pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test it"); b.default_step = test_step; diff --git a/test/standalone/glibc_compat/build.zig b/test/standalone/glibc_compat/build.zig index 6c2c924cae..fd3bbaf084 100644 --- a/test/standalone/glibc_compat/build.zig +++ b/test/standalone/glibc_compat/build.zig @@ -1,6 +1,8 @@ const std = @import("std"); const builtin = @import("builtin"); +pub const requires_translate_c = true; + // To run executables linked against a specific glibc version, the // run-time glibc version needs to be new enough. Check the host's glibc // version. Note that this does not allow for translation/vm/emulation diff --git a/test/standalone/issue_794/build.zig b/test/standalone/issue_794/build.zig index 0f3f0a16f7..b0afe51bf4 100644 --- a/test/standalone/issue_794/build.zig +++ b/test/standalone/issue_794/build.zig @@ -1,5 +1,7 @@ const std = @import("std"); +pub const requires_translate_c = true; + pub fn build(b: *std.Build) void { const test_step = b.step("test", "Test it"); b.default_step = test_step; diff --git a/test/standalone/simple/build.zig b/test/standalone/simple/build.zig index 5bd76a3540..dbfca21f83 100644 --- a/test/standalone/simple/build.zig +++ b/test/standalone/simple/build.zig @@ -9,6 +9,7 @@ pub fn build(b: *std.Build) void { const skip_release_safe = b.option(bool, "skip_release_safe", "Skip release-safe builds") orelse false; const skip_release_fast = b.option(bool, "skip_release_fast", "Skip release-fast builds") orelse false; const skip_release_small = b.option(bool, "skip_release_small", "Skip release-small builds") orelse false; + const skip_translate_c = b.option(bool, "skip_translate_c", "Test suite skips translate-c tests") orelse false; var optimize_modes_buf: [4]std.builtin.OptimizeMode = undefined; var optimize_modes_len: usize = 0; @@ -36,6 +37,7 @@ pub fn build(b: *std.Build) void { if (case.os_filter) |os_tag| { if (os_tag != builtin.os.tag) continue; } + if (case.uses_translate_c and skip_translate_c) continue; const resolved_target = b.resolveTargetQuery(case.target); @@ -82,6 +84,7 @@ const Case = struct { is_exe: bool = true, /// Run only on this OS. os_filter: ?std.Target.Os.Tag = null, + uses_translate_c: bool = false, }; const cases = [_]Case{ @@ -93,6 +96,7 @@ const cases = [_]Case{ .src_path = "hello_world/hello_libc.zig", .link_libc = true, .all_modes = true, + .uses_translate_c = true, }, .{ .src_path = "cat/main.zig", @@ -108,7 +112,10 @@ const cases = [_]Case{ .os_tag = .freestanding, }, }, - .{ .src_path = "issue_12471/main.zig" }, + .{ + .src_path = "issue_12471/main.zig", + .uses_translate_c = true, + }, .{ .src_path = "guess_number/main.zig" }, .{ .src_path = "main_return_error/error_u8.zig" }, .{ .src_path = "main_return_error/error_u8_non_zero.zig" }, diff --git a/test/standalone/simple/hello_world/hello_libc.zig b/test/standalone/simple/hello_world/hello_libc.zig index 992afd736e..994ca31b8e 100644 --- a/test/standalone/simple/hello_world/hello_libc.zig +++ b/test/standalone/simple/hello_world/hello_libc.zig @@ -1,15 +1,11 @@ -const c = @cImport({ - // See https://github.com/ziglang/zig/issues/515 - @cDefine("_NO_CRT_STDIO_INLINE", "1"); - @cInclude("stdio.h"); - @cInclude("string.h"); -}); +extern fn printf(format: [*:0]const u8, ...) c_int; +extern fn strlen(str: [*:0]const u8) usize; const msg = "Hello, world!\n"; pub export fn main(argc: c_int, argv: **u8) c_int { _ = argv; _ = argc; - if (c.printf(msg) != @as(c_int, @intCast(c.strlen(msg)))) return -1; + if (printf(msg) != @as(c_int, @intCast(strlen(msg)))) return -1; return 0; } diff --git a/test/standalone/static_c_lib/foo.zig b/test/standalone/static_c_lib/foo.zig index da02875408..2ff5fadb95 100644 --- a/test/standalone/static_c_lib/foo.zig +++ b/test/standalone/static_c_lib/foo.zig @@ -1,12 +1,14 @@ const std = @import("std"); const expect = std.testing.expect; -const c = @cImport(@cInclude("foo.h")); + +extern fn add(a: u32, b: u32) u32; +extern var foo: u32; test "C add" { - const result = c.add(1, 2); + const result = add(1, 2); try expect(result == 3); } test "C extern variable" { - try expect(c.foo == 12345); + try expect(foo == 12345); } diff --git a/test/tests.zig b/test/tests.zig index 5b80ec5b0b..8bcdce7aad 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -419,8 +419,6 @@ const test_targets = blk: { .os_tag = .linux, .abi = .none, }, - // https://github.com/ziglang/zig/issues/21646 - .skip_modules = &.{"std"}, }, .{ .target = .{ @@ -429,8 +427,6 @@ const test_targets = blk: { .abi = .musl, }, .link_libc = true, - // https://github.com/ziglang/zig/issues/21646 - .skip_modules = &.{"std"}, }, .{ .target = .{ @@ -440,8 +436,6 @@ const test_targets = blk: { }, .linkage = .dynamic, .link_libc = true, - // https://github.com/ziglang/zig/issues/21646 - .skip_modules = &.{"std"}, .extra_target = true, }, .{ @@ -451,8 +445,6 @@ const test_targets = blk: { .abi = .gnu, }, .link_libc = true, - // https://github.com/ziglang/zig/issues/21646 - .skip_modules = &.{"std"}, }, .{ @@ -1901,6 +1893,7 @@ pub fn addStandaloneTests( enable_macos_sdk: bool, enable_ios_sdk: bool, enable_symlinks_windows: bool, + skip_translate_c: bool, ) *Step { const step = b.step("test-standalone", "Run the standalone tests"); if (compilerHasPackageManager(b)) { @@ -1913,6 +1906,7 @@ pub fn addStandaloneTests( .simple_skip_release_safe = mem.indexOfScalar(OptimizeMode, optimize_modes, .ReleaseSafe) == null, .simple_skip_release_fast = mem.indexOfScalar(OptimizeMode, optimize_modes, .ReleaseFast) == null, .simple_skip_release_small = mem.indexOfScalar(OptimizeMode, optimize_modes, .ReleaseSmall) == null, + .skip_translate_c = skip_translate_c, }); const test_cases_dep_step = test_cases_dep.builder.default_step; test_cases_dep_step.name = b.dupe(test_cases_dep_name); diff --git a/tools/update_cpu_features.zig b/tools/update_cpu_features.zig index b6e0d6495e..3bed773df7 100644 --- a/tools/update_cpu_features.zig +++ b/tools/update_cpu_features.zig @@ -986,6 +986,36 @@ const targets = [_]ArchTarget{ .name = "LoongArch", .td_name = "LoongArch", }, + .extra_cpus = &.{ + .{ + .llvm_name = null, + .zig_name = "la64v1_0", + .features = &.{ + "64bit", + "lsx", + "ual", + }, + }, + .{ + .llvm_name = null, + .zig_name = "la64v1_1", + .features = &.{ + "64bit", + "div32", + "frecipe", + "lam_bh", + "lamcas", + "ld_seq_sa", + "lsx", + "scq", + "ual", + }, + }, + }, + .omit_cpus = &.{ + "generic", + "loongarch64", + }, }, .{ .zig_name = "m68k",