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

Plugin provided "intrinsics". #52136

Closed
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
37 changes: 37 additions & 0 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use rustc_data_structures::small_vec::SmallVec;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::sync::ReadGuard;
use rustc_serialize as serialize;
use std::collections::HashMap;
use std::borrow::Cow;
use std::fmt::{self, Debug, Formatter, Write};
use std::ops::{Index, IndexMut};
Expand Down Expand Up @@ -2923,3 +2924,39 @@ impl<'tcx> TypeFoldable<'tcx> for Literal<'tcx> {
}
}
}
pub type PluginIntrinsics = HashMap<String, Box<PluginIntrinsicCodegen>>;
pub trait PluginIntrinsicCodegen: Sync + Send {
/// Codegen a plugin-defined intrinsic. This is intended to be used to
/// "return" values based on the monomorphized and erased types of the
/// function call. Codegen will codegen the `extra_stmts` and then insert
/// an unconditional branch to the exit block.
///
/// Consider this to be highly unstable; it will likely change without
/// warning. There is also no spec for this, it is 100% implementation
/// defined, and may not be implemented at all for some codegen backends.
///
/// If the codegen backend is multithreaded, this will be called from
/// any number of threads, hence `Sync + Send`.
///
/// YOU ARE RESPONSIBLE FOR THE SAFETY OF THE EXTRA STATEMENTS.
/// You have been warned. Good luck, have fun.
fn codegen_simple_intrinsic<'a, 'tcx>(&self,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
source_info: SourceInfo,
sig: &ty::FnSig<'tcx>,
parent_mir: &Mir<'tcx>,
parent_param_substs: &'tcx Substs<'tcx>,
args: &Vec<Operand<'tcx>>,
dest: Place<'tcx>,
extra_stmts: &mut Vec<StatementKind<'tcx>>)
where 'tcx: 'a;

/// The following are used for during typeck:

/// The number of generic parameters expected.
fn generic_parameter_count<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> usize;
/// The types of the input args.
fn inputs<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<Ty<'tcx>>;
/// The return type.
fn output<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx>;
}
7 changes: 6 additions & 1 deletion src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ use lint;
use lint::builtin::BuiltinLintDiagnostics;
use middle::allocator::AllocatorKind;
use middle::dependency_format;
use mir::PluginIntrinsics;
use session::search_paths::PathKind;
use session::config::{OutputType};
use ty::tls;
use util::nodemap::{FxHashSet};
use util::common::{duration_to_secs_str, ErrorReported};
use util::common::ProfileQueriesMsg;

use rustc_data_structures::sync::{self, Lrc, Lock, LockCell, OneThread, Once, RwLock};
use rustc_data_structures::sync::{self, Lrc, Lock, LockCell, OneThread, Once,
RwLock, ArcCell, };

use syntax::ast::NodeId;
use errors::{self, DiagnosticBuilder, DiagnosticId};
Expand Down Expand Up @@ -54,6 +56,7 @@ use std::fmt;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;
use std::sync::Arc;
use std::sync::mpsc;
use std::sync::atomic::{AtomicUsize, Ordering};

Expand Down Expand Up @@ -92,6 +95,7 @@ pub struct Session {
pub one_time_diagnostics: Lock<FxHashSet<(DiagnosticMessageId, Option<Span>, String)>>,
pub plugin_llvm_passes: OneThread<RefCell<Vec<String>>>,
pub plugin_attributes: OneThread<RefCell<Vec<(String, AttributeType)>>>,
pub plugin_intrinsics: ArcCell<PluginIntrinsics>,
pub crate_types: Once<Vec<config::CrateType>>,
pub dependency_formats: Once<dependency_format::Dependencies>,
/// The crate_disambiguator is constructed out of all the `-C metadata`
Expand Down Expand Up @@ -1109,6 +1113,7 @@ pub fn build_session_(
one_time_diagnostics: Lock::new(FxHashSet()),
plugin_llvm_passes: OneThread::new(RefCell::new(Vec::new())),
plugin_attributes: OneThread::new(RefCell::new(Vec::new())),
plugin_intrinsics: ArcCell::new(Arc::new(HashMap::new())),
crate_types: Once::new(),
dependency_formats: Once::new(),
crate_disambiguator: Once::new(),
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_codegen_llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use type_of::PointeeInfo;

use rustc_data_structures::base_n;
use rustc::mir::mono::Stats;
use rustc::mir::PluginIntrinsics;
use rustc::session::config::{self, NoDebugInfo};
use rustc::session::Session;
use rustc::ty::layout::{LayoutError, LayoutOf, Size, TyLayout};
Expand Down Expand Up @@ -102,6 +103,7 @@ pub struct CodegenCx<'a, 'tcx: 'a> {
pub rust_try_fn: Cell<Option<ValueRef>>,

intrinsics: RefCell<FxHashMap<&'static str, ValueRef>>,
pub plugin_intrinsics: Arc<PluginIntrinsics>,

/// A counter that is used for generating local symbol names
local_gen_sym_counter: Cell<usize>,
Expand Down Expand Up @@ -306,6 +308,7 @@ impl<'a, 'tcx> CodegenCx<'a, 'tcx> {
eh_unwind_resume: Cell::new(None),
rust_try_fn: Cell::new(None),
intrinsics: RefCell::new(FxHashMap()),
plugin_intrinsics: tcx.sess.plugin_intrinsics.get(),
local_gen_sym_counter: Cell::new(0),
};
cx.isize_ty = Type::isize(&cx);
Expand Down
32 changes: 32 additions & 0 deletions src/librustc_codegen_llvm/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,38 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
return;
}

if let Some(intrinsic) = intrinsic {
if let Some(trans) = bx.cx.plugin_intrinsics.get(intrinsic) {
// Get the extra statements from the plugin:
let &(ref dest, target) = destination
.as_ref()
.unwrap();

let mut extra_stmts = Vec::new();
trans.codegen_simple_intrinsic(bx.tcx(),
terminator.source_info,
&sig,
self.mir,
self.param_substs,
args,
dest.clone(),
&mut extra_stmts);

// Now, codegen:
for stmt_kind in extra_stmts.into_iter() {
let stmt = mir::Statement {
source_info: terminator.source_info,
kind: stmt_kind,
};
bx = self.codegen_statement(bx, &stmt);
}

// Lastly, jump to the target block:
funclet_br(self, bx, target);
return;
}
}

let extra_args = &args[sig.inputs().len()..];
let extra_args = extra_args.iter().map(|op_arg| {
let op_ty = op_arg.ty(self.mir, bx.tcx());
Expand Down
53 changes: 53 additions & 0 deletions src/librustc_data_structures/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, atomic::AtomicPtr, atomic, };
use owning_ref::{Erased, OwningRef};

pub fn serial_join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
Expand Down Expand Up @@ -751,3 +752,55 @@ impl<T> DerefMut for OneThread<T> {
&mut self.inner
}
}

/// Provides atomic mutability by replacing the value inside this `ArcCell`.
/// Similar to the `crossbeam` structure of the same name.
#[derive(Debug)]
pub struct ArcCell<T>(AtomicPtr<T>);
impl<T> ArcCell<T> {
pub fn new(v: Arc<T>) -> Self {
ArcCell(AtomicPtr::new(Arc::into_raw(v) as *mut _))
}
pub fn get(&self) -> Arc<T> {
let ptr = self.0.load(atomic::Ordering::Acquire);
let arc = unsafe { Arc::from_raw(ptr as *const T) };
let ret = arc.clone();
// don't drop our copy:
::std::mem::forget(arc);
ret
}
/// Update the value, returning the previous value.
pub fn set(&self, v: Arc<T>) -> Arc<T> {
let new = Arc::into_raw(v) as *mut _;
let mut expected = self.0.load(atomic::Ordering::Acquire);
loop {
match self.0.compare_exchange_weak(expected, new,
atomic::Ordering::SeqCst,
atomic::Ordering::Acquire) {
Ok(old) => {
return unsafe { Arc::from_raw(old as *const T) };
},
Err(v) => {
expected = v;
},
}
}
}
}
impl<T> Drop for ArcCell<T> {
fn drop(&mut self) {
let ptr = self.0.load(atomic::Ordering::Acquire);
// drop our copy of the arc:
unsafe { Arc::from_raw(ptr as *const _) };
}
}
impl<T> Clone for ArcCell<T> {
fn clone(&self) -> Self {
ArcCell::new(self.get())
}
}
impl<T> From<Arc<T>> for ArcCell<T> {
fn from(v: Arc<T>) -> Self {
Self::new(v)
}
}
4 changes: 3 additions & 1 deletion src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use std::io::{self, Write};
use std::iter;
use std::path::{Path, PathBuf};
use rustc_data_structures::sync::{self, Lrc, Lock};
use std::sync::mpsc;
use std::sync::{mpsc, Arc, };
use syntax::{self, ast, attr, diagnostics, visit};
use syntax::ext::base::ExtCtxt;
use syntax::fold::Folder;
Expand Down Expand Up @@ -896,6 +896,7 @@ where
lint_groups,
llvm_passes,
attributes,
intrinsics,
..
} = registry;

Expand All @@ -914,6 +915,7 @@ where

*sess.plugin_llvm_passes.borrow_mut() = llvm_passes;
*sess.plugin_attributes.borrow_mut() = attributes.clone();
sess.plugin_intrinsics.set(Arc::new(intrinsics));
})?;

// Lint plugins are registered; now we can process command line flags.
Expand Down
16 changes: 16 additions & 0 deletions src/librustc_plugin/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//! Used by plugin crates to tell `rustc` about the plugins they provide.

use rustc::lint::{EarlyLintPassObject, LateLintPassObject, LintId, Lint};
use rustc::mir::{PluginIntrinsics, PluginIntrinsicCodegen};
use rustc::session::Session;

use syntax::ext::base::{SyntaxExtension, NamedSyntaxExtension, NormalTT, IdentTT};
Expand Down Expand Up @@ -61,6 +62,9 @@ pub struct Registry<'a> {
#[doc(hidden)]
pub attributes: Vec<(String, AttributeType)>,

#[doc(hidden)]
pub intrinsics: PluginIntrinsics,

whitelisted_custom_derives: Vec<ast::Name>,
}

Expand All @@ -77,6 +81,7 @@ impl<'a> Registry<'a> {
lint_groups: HashMap::new(),
llvm_passes: vec![],
attributes: vec![],
intrinsics: HashMap::new(),
whitelisted_custom_derives: Vec::new(),
}
}
Expand All @@ -95,6 +100,17 @@ impl<'a> Registry<'a> {
self.args_hidden.as_ref().map(|v| &v[..]).unwrap_or(&[])
}

/// Register a plugin intrinsic. Ignored if `name` is a normal Rust intrinsic.
///
/// When a function call to the named intrinsic is made, codegen (only LLVM, currently)
/// will replace the usual function call with the extra statements provided by the passed
/// trait object. It will then branch directly to the exit block. It is highly unsafe. Do not
/// use lightly.
pub fn register_intrinsic(&mut self, name: String,
codegen: Box<PluginIntrinsicCodegen>) {
self.intrinsics.insert(name, codegen);
}

/// Register a syntax extension of any kind.
///
/// This is the most general hook into `libsyntax`'s expansion behavior.
Expand Down
12 changes: 10 additions & 2 deletions src/librustc_typeck/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,20 @@ pub fn check_intrinsic_type<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
}

ref other => {
struct_span_err!(tcx.sess, it.span, E0093,
let plugin_intrinsics = tcx.sess.plugin_intrinsics.get();
if let Some(plugin) = plugin_intrinsics.get(*other) {
(plugin.generic_parameter_count(tcx),
plugin.inputs(tcx),
plugin.output(tcx))
} else {
struct_span_err!(tcx.sess, it.span, E0093,
"unrecognized intrinsic function: `{}`",
*other)
.span_label(it.span, "unrecognized intrinsic")
.emit();
return;
return;

}
}
};
(n_tps, inputs, output)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2018 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.

// force-host

#![feature(plugin_registrar, rustc_private)]
#![deny(plugin_as_library)] // should have no effect in a plugin crate

extern crate rustc;
extern crate rustc_plugin;


use rustc::mir::*;
use rustc::ty::{Ty, TyCtxt, FnSig, subst::Substs, };
use rustc_plugin::Registry;

#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
let codegen = Box::new(GenericCountMismatch) as Box<_>;
reg.register_intrinsic("generic_count_mismatch".into(), codegen);
let codegen = Box::new(InputOutputMismatch) as Box<_>;
reg.register_intrinsic("type_mismatch".into(), codegen);
}

struct GenericCountMismatch;
impl PluginIntrinsicCodegen for GenericCountMismatch {
fn codegen_simple_intrinsic<'a, 'tcx>(&self,
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
_source_info: SourceInfo,
_sig: &FnSig<'tcx>,
_parent_mir: &Mir<'tcx>,
_parent_param_substs: &'tcx Substs<'tcx>,
_args: &Vec<Operand<'tcx>>,
_dest: Place<'tcx>,
_extra_stmts: &mut Vec<StatementKind<'tcx>>)
where 'tcx: 'a,
{
unreachable!()
}

/// The number of generic parameters expected.
fn generic_parameter_count<'a, 'tcx>(&self, _tcx: TyCtxt<'a, 'tcx, 'tcx>) -> usize { 5 }
/// The types of the input args.
fn inputs<'a, 'tcx>(&self, _tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<Ty<'tcx>> { vec![] }
/// The return type.
fn output<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
tcx.mk_nil()
}
}

struct InputOutputMismatch;
impl PluginIntrinsicCodegen for InputOutputMismatch {
fn codegen_simple_intrinsic<'a, 'tcx>(&self,
_tcx: TyCtxt<'a, 'tcx, 'tcx>,
_source_info: SourceInfo,
_sig: &FnSig<'tcx>,
_parent_mir: &Mir<'tcx>,
_parent_param_substs: &'tcx Substs<'tcx>,
_args: &Vec<Operand<'tcx>>,
_dest: Place<'tcx>,
_extra_stmts: &mut Vec<StatementKind<'tcx>>)
where 'tcx: 'a,
{
unreachable!()
}

/// The number of generic parameters expected.
fn generic_parameter_count<'a, 'tcx>(&self, _tcx: TyCtxt<'a, 'tcx, 'tcx>) -> usize { 0 }
/// The types of the input args.
fn inputs<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Vec<Ty<'tcx>> {
vec![tcx.types.u64]
}
/// The return type.
fn output<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>) -> Ty<'tcx> {
tcx.types.u64
}
}
Loading