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

enable Gpu address spaces #10884

Merged
merged 2 commits into from
Feb 21, 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
6 changes: 6 additions & 0 deletions lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ pub const AddressSpace = enum {
gs,
fs,
ss,
// GPU address spaces
global,
constant,
param,
shared,
local,
};

/// This data structure is used by the Zig language code generation and
Expand Down
5 changes: 4 additions & 1 deletion src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18006,10 +18006,14 @@ pub fn analyzeAddrspace(
const address_space = addrspace_tv.val.toEnum(std.builtin.AddressSpace);
const target = sema.mod.getTarget();
const arch = target.cpu.arch;
const is_gpu = arch == .nvptx or arch == .nvptx64;

const supported = switch (address_space) {
.generic => true,
.gs, .fs, .ss => (arch == .i386 or arch == .x86_64) and ctx == .pointer,
// TODO: check that .shared and .local are left uninitialized
.global, .param, .shared, .local => is_gpu,
.constant => is_gpu and (ctx == .constant),
};

if (!supported) {
Expand All @@ -18020,7 +18024,6 @@ pub fn analyzeAddrspace(
.constant => "constant values",
.pointer => "pointers",
};

return sema.fail(
block,
src,
Expand Down
10 changes: 10 additions & 0 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,16 @@ pub const DeclGen = struct {
.gs => llvm.address_space.x86.gs,
.fs => llvm.address_space.x86.fs,
.ss => llvm.address_space.x86.ss,
else => unreachable,
},
.nvptx, .nvptx64 => switch (address_space) {
.generic => llvm.address_space.default,
.global => llvm.address_space.nvptx.global,
.constant => llvm.address_space.nvptx.constant,
.param => llvm.address_space.nvptx.param,
.shared => llvm.address_space.nvptx.shared,
.local => llvm.address_space.nvptx.local,
else => unreachable,
},
else => switch (address_space) {
.generic => llvm.address_space.default,
Expand Down
1 change: 1 addition & 0 deletions test/cases.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ pub fn addCases(ctx: *TestContext) !void {
try @import("stage2/riscv64.zig").addCases(ctx);
try @import("stage2/plan9.zig").addCases(ctx);
try @import("stage2/x86_64.zig").addCases(ctx);
try @import("stage2/nvptx.zig").addCases(ctx);
}
57 changes: 57 additions & 0 deletions test/stage2/nvptx.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const std = @import("std");
const TestContext = @import("../../src/test.zig").TestContext;

const nvptx = std.zig.CrossTarget{
.cpu_arch = .nvptx64,
.os_tag = .cuda,
};

pub fn addCases(ctx: *TestContext) !void {
{
var case = ctx.exeUsingLlvmBackend("simple addition and subtraction", nvptx);

case.compiles(
\\fn add(a: i32, b: i32) i32 {
\\ return a + b;
\\}
\\
\\pub export fn main(a: i32, out: *i32) callconv(.PtxKernel) void {
\\ const x = add(a, 7);
\\ var y = add(2, 0);
\\ y -= x;
\\ out.* = y;
\\}
);
}

{
var case = ctx.exeUsingLlvmBackend("read special registers", nvptx);

case.compiles(
\\fn tid() usize {
\\ var tid = asm volatile ("mov.u32 \t$0, %tid.x;"
\\ : [ret] "=r" (-> u32),
\\ );
\\ return @as(usize, tid);
\\}
\\
\\pub export fn main(a: []const i32, out: []i32) callconv(.PtxKernel) void {
\\ const i = tid();
\\ out[i] = a[i] + 7;
\\}
);
}

{
var case = ctx.exeUsingLlvmBackend("address spaces", nvptx);

case.compiles(
\\var x: u32 addrspace(.global) = 0;
\\
\\pub export fn increment(out: *i32) callconv(.PtxKernel) void {
\\ x += 1;
\\ out.* = x;
\\}
);
}
}