Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stage2 support for Nvptx target #10189

Merged
merged 2 commits into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub const CallingConvention = enum {
AAPCS,
AAPCSVFP,
SysV,
PtxKernel,
};

/// This data structure is used by the Zig language code generation and
Expand Down
4 changes: 4 additions & 0 deletions lib/std/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ pub const Target = struct {
raw,
/// Plan 9 from Bell Labs
plan9,
/// Nvidia PTX format
nvptx,

pub fn fileExt(of: ObjectFormat, cpu_arch: Cpu.Arch) [:0]const u8 {
return switch (of) {
Expand All @@ -589,6 +591,7 @@ pub const Target = struct {
.hex => ".ihex",
.raw => ".bin",
.plan9 => plan9Ext(cpu_arch),
.nvptx => ".ptx",
};
}
};
Expand Down Expand Up @@ -1391,6 +1394,7 @@ pub const Target = struct {
else => return switch (cpu_arch) {
.wasm32, .wasm64 => .wasm,
.spirv32, .spirv64 => .spirv,
.nvptx, .nvptx64 => .nvptx,
else => .elf,
},
};
Expand Down
1 change: 1 addition & 0 deletions lib/std/zig.zig
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ pub fn binNameAlloc(allocator: std.mem.Allocator, options: BinNameOptions) error
.Obj => return std.fmt.allocPrint(allocator, "{s}{s}", .{ root_name, ofmt.fileExt(target.cpu.arch) }),
.Lib => return std.fmt.allocPrint(allocator, "{s}{s}.a", .{ target.libPrefix(), root_name }),
},
.nvptx => return std.fmt.allocPrint(allocator, "{s}", .{root_name}),
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4163,7 +4163,7 @@ fn scanDecl(iter: *ScanDeclIter, decl_sub_index: usize, flags: u4) SemaError!voi
// in `Decl` to notice that the line number did not change.
mod.comp.work_queue.writeItemAssumeCapacity(.{ .update_line_number = decl });
},
.c, .wasm, .spirv => {},
.c, .wasm, .spirv, .nvptx => {},
}
}
}
Expand Down Expand Up @@ -4237,6 +4237,7 @@ pub fn clearDecl(
.c => .{ .c = {} },
.wasm => .{ .wasm = link.File.Wasm.DeclBlock.empty },
.spirv => .{ .spirv = {} },
.nvptx => .{ .nvptx = {} },
};
decl.fn_link = switch (mod.comp.bin_file.tag) {
.coff => .{ .coff = {} },
Expand All @@ -4246,6 +4247,7 @@ pub fn clearDecl(
.c => .{ .c = {} },
.wasm => .{ .wasm = link.File.Wasm.FnData.empty },
.spirv => .{ .spirv = .{} },
.nvptx => .{ .nvptx = .{} },
};
}
if (decl.getInnerNamespace()) |namespace| {
Expand Down Expand Up @@ -4573,6 +4575,7 @@ pub fn allocateNewDecl(
.c => .{ .c = {} },
.wasm => .{ .wasm = link.File.Wasm.DeclBlock.empty },
.spirv => .{ .spirv = {} },
.nvptx => .{ .nvptx = {} },
},
.fn_link = switch (mod.comp.bin_file.tag) {
.coff => .{ .coff = {} },
Expand All @@ -4582,6 +4585,7 @@ pub fn allocateNewDecl(
.c => .{ .c = {} },
.wasm => .{ .wasm = link.File.Wasm.FnData.empty },
.spirv => .{ .spirv = .{} },
.nvptx => .{ .nvptx = .{} },
},
.generation = 0,
.is_pub = false,
Expand Down
1 change: 1 addition & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3704,6 +3704,7 @@ pub fn analyzeExport(
.c => .{ .c = {} },
.wasm => .{ .wasm = {} },
.spirv => .{ .spirv = {} },
.nvptx => .{ .nvptx = {} },
},
.owner_decl = owner_decl,
.src_decl = src_decl,
Expand Down
6 changes: 5 additions & 1 deletion src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ pub const Object = struct {
const mod = comp.bin_file.options.module.?;
const cache_dir = mod.zig_cache_artifact_directory;

const emit_bin_path: ?[*:0]const u8 = if (comp.bin_file.options.emit) |emit|
var emit_bin_path: ?[*:0]const u8 = if (comp.bin_file.options.emit) |emit|
try emit.basenamePath(arena, try arena.dupeZ(u8, comp.bin_file.intermediary_basename.?))
else
null;
Expand Down Expand Up @@ -5049,6 +5049,10 @@ fn toLlvmCallConv(cc: std.builtin.CallingConvention, target: std.Target) llvm.Ca
},
.Signal => .AVR_SIGNAL,
.SysV => .X86_64_SysV,
.PtxKernel => return switch (target.cpu.arch) {
.nvptx, .nvptx64 => .PTX_Kernel,
else => unreachable,
},
};
}

Expand Down
30 changes: 25 additions & 5 deletions src/link.zig
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ pub const File = struct {
c: void,
wasm: Wasm.DeclBlock,
spirv: void,
nvptx: void,
};

pub const LinkFn = union {
Expand All @@ -222,6 +223,7 @@ pub const File = struct {
c: void,
wasm: Wasm.FnData,
spirv: SpirV.FnData,
nvptx: void,
};

pub const Export = union {
Expand All @@ -232,6 +234,7 @@ pub const File = struct {
c: void,
wasm: void,
spirv: void,
nvptx: void,
};

/// For DWARF .debug_info.
Expand Down Expand Up @@ -266,6 +269,7 @@ pub const File = struct {
.plan9 => return &(try Plan9.createEmpty(allocator, options)).base,
.c => unreachable, // Reported error earlier.
.spirv => &(try SpirV.createEmpty(allocator, options)).base,
.nvptx => &(try NvPtx.createEmpty(allocator, options)).base,
.hex => return error.HexObjectFormatUnimplemented,
.raw => return error.RawObjectFormatUnimplemented,
};
Expand All @@ -284,6 +288,7 @@ pub const File = struct {
.wasm => &(try Wasm.createEmpty(allocator, options)).base,
.c => unreachable, // Reported error earlier.
.spirv => &(try SpirV.createEmpty(allocator, options)).base,
.nvptx => &(try NvPtx.createEmpty(allocator, options)).base,
.hex => return error.HexObjectFormatUnimplemented,
.raw => return error.RawObjectFormatUnimplemented,
};
Expand All @@ -304,6 +309,7 @@ pub const File = struct {
.wasm => &(try Wasm.openPath(allocator, sub_path, options)).base,
.c => &(try C.openPath(allocator, sub_path, options)).base,
.spirv => &(try SpirV.openPath(allocator, sub_path, options)).base,
.nvptx => &(try NvPtx.openPath(allocator, sub_path, options)).base,
.hex => return error.HexObjectFormatUnimplemented,
.raw => return error.RawObjectFormatUnimplemented,
};
Expand Down Expand Up @@ -336,7 +342,7 @@ pub const File = struct {
.mode = determineMode(base.options),
});
},
.c, .wasm, .spirv => {},
.c, .wasm, .spirv, .nvptx => {},
}
}

Expand Down Expand Up @@ -381,7 +387,7 @@ pub const File = struct {
f.close();
base.file = null;
},
.c, .wasm, .spirv => {},
.c, .wasm, .spirv, .nvptx => {},
}
}

Expand Down Expand Up @@ -429,6 +435,7 @@ pub const File = struct {
.wasm => return @fieldParentPtr(Wasm, "base", base).updateDecl(module, decl),
.spirv => return @fieldParentPtr(SpirV, "base", base).updateDecl(module, decl),
.plan9 => return @fieldParentPtr(Plan9, "base", base).updateDecl(module, decl),
.nvptx => return @fieldParentPtr(NvPtx, "base", base).updateDecl(module, decl),
// zig fmt: on
}
}
Expand All @@ -448,6 +455,7 @@ pub const File = struct {
.wasm => return @fieldParentPtr(Wasm, "base", base).updateFunc(module, func, air, liveness),
.spirv => return @fieldParentPtr(SpirV, "base", base).updateFunc(module, func, air, liveness),
.plan9 => return @fieldParentPtr(Plan9, "base", base).updateFunc(module, func, air, liveness),
.nvptx => return @fieldParentPtr(NvPtx, "base", base).updateFunc(module, func, air, liveness),
// zig fmt: on
}
}
Expand All @@ -463,7 +471,7 @@ pub const File = struct {
.macho => return @fieldParentPtr(MachO, "base", base).updateDeclLineNumber(module, decl),
.c => return @fieldParentPtr(C, "base", base).updateDeclLineNumber(module, decl),
.plan9 => @panic("TODO: implement updateDeclLineNumber for plan9"),
.wasm, .spirv => {},
.wasm, .spirv, .nvptx => {},
}
}

Expand All @@ -485,7 +493,7 @@ pub const File = struct {
},
.wasm => return @fieldParentPtr(Wasm, "base", base).allocateDeclIndexes(decl),
.plan9 => return @fieldParentPtr(Plan9, "base", base).allocateDeclIndexes(decl),
.c, .spirv => {},
.c, .spirv, .nvptx => {},
}
}

Expand Down Expand Up @@ -543,6 +551,11 @@ pub const File = struct {
parent.deinit();
base.allocator.destroy(parent);
},
.nvptx => {
const parent = @fieldParentPtr(NvPtx, "base", base);
parent.deinit();
base.allocator.destroy(parent);
},
}
}

Expand Down Expand Up @@ -576,6 +589,7 @@ pub const File = struct {
.wasm => return @fieldParentPtr(Wasm, "base", base).flush(comp),
.spirv => return @fieldParentPtr(SpirV, "base", base).flush(comp),
.plan9 => return @fieldParentPtr(Plan9, "base", base).flush(comp),
.nvptx => return @fieldParentPtr(NvPtx, "base", base).flush(comp),
}
}

Expand All @@ -590,6 +604,7 @@ pub const File = struct {
.wasm => return @fieldParentPtr(Wasm, "base", base).flushModule(comp),
.spirv => return @fieldParentPtr(SpirV, "base", base).flushModule(comp),
.plan9 => return @fieldParentPtr(Plan9, "base", base).flushModule(comp),
.nvptx => return @fieldParentPtr(NvPtx, "base", base).flushModule(comp),
}
}

Expand All @@ -604,6 +619,7 @@ pub const File = struct {
.wasm => @fieldParentPtr(Wasm, "base", base).freeDecl(decl),
.spirv => @fieldParentPtr(SpirV, "base", base).freeDecl(decl),
.plan9 => @fieldParentPtr(Plan9, "base", base).freeDecl(decl),
.nvptx => @fieldParentPtr(NvPtx, "base", base).freeDecl(decl),
}
}

Expand All @@ -614,7 +630,7 @@ pub const File = struct {
.macho => return @fieldParentPtr(MachO, "base", base).error_flags,
.plan9 => return @fieldParentPtr(Plan9, "base", base).error_flags,
.c => return .{ .no_entry_point_found = false },
.wasm, .spirv => return ErrorFlags{},
.wasm, .spirv, .nvptx => return ErrorFlags{},
}
}

Expand All @@ -636,6 +652,7 @@ pub const File = struct {
.wasm => return @fieldParentPtr(Wasm, "base", base).updateDeclExports(module, decl, exports),
.spirv => return @fieldParentPtr(SpirV, "base", base).updateDeclExports(module, decl, exports),
.plan9 => return @fieldParentPtr(Plan9, "base", base).updateDeclExports(module, decl, exports),
.nvptx => return @fieldParentPtr(NvPtx, "base", base).updateDeclExports(module, decl, exports),
}
}

Expand All @@ -648,6 +665,7 @@ pub const File = struct {
.c => unreachable,
.wasm => unreachable,
.spirv => unreachable,
.nvptx => unreachable,
}
}

Expand Down Expand Up @@ -843,6 +861,7 @@ pub const File = struct {
wasm,
spirv,
plan9,
nvptx,
};

pub const ErrorFlags = struct {
Expand All @@ -856,6 +875,7 @@ 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 fn determineMode(options: Options) fs.File.Mode {
Expand Down
Loading