Commit aad983cf40

Guillaume Wenzek <gwenzek@users.noreply.github.com>
2022-09-16 22:21:14
sanitize qualified name for nvptx backend
1 parent 92a857b
Changed files (4)
lib/std/target.zig
@@ -951,6 +951,13 @@ pub const Target = struct {
                 };
             }
 
+            pub fn isNvptx(arch: Arch) bool {
+                return switch (arch) {
+                    .nvptx, .nvptx64 => true,
+                    else => false,
+                };
+            }
+
             pub fn parseCpuModel(arch: Arch, cpu_name: []const u8) !*const Cpu.Model {
                 for (arch.allCpuModels()) |cpu| {
                     if (mem.eql(u8, cpu_name, cpu.name)) {
src/link/NvPtx.zig
@@ -28,10 +28,7 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*NvPtx {
     if (!build_options.have_llvm) return error.PtxArchNotSupported;
     if (!options.use_llvm) return error.PtxArchNotSupported;
 
-    switch (options.target.cpu.arch) {
-        .nvptx, .nvptx64 => {},
-        else => return error.PtxArchNotSupported,
-    }
+    if (!options.target.cpu.arch.isNvptx()) return error.PtxArchNotSupported;
 
     switch (options.target.os.tag) {
         // TODO: does it also work with nvcl ?
@@ -59,9 +56,8 @@ pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Option
     if (!options.use_llvm) return error.PtxArchNotSupported;
     assert(options.target.ofmt == .nvptx);
 
-    const nvptx = try createEmpty(allocator, options);
-    log.info("Opening .ptx target file {s}", .{sub_path});
-    return nvptx;
+    log.debug("Opening .ptx target file {s}", .{sub_path});
+    return createEmpty(allocator, options);
 }
 
 pub fn deinit(self: *NvPtx) void {
@@ -76,15 +72,7 @@ pub fn updateFunc(self: *NvPtx, module: *Module, func: *Module.Fn, air: Air, liv
 
 pub fn updateDecl(self: *NvPtx, module: *Module, decl_index: Module.Decl.Index) !void {
     if (!build_options.have_llvm) return;
-    const decl = module.declPtr(decl_index);
-    log.info("updating {s}", .{decl.name});
     return self.llvm_object.updateDecl(module, decl_index);
-    // const decl_index = func.owner_decl;
-    // const decl = module.declPtr(decl_index);
-
-    // try mod.decl_exports.ensureUnusedCapacity(gpa, 1);
-    // try mod.export_owners.ensureUnusedCapacity(gpa, 1);
-    // mod.decl_exports.getOrPutAssumeCapacity(exported_decl_index);
 }
 
 pub fn updateDeclExports(
@@ -118,7 +106,7 @@ pub fn flushModule(self: *NvPtx, comp: *Compilation, prog_node: *std.Progress.No
     defer tracy.end();
 
     const outfile = comp.bin_file.options.emit.?;
-    // !!! We modify 'comp' before passing it to LLVM, but restore value afterwards
+    // We modify 'comp' before passing it to LLVM, but restore value afterwards.
     // We tell LLVM to not try to build a .o, only an "assembly" file.
     // This is required by the LLVM PTX backend.
     comp.bin_file.options.emit = null;
src/Module.zig
@@ -720,6 +720,15 @@ pub const Decl = struct {
         var buffer = std.ArrayList(u8).init(mod.gpa);
         defer buffer.deinit();
         try decl.renderFullyQualifiedName(mod, buffer.writer());
+
+        // Sanitize the name for nvptx which is more restrictive.
+        if (mod.comp.bin_file.options.target.cpu.arch.isNvptx()) {
+            for (buffer.items) |*byte| switch (byte.*) {
+                '{', '}', '*', '[', ']', '(', ')', ',', ' ', '\'' => byte.* = '_',
+                else => {},
+            };
+        }
+
         return buffer.toOwnedSliceSentinel(0);
     }
 
src/target.zig
@@ -411,13 +411,12 @@ pub fn classifyCompilerRtLibName(target: std.Target, name: []const u8) CompilerR
 }
 
 pub fn hasDebugInfo(target: std.Target) bool {
-    return switch (target.cpu.arch) {
-        .nvptx, .nvptx64 => {
-            // TODO: not sure to test "ptx >= 7.5" with featureset
-            return std.Target.nvptx.featureSetHas(target.cpu.features, .ptx75);
-        },
-        else => true
-    };
+    if (target.cpu.arch.isNvptx()) {
+        // TODO: not sure how to test "ptx >= 7.5" with featureset
+        return std.Target.nvptx.featureSetHas(target.cpu.features, .ptx75);
+    }
+
+    return true;
 }
 
 pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.Mode {