Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array cast to pointer #131

Merged
merged 5 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 125 additions & 50 deletions crates/concrete_codegen_mlir/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ fn compile_rvalue<'c: 'b, 'b>(
let target_ty = target_ty.clone();
let target_mlir_ty = compile_type(ctx.module_ctx, &target_ty);
let (value, current_ty) = compile_load_operand(ctx, block, op, locals)?;
let is_signed = target_ty.kind.is_signed();
let is_signed = current_ty.kind.is_signed();

if target_ty.kind.is_ptr_like() {
// int to ptr
Expand All @@ -525,8 +525,35 @@ fn compile_rvalue<'c: 'b, 'b>(
.into();
(value, target_ty.clone())
} else if current_ty.kind.is_ptr_like() {
// ptr to ptr: noop
(value, target_ty.clone())
} else if current_ty.kind.is_array() {
edg-l marked this conversation as resolved.
Show resolved Hide resolved
let k1 = block
.append_operation(arith::constant(
ctx.context(),
IntegerAttribute::new(IntegerType::new(ctx.context(), 64).into(), 1)
.into(),
location,
))
.result(0)?
.into();
let ptr = block
.append_operation(
ods::llvm::alloca(
ctx.context(),
pointer(ctx.context(), 0),
k1,
TypeAttribute::new(compile_type(ctx.module_ctx, &current_ty)),
location,
)
.into(),
)
.result(0)?
.into();
block.append_operation(
ods::llvm::store(ctx.context(), value, ptr, location).into(),
);

(ptr, target_ty.clone())
} else {
unreachable!("cast from {:?} to ptr", current_ty.kind)
}
Expand Down Expand Up @@ -1050,11 +1077,6 @@ fn compile_store_place<'c: 'b, 'b>(
}
}
PlaceElem::Index(local) => {
local_ty = match local_ty.kind {
TyKind::Array(inner, _) => *inner,
_ => unreachable!(),
};

let place = Place {
local: *local,
projection: vec![],
Expand All @@ -1063,34 +1085,58 @@ fn compile_store_place<'c: 'b, 'b>(
let (index, _) = compile_load_place(ctx, block, &place, locals)?;

ptr = block
.append_operation(llvm::get_element_ptr_dynamic(
ctx.context(),
ptr,
&[index],
compile_type(ctx.module_ctx, &local_ty),
pointer(ctx.context(), 0),
Location::unknown(ctx.context()),
))
.append_operation(
{
let mut op = ods::llvm::getelementptr(
ctx.context(),
pointer(ctx.context(), 0),
ptr,
&[index],
DenseI32ArrayAttribute::new(ctx.context(), &[0, i32::MIN]),
TypeAttribute::new(compile_type(ctx.module_ctx, &local_ty)),
Location::unknown(ctx.context()),
);
op.set_inbounds(Attribute::unit(ctx.context()));
op
}
.into(),
)
.result(0)?
.into();
}
PlaceElem::ConstantIndex(index) => {

local_ty = match local_ty.kind {
TyKind::Array(inner, _) => *inner,
_ => unreachable!(),
};

}
PlaceElem::ConstantIndex(index) => {
ptr = block
.append_operation(llvm::get_element_ptr(
ctx.context(),
ptr,
DenseI32ArrayAttribute::new(ctx.context(), &[(*index).try_into().unwrap()]),
compile_type(ctx.module_ctx, &local_ty),
pointer(ctx.context(), 0),
Location::unknown(ctx.context()),
))
.append_operation(
{
let mut op = ods::llvm::getelementptr(
ctx.context(),
pointer(ctx.context(), 0),
ptr,
&[],
DenseI32ArrayAttribute::new(
ctx.context(),
&[0, (*index).try_into().unwrap()],
),
TypeAttribute::new(compile_type(ctx.module_ctx, &local_ty)),
Location::unknown(ctx.context()),
);
op.set_inbounds(Attribute::unit(ctx.context()));
op
}
.into(),
)
.result(0)?
.into();

local_ty = match local_ty.kind {
TyKind::Array(inner, _) => *inner,
_ => unreachable!(),
};
}
}
}
Expand Down Expand Up @@ -1163,11 +1209,6 @@ fn compile_load_place<'c: 'b, 'b>(
}
}
PlaceElem::Index(local) => {
local_ty = match local_ty.kind {
TyKind::Array(inner, _) => *inner,
_ => unreachable!(),
};

let place = Place {
local: *local,
projection: Default::default(),
Expand All @@ -1176,33 +1217,58 @@ fn compile_load_place<'c: 'b, 'b>(
let (index, _) = compile_load_place(ctx, block, &place, locals)?;

ptr = block
.append_operation(llvm::get_element_ptr_dynamic(
ctx.context(),
ptr,
&[index],
compile_type(ctx.module_ctx, &local_ty),
pointer(ctx.context(), 0),
Location::unknown(ctx.context()),
))
.append_operation(
{
let mut op = ods::llvm::getelementptr(
ctx.context(),
pointer(ctx.context(), 0),
ptr,
&[index],
DenseI32ArrayAttribute::new(ctx.context(), &[0, i32::MIN]),
TypeAttribute::new(compile_type(ctx.module_ctx, &local_ty)),
Location::unknown(ctx.context()),
);
op.set_inbounds(Attribute::unit(ctx.context()));
op
}
.into(),
)
.result(0)?
.into();
}
PlaceElem::ConstantIndex(index) => {

local_ty = match local_ty.kind {
TyKind::Array(inner, _) => *inner,
_ => unreachable!(),
};
}
PlaceElem::ConstantIndex(index) => {
ptr = block
.append_operation(llvm::get_element_ptr(
ctx.context(),
ptr,
DenseI32ArrayAttribute::new(ctx.context(), &[(*index).try_into().unwrap()]),
compile_type(ctx.module_ctx, &local_ty),
pointer(ctx.context(), 0),
Location::unknown(ctx.context()),
))
.append_operation(
{
let mut op = ods::llvm::getelementptr(
ctx.context(),
pointer(ctx.context(), 0),
ptr,
&[],
DenseI32ArrayAttribute::new(
ctx.context(),
&[0, (*index).try_into().unwrap()],
),
TypeAttribute::new(compile_type(ctx.module_ctx, &local_ty)),
Location::unknown(ctx.context()),
);
op.set_inbounds(Attribute::unit(ctx.context()));
op
}
.into(),
)
.result(0)?
.into();

local_ty = match local_ty.kind {
TyKind::Array(inner, _) => *inner,
_ => unreachable!(),
};
}
}
}
Expand Down Expand Up @@ -1233,6 +1299,7 @@ fn value_tree_to_int(value: &ValueTree) -> Option<i64> {
concrete_ir::ConstValue::I64(value) => Some(*value),
concrete_ir::ConstValue::I128(value) => Some((*value) as i64),
concrete_ir::ConstValue::U8(value) => Some((*value) as i64),
concrete_ir::ConstValue::Char(value) => Some((*value) as i64),
concrete_ir::ConstValue::U16(value) => Some((*value) as i64),
concrete_ir::ConstValue::U32(value) => Some((*value) as i64),
concrete_ir::ConstValue::U64(value) => Some((*value) as i64),
Expand Down Expand Up @@ -1300,6 +1367,14 @@ fn compile_value_tree<'c: 'b, 'b>(
))
.result(0)?
.into(),
concrete_ir::ConstValue::Char(value) => block
.append_operation(arith::constant(
ctx.context(),
Attribute::parse(ctx.context(), &format!("{} : i8", value)).unwrap(),
Location::unknown(ctx.context()),
))
.result(0)?
.into(),
concrete_ir::ConstValue::U8(value) => block
.append_operation(arith::constant(
ctx.context(),
Expand Down Expand Up @@ -1370,7 +1445,7 @@ fn compile_type<'c>(ctx: ModuleCodegenCtx<'c>, ty: &Ty) -> Type<'c> {
match &ty.kind {
concrete_ir::TyKind::Unit => Type::none(ctx.ctx.mlir_context),
concrete_ir::TyKind::Bool => IntegerType::new(ctx.ctx.mlir_context, 1).into(),
concrete_ir::TyKind::Char => IntegerType::new(ctx.ctx.mlir_context, 32).into(),
concrete_ir::TyKind::Char => IntegerType::new(ctx.ctx.mlir_context, 8).into(),
concrete_ir::TyKind::Int(int_ty) => match int_ty {
concrete_ir::IntTy::I8 => IntegerType::new(ctx.ctx.mlir_context, 8).into(),
concrete_ir::IntTy::I16 => IntegerType::new(ctx.ctx.mlir_context, 16).into(),
Expand Down
1 change: 1 addition & 0 deletions crates/concrete_driver/tests/examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32) {
}

#[test_case(include_str!("../../../examples/hello_world_hacky.con"), "hello_world_hacky", false, "Hello World\n" ; "hello_world_hacky.con")]
#[test_case(include_str!("../../../examples/hello_world_array.con"), "hello_world_array", false, "hello world!\n" ; "hello_world_array.con")]
fn example_tests_with_output(source: &str, name: &str, is_library: bool, result: &str) {
assert_eq!(
result,
Expand Down
7 changes: 6 additions & 1 deletion crates/concrete_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ impl TyKind {
matches!(self, TyKind::Ptr(_, _) | TyKind::Ref(_, _))
}

pub fn is_array(&self) -> bool {
matches!(self, TyKind::Array(_, _))
}

pub fn is_int(&self) -> bool {
matches!(self, TyKind::Int(_) | TyKind::Uint(_))
}
Expand Down Expand Up @@ -478,7 +482,7 @@ impl TyKind {
match self {
TyKind::Unit => unreachable!(),
TyKind::Bool => ValueTree::Leaf(ConstValue::Bool(false)),
TyKind::Char => todo!(),
TyKind::Char => ValueTree::Leaf(ConstValue::Char(0)),
TyKind::Int(ty) => match ty {
IntTy::I8 => ValueTree::Leaf(ConstValue::I8(0)),
IntTy::I16 => ValueTree::Leaf(ConstValue::I16(0)),
Expand Down Expand Up @@ -609,6 +613,7 @@ pub enum UnOp {
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub enum ConstValue {
Bool(bool),
Char(u8),
I8(i8),
I16(i16),
I32(i32),
Expand Down
2 changes: 1 addition & 1 deletion crates/concrete_ir/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ fn lower_value_expr(
span: Some(*const_span),
kind: TyKind::Char,
},
data: ConstKind::Value(ValueTree::Leaf(ConstValue::U32((*value) as u32))),
data: ConstKind::Value(ValueTree::Leaf(ConstValue::Char((*value) as u8))),
})),
Ty {
span: Some(*const_span),
Expand Down
10 changes: 10 additions & 0 deletions examples/hello_world_array.con
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mod Main {
pub extern fn puts(data: *mut char) -> i32;

pub fn main() -> i32 {
let x: [char; 13] = ['h','e','l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\0'];
let z: *mut char = x as *mut char;
puts(z);
return 0;
}
}
Loading