From 837e0f9c377d66f323926427551cc7db84a7151d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Thu, 1 May 2025 21:14:57 +0200 Subject: [PATCH] std.Target: Remove ObjectFormat.nvptx (and associated linker code). Textual PTX is just assembly language like any other. And if we do ever add support for emitting PTX object files after reverse engineering the bytecode format, we'd be emitting ELF files like the CUDA toolchain. So there's really no need for a special ObjectFormat tag here, nor linker code that treats it as a distinct format. --- CMakeLists.txt | 1 - lib/std/Target.zig | 4 -- lib/std/zig.zig | 1 - src/Compilation/Config.zig | 2 +- src/codegen.zig | 2 - src/dev.zig | 2 - src/link.zig | 15 +---- src/link/NvPtx.zig | 123 ------------------------------------- src/target.zig | 1 - 9 files changed, 4 insertions(+), 147 deletions(-) delete mode 100644 src/link/NvPtx.zig diff --git a/CMakeLists.txt b/CMakeLists.txt index 12bcec0b3a068a1409d136dc385d2f052bfe75a6..c19ee7a4983c1614f155d178fcfe62aeb60cb5a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -640,7 +640,6 @@ set(ZIG_STAGE2_SOURCES src/link/MachO/synthetic.zig src/link/MachO/Thunk.zig src/link/MachO/uuid.zig - src/link/NvPtx.zig src/link/Plan9.zig src/link/Plan9/aout.zig src/link/SpirV.zig diff --git a/lib/std/Target.zig b/lib/std/Target.zig index 88477354f6fef58f401862589d942081b5f89507..dcce4f1dd861105fd0f5c86a39fae70bc1cad7af 100644 --- a/lib/std/Target.zig +++ b/lib/std/Target.zig @@ -1017,8 +1017,6 @@ pub const ObjectFormat = enum { hex, /// The Mach object format used by macOS and other Apple platforms. macho, - /// Nvidia's PTX (Parallel Thread Execution) assembly language. - nvptx, /// The a.out format used by Plan 9 from Bell Labs. plan9, /// Machine code with no metadata. @@ -1039,7 +1037,6 @@ pub const ObjectFormat = enum { .coff => ".obj", .elf, .goff, .macho, .wasm, .xcoff => ".o", .hex => ".ihex", - .nvptx => ".ptx", .plan9 => arch.plan9Ext(), .raw => ".bin", .spirv => ".spv", @@ -1054,7 +1051,6 @@ pub const ObjectFormat = enum { .uefi, .windows => .coff, .zos => .goff, else => switch (arch) { - .nvptx, .nvptx64 => .nvptx, .spirv, .spirv32, .spirv64 => .spirv, .wasm32, .wasm64 => .wasm, else => .elf, diff --git a/lib/std/zig.zig b/lib/std/zig.zig index f70e9adec0ac4d98c7563150f71fcc69b5790a34..a946fe5e8bc7f506f65589f5ae01287c3d2b08a3 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -232,7 +232,6 @@ pub fn binNameAlloc(allocator: Allocator, options: BinNameOptions) error{OutOfMe t.libPrefix(), root_name, }), }, - .nvptx => return std.fmt.allocPrint(allocator, "{s}.ptx", .{root_name}), } } diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index 71df1d9311f5e2e44ecb5a055689dd505cbe201e..b46837679a5f7dc45470a5e83bb1edd121795299 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -434,7 +434,7 @@ pub fn resolve(options: Options) ResolveError!Config { .windows, .uefi => .code_view, else => .{ .dwarf = .@"32" }, }, - .spirv, .nvptx, .hex, .raw, .plan9 => .strip, + .spirv, .hex, .raw, .plan9 => .strip, }; }; diff --git a/src/codegen.zig b/src/codegen.zig index bad8a97f1c21b1c0592271e738d7b9ff2ee09da0..3e37a3dbb397e9f0ebb055e681670fede05e259d 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -666,7 +666,6 @@ fn lowerUavRef( switch (lf.tag) { .c => unreachable, .spirv => unreachable, - .nvptx => unreachable, .wasm => { dev.check(link.File.Tag.wasm.devFeature()); const wasm = lf.cast(.wasm).?; @@ -739,7 +738,6 @@ fn lowerNavRef( switch (lf.tag) { .c => unreachable, .spirv => unreachable, - .nvptx => unreachable, .wasm => { dev.check(link.File.Tag.wasm.devFeature()); const wasm = lf.cast(.wasm).?; diff --git a/src/dev.zig b/src/dev.zig index 0d902932462a022e48b612175aa21c9cf4ad6473..11b8be785c675445cda8f9a7cc24daaf104fc430 100644 --- a/src/dev.zig +++ b/src/dev.zig @@ -81,7 +81,6 @@ pub const Env = enum { .wasm_linker, .spirv_linker, .plan9_linker, - .nvptx_linker, .goff_linker, .xcoff_linker, => true, @@ -229,7 +228,6 @@ pub const Feature = enum { wasm_linker, spirv_linker, plan9_linker, - nvptx_linker, goff_linker, xcoff_linker, }; diff --git a/src/link.zig b/src/link.zig index 2f04751aac4652d9e30bca1096476e172074cb2f..d245061bd072541222f265ed130e57b944354fba 100644 --- a/src/link.zig +++ b/src/link.zig @@ -597,7 +597,7 @@ pub const File = struct { .mode = determineMode(use_lld, output_mode, link_mode), }); }, - .c, .spirv, .nvptx => dev.checkAny(&.{ .c_linker, .spirv_linker, .nvptx_linker }), + .c, .spirv => dev.checkAny(&.{ .c_linker, .spirv_linker }), } } @@ -670,7 +670,7 @@ pub const File = struct { } } }, - .c, .spirv, .nvptx => dev.checkAny(&.{ .c_linker, .spirv_linker, .nvptx_linker }), + .c, .spirv => dev.checkAny(&.{ .c_linker, .spirv_linker }), } } @@ -697,7 +697,6 @@ pub const File = struct { .plan9 => unreachable, .spirv => unreachable, .c => unreachable, - .nvptx => unreachable, inline else => |tag| { dev.check(tag.devFeature()); return @as(*tag.Type(), @fieldParentPtr("base", base)).getGlobalSymbol(name, lib_name); @@ -766,7 +765,7 @@ pub const File = struct { } switch (base.tag) { - .spirv, .nvptx => {}, + .spirv => {}, .goff, .xcoff => {}, inline else => |tag| { dev.check(tag.devFeature()); @@ -901,7 +900,6 @@ pub const File = struct { switch (base.tag) { .c => unreachable, .spirv => unreachable, - .nvptx => unreachable, .wasm => unreachable, .goff, .xcoff => unreachable, inline else => |tag| { @@ -921,7 +919,6 @@ pub const File = struct { switch (base.tag) { .c => unreachable, .spirv => unreachable, - .nvptx => unreachable, .wasm => unreachable, .goff, .xcoff => unreachable, inline else => |tag| { @@ -935,7 +932,6 @@ pub const File = struct { switch (base.tag) { .c => unreachable, .spirv => unreachable, - .nvptx => unreachable, .wasm => unreachable, .goff, .xcoff => unreachable, inline else => |tag| { @@ -953,7 +949,6 @@ pub const File = struct { switch (base.tag) { .plan9, .spirv, - .nvptx, .goff, .xcoff, => {}, @@ -1251,7 +1246,6 @@ pub const File = struct { wasm, spirv, plan9, - nvptx, goff, xcoff, @@ -1264,7 +1258,6 @@ pub const File = struct { .wasm => Wasm, .spirv => SpirV, .plan9 => Plan9, - .nvptx => NvPtx, .goff => Goff, .xcoff => Xcoff, }; @@ -1279,7 +1272,6 @@ pub const File = struct { .plan9 => .plan9, .c => .c, .spirv => .spirv, - .nvptx => .nvptx, .goff => .goff, .xcoff => .xcoff, .hex => @panic("TODO implement hex object format"), @@ -1386,7 +1378,6 @@ pub const File = struct { pub const MachO = @import("link/MachO.zig"); pub const SpirV = @import("link/SpirV.zig"); pub const Wasm = @import("link/Wasm.zig"); - pub const NvPtx = @import("link/NvPtx.zig"); pub const Goff = @import("link/Goff.zig"); pub const Xcoff = @import("link/Xcoff.zig"); pub const Dwarf = @import("link/Dwarf.zig"); diff --git a/src/link/NvPtx.zig b/src/link/NvPtx.zig deleted file mode 100644 index df2467ce4438dde89e046e6c615bc90340c48725..0000000000000000000000000000000000000000 --- a/src/link/NvPtx.zig +++ /dev/null @@ -1,123 +0,0 @@ -//! NVidia PTX (Parallel Thread Execution) -//! https://docs.nvidia.com/cuda/parallel-thread-execution/index.html -//! For this we rely on the nvptx backend of LLVM -//! Kernel functions need to be marked both as "export" and "callconv(.kernel)" - -const NvPtx = @This(); - -const std = @import("std"); -const builtin = @import("builtin"); - -const Allocator = std.mem.Allocator; -const assert = std.debug.assert; -const log = std.log.scoped(.link); -const Path = std.Build.Cache.Path; - -const Zcu = @import("../Zcu.zig"); -const InternPool = @import("../InternPool.zig"); -const Compilation = @import("../Compilation.zig"); -const link = @import("../link.zig"); -const trace = @import("../tracy.zig").trace; -const build_options = @import("build_options"); -const Air = @import("../Air.zig"); -const Liveness = @import("../Liveness.zig"); -const LlvmObject = @import("../codegen/llvm.zig").Object; - -base: link.File, -llvm_object: LlvmObject.Ptr, - -pub fn createEmpty( - arena: Allocator, - comp: *Compilation, - emit: Path, - options: link.File.OpenOptions, -) !*NvPtx { - const target = comp.root_mod.resolved_target.result; - const use_lld = build_options.have_llvm and comp.config.use_lld; - const use_llvm = comp.config.use_llvm; - - assert(use_llvm); // Caught by Compilation.Config.resolve. - assert(!use_lld); // Caught by Compilation.Config.resolve. - assert(target.cpu.arch.isNvptx()); // Caught by Compilation.Config.resolve. - - switch (target.os.tag) { - // TODO: does it also work with nvcl ? - .cuda => {}, - else => return error.PtxArchNotSupported, - } - - const llvm_object = try LlvmObject.create(arena, comp); - const nvptx = try arena.create(NvPtx); - nvptx.* = .{ - .base = .{ - .tag = .nvptx, - .comp = comp, - .emit = emit, - .zcu_object_sub_path = emit.sub_path, - .gc_sections = options.gc_sections orelse false, - .print_gc_sections = options.print_gc_sections, - .stack_size = options.stack_size orelse 0, - .allow_shlib_undefined = options.allow_shlib_undefined orelse false, - .file = null, - .disable_lld_caching = options.disable_lld_caching, - .build_id = options.build_id, - }, - .llvm_object = llvm_object, - }; - - return nvptx; -} - -pub fn open( - arena: Allocator, - comp: *Compilation, - emit: Path, - options: link.File.OpenOptions, -) !*NvPtx { - const target = comp.root_mod.resolved_target.result; - assert(target.ofmt == .nvptx); - return createEmpty(arena, comp, emit, options); -} - -pub fn deinit(self: *NvPtx) void { - self.llvm_object.deinit(); -} - -pub fn updateFunc( - self: *NvPtx, - pt: Zcu.PerThread, - func_index: InternPool.Index, - air: Air, - liveness: Liveness, -) link.File.UpdateNavError!void { - try self.llvm_object.updateFunc(pt, func_index, air, liveness); -} - -pub fn updateNav(self: *NvPtx, pt: Zcu.PerThread, nav: InternPool.Nav.Index) link.File.UpdateNavError!void { - return self.llvm_object.updateNav(pt, nav); -} - -pub fn updateExports( - self: *NvPtx, - pt: Zcu.PerThread, - exported: Zcu.Exported, - export_indices: []const Zcu.Export.Index, -) !void { - if (build_options.skip_non_native and builtin.object_format != .nvptx) - @panic("Attempted to compile for object format that was disabled by build configuration"); - - return self.llvm_object.updateExports(pt, exported, export_indices); -} - -pub fn flush(self: *NvPtx, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { - return self.flushModule(arena, tid, prog_node); -} - -pub fn flushModule(self: *NvPtx, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { - if (build_options.skip_non_native) - @panic("Attempted to compile for architecture that was disabled by build configuration"); - - _ = tid; - - try self.base.emitLlvmObject(arena, self.llvm_object, prog_node); -} diff --git a/src/target.zig b/src/target.zig index 6b67de4562b5c075874a2b52471d4ab2987c2464..03686d76878ec318e721798bd942778203901dc2 100644 --- a/src/target.zig +++ b/src/target.zig @@ -140,7 +140,6 @@ pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool { .goff, .hex, .macho, - .nvptx, .spirv, .raw, .wasm, -- 2.54.0