Skip to content

Commit

Permalink
Merge pull request rust-lang#14 from japaric/platform-intrinsics
Browse files Browse the repository at this point in the history
NVPTX: platform intrinsics + tests
  • Loading branch information
Jorge Aparicio authored Jul 12, 2016
2 parents 1bf24cd + e8149cc commit 5e7c419
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 53 deletions.
13 changes: 13 additions & 0 deletions src/etc/platform-intrinsics/nvptx/cuda.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"intrinsic_prefix": "_",
"llvm_prefix": "llvm.cuda.",
"intrinsics": [
{
"intrinsic": "syncthreads",
"width": ["0"],
"llvm": "syncthreads",
"ret": "V",
"args": []
}
]
}
7 changes: 7 additions & 0 deletions src/etc/platform-intrinsics/nvptx/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"platform": "nvptx",
"number_info": {
"signed": {}
},
"width_info": {}
}
90 changes: 90 additions & 0 deletions src/etc/platform-intrinsics/nvptx/sreg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"intrinsic_prefix": "_",
"llvm_prefix": "llvm.nvvm.read.ptx.sreg.",
"intrinsics": [
{
"intrinsic": "block_dim_x",
"width": ["0"],
"llvm": "ntid.x",
"ret": "S32",
"args": []
},
{
"intrinsic": "block_dim_y",
"width": ["0"],
"llvm": "ntid.y",
"ret": "S32",
"args": []
},
{
"intrinsic": "block_dim_z",
"width": ["0"],
"llvm": "ntid.z",
"ret": "S32",
"args": []
},
{
"intrinsic": "block_idx_x",
"width": ["0"],
"llvm": "ctaid.x",
"ret": "S32",
"args": []
},
{
"intrinsic": "block_idx_y",
"width": ["0"],
"llvm": "ctaid.y",
"ret": "S32",
"args": []
},
{
"intrinsic": "block_idx_z",
"width": ["0"],
"llvm": "ctaid.z",
"ret": "S32",
"args": []
},
{
"intrinsic": "grid_dim_x",
"width": ["0"],
"llvm": "nctaid.x",
"ret": "S32",
"args": []
},
{
"intrinsic": "grid_dim_y",
"width": ["0"],
"llvm": "nctaid.y",
"ret": "S32",
"args": []
},
{
"intrinsic": "grid_dim_z",
"width": ["0"],
"llvm": "nctaid.z",
"ret": "S32",
"args": []
},
{
"intrinsic": "thread_idx_x",
"width": ["0"],
"llvm": "tid.x",
"ret": "S32",
"args": []
},
{
"intrinsic": "thread_idx_y",
"width": ["0"],
"llvm": "tid.y",
"ret": "S32",
"args": []
},
{
"intrinsic": "thread_idx_z",
"width": ["0"],
"llvm": "tid.z",
"ret": "S32",
"args": []
}
]
}
18 changes: 0 additions & 18 deletions src/libcore/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,21 +598,3 @@ extern "rust-intrinsic" {
pub fn try(f: fn(*mut u8), data: *mut u8, local_ptr: *mut u8) -> i32;

}

#[cfg(not(stage0))]
#[cfg(arch = "nvptx")]
extern "rust-intrinsic" {
pub fn thread_idx_x() -> i32;
pub fn thread_idx_y() -> i32;
pub fn thread_idx_z() -> i32;
pub fn block_idx_x() -> i32;
pub fn block_idx_y() -> i32;
pub fn block_idx_z() -> i32;
pub fn block_dim_x() -> i32;
pub fn block_dim_y() -> i32;
pub fn block_dim_z() -> i32;
pub fn grid_dim_x() -> i32;
pub fn grid_dim_y() -> i32;
pub fn grid_dim_z() -> i32;
pub fn syncthreads();
}
3 changes: 3 additions & 0 deletions src/librustc_platform_intrinsics/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ static VOID: Type = Type::Void;
mod x86;
mod arm;
mod aarch64;
mod nvptx;

impl Intrinsic {
pub fn find(name: &str) -> Option<Intrinsic> {
Expand All @@ -104,6 +105,8 @@ impl Intrinsic {
arm::find(name)
} else if name.starts_with("aarch64_") {
aarch64::find(name)
} else if name.starts_with("nvptx_") {
nvptx::find(name)
} else {
None
}
Expand Down
92 changes: 92 additions & 0 deletions src/librustc_platform_intrinsics/nvptx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// DO NOT EDIT: autogenerated by etc/platform-intrinsics/generator.py
// ignore-tidy-linelength

#![allow(unused_imports)]

use {Intrinsic, Type};
use IntrinsicDef::Named;

// The default inlining settings trigger a pathological behaviour in
// LLVM, which causes makes compilation very slow. See #28273.
#[inline(never)]
pub fn find(name: &str) -> Option<Intrinsic> {
if !name.starts_with("nvptx") { return None }
Some(match &name["nvptx".len()..] {
"_syncthreads" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::VOID,
definition: Named("llvm.cuda.syncthreads")
},
"_block_dim_x" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.ntid.x")
},
"_block_dim_y" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.ntid.y")
},
"_block_dim_z" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.ntid.z")
},
"_block_idx_x" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.ctaid.x")
},
"_block_idx_y" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.ctaid.y")
},
"_block_idx_z" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.ctaid.z")
},
"_grid_dim_x" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.nctaid.x")
},
"_grid_dim_y" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.nctaid.y")
},
"_grid_dim_z" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.nctaid.z")
},
"_thread_idx_x" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.tid.x")
},
"_thread_idx_y" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.tid.y")
},
"_thread_idx_z" => Intrinsic {
inputs: { static INPUTS: [&'static Type; 0] = []; &INPUTS },
output: &::I32,
definition: Named("llvm.nvvm.read.ptx.sreg.tid.z")
},
_ => return None,
})
}
13 changes: 0 additions & 13 deletions src/librustc_trans/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,19 +1084,6 @@ fn declare_intrinsic(ccx: &CrateContext, key: &str) -> Option<ValueRef> {
ifn!("llvm.localrecover", fn(i8p, i8p, t_i32) -> i8p);
ifn!("llvm.x86.seh.recoverfp", fn(i8p, i8p) -> i8p);

ifn!("llvm.cuda.syncthreads", fn() -> void);
ifn!("llvm.nvvm.read.ptx.sreg.tid.x", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.tid.y", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.tid.z", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.ctaid.x", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.ctaid.y", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.ctaid.z", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.ntid.x", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.ntid.y", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.ntid.z", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.nctaid.x", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.nctaid.y", fn() -> t_i32);
ifn!("llvm.nvvm.read.ptx.sreg.nctaid.z", fn() -> t_i32);
ifn!("llvm.assume", fn(i1) -> void);

if ccx.sess().opts.debuginfo != NoDebugInfo {
Expand Down
13 changes: 0 additions & 13 deletions src/librustc_trans/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,6 @@ fn get_simple_intrinsic(ccx: &CrateContext, name: &str) -> Option<ValueRef> {
"roundf32" => "llvm.round.f32",
"roundf64" => "llvm.round.f64",
"assume" => "llvm.assume",
"thread_idx_x" => "llvm.nvvm.read.ptx.sreg.tid.x",
"thread_idx_y" => "llvm.nvvm.read.ptx.sreg.tid.y",
"thread_idx_z" => "llvm.nvvm.read.ptx.sreg.tid.z",
"block_idx_x" => "llvm.nvvm.read.ptx.sreg.ctaid.x",
"block_idx_y" => "llvm.nvvm.read.ptx.sreg.ctaid.y",
"block_idx_z" => "llvm.nvvm.read.ptx.sreg.ctaid.z",
"block_dim_x" => "llvm.nvvm.read.ptx.sreg.ntid.x",
"block_dim_y" => "llvm.nvvm.read.ptx.sreg.ntid.y",
"block_dim_z" => "llvm.nvvm.read.ptx.sreg.ntid.z",
"grid_dim_x" => "llvm.nvvm.read.ptx.sreg.nctaid.x",
"grid_dim_y" => "llvm.nvvm.read.ptx.sreg.nctaid.y",
"grid_dim_z" => "llvm.nvvm.read.ptx.sreg.nctaid.z",
"syncthreads" => "llvm.cuda.syncthreads",
_ => return None
};
Some(ccx.get_intrinsic(&llvm_name))
Expand Down
9 changes: 0 additions & 9 deletions src/librustc_typeck/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,15 +296,6 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &hir::ForeignItem) {
(0, vec![tcx.mk_fn_ptr(fn_ty), mut_u8, mut_u8], tcx.types.i32)
}

"thread_idx_x" | "thread_idx_y" | "thread_idx_z" |
"block_idx_x" | "block_idx_y" | "block_idx_z" |
"block_dim_x" | "block_dim_y" | "block_dim_z" |
"grid_dim_x" | "grid_dim_y" | "grid_dim_z" => {
(0, vec![], tcx.types.i32)
}

"syncthreads" => (0, vec![], tcx.mk_nil()),

ref other => {
span_err!(tcx.sess, it.span, E0093,
"unrecognized intrinsic function: `{}`", *other);
Expand Down
32 changes: 32 additions & 0 deletions src/test/run-make/nvptx/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-include ../tools.mk

NVPTX = nvptx-unknown-unknown
NVPTX64 = nvptx64-unknown-unknown

all: smoke syncthreads thread_idx

smoke:
$(RUSTC) --target $(NVPTX) --emit=asm $@.rs
grep '\.address_size *32' $(TMPDIR)/$@.s
$(RUSTC) --target $(NVPTX64) --emit=asm $@.rs
grep '\.address_size *64' $(TMPDIR)/$@.s

syncthreads:
$(RUSTC) --target $(NVPTX) --emit=llvm-ir $@.rs
grep 'call.*syncthreads' $(TMPDIR)/$@.ll
$(RUSTC) --target $(NVPTX64) --emit=llvm-ir $@.rs
grep 'call.*syncthreads' $(TMPDIR)/$@.ll
$(RUSTC) --target $(NVPTX) --emit=asm $@.rs
grep 'bar\.sync' $(TMPDIR)/$@.s
$(RUSTC) --target $(NVPTX64) --emit=asm $@.rs
grep 'bar\.sync' $(TMPDIR)/$@.s

thread_idx:
$(RUSTC) --target $(NVPTX) --emit=llvm-ir $@.rs
grep 'call.*tid\.x' $(TMPDIR)/$@.ll
$(RUSTC) --target $(NVPTX64) --emit=llvm-ir $@.rs
grep 'call.*tid\.x' $(TMPDIR)/$@.ll
$(RUSTC) --target $(NVPTX) --emit=asm $@.rs
grep '%tid\.x' $(TMPDIR)/$@.s
$(RUSTC) --target $(NVPTX64) --emit=asm $@.rs
grep '%tid\.x' $(TMPDIR)/$@.s
13 changes: 13 additions & 0 deletions src/test/run-make/nvptx/smoke.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(no_core)]

#![no_core]
33 changes: 33 additions & 0 deletions src/test/run-make/nvptx/syncthreads.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(lang_items)]
#![feature(no_core)]
#![feature(platform_intrinsics)]

#![no_core]

#![allow(dead_code)]

extern "platform-intrinsic" {
fn nvptx_syncthreads();
}

fn syncthreads() {
unsafe {
nvptx_syncthreads();
}
}

#[lang = "copy"]
trait Copy {}

#[lang = "sized"]
trait Sized {}
Loading

0 comments on commit 5e7c419

Please sign in to comment.