Skip to content

Commit

Permalink
merge: global2local pass
Browse files Browse the repository at this point in the history
Global2Local dev
  • Loading branch information
JuniMay authored Jul 26, 2024
2 parents 2281ed3 + d23398b commit c3e1196
Show file tree
Hide file tree
Showing 9 changed files with 954 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/bin/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use orzcc::{
loops::{LoopInvariantMotion, LoopUnroll, LOOP_INVARIANT_MOTION, LOOP_UNROLL},
mem2reg::{Mem2reg, MEM2REG},
simple_dce::{SimpleDce, SIMPLE_DCE},
global2local::{Global2Local, GLOBAL2LOCAL}
},
passman::{PassManager, Pipeline, TransformPass},
},
Expand Down Expand Up @@ -71,12 +72,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
std::fs::write(emit_typed_ast, format!("{:#?}", ast))?;
}

let mut ir = sysy::irgen(&ast);
let mut ir = sysy::irgen(&ast);

if cmd.opt > 0 {
passman.run_transform(GLOBAL2LOCAL, &mut ir, 1);
passman.run_transform(MEM2REG, &mut ir, 1);

let mut opt_pipeline = Pipeline::default();
opt_pipeline.add_pass(GLOBAL2LOCAL);
opt_pipeline.add_pass(CFG_SIMPLIFY);
opt_pipeline.add_pass(CONSTANT_FOLDING);
opt_pipeline.add_pass(SIMPLE_DCE);
Expand Down Expand Up @@ -147,6 +150,7 @@ fn register_passes(passman: &mut PassManager) {
ConstantFolding::register(passman);
InstCombine::register(passman);
Inline::register(passman);
Global2Local::register(passman);

LoopInvariantMotion::register(passman);
LoopUnroll::register(passman);
Expand Down
7 changes: 7 additions & 0 deletions src/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ impl Constant {
pub fn source_span(&self) -> Span { self.1 }

pub fn kind(&self) -> &ConstantKind { &self.0 }

pub fn get_bytes(&self) -> Option<&Vec<u8>> {
match self.kind() {
ConstantKind::Bytes(bytes) => Some(bytes),
_ => None,
}
}
}

impl From<bool> for Constant {
Expand Down
16 changes: 15 additions & 1 deletion src/ir/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{debug::CommentPos, source_loc::Span, Block, Constant, Context, Signa
use crate::{
collections::{
linked_list::LinkedListContainerPtr,
storage::{ArenaAlloc, ArenaPtr, BaseArenaPtr},
storage::{ArenaAlloc, ArenaFree, ArenaPtr, BaseArenaPtr},
},
impl_arena,
utils::cfg::CfgRegion,
Expand Down Expand Up @@ -217,6 +217,20 @@ impl GlobalSlot {
pub fn comment(&self, ctx: &mut Context, pos: CommentPos, content: impl Into<String>) {
self.deref(ctx).name.clone().comment(ctx, pos, content);
}

pub fn remove(self, ctx: &mut Context) {
let symbol = self.name(ctx).clone();
// 如果:
// 1. symbols: HashMap<Symbol, SymbolKind, FxBuildHasher>中有以symbol为键的项
// 2. 该项的值是GlobalSlot类型
// 则删除该项及对应的GlobalSlot,并返回GlobalSlot
if let Some(SymbolKind::GlobalSlot(_)) = ctx.symbols.get(&symbol) {
ctx.symbols.remove(&symbol);
ctx.global_slots.free(self.0);
} else {
panic!("symbol {:?} is not a global slot", symbol);
}
}
}

pub struct DisplayGlobalSlot<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/ir/inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ pub enum InstKind {
CallIndirect(Signature),
/// Return instruction.
Ret,
/// Get global symbol as local pinter.
/// Get global symbol as local pointer.
///
/// This applies to function and global slot. The corresponding instruction
/// in RISC-V is `LA`, i.e., load address (`LA` a pseudo instruction)
Expand Down
Loading

0 comments on commit c3e1196

Please sign in to comment.