From aad983cf40dad209ccc79b1e5ef4531e1b4d4ca7 Mon Sep 17 00:00:00 2001 From: Guillaume Wenzek Date: Fri, 16 Sep 2022 22:21:14 +0200 Subject: [PATCH] sanitize qualified name for nvptx backend --- lib/std/target.zig | 7 +++++++ src/Module.zig | 9 +++++++++ src/link/NvPtx.zig | 20 ++++---------------- src/target.zig | 13 ++++++------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/lib/std/target.zig b/lib/std/target.zig index 201fac222c..5deba28d2c 100644 --- a/lib/std/target.zig +++ b/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)) { diff --git a/src/Module.zig b/src/Module.zig index 7d87bdba53..fd5cf29516 100644 --- a/src/Module.zig +++ b/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); } diff --git a/src/link/NvPtx.zig b/src/link/NvPtx.zig index 501575fafb..4873511d55 100644 --- a/src/link/NvPtx.zig +++ b/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; diff --git a/src/target.zig b/src/target.zig index 2c05a80258..01db8555da 100644 --- a/src/target.zig +++ b/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 {