authorgravatar for gwenzek@users.noreply.github.comGuillaume Wenzek <gwenzek@users.noreply.github.com> 2022-09-16 22:21:14+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-10-15 10:39:19-07:00
logaad983cf40dad209ccc79b1e5ef4531e1b4d4ca7
treefc56b5007f1a66e8b1e693e33518a9c937140435
parent92a857b76c9a6ff7b885b623ae86844ca77ed646

sanitize qualified name for nvptx backend


4 files changed, 26 insertions(+), 23 deletions(-)

lib/std/target.zig+7
...@@ -951,6 +951,13 @@ pub const Target = struct {...@@ -951,6 +951,13 @@ pub const Target = struct {
951 };951 };
952 }952 }
953953
954 pub fn isNvptx(arch: Arch) bool {
955 return switch (arch) {
956 .nvptx, .nvptx64 => true,
957 else => false,
958 };
959 }
960
954 pub fn parseCpuModel(arch: Arch, cpu_name: []const u8) !*const Cpu.Model {961 pub fn parseCpuModel(arch: Arch, cpu_name: []const u8) !*const Cpu.Model {
955 for (arch.allCpuModels()) |cpu| {962 for (arch.allCpuModels()) |cpu| {
956 if (mem.eql(u8, cpu_name, cpu.name)) {963 if (mem.eql(u8, cpu_name, cpu.name)) {
src/Module.zig+9
...@@ -720,6 +720,15 @@ pub const Decl = struct {...@@ -720,6 +720,15 @@ pub const Decl = struct {
720 var buffer = std.ArrayList(u8).init(mod.gpa);720 var buffer = std.ArrayList(u8).init(mod.gpa);
721 defer buffer.deinit();721 defer buffer.deinit();
722 try decl.renderFullyQualifiedName(mod, buffer.writer());722 try decl.renderFullyQualifiedName(mod, buffer.writer());
723
724 // Sanitize the name for nvptx which is more restrictive.
725 if (mod.comp.bin_file.options.target.cpu.arch.isNvptx()) {
726 for (buffer.items) |*byte| switch (byte.*) {
727 '{', '}', '*', '[', ']', '(', ')', ',', ' ', '\'' => byte.* = '_',
728 else => {},
729 };
730 }
731
723 return buffer.toOwnedSliceSentinel(0);732 return buffer.toOwnedSliceSentinel(0);
724 }733 }
725734
src/link/NvPtx.zig+4-16
...@@ -28,10 +28,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*NvPtx {...@@ -28,10 +28,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*NvPtx {
28 if (!build_options.have_llvm) return error.PtxArchNotSupported;28 if (!build_options.have_llvm) return error.PtxArchNotSupported;
29 if (!options.use_llvm) return error.PtxArchNotSupported;29 if (!options.use_llvm) return error.PtxArchNotSupported;
3030
31 switch (options.target.cpu.arch) {31 if (!options.target.cpu.arch.isNvptx()) return error.PtxArchNotSupported;
32 .nvptx, .nvptx64 => {},
33 else => return error.PtxArchNotSupported,
34 }
3532
36 switch (options.target.os.tag) {33 switch (options.target.os.tag) {
37 // TODO: does it also work with nvcl ?34 // TODO: does it also work with nvcl ?
...@@ -59,9 +56,8 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option...@@ -59,9 +56,8 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
59 if (!options.use_llvm) return error.PtxArchNotSupported;56 if (!options.use_llvm) return error.PtxArchNotSupported;
60 assert(options.target.ofmt == .nvptx);57 assert(options.target.ofmt == .nvptx);
6158
62 const nvptx = try createEmpty(allocator, options);59 log.debug("Opening .ptx target file {s}", .{sub_path});
63 log.info("Opening .ptx target file {s}", .{sub_path});60 return createEmpty(allocator, options);
64 return nvptx;
65}61}
6662
67pub fn deinit(self: *NvPtx) void {63pub fn deinit(self: *NvPtx) void {
...@@ -76,15 +72,7 @@ pub fn updateFunc(self: *NvPtx, module: *Module, func: *Module.Fn, air: Air, liv...@@ -76,15 +72,7 @@ pub fn updateFunc(self: *NvPtx, module: *Module, func: *Module.Fn, air: Air, liv
7672
77pub fn updateDecl(self: *NvPtx, module: *Module, decl_index: Module.Decl.Index) !void {73pub fn updateDecl(self: *NvPtx, module: *Module, decl_index: Module.Decl.Index) !void {
78 if (!build_options.have_llvm) return;74 if (!build_options.have_llvm) return;
79 const decl = module.declPtr(decl_index);
80 log.info("updating {s}", .{decl.name});
81 return self.llvm_object.updateDecl(module, decl_index);75 return self.llvm_object.updateDecl(module, decl_index);
82 // const decl_index = func.owner_decl;
83 // const decl = module.declPtr(decl_index);
84
85 // try mod.decl_exports.ensureUnusedCapacity(gpa, 1);
86 // try mod.export_owners.ensureUnusedCapacity(gpa, 1);
87 // mod.decl_exports.getOrPutAssumeCapacity(exported_decl_index);
88}76}
8977
90pub fn updateDeclExports(78pub fn updateDeclExports(
...@@ -118,7 +106,7 @@ pub fn flushModule(self: *NvPtx, comp: *Compilation, prog_node: *std.Progress.No...@@ -118,7 +106,7 @@ pub fn flushModule(self: *NvPtx, comp: *Compilation, prog_node: *std.Progress.No
118 defer tracy.end();106 defer tracy.end();
119107
120 const outfile = comp.bin_file.options.emit.?;108 const outfile = comp.bin_file.options.emit.?;
121 // !!! We modify 'comp' before passing it to LLVM, but restore value afterwards109 // We modify 'comp' before passing it to LLVM, but restore value afterwards.
122 // We tell LLVM to not try to build a .o, only an "assembly" file.110 // We tell LLVM to not try to build a .o, only an "assembly" file.
123 // This is required by the LLVM PTX backend.111 // This is required by the LLVM PTX backend.
124 comp.bin_file.options.emit = null;112 comp.bin_file.options.emit = null;
src/target.zig+6-7
...@@ -411,13 +411,12 @@ pub fn classifyCompilerRtLibName(target: std.Target, name: []const u8) CompilerR...@@ -411,13 +411,12 @@ pub fn classifyCompilerRtLibName(target: std.Target, name: []const u8) CompilerR
411}411}
412412
413pub fn hasDebugInfo(target: std.Target) bool {413pub fn hasDebugInfo(target: std.Target) bool {
414 return switch (target.cpu.arch) {414 if (target.cpu.arch.isNvptx()) {
415 .nvptx, .nvptx64 => {415 // TODO: not sure how to test "ptx >= 7.5" with featureset
416 // TODO: not sure to test "ptx >= 7.5" with featureset416 return std.Target.nvptx.featureSetHas(target.cpu.features, .ptx75);
417 return std.Target.nvptx.featureSetHas(target.cpu.features, .ptx75);417 }
418 },418
419 else => true419 return true;
420 };
421}420}
422421
423pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.Mode {422pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.Mode {