authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-01 21:14:57+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-05-10 12:21:57+02:00
log837e0f9c377d66f323926427551cc7db84a7151d
treeb855afbf64f9af7793c629b84131d1cca94b0dfb
parent2c241b263cf165cdde4b3b1df3ce551d915ee476

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.

9 files changed, 4 insertions(+), 147 deletions(-)

CMakeLists.txt-1
...@@ -640,7 +640,6 @@ set(ZIG_STAGE2_SOURCES...@@ -640,7 +640,6 @@ set(ZIG_STAGE2_SOURCES
640 src/link/MachO/synthetic.zig640 src/link/MachO/synthetic.zig
641 src/link/MachO/Thunk.zig641 src/link/MachO/Thunk.zig
642 src/link/MachO/uuid.zig642 src/link/MachO/uuid.zig
643 src/link/NvPtx.zig
644 src/link/Plan9.zig643 src/link/Plan9.zig
645 src/link/Plan9/aout.zig644 src/link/Plan9/aout.zig
646 src/link/SpirV.zig645 src/link/SpirV.zig
lib/std/Target.zig-4
...@@ -1017,8 +1017,6 @@ pub const ObjectFormat = enum {...@@ -1017,8 +1017,6 @@ pub const ObjectFormat = enum {
1017 hex,1017 hex,
1018 /// The Mach object format used by macOS and other Apple platforms.1018 /// The Mach object format used by macOS and other Apple platforms.
1019 macho,1019 macho,
1020 /// Nvidia's PTX (Parallel Thread Execution) assembly language.
1021 nvptx,
1022 /// The a.out format used by Plan 9 from Bell Labs.1020 /// The a.out format used by Plan 9 from Bell Labs.
1023 plan9,1021 plan9,
1024 /// Machine code with no metadata.1022 /// Machine code with no metadata.
...@@ -1039,7 +1037,6 @@ pub const ObjectFormat = enum {...@@ -1039,7 +1037,6 @@ pub const ObjectFormat = enum {
1039 .coff => ".obj",1037 .coff => ".obj",
1040 .elf, .goff, .macho, .wasm, .xcoff => ".o",1038 .elf, .goff, .macho, .wasm, .xcoff => ".o",
1041 .hex => ".ihex",1039 .hex => ".ihex",
1042 .nvptx => ".ptx",
1043 .plan9 => arch.plan9Ext(),1040 .plan9 => arch.plan9Ext(),
1044 .raw => ".bin",1041 .raw => ".bin",
1045 .spirv => ".spv",1042 .spirv => ".spv",
...@@ -1054,7 +1051,6 @@ pub const ObjectFormat = enum {...@@ -1054,7 +1051,6 @@ pub const ObjectFormat = enum {
1054 .uefi, .windows => .coff,1051 .uefi, .windows => .coff,
1055 .zos => .goff,1052 .zos => .goff,
1056 else => switch (arch) {1053 else => switch (arch) {
1057 .nvptx, .nvptx64 => .nvptx,
1058 .spirv, .spirv32, .spirv64 => .spirv,1054 .spirv, .spirv32, .spirv64 => .spirv,
1059 .wasm32, .wasm64 => .wasm,1055 .wasm32, .wasm64 => .wasm,
1060 else => .elf,1056 else => .elf,
lib/std/zig.zig-1
...@@ -232,7 +232,6 @@ pub fn binNameAlloc(allocator: Allocator, options: BinNameOptions) error{OutOfMe...@@ -232,7 +232,6 @@ pub fn binNameAlloc(allocator: Allocator, options: BinNameOptions) error{OutOfMe
232 t.libPrefix(), root_name,232 t.libPrefix(), root_name,
233 }),233 }),
234 },234 },
235 .nvptx => return std.fmt.allocPrint(allocator, "{s}.ptx", .{root_name}),
236 }235 }
237}236}
238237
src/Compilation/Config.zig+1-1
...@@ -434,7 +434,7 @@ pub fn resolve(options: Options) ResolveError!Config {...@@ -434,7 +434,7 @@ pub fn resolve(options: Options) ResolveError!Config {
434 .windows, .uefi => .code_view,434 .windows, .uefi => .code_view,
435 else => .{ .dwarf = .@"32" },435 else => .{ .dwarf = .@"32" },
436 },436 },
437 .spirv, .nvptx, .hex, .raw, .plan9 => .strip,437 .spirv, .hex, .raw, .plan9 => .strip,
438 };438 };
439 };439 };
440440
src/codegen.zig-2
...@@ -666,7 +666,6 @@ fn lowerUavRef(...@@ -666,7 +666,6 @@ fn lowerUavRef(
666 switch (lf.tag) {666 switch (lf.tag) {
667 .c => unreachable,667 .c => unreachable,
668 .spirv => unreachable,668 .spirv => unreachable,
669 .nvptx => unreachable,
670 .wasm => {669 .wasm => {
671 dev.check(link.File.Tag.wasm.devFeature());670 dev.check(link.File.Tag.wasm.devFeature());
672 const wasm = lf.cast(.wasm).?;671 const wasm = lf.cast(.wasm).?;
...@@ -739,7 +738,6 @@ fn lowerNavRef(...@@ -739,7 +738,6 @@ fn lowerNavRef(
739 switch (lf.tag) {738 switch (lf.tag) {
740 .c => unreachable,739 .c => unreachable,
741 .spirv => unreachable,740 .spirv => unreachable,
742 .nvptx => unreachable,
743 .wasm => {741 .wasm => {
744 dev.check(link.File.Tag.wasm.devFeature());742 dev.check(link.File.Tag.wasm.devFeature());
745 const wasm = lf.cast(.wasm).?;743 const wasm = lf.cast(.wasm).?;
src/dev.zig-2
...@@ -81,7 +81,6 @@ pub const Env = enum {...@@ -81,7 +81,6 @@ pub const Env = enum {
81 .wasm_linker,81 .wasm_linker,
82 .spirv_linker,82 .spirv_linker,
83 .plan9_linker,83 .plan9_linker,
84 .nvptx_linker,
85 .goff_linker,84 .goff_linker,
86 .xcoff_linker,85 .xcoff_linker,
87 => true,86 => true,
...@@ -229,7 +228,6 @@ pub const Feature = enum {...@@ -229,7 +228,6 @@ pub const Feature = enum {
229 wasm_linker,228 wasm_linker,
230 spirv_linker,229 spirv_linker,
231 plan9_linker,230 plan9_linker,
232 nvptx_linker,
233 goff_linker,231 goff_linker,
234 xcoff_linker,232 xcoff_linker,
235};233};
src/link.zig+3-12
...@@ -597,7 +597,7 @@ pub const File = struct {...@@ -597,7 +597,7 @@ pub const File = struct {
597 .mode = determineMode(use_lld, output_mode, link_mode),597 .mode = determineMode(use_lld, output_mode, link_mode),
598 });598 });
599 },599 },
600 .c, .spirv, .nvptx => dev.checkAny(&.{ .c_linker, .spirv_linker, .nvptx_linker }),600 .c, .spirv => dev.checkAny(&.{ .c_linker, .spirv_linker }),
601 }601 }
602 }602 }
603603
...@@ -670,7 +670,7 @@ pub const File = struct {...@@ -670,7 +670,7 @@ pub const File = struct {
670 }670 }
671 }671 }
672 },672 },
673 .c, .spirv, .nvptx => dev.checkAny(&.{ .c_linker, .spirv_linker, .nvptx_linker }),673 .c, .spirv => dev.checkAny(&.{ .c_linker, .spirv_linker }),
674 }674 }
675 }675 }
676676
...@@ -697,7 +697,6 @@ pub const File = struct {...@@ -697,7 +697,6 @@ pub const File = struct {
697 .plan9 => unreachable,697 .plan9 => unreachable,
698 .spirv => unreachable,698 .spirv => unreachable,
699 .c => unreachable,699 .c => unreachable,
700 .nvptx => unreachable,
701 inline else => |tag| {700 inline else => |tag| {
702 dev.check(tag.devFeature());701 dev.check(tag.devFeature());
703 return @as(*tag.Type(), @fieldParentPtr("base", base)).getGlobalSymbol(name, lib_name);702 return @as(*tag.Type(), @fieldParentPtr("base", base)).getGlobalSymbol(name, lib_name);
...@@ -766,7 +765,7 @@ pub const File = struct {...@@ -766,7 +765,7 @@ pub const File = struct {
766 }765 }
767766
768 switch (base.tag) {767 switch (base.tag) {
769 .spirv, .nvptx => {},768 .spirv => {},
770 .goff, .xcoff => {},769 .goff, .xcoff => {},
771 inline else => |tag| {770 inline else => |tag| {
772 dev.check(tag.devFeature());771 dev.check(tag.devFeature());
...@@ -901,7 +900,6 @@ pub const File = struct {...@@ -901,7 +900,6 @@ pub const File = struct {
901 switch (base.tag) {900 switch (base.tag) {
902 .c => unreachable,901 .c => unreachable,
903 .spirv => unreachable,902 .spirv => unreachable,
904 .nvptx => unreachable,
905 .wasm => unreachable,903 .wasm => unreachable,
906 .goff, .xcoff => unreachable,904 .goff, .xcoff => unreachable,
907 inline else => |tag| {905 inline else => |tag| {
...@@ -921,7 +919,6 @@ pub const File = struct {...@@ -921,7 +919,6 @@ pub const File = struct {
921 switch (base.tag) {919 switch (base.tag) {
922 .c => unreachable,920 .c => unreachable,
923 .spirv => unreachable,921 .spirv => unreachable,
924 .nvptx => unreachable,
925 .wasm => unreachable,922 .wasm => unreachable,
926 .goff, .xcoff => unreachable,923 .goff, .xcoff => unreachable,
927 inline else => |tag| {924 inline else => |tag| {
...@@ -935,7 +932,6 @@ pub const File = struct {...@@ -935,7 +932,6 @@ pub const File = struct {
935 switch (base.tag) {932 switch (base.tag) {
936 .c => unreachable,933 .c => unreachable,
937 .spirv => unreachable,934 .spirv => unreachable,
938 .nvptx => unreachable,
939 .wasm => unreachable,935 .wasm => unreachable,
940 .goff, .xcoff => unreachable,936 .goff, .xcoff => unreachable,
941 inline else => |tag| {937 inline else => |tag| {
...@@ -953,7 +949,6 @@ pub const File = struct {...@@ -953,7 +949,6 @@ pub const File = struct {
953 switch (base.tag) {949 switch (base.tag) {
954 .plan9,950 .plan9,
955 .spirv,951 .spirv,
956 .nvptx,
957 .goff,952 .goff,
958 .xcoff,953 .xcoff,
959 => {},954 => {},
...@@ -1251,7 +1246,6 @@ pub const File = struct {...@@ -1251,7 +1246,6 @@ pub const File = struct {
1251 wasm,1246 wasm,
1252 spirv,1247 spirv,
1253 plan9,1248 plan9,
1254 nvptx,
1255 goff,1249 goff,
1256 xcoff,1250 xcoff,
12571251
...@@ -1264,7 +1258,6 @@ pub const File = struct {...@@ -1264,7 +1258,6 @@ pub const File = struct {
1264 .wasm => Wasm,1258 .wasm => Wasm,
1265 .spirv => SpirV,1259 .spirv => SpirV,
1266 .plan9 => Plan9,1260 .plan9 => Plan9,
1267 .nvptx => NvPtx,
1268 .goff => Goff,1261 .goff => Goff,
1269 .xcoff => Xcoff,1262 .xcoff => Xcoff,
1270 };1263 };
...@@ -1279,7 +1272,6 @@ pub const File = struct {...@@ -1279,7 +1272,6 @@ pub const File = struct {
1279 .plan9 => .plan9,1272 .plan9 => .plan9,
1280 .c => .c,1273 .c => .c,
1281 .spirv => .spirv,1274 .spirv => .spirv,
1282 .nvptx => .nvptx,
1283 .goff => .goff,1275 .goff => .goff,
1284 .xcoff => .xcoff,1276 .xcoff => .xcoff,
1285 .hex => @panic("TODO implement hex object format"),1277 .hex => @panic("TODO implement hex object format"),
...@@ -1386,7 +1378,6 @@ pub const File = struct {...@@ -1386,7 +1378,6 @@ pub const File = struct {
1386 pub const MachO = @import("link/MachO.zig");1378 pub const MachO = @import("link/MachO.zig");
1387 pub const SpirV = @import("link/SpirV.zig");1379 pub const SpirV = @import("link/SpirV.zig");
1388 pub const Wasm = @import("link/Wasm.zig");1380 pub const Wasm = @import("link/Wasm.zig");
1389 pub const NvPtx = @import("link/NvPtx.zig");
1390 pub const Goff = @import("link/Goff.zig");1381 pub const Goff = @import("link/Goff.zig");
1391 pub const Xcoff = @import("link/Xcoff.zig");1382 pub const Xcoff = @import("link/Xcoff.zig");
1392 pub const Dwarf = @import("link/Dwarf.zig");1383 pub const Dwarf = @import("link/Dwarf.zig");
src/link/NvPtx.zig deleted-123
...@@ -1,123 +0,0 @@
1//! NVidia PTX (Parallel Thread Execution)
2//! https://docs.nvidia.com/cuda/parallel-thread-execution/index.html
3//! For this we rely on the nvptx backend of LLVM
4//! Kernel functions need to be marked both as "export" and "callconv(.kernel)"
5
6const NvPtx = @This();
7
8const std = @import("std");
9const builtin = @import("builtin");
10
11const Allocator = std.mem.Allocator;
12const assert = std.debug.assert;
13const log = std.log.scoped(.link);
14const Path = std.Build.Cache.Path;
15
16const Zcu = @import("../Zcu.zig");
17const InternPool = @import("../InternPool.zig");
18const Compilation = @import("../Compilation.zig");
19const link = @import("../link.zig");
20const trace = @import("../tracy.zig").trace;
21const build_options = @import("build_options");
22const Air = @import("../Air.zig");
23const Liveness = @import("../Liveness.zig");
24const LlvmObject = @import("../codegen/llvm.zig").Object;
25
26base: link.File,
27llvm_object: LlvmObject.Ptr,
28
29pub fn createEmpty(
30 arena: Allocator,
31 comp: *Compilation,
32 emit: Path,
33 options: link.File.OpenOptions,
34) !*NvPtx {
35 const target = comp.root_mod.resolved_target.result;
36 const use_lld = build_options.have_llvm and comp.config.use_lld;
37 const use_llvm = comp.config.use_llvm;
38
39 assert(use_llvm); // Caught by Compilation.Config.resolve.
40 assert(!use_lld); // Caught by Compilation.Config.resolve.
41 assert(target.cpu.arch.isNvptx()); // Caught by Compilation.Config.resolve.
42
43 switch (target.os.tag) {
44 // TODO: does it also work with nvcl ?
45 .cuda => {},
46 else => return error.PtxArchNotSupported,
47 }
48
49 const llvm_object = try LlvmObject.create(arena, comp);
50 const nvptx = try arena.create(NvPtx);
51 nvptx.* = .{
52 .base = .{
53 .tag = .nvptx,
54 .comp = comp,
55 .emit = emit,
56 .zcu_object_sub_path = emit.sub_path,
57 .gc_sections = options.gc_sections orelse false,
58 .print_gc_sections = options.print_gc_sections,
59 .stack_size = options.stack_size orelse 0,
60 .allow_shlib_undefined = options.allow_shlib_undefined orelse false,
61 .file = null,
62 .disable_lld_caching = options.disable_lld_caching,
63 .build_id = options.build_id,
64 },
65 .llvm_object = llvm_object,
66 };
67
68 return nvptx;
69}
70
71pub fn open(
72 arena: Allocator,
73 comp: *Compilation,
74 emit: Path,
75 options: link.File.OpenOptions,
76) !*NvPtx {
77 const target = comp.root_mod.resolved_target.result;
78 assert(target.ofmt == .nvptx);
79 return createEmpty(arena, comp, emit, options);
80}
81
82pub fn deinit(self: *NvPtx) void {
83 self.llvm_object.deinit();
84}
85
86pub fn updateFunc(
87 self: *NvPtx,
88 pt: Zcu.PerThread,
89 func_index: InternPool.Index,
90 air: Air,
91 liveness: Liveness,
92) link.File.UpdateNavError!void {
93 try self.llvm_object.updateFunc(pt, func_index, air, liveness);
94}
95
96pub fn updateNav(self: *NvPtx, pt: Zcu.PerThread, nav: InternPool.Nav.Index) link.File.UpdateNavError!void {
97 return self.llvm_object.updateNav(pt, nav);
98}
99
100pub fn updateExports(
101 self: *NvPtx,
102 pt: Zcu.PerThread,
103 exported: Zcu.Exported,
104 export_indices: []const Zcu.Export.Index,
105) !void {
106 if (build_options.skip_non_native and builtin.object_format != .nvptx)
107 @panic("Attempted to compile for object format that was disabled by build configuration");
108
109 return self.llvm_object.updateExports(pt, exported, export_indices);
110}
111
112pub fn flush(self: *NvPtx, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void {
113 return self.flushModule(arena, tid, prog_node);
114}
115
116pub fn flushModule(self: *NvPtx, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void {
117 if (build_options.skip_non_native)
118 @panic("Attempted to compile for architecture that was disabled by build configuration");
119
120 _ = tid;
121
122 try self.base.emitLlvmObject(arena, self.llvm_object, prog_node);
123}
src/target.zig-1
...@@ -140,7 +140,6 @@ pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool {...@@ -140,7 +140,6 @@ pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool {
140 .goff,140 .goff,
141 .hex,141 .hex,
142 .macho,142 .macho,
143 .nvptx,
144 .spirv,143 .spirv,
145 .raw,144 .raw,
146 .wasm,145 .wasm,