core: Add size_t to primitive store

Used for ndims in ndarray.
pull/371/head
David Mak 2023-12-15 14:02:30 +08:00
parent 1c3a823670
commit bd792904f9
6 changed files with 38 additions and 12 deletions

View File

@ -63,6 +63,17 @@ enum Isa {
CortexA9, CortexA9,
} }
impl Isa {
/// Returns the number of bits in `size_t` for the [`Isa`].
fn get_size_type(&self) -> u32 {
if self == &Isa::Host {
64u32
} else {
32u32
}
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct PrimitivePythonId { pub struct PrimitivePythonId {
int: u64, int: u64,
@ -277,9 +288,11 @@ impl Nac3 {
py: Python, py: Python,
link_fn: &dyn Fn(&Module) -> PyResult<T>, link_fn: &dyn Fn(&Module) -> PyResult<T>,
) -> PyResult<T> { ) -> PyResult<T> {
let size_t = self.isa.get_size_type();
let (mut composer, mut builtins_def, mut builtins_ty) = TopLevelComposer::new( let (mut composer, mut builtins_def, mut builtins_ty) = TopLevelComposer::new(
self.builtins.clone(), self.builtins.clone(),
ComposerConfig { kernel_ann: Some("Kernel"), kernel_invariant_ann: "KernelInvariant" }, ComposerConfig { kernel_ann: Some("Kernel"), kernel_invariant_ann: "KernelInvariant" },
size_t,
); );
let builtins = PyModule::import(py, "builtins")?; let builtins = PyModule::import(py, "builtins")?;
@ -792,7 +805,7 @@ impl Nac3 {
Isa::RiscV32IMA => &timeline::NOW_PINNING_TIME_FNS, Isa::RiscV32IMA => &timeline::NOW_PINNING_TIME_FNS,
Isa::CortexA9 | Isa::Host => &timeline::EXTERN_TIME_FNS, Isa::CortexA9 | Isa::Host => &timeline::EXTERN_TIME_FNS,
}; };
let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; let primitive: PrimitiveStore = TopLevelComposer::make_primitives(isa.get_size_type()).0;
let builtins = vec![ let builtins = vec![
( (
"now_mu".into(), "now_mu".into(),

View File

@ -614,6 +614,7 @@ pub fn gen_func_impl<'ctx, G: CodeGenerator, F: FnOnce(&mut G, &mut CodeGenConte
str: unifier.get_representative(primitives.str), str: unifier.get_representative(primitives.str),
exception: unifier.get_representative(primitives.exception), exception: unifier.get_representative(primitives.exception),
option: unifier.get_representative(primitives.option), option: unifier.get_representative(primitives.option),
..primitives
}; };
let mut type_cache: HashMap<_, _> = [ let mut type_cache: HashMap<_, _> = [

View File

@ -37,12 +37,8 @@ pub struct TopLevelComposer {
// number of built-in function and classes in the definition list, later skip // number of built-in function and classes in the definition list, later skip
pub builtin_num: usize, pub builtin_num: usize,
pub core_config: ComposerConfig, pub core_config: ComposerConfig,
} /// The size of a native word on the target platform.
pub size_t: u32,
impl Default for TopLevelComposer {
fn default() -> Self {
Self::new(vec![], ComposerConfig::default()).0
}
} }
impl TopLevelComposer { impl TopLevelComposer {
@ -52,8 +48,9 @@ impl TopLevelComposer {
pub fn new( pub fn new(
builtins: Vec<(StrRef, FunSignature, Arc<GenCall>)>, builtins: Vec<(StrRef, FunSignature, Arc<GenCall>)>,
core_config: ComposerConfig, core_config: ComposerConfig,
size_t: u32,
) -> (Self, HashMap<StrRef, DefinitionId>, HashMap<StrRef, Type>) { ) -> (Self, HashMap<StrRef, DefinitionId>, HashMap<StrRef, Type>) {
let mut primitives = Self::make_primitives(); let mut primitives = Self::make_primitives(size_t);
let mut definition_ast_list = builtins::get_builtins(&mut primitives); let mut definition_ast_list = builtins::get_builtins(&mut primitives);
let primitives_ty = primitives.0; let primitives_ty = primitives.0;
let mut unifier = primitives.1; let mut unifier = primitives.1;
@ -146,6 +143,7 @@ impl TopLevelComposer {
defined_names, defined_names,
method_class, method_class,
core_config, core_config,
size_t,
}, },
builtin_id, builtin_id,
builtin_ty, builtin_ty,

View File

@ -44,7 +44,7 @@ impl TopLevelDef {
impl TopLevelComposer { impl TopLevelComposer {
#[must_use] #[must_use]
pub fn make_primitives() -> (PrimitiveStore, Unifier) { pub fn make_primitives(size_t: u32) -> (PrimitiveStore, Unifier) {
let mut unifier = Unifier::new(); let mut unifier = Unifier::new();
let int32 = unifier.add_ty(TypeEnum::TObj { let int32 = unifier.add_ty(TypeEnum::TObj {
obj_id: DefinitionId(0), obj_id: DefinitionId(0),
@ -144,6 +144,7 @@ impl TopLevelComposer {
str, str,
exception, exception,
option, option,
size_t,
}; };
unifier.put_primitive_store(&primitives); unifier.put_primitive_store(&primitives);
crate::typecheck::magic_methods::set_primitives_magic_methods(&primitives, &mut unifier); crate::typecheck::magic_methods::set_primitives_magic_methods(&primitives, &mut unifier);

View File

@ -41,6 +41,18 @@ pub struct PrimitiveStore {
pub str: Type, pub str: Type,
pub exception: Type, pub exception: Type,
pub option: Type, pub option: Type,
pub size_t: u32,
}
impl PrimitiveStore {
/// Returns a [Type] representing `size_t`.
pub fn usize(&self) -> Type {
match self.size_t {
32 => self.uint32,
64 => self.uint64,
_ => unreachable!(),
}
}
} }
pub struct FunctionData { pub struct FunctionData {

View File

@ -286,6 +286,7 @@ fn main() {
// The default behavior for -O<n> where n>3 defaults to O3 for both Clang and GCC // The default behavior for -O<n> where n>3 defaults to O3 for both Clang and GCC
_ => OptimizationLevel::Aggressive, _ => OptimizationLevel::Aggressive,
}; };
const SIZE_T: u32 = 64;
let program = match fs::read_to_string(file_name.clone()) { let program = match fs::read_to_string(file_name.clone()) {
Ok(program) => program, Ok(program) => program,
@ -295,9 +296,9 @@ fn main() {
} }
}; };
let primitive: PrimitiveStore = TopLevelComposer::make_primitives().0; let primitive: PrimitiveStore = TopLevelComposer::make_primitives(SIZE_T).0;
let (mut composer, builtins_def, builtins_ty) = let (mut composer, builtins_def, builtins_ty) =
TopLevelComposer::new(vec![], ComposerConfig::default()); TopLevelComposer::new(vec![], ComposerConfig::default(), SIZE_T);
let internal_resolver: Arc<ResolverInternal> = ResolverInternal { let internal_resolver: Arc<ResolverInternal> = ResolverInternal {
id_to_type: builtins_ty.into(), id_to_type: builtins_ty.into(),
@ -400,7 +401,7 @@ fn main() {
membuffer.lock().push(buffer); membuffer.lock().push(buffer);
}))); })));
let threads = (0..threads) let threads = (0..threads)
.map(|i| Box::new(DefaultCodeGenerator::new(format!("module{i}"), 64))) .map(|i| Box::new(DefaultCodeGenerator::new(format!("module{i}"), SIZE_T)))
.collect(); .collect();
let (registry, handles) = WorkerRegistry::create_workers(threads, top_level, &llvm_options, &f); let (registry, handles) = WorkerRegistry::create_workers(threads, top_level, &llvm_options, &f);
registry.add_task(task); registry.add_task(task);