Skip to content

Commit

Permalink
compiles
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Feb 12, 2024
1 parent 2cc7df1 commit 3ac546e
Showing 1 changed file with 89 additions and 69 deletions.
158 changes: 89 additions & 69 deletions crates/concrete_codegen_mlir/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,17 @@ pub fn compile_function(ctx: FunctionCodegenCtx) -> Result<(), Box<dyn std::erro
entry_block.append_operation(cf::br(&blocks[0], &[], Location::unknown(ctx.context())));

for (block, mlir_block) in body.basic_blocks.iter().zip(blocks.iter()) {
let mut ops: Vec<Operation> = Vec::new();
for statement in &block.statements {
match &statement.kind {
concrete_ir::StatementKind::Assign(place, rvalue) => {
let (value, ty) = compile_rvalue(&ctx, &mut ops, rvalue, &locals);
compile_store_place(&ctx, &mut ops, place, *locals.get(&place.local).unwrap(), value);
let (value, ty) = compile_rvalue(&ctx, mlir_block, rvalue, &locals);
compile_store_place(
&ctx,
mlir_block,
place,
*locals.get(&place.local).unwrap(),
value,
);
}
concrete_ir::StatementKind::StorageLive(_) => todo!(),
concrete_ir::StatementKind::StorageDead(_) => todo!(),
Expand Down Expand Up @@ -240,12 +245,12 @@ pub fn compile_function(ctx: FunctionCodegenCtx) -> Result<(), Box<dyn std::erro

fn compile_rvalue<'c: 'b, 'b>(
ctx: &'c FunctionCodegenCtx,
ops: &'b mut Vec<Operation<'c>>,
block: &'b Block<'c>,
info: &Rvalue,
locals: &HashMap<usize, Value<'c, '_>>,
) -> (Value<'c, 'b>, TyKind) {
match info {
Rvalue::Use(info) => compile_load_operand(ctx, ops, info, locals),
Rvalue::Use(info) => compile_load_operand(ctx, block, info, locals),
Rvalue::LogicOp(_, _) => todo!(),
Rvalue::BinaryOp(_, _) => todo!(),
Rvalue::UnaryOp(_, _) => todo!(),
Expand All @@ -255,16 +260,16 @@ fn compile_rvalue<'c: 'b, 'b>(

fn compile_load_operand<'c: 'b, 'b>(
ctx: &'c FunctionCodegenCtx,
ops: &'b mut Vec<Operation<'c>>,
block: &'b Block<'c>,
info: &Operand,
locals: &HashMap<usize, Value<'c, '_>>,
) -> (Value<'c, 'b>, TyKind) {
match info {
Operand::Place(info) => compile_load_place(ctx, ops, info, locals),
Operand::Place(info) => compile_load_place(ctx, block, info, locals),
Operand::Const(data) => match &data.data {
concrete_ir::ConstKind::Param(_) => todo!(),
concrete_ir::ConstKind::Value(value) => {
(compile_value_tree(ctx, ops, value), data.ty.clone())
(compile_value_tree(ctx, block, value), data.ty.clone())
}
concrete_ir::ConstKind::Expr(_) => todo!(),
},
Expand All @@ -273,12 +278,12 @@ fn compile_load_operand<'c: 'b, 'b>(

fn compile_store_place<'c: 'b, 'b>(
ctx: &'c FunctionCodegenCtx,
ops: &'b mut Vec<Operation<'c>>,
block: &'b Block<'c>,
info: &Place,
ptr: Value<'c, 'b>,
value: Value<'c, 'b>,
) {
ops.push(memref::store(
block.append_operation(memref::store(
value,
ptr,
&[],
Expand All @@ -288,153 +293,168 @@ fn compile_store_place<'c: 'b, 'b>(

fn compile_load_place<'c: 'b, 'b>(
ctx: &'c FunctionCodegenCtx,
ops: &'b mut Vec<Operation<'c>>,
block: &'b Block<'c>,
info: &Place,
locals: &HashMap<usize, Value<'c, '_>>,
) -> (Value<'c, 'b>, TyKind) {
let ptr = locals.get(&info.local).unwrap();
let body = ctx.get_fn_body();
let local_ty = body.locals[info.local].ty.kind.clone();
ops.push(memref::load(*ptr, &[], Location::unknown(ctx.context())));
(ops.last().unwrap().result(0).unwrap().into(), local_ty)
let val = block
.append_operation(memref::load(*ptr, &[], Location::unknown(ctx.context())))
.result(0)
.unwrap()
.into();
(val, local_ty)
}

fn compile_value_tree<'c: 'b, 'b>(
ctx: &'c FunctionCodegenCtx,
ops: &'b mut Vec<Operation<'c>>,
block: &'b Block<'c>,
value: &ValueTree,
) -> Value<'c, 'b> {
match value {
ValueTree::Leaf(value) => match value {
concrete_ir::ConstValue::Bool(value) => {
ops.push(arith::constant(
concrete_ir::ConstValue::Bool(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 1).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::I8(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::I8(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 8).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::I16(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::I16(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 16).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::I32(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::I32(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 32).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::I64(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::I64(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new((*value), IntegerType::new(ctx.context(), 64).into())
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::I128(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::I128(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 128).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::U8(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::U8(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 8).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::U16(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::U16(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 16).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::U32(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::U32(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 32).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::U64(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::U64(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 64).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
concrete_ir::ConstValue::U128(value) => {
ops.push(arith::constant(
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::U128(value) => block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(
(*value) as i64,
IntegerType::new(ctx.context(), 128).into(),
)
.into(),
Location::unknown(ctx.context()),
));
ops.last().unwrap().result(0).unwrap().into()
}
))
.result(0)
.unwrap()
.into(),
concrete_ir::ConstValue::F32(_) => todo!(),
concrete_ir::ConstValue::F64(_) => todo!(),
},
Expand Down

0 comments on commit 3ac546e

Please sign in to comment.