Skip to content

Commit 5e7c419

Browse files
author
Jorge Aparicio
authored
Merge pull request rust-lang#14 from japaric/platform-intrinsics
NVPTX: platform intrinsics + tests
2 parents 1bf24cd + e8149cc commit 5e7c419

File tree

13 files changed

+316
-53
lines changed

13 files changed

+316
-53
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"intrinsic_prefix": "_",
3+
"llvm_prefix": "llvm.cuda.",
4+
"intrinsics": [
5+
{
6+
"intrinsic": "syncthreads",
7+
"width": ["0"],
8+
"llvm": "syncthreads",
9+
"ret": "V",
10+
"args": []
11+
}
12+
]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"platform": "nvptx",
3+
"number_info": {
4+
"signed": {}
5+
},
6+
"width_info": {}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"intrinsic_prefix": "_",
3+
"llvm_prefix": "llvm.nvvm.read.ptx.sreg.",
4+
"intrinsics": [
5+
{
6+
"intrinsic": "block_dim_x",
7+
"width": ["0"],
8+
"llvm": "ntid.x",
9+
"ret": "S32",
10+
"args": []
11+
},
12+
{
13+
"intrinsic": "block_dim_y",
14+
"width": ["0"],
15+
"llvm": "ntid.y",
16+
"ret": "S32",
17+
"args": []
18+
},
19+
{
20+
"intrinsic": "block_dim_z",
21+
"width": ["0"],
22+
"llvm": "ntid.z",
23+
"ret": "S32",
24+
"args": []
25+
},
26+
{
27+
"intrinsic": "block_idx_x",
28+
"width": ["0"],
29+
"llvm": "ctaid.x",
30+
"ret": "S32",
31+
"args": []
32+
},
33+
{
34+
"intrinsic": "block_idx_y",
35+
"width": ["0"],
36+
"llvm": "ctaid.y",
37+
"ret": "S32",
38+
"args": []
39+
},
40+
{
41+
"intrinsic": "block_idx_z",
42+
"width": ["0"],
43+
"llvm": "ctaid.z",
44+
"ret": "S32",
45+
"args": []
46+
},
47+
{
48+
"intrinsic": "grid_dim_x",
49+
"width": ["0"],
50+
"llvm": "nctaid.x",
51+
"ret": "S32",
52+
"args": []
53+
},
54+
{
55+
"intrinsic": "grid_dim_y",
56+
"width": ["0"],
57+
"llvm": "nctaid.y",
58+
"ret": "S32",
59+
"args": []
60+
},
61+
{
62+
"intrinsic": "grid_dim_z",
63+
"width": ["0"],
64+
"llvm": "nctaid.z",
65+
"ret": "S32",
66+
"args": []
67+
},
68+
{
69+
"intrinsic": "thread_idx_x",
70+
"width": ["0"],
71+
"llvm": "tid.x",
72+
"ret": "S32",
73+
"args": []
74+
},
75+
{
76+
"intrinsic": "thread_idx_y",
77+
"width": ["0"],
78+
"llvm": "tid.y",
79+
"ret": "S32",
80+
"args": []
81+
},
82+
{
83+
"intrinsic": "thread_idx_z",
84+
"width": ["0"],
85+
"llvm": "tid.z",
86+
"ret": "S32",
87+
"args": []
88+
}
89+
]
90+
}

src/libcore/intrinsics.rs

-18
Original file line numberDiff line numberDiff line change
@@ -598,21 +598,3 @@ extern "rust-intrinsic" {
598598
pub fn try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;
599599

600600
}
601-
602-
#[cfg(not(stage0))]
603-
#[cfg(arch = "nvptx")]
604-
extern "rust-intrinsic" {
605-
pub fn thread_idx_x() -> i32;
606-
pub fn thread_idx_y() -> i32;
607-
pub fn thread_idx_z() -> i32;
608-
pub fn block_idx_x() -> i32;
609-
pub fn block_idx_y() -> i32;
610-
pub fn block_idx_z() -> i32;
611-
pub fn block_dim_x() -> i32;
612-
pub fn block_dim_y() -> i32;
613-
pub fn block_dim_z() -> i32;
614-
pub fn grid_dim_x() -> i32;
615-
pub fn grid_dim_y() -> i32;
616-
pub fn grid_dim_z() -> i32;
617-
pub fn syncthreads();
618-
}

src/librustc_platform_intrinsics/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ static VOID: Type = Type::Void;
9595
mod x86;
9696
mod arm;
9797
mod aarch64;
98+
mod nvptx;
9899

99100
impl Intrinsic {
100101
pub fn find(name: &str) -> Option<Intrinsic> {
@@ -104,6 +105,8 @@ impl Intrinsic {
104105
arm::find(name)
105106
} else if name.starts_with("aarch64_") {
106107
aarch64::find(name)
108+
} else if name.starts_with("nvptx_") {
109+
nvptx::find(name)
107110
} else {
108111
None
109112
}
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// DO NOT EDIT: autogenerated by etc/platform-intrinsics/generator.py
12+
// ignore-tidy-linelength
13+
14+
#![allow(unused_imports)]
15+
16+
use {Intrinsic, Type};
17+
use IntrinsicDef::Named;
18+
19+
// The default inlining settings trigger a pathological behaviour in
20+
// LLVM, which causes makes compilation very slow. See #28273.
21+
#[inline(never)]
22+
pub fn find(name: &str) -> Option<Intrinsic> {
23+
if !name.starts_with("nvptx") { return None }
24+
Some(match &name["nvptx".len()..] {
25+
"_syncthreads" => Intrinsic {
26+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
27+
output: &::VOID,
28+
definition: Named("llvm.cuda.syncthreads")
29+
},
30+
"_block_dim_x" => Intrinsic {
31+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
32+
output: &::I32,
33+
definition: Named("llvm.nvvm.read.ptx.sreg.ntid.x")
34+
},
35+
"_block_dim_y" => Intrinsic {
36+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
37+
output: &::I32,
38+
definition: Named("llvm.nvvm.read.ptx.sreg.ntid.y")
39+
},
40+
"_block_dim_z" => Intrinsic {
41+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
42+
output: &::I32,
43+
definition: Named("llvm.nvvm.read.ptx.sreg.ntid.z")
44+
},
45+
"_block_idx_x" => Intrinsic {
46+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
47+
output: &::I32,
48+
definition: Named("llvm.nvvm.read.ptx.sreg.ctaid.x")
49+
},
50+
"_block_idx_y" => Intrinsic {
51+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
52+
output: &::I32,
53+
definition: Named("llvm.nvvm.read.ptx.sreg.ctaid.y")
54+
},
55+
"_block_idx_z" => Intrinsic {
56+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
57+
output: &::I32,
58+
definition: Named("llvm.nvvm.read.ptx.sreg.ctaid.z")
59+
},
60+
"_grid_dim_x" => Intrinsic {
61+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
62+
output: &::I32,
63+
definition: Named("llvm.nvvm.read.ptx.sreg.nctaid.x")
64+
},
65+
"_grid_dim_y" => Intrinsic {
66+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
67+
output: &::I32,
68+
definition: Named("llvm.nvvm.read.ptx.sreg.nctaid.y")
69+
},
70+
"_grid_dim_z" => Intrinsic {
71+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
72+
output: &::I32,
73+
definition: Named("llvm.nvvm.read.ptx.sreg.nctaid.z")
74+
},
75+
"_thread_idx_x" => Intrinsic {
76+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
77+
output: &::I32,
78+
definition: Named("llvm.nvvm.read.ptx.sreg.tid.x")
79+
},
80+
"_thread_idx_y" => Intrinsic {
81+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
82+
output: &::I32,
83+
definition: Named("llvm.nvvm.read.ptx.sreg.tid.y")
84+
},
85+
"_thread_idx_z" => Intrinsic {
86+
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
87+
output: &::I32,
88+
definition: Named("llvm.nvvm.read.ptx.sreg.tid.z")
89+
},
90+
_ => return None,
91+
})
92+
}

src/librustc_trans/context.rs

-13
Original file line numberDiff line numberDiff line change
@@ -1084,19 +1084,6 @@ fn declare_intrinsic(ccx: &CrateContext, key: &str) -> Option<ValueRef> {
10841084
ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
10851085
ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);
10861086

1087-
ifn!("llvm.cuda.syncthreads", fn() -> void);
1088-
ifn!("llvm.nvvm.read.ptx.sreg.tid.x", fn() -> t_i32);
1089-
ifn!("llvm.nvvm.read.ptx.sreg.tid.y", fn() -> t_i32);
1090-
ifn!("llvm.nvvm.read.ptx.sreg.tid.z", fn() -> t_i32);
1091-
ifn!("llvm.nvvm.read.ptx.sreg.ctaid.x", fn() -> t_i32);
1092-
ifn!("llvm.nvvm.read.ptx.sreg.ctaid.y", fn() -> t_i32);
1093-
ifn!("llvm.nvvm.read.ptx.sreg.ctaid.z", fn() -> t_i32);
1094-
ifn!("llvm.nvvm.read.ptx.sreg.ntid.x", fn() -> t_i32);
1095-
ifn!("llvm.nvvm.read.ptx.sreg.ntid.y", fn() -> t_i32);
1096-
ifn!("llvm.nvvm.read.ptx.sreg.ntid.z", fn() -> t_i32);
1097-
ifn!("llvm.nvvm.read.ptx.sreg.nctaid.x", fn() -> t_i32);
1098-
ifn!("llvm.nvvm.read.ptx.sreg.nctaid.y", fn() -> t_i32);
1099-
ifn!("llvm.nvvm.read.ptx.sreg.nctaid.z", fn() -> t_i32);
11001087
ifn!("llvm.assume", fn(i1) -> void);
11011088

11021089
if ccx.sess().opts.debuginfo != NoDebugInfo {

src/librustc_trans/intrinsic.rs

-13
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,6 @@ fn get_simple_intrinsic(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
8989
"roundf32" => "llvm.round.f32",
9090
"roundf64" => "llvm.round.f64",
9191
"assume" => "llvm.assume",
92-
"thread_idx_x" => "llvm.nvvm.read.ptx.sreg.tid.x",
93-
"thread_idx_y" => "llvm.nvvm.read.ptx.sreg.tid.y",
94-
"thread_idx_z" => "llvm.nvvm.read.ptx.sreg.tid.z",
95-
"block_idx_x" => "llvm.nvvm.read.ptx.sreg.ctaid.x",
96-
"block_idx_y" => "llvm.nvvm.read.ptx.sreg.ctaid.y",
97-
"block_idx_z" => "llvm.nvvm.read.ptx.sreg.ctaid.z",
98-
"block_dim_x" => "llvm.nvvm.read.ptx.sreg.ntid.x",
99-
"block_dim_y" => "llvm.nvvm.read.ptx.sreg.ntid.y",
100-
"block_dim_z" => "llvm.nvvm.read.ptx.sreg.ntid.z",
101-
"grid_dim_x" => "llvm.nvvm.read.ptx.sreg.nctaid.x",
102-
"grid_dim_y" => "llvm.nvvm.read.ptx.sreg.nctaid.y",
103-
"grid_dim_z" => "llvm.nvvm.read.ptx.sreg.nctaid.z",
104-
"syncthreads" => "llvm.cuda.syncthreads",
10592
_ => return None
10693
};
10794
Some(ccx.get_intrinsic(&llvm_name))

src/librustc_typeck/check/intrinsic.rs

-9
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,6 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &hir::ForeignItem) {
296296
(0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
297297
}
298298

299-
"thread_idx_x" | "thread_idx_y" | "thread_idx_z" |
300-
"block_idx_x" | "block_idx_y" | "block_idx_z" |
301-
"block_dim_x" | "block_dim_y" | "block_dim_z" |
302-
"grid_dim_x" | "grid_dim_y" | "grid_dim_z" => {
303-
(0, vec![], tcx.types.i32)
304-
}
305-
306-
"syncthreads" => (0, vec![], tcx.mk_nil()),
307-
308299
ref other => {
309300
span_err!(tcx.sess, it.span, E0093,
310301
"unrecognized intrinsic function: `{}`", *other);

src/test/run-make/nvptx/Makefile

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-include ../tools.mk
2+
3+
NVPTX = nvptx-unknown-unknown
4+
NVPTX64 = nvptx64-unknown-unknown
5+
6+
all: smoke syncthreads thread_idx
7+
8+
smoke:
9+
$(RUSTC) --target $(NVPTX) --emit=asm $@.rs
10+
grep '\.address_size *32' $(TMPDIR)/$@.s
11+
$(RUSTC) --target $(NVPTX64) --emit=asm $@.rs
12+
grep '\.address_size *64' $(TMPDIR)/$@.s
13+
14+
syncthreads:
15+
$(RUSTC) --target $(NVPTX) --emit=llvm-ir $@.rs
16+
grep 'call.*syncthreads' $(TMPDIR)/$@.ll
17+
$(RUSTC) --target $(NVPTX64) --emit=llvm-ir $@.rs
18+
grep 'call.*syncthreads' $(TMPDIR)/$@.ll
19+
$(RUSTC) --target $(NVPTX) --emit=asm $@.rs
20+
grep 'bar\.sync' $(TMPDIR)/$@.s
21+
$(RUSTC) --target $(NVPTX64) --emit=asm $@.rs
22+
grep 'bar\.sync' $(TMPDIR)/$@.s
23+
24+
thread_idx:
25+
$(RUSTC) --target $(NVPTX) --emit=llvm-ir $@.rs
26+
grep 'call.*tid\.x' $(TMPDIR)/$@.ll
27+
$(RUSTC) --target $(NVPTX64) --emit=llvm-ir $@.rs
28+
grep 'call.*tid\.x' $(TMPDIR)/$@.ll
29+
$(RUSTC) --target $(NVPTX) --emit=asm $@.rs
30+
grep '%tid\.x' $(TMPDIR)/$@.s
31+
$(RUSTC) --target $(NVPTX64) --emit=asm $@.rs
32+
grep '%tid\.x' $(TMPDIR)/$@.s

src/test/run-make/nvptx/smoke.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(no_core)]
12+
13+
#![no_core]
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(lang_items)]
12+
#![feature(no_core)]
13+
#![feature(platform_intrinsics)]
14+
15+
#![no_core]
16+
17+
#![allow(dead_code)]
18+
19+
extern "platform-intrinsic" {
20+
fn nvptx_syncthreads();
21+
}
22+
23+
fn syncthreads() {
24+
unsafe {
25+
nvptx_syncthreads();
26+
}
27+
}
28+
29+
#[lang = "copy"]
30+
trait Copy {}
31+
32+
#[lang = "sized"]
33+
trait Sized {}

0 commit comments

Comments
 (0)