Skip to content

Commit

Permalink
Allow building SGX modules
Browse files Browse the repository at this point in the history
  • Loading branch information
nhynes committed Sep 26, 2018
1 parent 2c7117c commit 7118bdf
Show file tree
Hide file tree
Showing 15 changed files with 1,124 additions and 200 deletions.
26 changes: 14 additions & 12 deletions rust/.rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
max_width = 100
hard_tabs = false
tab_spaces = 2
newline_style = "Unix"
use_small_heuristics = true
newline_style = "Auto"
use_small_heuristics = "Default"
indent_style = "Block"
wrap_comments = false
comment_width = 80
normalize_comments = false
format_strings = true
format_strings = false
format_macro_matchers = false
format_macro_bodies = true
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
Expand All @@ -22,9 +24,8 @@ type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
spaces_within_parens_and_brackets = false
binop_separator = "Front"
remove_blank_lines_at_start_or_end_of_block = true
remove_nested_parens = true
combine_control_expr = true
struct_field_align_threshold = 0
match_arm_blocks = true
Expand All @@ -37,21 +38,22 @@ trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "Edition2015"
merge_derives = true
use_try_shorthand = true
condense_wildcard_suffixes = true
force_explicit_abi = true
use_field_init_shorthand = false
write_mode = "Overwrite"
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
required_version = "0.6.1"
required_version = "0.99.4"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = true
error_on_unformatted = true
error_on_line_overflow = false
error_on_unformatted = false
report_todo = "Never"
report_fixme = "Never"
ignore = []
verbose_diff = false
emit_mode = "Files"
make_backup = false
14 changes: 6 additions & 8 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ name = "tvm"
version = "0.1.0"
license = "Apache-2.0"
description = "TVM Rust runtime"
repository = "https://github.com/nhynes/tvm-rs"
repository = "https://github.com/dmlc/tvm"
readme = "README.md"
keywords = ["tvm", "nnvm"]
categories = ["api-bindings", "science"]
authors = ["Nick Hynes <nhynes@berkeley.edu>"]

[features]
par-launch-alloc = []
default = ["nom/std"]
sgx = ["nom/alloc"]

[dependencies]
bounded-spsc-queue = "0.4.0"
error-chain = { version = "0.12.0", default-features = false }
itertools = "0.7.8"
lazy_static = "1.0.0"
lazy_static = "1.1.0"
ndarray = "0.11.2"
nom = "4.0.0"
nom = {version = "4.0.0", default-features = false }
serde = "1.0.59"
serde_derive = "1.0.59"
serde_derive = "1.0.79"
serde_json = "1.0.17"

[target.'cfg(not(target_env = "sgx"))'.dependencies]
num_cpus = "1.8.0"

[build-dependencies]
bindgen = "0.37"
47 changes: 0 additions & 47 deletions rust/build.rs

This file was deleted.

13 changes: 11 additions & 2 deletions rust/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{alloc, num};
#[cfg(target_env = "sgx")]
use alloc::alloc;
#[cfg(not(target_env = "sgx"))]
use std::alloc;
use std::num;

use ndarray;
use serde_json;
Expand All @@ -22,9 +26,14 @@ error_chain! {
}
foreign_links {
Alloc(alloc::AllocErr);
Layout(alloc::LayoutErr);
GraphDeserialize(serde_json::Error);
ParseInt(num::ParseIntError);
ShapeError(ndarray::ShapeError);
}
}

impl From<alloc::LayoutErr> for Error {
fn from(_err: alloc::LayoutErr) -> Error {
Error::from_kind(ErrorKind::Msg("Layout error".to_string()))
}
}
27 changes: 24 additions & 3 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#![feature(allocator_api, box_syntax, fn_traits, try_from, unboxed_closures)]
#![feature(
alloc,
allocator_api,
box_syntax,
extern_prelude,
fn_traits,
try_from,
unboxed_closures,
vec_remove_item
)]

#[cfg(target_env = "sgx")]
extern crate alloc;
extern crate bounded_spsc_queue;
#[cfg(target_env = "sgx")]
extern crate core;
#[macro_use]
extern crate error_chain;
#[macro_use]
Expand All @@ -18,12 +31,20 @@ extern crate serde_derive;
extern crate serde_json;

pub mod ffi {
#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals, unused)]
#![allow(
non_camel_case_types,
non_snake_case,
non_upper_case_globals,
unused
)]

pub mod runtime {
use std::os::raw::{c_char, c_int, c_void};

include!(concat!(env!("OUT_DIR"), "/c_runtime_api.rs"));
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/runtime/c_runtime_api.rs"
));

pub type BackendPackedCFunc =
extern "C" fn(args: *const TVMValue, type_codes: *const c_int, num_args: c_int) -> c_int;
Expand Down
3 changes: 3 additions & 0 deletions rust/src/runtime/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(target_env = "sgx")]
use alloc::alloc::{self, Layout};
#[cfg(not(target_env = "sgx"))]
use std::alloc::{self, Layout};

use errors::*;
Expand Down
16 changes: 9 additions & 7 deletions rust/src/runtime/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use std::{
convert::TryFrom,
mem,
os::raw::{c_int, c_void},
ptr,
slice,
ptr, slice,
};

use ndarray;
Expand Down Expand Up @@ -172,8 +171,7 @@ impl<'a> Tensor<'a> {
expected_stride * (*shape as usize),
)
},
)
.0
).0
}
}
}
Expand Down Expand Up @@ -254,7 +252,11 @@ macro_rules! impl_ndarray_try_from_tensor {
tensor.dtype
);
Ok(ndarray::Array::from_shape_vec(
tensor.shape.iter().map(|s| *s as usize).collect::<Vec<usize>>(),
tensor
.shape
.iter()
.map(|s| *s as usize)
.collect::<Vec<usize>>(),
tensor.to_vec::<$type>(),
)?)
}
Expand Down Expand Up @@ -347,12 +349,12 @@ macro_rules! make_dtype_const {
bits: $bits,
lanes: $lanes,
};
}
};
}

make_dtype_const!(DTYPE_INT32, DLDataTypeCode_kDLInt, 32, 1);
make_dtype_const!(DTYPE_UINT32, DLDataTypeCode_kDLUInt, 32, 1);
make_dtype_const!(DTYPE_FLOAT16, DLDataTypeCode_kDLFloat, 16, 1);
// make_dtype_const!(DTYPE_FLOAT16, DLDataTypeCode_kDLFloat, 16, 1);
make_dtype_const!(DTYPE_FLOAT32, DLDataTypeCode_kDLFloat, 32, 1);
make_dtype_const!(DTYPE_FLOAT64, DLDataTypeCode_kDLFloat, 64, 1);

Expand Down
Loading

0 comments on commit 7118bdf

Please sign in to comment.