Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed May 10, 2024
2 parents 5644d2f + e0e6fb2 commit 486b390
Show file tree
Hide file tree
Showing 23 changed files with 438 additions and 32 deletions.
20 changes: 19 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,25 @@ jobs:
run: sudo apt-get install libc-dev build-essential
- name: test
run: make test

test-macos:
name: test (macOS)
runs-on: macos-14
env:
CARGO_TERM_COLOR: always
LIBRARY_PATH: /opt/homebrew/lib
MLIR_SYS_180_PREFIX: /opt/homebrew/opt/llvm@18
LLVM_SYS_180_PREFIX: /opt/homebrew/opt/llvm@18
TABLEGEN_180_PREFIX: /opt/homebrew/opt/llvm@18
RUST_LOG: debug
steps:
- uses: actions/checkout@v4
- name: Rustup toolchain install
uses: dtolnay/[email protected]
- uses: homebrew/actions/setup-homebrew@master
- name: install llvm
run: brew install llvm@18
- name: Run tests
run: make test
coverage:
name: coverage
runs-on: ubuntu-latest
Expand Down
7 changes: 7 additions & 0 deletions crates/concrete_ast/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ pub struct GenericParam {
pub params: Vec<TypeSpec>,
pub span: Span,
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Attribute {
pub name: String,
pub value: Option<String>,
pub span: Span,
}
3 changes: 2 additions & 1 deletion crates/concrete_ast/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
common::{DocString, GenericParam, Ident, Span},
common::{Attribute, DocString, GenericParam, Ident, Span},
statements::Statement,
types::TypeSpec,
};
Expand All @@ -13,6 +13,7 @@ pub struct FunctionDecl {
pub ret_type: Option<TypeSpec>,
pub is_extern: bool,
pub is_pub: bool,
pub attributes: Vec<Attribute>,
pub span: Span,
}

Expand Down
47 changes: 47 additions & 0 deletions crates/concrete_check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,52 @@ pub fn lowering_error_to_report(
)
.finish()
}

LoweringError::NotMutable {
span,
declare_span,
program_id,
} => {
let path = file_paths[program_id].display().to_string();
let mut report = Report::build(ReportKind::Error, path.clone(), span.from)
.with_code("NotMutable")
.with_label(
Label::new((path.clone(), span.into()))
.with_message("can't mutate this variable because it's not mutable")
.with_color(colors.next()),
);

if let Some(declare_span) = declare_span {
report = report.with_label(
Label::new((path, declare_span.into()))
.with_message("variable declared here")
.with_color(colors.next()),
);
}
report.finish()
}
LoweringError::CantTakeMutableBorrow {
span,
declare_span,
program_id,
} => {
let path = file_paths[program_id].display().to_string();
let mut report = Report::build(ReportKind::Error, path.clone(), span.from)
.with_code("CantTakeMutableBorrow")
.with_label(
Label::new((path.clone(), span.into()))
.with_message("can't take a mutate borrow to this variable because it's not declared mutable")
.with_color(colors.next()),
);

if let Some(declare_span) = declare_span {
report = report.with_label(
Label::new((path, declare_span.into()))
.with_message("variable declared here")
.with_color(colors.next()),
);
}
report.finish()
}
}
}
39 changes: 38 additions & 1 deletion crates/concrete_codegen_mlir/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,10 +613,32 @@ fn compile_binop<'c: 'b, 'b>(

let is_float = matches!(lhs_ty.kind, TyKind::Float(_));
let is_signed = matches!(lhs_ty.kind, TyKind::Int(_));
let is_ptr = if let TyKind::Ptr(inner, _) = &lhs_ty.kind {
Some((*inner).clone())
} else {
None
};

Ok(match op {
BinOp::Add => {
let value = if is_float {
let value = if let Some(inner) = is_ptr {
let inner_ty = compile_type(ctx.module_ctx, &inner);
block
.append_operation(
ods::llvm::getelementptr(
ctx.context(),
pointer(ctx.context(), 0),
lhs,
&[rhs],
DenseI32ArrayAttribute::new(ctx.context(), &[i32::MIN]),
TypeAttribute::new(inner_ty),
location,
)
.into(),
)
.result(0)?
.into()
} else if is_float {
block
.append_operation(arith::addf(lhs, rhs, location))
.result(0)?
Expand All @@ -630,6 +652,11 @@ fn compile_binop<'c: 'b, 'b>(
(value, lhs_ty)
}
BinOp::Sub => {
if is_ptr.is_some() {
return Err(CodegenError::NotImplemented(
"substracting from a pointer is not yet implemented".to_string(),
));
}
let value = if is_float {
block
.append_operation(arith::subf(lhs, rhs, location))
Expand All @@ -644,6 +671,11 @@ fn compile_binop<'c: 'b, 'b>(
(value, lhs_ty)
}
BinOp::Mul => {
if is_ptr.is_some() {
return Err(CodegenError::NotImplemented(
"multiplying a pointer is not yet implemented".to_string(),
));
}
let value = if is_float {
block
.append_operation(arith::mulf(lhs, rhs, location))
Expand All @@ -658,6 +690,11 @@ fn compile_binop<'c: 'b, 'b>(
(value, lhs_ty)
}
BinOp::Div => {
if is_ptr.is_some() {
return Err(CodegenError::NotImplemented(
"dividing a pointer is not yet implemented".to_string(),
));
}
let value = if is_float {
block
.append_operation(arith::divf(lhs, rhs, location))
Expand Down
2 changes: 2 additions & 0 deletions crates/concrete_codegen_mlir/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ pub enum CodegenError {
LLVMCompileError(String),
#[error("melior error: {0}")]
MeliorError(#[from] melior::Error),
#[error("not yet implemented: {0}")]
NotImplemented(String),
}
55 changes: 50 additions & 5 deletions crates/concrete_driver/tests/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ fn import_not_found() {

#[test]
fn invalid_borrow_mut() {
let (source, name) = (include_str!("invalid_programs/refmut.con"), "refmut");
let (source, name) = (
include_str!("invalid_programs/invalid_borrow_mut.con"),
"invalid_borrow_mut",
);
let error = check_invalid_program(source, name);

assert!(
matches!(
&error,
LoweringError::BorrowNotMutable { name, .. } if name == "a"
),
matches!(&error, LoweringError::NotMutable { .. }),
"{:#?}",
error
);
Expand Down Expand Up @@ -135,3 +135,48 @@ fn call_param_type_mismatch() {
error
);
}

#[test]
fn invalid_assign() {
let (source, name) = (
include_str!("invalid_programs/invalid_assign.con"),
"invalid_assign",
);
let error = check_invalid_program(source, name);

assert!(
matches!(&error, LoweringError::UnexpectedType { .. }),
"{:#?}",
error
);
}

#[test]
fn immutable_mutation() {
let (source, name) = (
include_str!("invalid_programs/immutable_mutation.con"),
"immutable_mutation",
);
let error = check_invalid_program(source, name);

assert!(
matches!(&error, LoweringError::NotMutable { .. }),
"{:#?}",
error
);
}

#[test]
fn mutable_nonmut_borrow() {
let (source, name) = (
include_str!("invalid_programs/mutable_nonmut_borrow.con"),
"mutable_nonmut_borrow",
);
let error = check_invalid_program(source, name);

assert!(
matches!(&error, LoweringError::CantTakeMutableBorrow { .. }),
"{:#?}",
error
);
}
18 changes: 17 additions & 1 deletion crates/concrete_driver/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{
borrow::Cow,
fmt,
path::{Path, PathBuf},
process::Output,
process::{Output, Stdio},
};

use ariadne::Source;
Expand Down Expand Up @@ -110,6 +110,7 @@ pub fn compile_program(

pub fn run_program(program: &Path) -> Result<Output, std::io::Error> {
std::process::Command::new(program)
.stdout(Stdio::piped())
.spawn()?
.wait_with_output()
}
Expand All @@ -122,3 +123,18 @@ pub fn compile_and_run(source: &str, name: &str, library: bool, optlevel: OptLev

output.status.code().unwrap()
}

#[allow(unused)] // false positive
#[track_caller]
pub fn compile_and_run_output(
source: &str,
name: &str,
library: bool,
optlevel: OptLevel,
) -> String {
let result = compile_program(source, name, library, optlevel).expect("failed to compile");

let output = run_program(&result.binary_file).expect("failed to run");

std::str::from_utf8(&output.stdout).unwrap().to_string()
}
22 changes: 21 additions & 1 deletion crates/concrete_driver/tests/examples.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::compile_and_run;
use crate::common::{compile_and_run, compile_and_run_output};
use concrete_session::config::OptLevel;
use test_case::test_case;

Expand Down Expand Up @@ -39,3 +39,23 @@ fn example_tests(source: &str, name: &str, is_library: bool, status_code: i32) {
compile_and_run(source, name, is_library, OptLevel::Aggressive)
);
}

#[test_case(include_str!("../../../examples/hello_world_hacky.con"), "hello_world_hacky", false, "Hello World\n" ; "hello_world_hacky.con")]
fn example_tests_with_output(source: &str, name: &str, is_library: bool, result: &str) {
assert_eq!(
result,
compile_and_run_output(source, name, is_library, OptLevel::None)
);
assert_eq!(
result,
compile_and_run_output(source, name, is_library, OptLevel::Less)
);
assert_eq!(
result,
compile_and_run_output(source, name, is_library, OptLevel::Default)
);
assert_eq!(
result,
compile_and_run_output(source, name, is_library, OptLevel::Aggressive)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod Simple {
fn main() -> i32 {
let x: i32 = 2;
x = 4;
return x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod Simple {
fn main() -> i32 {
let mut x: i32 = 2;
let y: i64 = 4;
x = y;
return x;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod Simple {

fn main() -> i32 {
let x: i32 = 1;
let mut x: i32 = 1;
hello(&x);
return x;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod Simple {
fn main() -> i32 {
let x: i32 = 2;
let y: &mut i32 = &mut x;
*y = 4;
return *y;
}
}
Loading

0 comments on commit 486b390

Please sign in to comment.