From 280ced66eb712f5c74835b10fa6ba0bd915ee96b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Fri, 29 Nov 2024 01:31:49 +0100 Subject: [PATCH] std.Target: Define and use lime1 as the baseline CPU model for WebAssembly. See: https://github.com/WebAssembly/tool-conventions/pull/235 This is not *quite* using the same features as the spec'd lime1 model because LLVM 19 doesn't have the level of feature granularity that we need for that. This will be fixed once we upgrade to LLVM 20. Part of #21818. --- build.zig | 14 +++++++------- lib/std/Target.zig | 2 +- lib/std/Target/wasm.zig | 12 ++++++++++++ src/Compilation.zig | 5 ----- tools/update_cpu_features.zig | 14 ++++++++++++++ 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/build.zig b/build.zig index a513a99399..24d30f2ec2 100644 --- a/build.zig +++ b/build.zig @@ -594,15 +594,14 @@ pub fn build(b: *std.Build) !void { fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void { const semver = try std.SemanticVersion.parse(version); - var target_query: std.Target.Query = .{ - .cpu_arch = .wasm32, - .os_tag = .wasi, - }; - target_query.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory)); - const exe = addCompilerStep(b, .{ .optimize = .ReleaseSmall, - .target = b.resolveTargetQuery(target_query), + .target = b.resolveTargetQuery(std.Target.Query.parse(.{ + .arch_os_abi = "wasm32-wasi", + // `extended_const` is not supported by the `wasm-opt` version in CI. + // `nontrapping_fptoint` is not supported by `wasm2c`. + .cpu_features = "baseline-extended_const-nontrapping_fptoint", + }) catch unreachable), }); const exe_options = b.addOptions(); @@ -644,6 +643,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void { "wasm-opt", "-Oz", "--enable-bulk-memory", + "--enable-mutable-globals", "--enable-sign-ext", }); run_opt.addArtifactArg(exe); diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 2bb8a3361a..ce6eaa9f2f 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -2012,7 +2012,7 @@ pub const Cpu = struct { else => generic(arch), }, .xcore => &xcore.cpu.xs1b_generic, - .wasm32, .wasm64 => &wasm.cpu.generic, + .wasm32, .wasm64 => &wasm.cpu.lime1, else => generic(arch), }; diff --git a/lib/std/Target/wasm.zig b/lib/std/Target/wasm.zig index 0507333d96..eba4f9f01f 100644 --- a/lib/std/Target/wasm.zig +++ b/lib/std/Target/wasm.zig @@ -139,6 +139,18 @@ pub const cpu = struct { .sign_ext, }), }; + pub const lime1: CpuModel = .{ + .name = "lime1", + .llvm_name = null, + .features = featureSet(&[_]Feature{ + .bulk_memory, + .extended_const, + .multivalue, + .mutable_globals, + .nontrapping_fptoint, + .sign_ext, + }), + }; pub const mvp: CpuModel = .{ .name = "mvp", .llvm_name = "mvp", diff --git a/src/Compilation.zig b/src/Compilation.zig index b85033264a..ac8fb8a59b 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -4150,14 +4150,9 @@ fn workerDocsWasmFallible(comp: *Compilation, prog_node: std.Progress.Node) anye .os_tag = .freestanding, .cpu_features_add = std.Target.wasm.featureSet(&.{ .atomics, - .bulk_memory, // .extended_const, not supported by Safari - .multivalue, - .mutable_globals, - .nontrapping_fptoint, .reference_types, //.relaxed_simd, not supported by Firefox or Safari - .sign_ext, // observed to cause Error occured during wast conversion : // Unknown operator: 0xfd058 in Firefox 117 //.simd128, diff --git a/tools/update_cpu_features.zig b/tools/update_cpu_features.zig index 065d40d3f4..5a4e06354f 100644 --- a/tools/update_cpu_features.zig +++ b/tools/update_cpu_features.zig @@ -1033,6 +1033,20 @@ const llvm_targets = [_]LlvmTarget{ .zig_name = "wasm", .llvm_name = "WebAssembly", .td_name = "WebAssembly.td", + .extra_cpus = &.{ + .{ + .llvm_name = null, + .zig_name = "lime1", + .features = &.{ + "bulk_memory", + "extended_const", + "multivalue", + "mutable_globals", + "nontrapping_fptoint", + "sign_ext", + }, + }, + }, }, .{ .zig_name = "x86",