authorgravatar for gwenzek@users.noreply.github.comGuillaume Wenzek <gwenzek@users.noreply.github.com> 2022-03-01 23:26:43+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-12 14:25:59-05:00
logf000f8a59a8bf1121ecbe9b60ae50cc0218d3ba3
treef4578be5065b377c3c158562206311ed4d28f99d
parent5ff7b04a6ad72eb86b6d467dfdc25bea1a9ecf63

fix nvptx test failure #10968

allow test cases to chose wether to link libc or not. default behavior is to not link libc, except for `exeUsingLLVMBackend`

4 files changed, 52 insertions(+), 42 deletions(-)

src/link/NvPtx.zig+16-17
......@@ -25,8 +25,21 @@ base: link.File,
2525llvm_object: *LlvmObject,
2626
2727pub fn createEmpty(gpa: Allocator, options: link.Options) !*NvPtx {
28 if (!build_options.have_llvm) return error.TODOArchNotSupported;
28 if (!build_options.have_llvm) return error.PtxArchNotSupported;
29 if (!options.use_llvm) return error.PtxArchNotSupported;
2930
31 switch (options.target.cpu.arch) {
32 .nvptx, .nvptx64 => {},
33 else => return error.PtxArchNotSupported,
34 }
35
36 switch (options.target.os.tag) {
37 // TODO: does it also work with nvcl ?
38 .cuda => {},
39 else => return error.PtxArchNotSupported,
40 }
41
42 const llvm_object = try LlvmObject.create(gpa, options);
3043 const nvptx = try gpa.create(NvPtx);
3144 nvptx.* = .{
3245 .base = .{
......@@ -35,32 +48,19 @@ pub fn createEmpty(gpa: Allocator, options: link.Options) !*NvPtx {
3548 .file = null,
3649 .allocator = gpa,
3750 },
38 .llvm_object = undefined,
51 .llvm_object = llvm_object,
3952 };
4053
41 switch (options.target.cpu.arch) {
42 .nvptx, .nvptx64 => {},
43 else => return error.TODOArchNotSupported,
44 }
45
46 switch (options.target.os.tag) {
47 // TODO: does it also work with nvcl ?
48 .cuda => {},
49 else => return error.TODOOsNotSupported,
50 }
51
5254 return nvptx;
5355}
5456
5557pub fn openPath(allocator: Allocator, sub_path: []const u8, options: link.Options) !*NvPtx {
5658 if (!build_options.have_llvm) @panic("nvptx target requires a zig compiler with llvm enabled.");
57 if (!options.use_llvm) return error.TODOArchNotSupported;
59 if (!options.use_llvm) return error.PtxArchNotSupported;
5860 assert(options.object_format == .nvptx);
5961
6062 const nvptx = try createEmpty(allocator, options);
61 errdefer nvptx.base.destroy();
6263 log.info("Opening .ptx target file {s}", .{sub_path});
63 nvptx.llvm_object = try LlvmObject.create(allocator, options);
6464 return nvptx;
6565}
6666
......@@ -117,6 +117,5 @@ pub fn flushModule(self: *NvPtx, comp: *Compilation) !void {
117117 };
118118 hack_comp.bin_file.options.emit = null;
119119 }
120
121120 return try self.llvm_object.flushModule(hack_comp);
122121}
src/test.zig+6-10
......@@ -175,6 +175,7 @@ pub const TestContext = struct {
175175 is_test: bool = false,
176176 expect_exact: bool = false,
177177 backend: Backend = .stage2,
178 link_libc: bool = false,
178179
179180 files: std.ArrayList(File),
180181
......@@ -331,6 +332,7 @@ pub const TestContext = struct {
331332 .output_mode = .Exe,
332333 .files = std.ArrayList(File).init(ctx.cases.allocator),
333334 .backend = .llvm,
335 .link_libc = true,
334336 }) catch @panic("out of memory");
335337 return &ctx.cases.items[ctx.cases.items.len - 1];
336338 }
......@@ -888,11 +890,6 @@ pub const TestContext = struct {
888890 .llvm => true,
889891 else => null,
890892 };
891 const use_stage1: ?bool = switch (case.backend) {
892 .stage1 => true,
893 else => null,
894 };
895 const link_libc = case.backend == .llvm;
896893 const comp = try Compilation.create(allocator, .{
897894 .local_cache_directory = zig_cache_directory,
898895 .global_cache_directory = global_cache_directory,
......@@ -914,9 +911,9 @@ pub const TestContext = struct {
914911 .is_native_os = case.target.isNativeOs(),
915912 .is_native_abi = case.target.isNativeAbi(),
916913 .dynamic_linker = target_info.dynamic_linker.get(),
917 .link_libc = link_libc,
914 .link_libc = case.link_libc,
918915 .use_llvm = use_llvm,
919 .use_stage1 = use_stage1,
916 .use_stage1 = null, // We already handled stage1 tests
920917 .self_exe_path = std.testing.zig_exe_path,
921918 });
922919 defer comp.destroy();
......@@ -1145,7 +1142,7 @@ pub const TestContext = struct {
11451142 "-lc",
11461143 exe_path,
11471144 });
1148 } else switch (host.getExternalExecutor(target_info, .{ .link_libc = link_libc })) {
1145 } else switch (host.getExternalExecutor(target_info, .{ .link_libc = case.link_libc })) {
11491146 .native => try argv.append(exe_path),
11501147 .bad_dl, .bad_os_or_cpu => return, // Pass test.
11511148
......@@ -1156,8 +1153,7 @@ pub const TestContext = struct {
11561153 },
11571154
11581155 .qemu => |qemu_bin_name| if (enable_qemu) {
1159 // TODO Ability for test cases to specify whether to link libc.
1160 const need_cross_glibc = false; // target.isGnuLibC() and self.is_linking_libc;
1156 const need_cross_glibc = target.isGnuLibC() and case.link_libc;
11611157 const glibc_dir_arg = if (need_cross_glibc)
11621158 glibc_runtimes_dir orelse return // glibc dir not available; pass test
11631159 else
test/cases.zig+1-2
......@@ -16,6 +16,5 @@ pub fn addCases(ctx: *TestContext) !void {
1616 try @import("stage2/riscv64.zig").addCases(ctx);
1717 try @import("stage2/plan9.zig").addCases(ctx);
1818 try @import("stage2/x86_64.zig").addCases(ctx);
19 // TODO https://github.com/ziglang/zig/issues/10968
20 //try @import("stage2/nvptx.zig").addCases(ctx);
19 try @import("stage2/nvptx.zig").addCases(ctx);
2120}
test/stage2/nvptx.zig+29-13
......@@ -1,21 +1,16 @@
11const std = @import("std");
22const TestContext = @import("../../src/test.zig").TestContext;
33
4const nvptx = std.zig.CrossTarget{
5 .cpu_arch = .nvptx64,
6 .os_tag = .cuda,
7};
8
94pub fn addCases(ctx: *TestContext) !void {
105 {
11 var case = ctx.exeUsingLlvmBackend("simple addition and subtraction", nvptx);
6 var case = addPtx(ctx, "nvptx: simple addition and subtraction");
127
138 case.compiles(
149 \\fn add(a: i32, b: i32) i32 {
1510 \\ return a + b;
1611 \\}
1712 \\
18 \\pub export fn main(a: i32, out: *i32) callconv(.PtxKernel) void {
13 \\pub export fn add_and_substract(a: i32, out: *i32) callconv(.PtxKernel) void {
1914 \\ const x = add(a, 7);
2015 \\ var y = add(2, 0);
2116 \\ y -= x;
......@@ -25,28 +20,28 @@ pub fn addCases(ctx: *TestContext) !void {
2520 }
2621
2722 {
28 var case = ctx.exeUsingLlvmBackend("read special registers", nvptx);
23 var case = addPtx(ctx, "nvptx: read special registers");
2924
3025 case.compiles(
31 \\fn tid() usize {
26 \\fn threadIdX() usize {
3227 \\ var tid = asm volatile ("mov.u32 \t$0, %tid.x;"
3328 \\ : [ret] "=r" (-> u32),
3429 \\ );
3530 \\ return @as(usize, tid);
3631 \\}
3732 \\
38 \\pub export fn main(a: []const i32, out: []i32) callconv(.PtxKernel) void {
39 \\ const i = tid();
33 \\pub export fn special_reg(a: []const i32, out: []i32) callconv(.PtxKernel) void {
34 \\ const i = threadIdX();
4035 \\ out[i] = a[i] + 7;
4136 \\}
4237 );
4338 }
4439
4540 {
46 var case = ctx.exeUsingLlvmBackend("address spaces", nvptx);
41 var case = addPtx(ctx, "nvptx: address spaces");
4742
4843 case.compiles(
49 \\var x: u32 addrspace(.global) = 0;
44 \\var x: i32 addrspace(.global) = 0;
5045 \\
5146 \\pub export fn increment(out: *i32) callconv(.PtxKernel) void {
5247 \\ x += 1;
......@@ -55,3 +50,24 @@ pub fn addCases(ctx: *TestContext) !void {
5550 );
5651 }
5752}
53
54const nvptx_target = std.zig.CrossTarget{
55 .cpu_arch = .nvptx64,
56 .os_tag = .cuda,
57};
58
59pub fn addPtx(
60 ctx: *TestContext,
61 name: []const u8,
62) *TestContext.Case {
63 ctx.cases.append(TestContext.Case{
64 .name = name,
65 .target = nvptx_target,
66 .updates = std.ArrayList(TestContext.Update).init(ctx.cases.allocator),
67 .output_mode = .Obj,
68 .files = std.ArrayList(TestContext.File).init(ctx.cases.allocator),
69 .link_libc = false,
70 .backend = .llvm,
71 }) catch @panic("out of memory");
72 return &ctx.cases.items[ctx.cases.items.len - 1];
73}