Skip to content

Commit 4def624

Browse files
committed
Refactored IR to use arenas
1 parent 9e6ad77 commit 4def624

File tree

28 files changed

+263
-201
lines changed

28 files changed

+263
-201
lines changed

Cargo.lock

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/build_ir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
append-only-vec.workspace = true
8+
arena = { version = "0.1.0", path = "../../support/arena" }
89
asg = { version = "0.1.0", path = "../../representations/asg" }
910
attributes = { version = "0.1.0", path = "../../representations/attributes" }
1011
compiler = { version = "0.1.0", path = "../../support/compiler" }

src/components/build_ir/src/funcs.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
use crate::error::{LowerError, LowerErrorKind};
22
use append_only_vec::AppendOnlyVec;
3+
use arena::{Arena, LockFreeArena};
34
use asg::PolyRecipe;
45
use attributes::Tag;
56
use std::{borrow::Borrow, cell::OnceCell, collections::HashMap, sync::RwLock};
67

78
#[derive(Default)]
89
pub struct Funcs {
9-
funcs: AppendOnlyVec<ir::Func>,
10+
funcs: LockFreeArena<ir::FuncId, ir::Func>,
1011
monomorphized: RwLock<HashMap<(asg::FuncRef, PolyRecipe), ir::FuncRef>>,
1112
jobs: AppendOnlyVec<(asg::FuncRef, PolyRecipe, ir::FuncRef)>,
1213
interpreter_entry_point: OnceCell<ir::FuncRef>,
1314
}
1415

1516
impl Funcs {
16-
pub fn build(self) -> ir::Funcs {
17-
ir::Funcs::new(self.funcs.into_iter().collect())
17+
pub fn build(self) -> Arena<ir::FuncId, ir::Func> {
18+
self.funcs.into_arena()
1819
}
1920

2021
pub fn insert(&self, func_ref: asg::FuncRef, function: ir::Func) -> ir::FuncRef {
21-
let index = self.funcs.len();
22-
self.funcs.push(function);
23-
let ir_func_ref = ir::FuncRef::new(index);
22+
let ir_func_ref = self.funcs.alloc(function);
2423
self.monomorphized
2524
.write()
2625
.unwrap()
@@ -62,7 +61,7 @@ impl Funcs {
6261
}
6362

6463
pub fn get_mut(&mut self, key: ir::FuncRef) -> &mut ir::Func {
65-
&mut self.funcs[key.get()]
64+
&mut self.funcs[key]
6665
}
6766

6867
pub fn monomorphized<'a>(

src/components/build_ir/src/globals.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use append_only_vec::AppendOnlyVec;
1+
use arena::{Arena, LockFreeArena};
22
use std::{collections::HashMap, sync::RwLock};
33

44
#[derive(Debug, Default)]
55
pub struct Globals {
6-
globals: AppendOnlyVec<ir::Global>,
6+
globals: LockFreeArena<ir::GlobalId, ir::Global>,
77
lowered: RwLock<HashMap<asg::GlobalRef, ir::GlobalRef>>,
88
}
99

1010
impl Globals {
11-
pub fn build(self) -> ir::Globals {
12-
ir::Globals::new(self.globals.into_iter().collect())
11+
pub fn build(self) -> Arena<ir::GlobalId, ir::Global> {
12+
self.globals.into_arena()
1313
}
1414

1515
pub fn translate(&self, key: asg::GlobalRef) -> ir::GlobalRef {
@@ -22,7 +22,7 @@ impl Globals {
2222
}
2323

2424
pub fn insert(&self, asg_global_ref: asg::GlobalRef, global: ir::Global) -> ir::GlobalRef {
25-
let ir_global_ref = ir::GlobalRef::new(self.globals.push(global));
25+
let ir_global_ref = self.globals.alloc(global);
2626

2727
self.lowered
2828
.write()

src/components/build_ir/src/structs.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use append_only_vec::AppendOnlyVec;
1+
use arena::{Arena, LockFreeArena};
22
use asg::PolyRecipe;
33
use std::{collections::HashMap, sync::RwLock};
44

55
#[derive(Debug, Default)]
66
pub struct Structs {
7-
structs: AppendOnlyVec<ir::Struct>,
7+
structs: LockFreeArena<ir::StructId, ir::Struct>,
88
monomorphized: RwLock<HashMap<(asg::StructRef, PolyRecipe), ir::StructRef>>,
99
}
1010

1111
impl Structs {
12-
pub fn build(self) -> ir::Structs {
13-
ir::Structs::new(self.structs.into_iter().collect())
12+
pub fn build(self) -> Arena<ir::StructId, ir::Struct> {
13+
self.structs.into_arena()
1414
}
1515

1616
pub fn insert(
@@ -19,7 +19,7 @@ impl Structs {
1919
structure: ir::Struct,
2020
poly_recipe: PolyRecipe,
2121
) -> ir::StructRef {
22-
let ir_struct_ref = ir::StructRef::new(self.structs.push(structure));
22+
let ir_struct_ref = self.structs.alloc(structure);
2323

2424
let key = (asg_struct_ref, poly_recipe);
2525
self.monomorphized

src/components/build_llvm_ir/src/abi/homo_aggregate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn is_homo_aggregate<'a>(
3939
})
4040
}
4141
ir::Type::Struct(struct_ref) => {
42-
let structure = ir_module.structs.get(*struct_ref);
42+
let structure = &ir_module.structs[*struct_ref];
4343

4444
is_homo_aggregate_record(
4545
decider,

src/components/build_llvm_ir/src/functions/block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ unsafe fn emit_call(
773773
fn_ctx: &FnCtx,
774774
value_catalog: &mut ValueCatalog,
775775
) -> Result<LLVMValueRef, BackendError> {
776-
let ir_function = ctx.ir_module.funcs.get(call.func);
776+
let ir_function = &ctx.ir_module.funcs[call.func];
777777

778778
let skeleton = ctx
779779
.func_skeletons

src/components/build_llvm_ir/src/functions/body.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct FnCtx {
3030

3131
pub unsafe fn create_function_bodies(ctx: &mut BackendCtx) -> Result<(), BackendError> {
3232
for (ir_func_ref, skeleton) in ctx.func_skeletons.iter() {
33-
let ir_function = ctx.ir_module.funcs.get(*ir_func_ref);
33+
let ir_function = &ctx.ir_module.funcs[*ir_func_ref];
3434

3535
let mut builder = Builder::new();
3636
let mut value_catalog = ValueCatalog::new(ir_function.basicblocks.len());

src/components/build_llvm_ir/src/functions/epilogue/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn emit_epilogue(
5454
});
5555
};
5656

57-
let ir_function = ctx.ir_module.funcs.get(skeleton.ir_func_ref);
57+
let ir_function = &ctx.ir_module.funcs[skeleton.ir_func_ref];
5858

5959
let ir_return_type = &ir_function.return_type;
6060
let abi_return_info = &abi_function.return_type;

src/components/build_llvm_ir/src/functions/prologue/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn emit_prologue(
3434
alloca_point: LLVMValueRef,
3535
entry_basicblock: LLVMBasicBlockRef,
3636
) -> Result<PrologueInfo, BackendError> {
37-
let ir_function = ctx.ir_module.funcs.get(skeleton.ir_func_ref);
37+
let ir_function = &ctx.ir_module.funcs[skeleton.ir_func_ref];
3838
let abi_return_info = &abi_function.return_type;
3939
let returns_ir_void = ir_function.return_type.is_void();
4040

@@ -105,7 +105,7 @@ pub fn emit_prologue(
105105
}
106106

107107
let mut param_values = ParamValues::new();
108-
let ir_function = ctx.ir_module.funcs.get(skeleton.ir_func_ref);
108+
let ir_function = &ctx.ir_module.funcs[skeleton.ir_func_ref];
109109

110110
assert_eq!(abi_function.parameter_types.len(), ir_function.params.len());
111111

src/components/build_llvm_ir/src/functions/return_location.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl ReturnLocation {
6969
inalloca: &InAlloca,
7070
inalloca_subtypes: &[LLVMTypeRef],
7171
) -> Result<Self, BackendError> {
72-
let ir_function = ctx.ir_module.funcs.get(skeleton.ir_func_ref);
72+
let ir_function = &ctx.ir_module.funcs[skeleton.ir_func_ref];
7373
let last_argument = unsafe { LLVMGetLastParam(skeleton.function) };
7474

7575
let inalloca_combined_struct = skeleton

src/components/build_llvm_ir/src/structure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub unsafe fn to_backend_struct_type(
1616

1717
// Get cached type or insert computed type
1818
ctx.struct_cache.cache.try_insert_cloned(struct_ref, |_| {
19-
let ir_structure = ctx.ir_module.structs.get(struct_ref);
19+
let ir_structure = &ctx.ir_module.structs[struct_ref];
2020

2121
ctx.visited.borrow_mut().insert(struct_ref);
2222
let mut subtypes =

src/components/interpret/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl<'a, S: SyscallHandler> Interpreter<'a, S> {
6060
func_ref: ir::FuncRef,
6161
args: Vec<Value<'a>>,
6262
) -> Result<Value<'a>, InterpreterError> {
63-
let function = self.ir_module.funcs.get(func_ref);
63+
let function = &self.ir_module.funcs[func_ref];
6464

6565
if function.is_cstyle_variadic {
6666
todo!(

src/components/interpret/src/size_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn size_of(ir_type: &ir::Type, ir_module: &ir::Module) -> u64 {
1717
ir::Type::Void => 0,
1818
ir::Type::Union(_) => todo!("interpreter write union"),
1919
ir::Type::Struct(struct_ref) => {
20-
let structure = ir_module.structs.get(*struct_ref);
20+
let structure = &ir_module.structs[*struct_ref];
2121

2222
// NOTE: We don't do alignment in the interpreter
2323
structure

src/components/job/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
use arena::{Arena, ArenaMap, Id, new_id_with_niche};
2-
use std::{collections::VecDeque, num::NonZero};
1+
2+
3+

src/representations/ir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2024"
55

66
[dependencies]
77
append-only-vec.workspace = true
8+
arena = { version = "0.1.0", path = "../../support/arena" }
89
attributes = { version = "0.1.0", path = "../attributes" }
910
data_units = { version = "0.1.0", path = "../../support/data_units" }
1011
derivative.workspace = true

src/representations/ir/src/funcs.rs

-58
This file was deleted.

src/representations/ir/src/global.rs

-47
This file was deleted.

src/representations/ir/src/instr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Type, funcs::FuncRef, global::GlobalRef, value::Value};
1+
use crate::{FuncRef, GlobalRef, Type, value::Value};
22
use primitives::{FloatOrInteger, FloatOrSign, IntegerBits, IntegerSign};
33

44
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)