Skip to content

Prefetch intrinsics support #14921

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

Closed
wants to merge 1 commit into from
Closed
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 src/librustc/middle/trans/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ fn declare_intrinsic(ccx: &CrateContext, key: & &'static str) -> Option<ValueRef
ifn!("llvm.memset.p0i8.i32" fn(i8p, t_i8, t_i32, t_i32, i1) -> void);
ifn!("llvm.memset.p0i8.i64" fn(i8p, t_i8, t_i64, t_i32, i1) -> void);

ifn!("llvm.prefetch" fn(i8p, t_i32, t_i32, t_i32) -> void);
ifn!("llvm.trap" fn() -> void);
ifn!("llvm.debugtrap" fn() -> void);
ifn!("llvm.frameaddress" fn(t_i32) -> i8p);
Expand Down
47 changes: 47 additions & 0 deletions src/librustc/middle/trans/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ pub fn trans_intrinsic(ccx: &CrateContext,
Ret(bcx, llcall);
}

fn prefetch_intrinsic(bcx: &Block, rw: i32, locality: i32, cache: i32) {
let ccx = bcx.ccx();
let ptr = get_param(bcx.fcx.llfn, bcx.fcx.arg_pos(0u));
let ptri8 = PointerCast(bcx, ptr, Type::i8p(bcx.ccx()));
let llfn = bcx.ccx().get_intrinsic(&("llvm.prefetch"));
Call(bcx, llfn, [ptri8, C_i32(ccx, rw), C_i32(ccx, locality), C_i32(ccx, cache)], []);
RetVoid(bcx)
}

let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx(), item.id));

let arena = TypedArena::new();
Expand Down Expand Up @@ -283,6 +292,44 @@ pub fn trans_intrinsic(ccx: &CrateContext,
return;
}

if name.get().starts_with("prefetch") {
let mut rw = None;
let rw_default = 0; // read
let mut locality = None;
let locality_default = 3; // extreme
let mut cache = None;
let cache_default = 1; // data cache

let setparam = |param: &mut Option<i32>, val: i32| {
match *param {
None => {
*param = Some(val);
}
_ => ccx.sess().span_fatal(item.span, "conflicting arguments in prefetch operation name")
}
};

for part in name.get().split('_').skip(1) {
match part {
"read" => setparam(&mut rw, 0),
"write" => setparam(&mut rw, 1),
"extreme" => setparam(&mut locality, 3),
"high" => setparam(&mut locality, 2),
"low" => setparam(&mut locality, 1),
"none" => setparam(&mut locality, 0),
"dcache" => setparam(&mut cache, 1),
"icache" => setparam(&mut cache, 0),
_ => ccx.sess().span_fatal(item.span, "unknown part in prefetch operation name")
}
}
prefetch_intrinsic(bcx,
rw.unwrap_or(rw_default),
locality.unwrap_or(locality_default),
cache.unwrap_or(cache_default));
fcx.cleanup();
return;
}

match name.get() {
"abort" => {
let llfn = bcx.ccx().get_intrinsic(&("llvm.trap"));
Expand Down
5 changes: 5 additions & 0 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4392,6 +4392,11 @@ pub fn check_intrinsic_type(ccx: &CrateCtxt, it: &ast::ForeignItem) {
}
}

} else if name.get().starts_with("prefetch") {
match name.get().find_str("write") {
None => (1, vec!(ty::mk_imm_ptr(tcx, param(ccx, 0))), ty::mk_nil()),
_ => (1, vec!(ty::mk_mut_ptr(tcx, param(ccx, 0))), ty::mk_nil())
}
} else {
match name.get() {
"abort" => (0, Vec::new(), ty::mk_bot()),
Expand Down
30 changes: 30 additions & 0 deletions src/test/run-pass/intrinsic-prefetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2014 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.

mod rusti {
extern "rust-intrinsic" {
pub fn prefetch<T>(address: *T);
pub fn prefetch_write<T>(address: *mut T);
}
}

fn main() {
let i = 1;
let j = box 1;
unsafe {
let ip: *int = &i;
let jp: *mut int = std::mem::transmute(j);

rusti::prefetch(ip);
rusti::prefetch_write(jp);

drop(std::mem::transmute::<_, Box<int>>(jp));
}
}