From 503e09e572c4c0d9e10173730482a1ccf90d9dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 25 Apr 2024 17:49:12 -0300 Subject: [PATCH 01/10] Add local_ty for ConstantIndex store --- crates/concrete_codegen_mlir/src/codegen.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/concrete_codegen_mlir/src/codegen.rs b/crates/concrete_codegen_mlir/src/codegen.rs index 8e5f8c0..2936756 100644 --- a/crates/concrete_codegen_mlir/src/codegen.rs +++ b/crates/concrete_codegen_mlir/src/codegen.rs @@ -1013,7 +1013,12 @@ fn compile_store_place<'c: 'b, 'b>( } } PlaceElem::Index(_) => todo!(), - PlaceElem::ConstantIndex(_) => todo!(), + PlaceElem::ConstantIndex(index) => { + local_ty = match local_ty.kind { + TyKind::Array(inner, _) => *inner, + _ => unreachable!(), + } + } } } From 8b53d8c55a2ca211c0c1da617d293da9c666a268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 25 Apr 2024 18:27:36 -0300 Subject: [PATCH 02/10] Compile array type --- crates/concrete_codegen_mlir/src/codegen.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/concrete_codegen_mlir/src/codegen.rs b/crates/concrete_codegen_mlir/src/codegen.rs index 2936756..0a13911 100644 --- a/crates/concrete_codegen_mlir/src/codegen.rs +++ b/crates/concrete_codegen_mlir/src/codegen.rs @@ -1277,7 +1277,15 @@ fn compile_type<'c>(ctx: ModuleCodegenCtx<'c>, ty: &Ty) -> Type<'c> { concrete_ir::FloatTy::F64 => Type::float64(ctx.ctx.mlir_context), }, concrete_ir::TyKind::String => todo!(), - concrete_ir::TyKind::Array(_, _) => todo!(), + concrete_ir::TyKind::Array(inner_type, length) => { + let inner_type = compile_type(ctx, inner_type); + let length = match length.data { + concrete_ir::ConstKind::Value(ValueTree::Leaf(ConstValue::U64(length))) => length, + _ => unimplemented!(), + }; + + melior::dialect::llvm::r#type::array(inner_type, length as u32) + } concrete_ir::TyKind::Ref(_inner_ty, _) | concrete_ir::TyKind::Ptr(_inner_ty, _) => { llvm::r#type::opaque_pointer(ctx.ctx.mlir_context) } From 09a13a113560348ae913f40f50bf6d4924de0f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 25 Apr 2024 18:35:35 -0300 Subject: [PATCH 03/10] Add ptr for ConstantIndex store --- crates/concrete_codegen_mlir/src/codegen.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/concrete_codegen_mlir/src/codegen.rs b/crates/concrete_codegen_mlir/src/codegen.rs index 0a13911..77fd197 100644 --- a/crates/concrete_codegen_mlir/src/codegen.rs +++ b/crates/concrete_codegen_mlir/src/codegen.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; use concrete_ir::{ - BinOp, DefId, FnBody, LocalKind, ModuleBody, Operand, Place, PlaceElem, ProgramBody, Rvalue, - Span, Ty, TyKind, ValueTree, + BinOp, ConstValue, DefId, FnBody, LocalKind, ModuleBody, Operand, Place, PlaceElem, + ProgramBody, Rvalue, Span, Ty, TyKind, ValueTree, }; use concrete_session::Session; use melior::{ @@ -1014,6 +1014,21 @@ fn compile_store_place<'c: 'b, 'b>( } PlaceElem::Index(_) => todo!(), PlaceElem::ConstantIndex(index) => { + ptr = block + .append_operation(llvm::get_element_ptr( + ctx.context(), + ptr, + DenseI32ArrayAttribute::new( + ctx.context(), + &[0, (*index).try_into().unwrap()], + ), + compile_type(ctx.module_ctx, &local_ty), + opaque_pointer(ctx.context()), + Location::unknown(ctx.context()), + )) + .result(0)? + .into(); + local_ty = match local_ty.kind { TyKind::Array(inner, _) => *inner, _ => unreachable!(), From 3946b87dbe404c04703b3861d74a6ba8d12cb782 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 26 Apr 2024 13:08:10 -0300 Subject: [PATCH 04/10] Use GEP correctly --- crates/concrete_codegen_mlir/src/codegen.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/concrete_codegen_mlir/src/codegen.rs b/crates/concrete_codegen_mlir/src/codegen.rs index 77fd197..300da75 100644 --- a/crates/concrete_codegen_mlir/src/codegen.rs +++ b/crates/concrete_codegen_mlir/src/codegen.rs @@ -1014,25 +1014,22 @@ fn compile_store_place<'c: 'b, 'b>( } PlaceElem::Index(_) => todo!(), PlaceElem::ConstantIndex(index) => { + local_ty = match local_ty.kind { + TyKind::Array(inner, _) => *inner, + _ => unreachable!(), + }; + ptr = block .append_operation(llvm::get_element_ptr( ctx.context(), ptr, - DenseI32ArrayAttribute::new( - ctx.context(), - &[0, (*index).try_into().unwrap()], - ), + DenseI32ArrayAttribute::new(ctx.context(), &[(*index).try_into().unwrap()]), compile_type(ctx.module_ctx, &local_ty), opaque_pointer(ctx.context()), Location::unknown(ctx.context()), )) .result(0)? .into(); - - local_ty = match local_ty.kind { - TyKind::Array(inner, _) => *inner, - _ => unreachable!(), - } } } } From b5bf99f4535f3a5f1a5cc32fb16318156e8af68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 26 Apr 2024 13:10:12 -0300 Subject: [PATCH 05/10] Implement PlaceElem::Index in load_place --- crates/concrete_codegen_mlir/src/codegen.rs | 26 ++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/concrete_codegen_mlir/src/codegen.rs b/crates/concrete_codegen_mlir/src/codegen.rs index 300da75..9336736 100644 --- a/crates/concrete_codegen_mlir/src/codegen.rs +++ b/crates/concrete_codegen_mlir/src/codegen.rs @@ -1101,7 +1101,31 @@ fn compile_load_place<'c: 'b, 'b>( _ => unreachable!(), } } - PlaceElem::Index(_) => todo!(), + PlaceElem::Index(local) => { + local_ty = match local_ty.kind { + TyKind::Array(inner, _) => *inner, + _ => unreachable!(), + }; + + let place = Place { + local, + projection: None, + }; + + 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), + opaque_pointer(ctx.context()), + Location::unknown(ctx.context()), + )) + .result(0)? + .into(); + } PlaceElem::ConstantIndex(_) => todo!(), } } From 55b0c0ceb1826156c76bccd6441e705e4c04486b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 26 Apr 2024 13:23:36 -0300 Subject: [PATCH 06/10] Fix bug --- crates/concrete_codegen_mlir/src/codegen.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/concrete_codegen_mlir/src/codegen.rs b/crates/concrete_codegen_mlir/src/codegen.rs index 9336736..554b3ed 100644 --- a/crates/concrete_codegen_mlir/src/codegen.rs +++ b/crates/concrete_codegen_mlir/src/codegen.rs @@ -1108,8 +1108,8 @@ fn compile_load_place<'c: 'b, 'b>( }; let place = Place { - local, - projection: None, + local: *local, + projection: Default::default(), }; let (index, _) = compile_load_place(ctx, block, &place, locals)?; From 31f70a2f6f55975da4e4291bc477dd463b3fb7e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 26 Apr 2024 13:31:05 -0300 Subject: [PATCH 07/10] Add array modifying to example --- examples/arrays.con | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/arrays.con b/examples/arrays.con index 9721fd7..52ade15 100644 --- a/examples/arrays.con +++ b/examples/arrays.con @@ -1,7 +1,10 @@ mod Example { fn main() -> i32 { - let array: [i32; 4] = [1, 2, 3, 4]; - let nested_array: [[i32; 2]; 2] = [[1, 2], [3, 4]]; + let mut array: [i32; 4] = [1, 9, 3, 4]; + let nested_array: [[i32; 2]; 2] = [[1, 2], [9, 9]]; + + array[1] = 2; + nested_array[1] = [3, 4]; let a: i32 = array[1]; let b: i32 = nested_array[1][0]; From f800fd3e176d50e7bd6969f7de796fbb1bf86aa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 26 Apr 2024 13:33:10 -0300 Subject: [PATCH 08/10] Add constant index to load_place --- crates/concrete_codegen_mlir/src/codegen.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/concrete_codegen_mlir/src/codegen.rs b/crates/concrete_codegen_mlir/src/codegen.rs index 554b3ed..c15c7b7 100644 --- a/crates/concrete_codegen_mlir/src/codegen.rs +++ b/crates/concrete_codegen_mlir/src/codegen.rs @@ -1126,7 +1126,23 @@ fn compile_load_place<'c: 'b, 'b>( .result(0)? .into(); } - PlaceElem::ConstantIndex(_) => todo!(), + PlaceElem::ConstantIndex(index) => { + local_ty = match local_ty.kind { + TyKind::Array(inner, _) => *inner, + _ => unreachable!(), + }; + 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), + opaque_pointer(ctx.context()), + Location::unknown(ctx.context()), + )) + .result(0)? + .into(); + } } } From f92aeb9645a58b9509cdbd97fb1f3edd9f137618 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 26 Apr 2024 13:34:32 -0300 Subject: [PATCH 09/10] Add Index to store place --- crates/concrete_codegen_mlir/src/codegen.rs | 26 ++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/concrete_codegen_mlir/src/codegen.rs b/crates/concrete_codegen_mlir/src/codegen.rs index c15c7b7..cb1e1d4 100644 --- a/crates/concrete_codegen_mlir/src/codegen.rs +++ b/crates/concrete_codegen_mlir/src/codegen.rs @@ -1012,7 +1012,31 @@ fn compile_store_place<'c: 'b, 'b>( _ => unreachable!(), } } - PlaceElem::Index(_) => todo!(), + PlaceElem::Index(local) => { + local_ty = match local_ty.kind { + TyKind::Array(inner, _) => *inner, + _ => unreachable!(), + }; + + let place = Place { + local: *local, + projection: vec![], + }; + + 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), + opaque_pointer(ctx.context()), + Location::unknown(ctx.context()), + )) + .result(0)? + .into(); + } PlaceElem::ConstantIndex(index) => { local_ty = match local_ty.kind { TyKind::Array(inner, _) => *inner, From 7d963374e7c215c12a1ee2ddfc5d81816183a309 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 26 Apr 2024 13:44:21 -0300 Subject: [PATCH 10/10] Add test for array --- crates/concrete_driver/tests/examples.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/concrete_driver/tests/examples.rs b/crates/concrete_driver/tests/examples.rs index 8ca3e6c..455f58e 100644 --- a/crates/concrete_driver/tests/examples.rs +++ b/crates/concrete_driver/tests/examples.rs @@ -20,6 +20,7 @@ mod common; #[test_case(include_str!("../../../examples/if_if_false.con"), "if_if_false", false, 7 ; "if_if_false.con")] #[test_case(include_str!("../../../examples/for.con"), "for", false, 10 ; "for.con")] #[test_case(include_str!("../../../examples/for_while.con"), "for_while", false, 10 ; "for_while.con")] +#[test_case(include_str!("../../../examples/arrays.con"), "arrays", false, 5 ; "arrays.con")] fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32) { assert_eq!( status_code,