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

JIT: Support block statements #1841

Merged
merged 67 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
e4c35fe
Experimental pil to rust compiler.
chriseth Feb 29, 2024
686bdc6
Compile and dlopen.
chriseth Aug 3, 2024
b2f8d88
oeu
chriseth Aug 3, 2024
392afc1
oeu
chriseth Aug 3, 2024
6b2029a
loaded sym
chriseth Aug 3, 2024
7ee9afc
log time
chriseth Aug 3, 2024
44e80b8
new crate.
chriseth Sep 12, 2024
65e5f44
fix
chriseth Sep 12, 2024
ec40700
work
chriseth Sep 12, 2024
0be7ac0
work
chriseth Sep 12, 2024
6aa9f45
wor
chriseth Sep 12, 2024
01272e8
loading
chriseth Sep 12, 2024
249f3c8
fix
chriseth Sep 12, 2024
1570fb0
sqrt
chriseth Sep 12, 2024
81dca2b
add benchmark
chriseth Sep 12, 2024
641f1f2
forgot test file
chriseth Sep 12, 2024
65c6c04
clean.
chriseth Sep 13, 2024
1578b1e
fix
chriseth Sep 13, 2024
ffa6187
Some logging.
chriseth Sep 13, 2024
51cae14
size in mb.
chriseth Sep 13, 2024
22de29a
Update pil-analyzer/tests/types.rs
chriseth Sep 13, 2024
2682d27
Use ibig.
chriseth Sep 13, 2024
e9d2910
Use native cpu.
chriseth Sep 13, 2024
541d8dc
clippy
chriseth Sep 19, 2024
306cbb2
Merge remote-tracking branch 'origin/main' into compi
chriseth Sep 23, 2024
5aea6b2
merge fix.
chriseth Sep 23, 2024
1a6db99
Remove ibig features.
chriseth Sep 23, 2024
5322ccd
Merge remote-tracking branch 'origin/main' into compi
chriseth Sep 23, 2024
cdf3ba9
Nicer error messages.
chriseth Sep 24, 2024
7b0dceb
Partial compile.
chriseth Sep 24, 2024
76e6389
clippy
chriseth Sep 24, 2024
00b56e6
fmt
chriseth Sep 24, 2024
3544d57
Update jit-compiler/src/lib.rs
chriseth Sep 24, 2024
5cf259c
Portability.
chriseth Sep 24, 2024
dc7e113
Merge branch 'compi' of ssh://github.com/powdr-labs/powdr into compi
chriseth Sep 24, 2024
e928a37
Update jit-compiler/tests/execution.rs
chriseth Sep 24, 2024
86b0a2d
fix error message.
chriseth Sep 24, 2024
3327e52
clippy
chriseth Sep 24, 2024
2a45653
Use extern c.
chriseth Sep 25, 2024
5647eb6
Use libloading.
chriseth Sep 25, 2024
540671d
Update jit-compiler/src/compiler.rs
chriseth Sep 25, 2024
2937860
Extract sqrt code.
chriseth Sep 25, 2024
681937a
Remove drop.
chriseth Sep 25, 2024
6a96b72
Add release - we need the variable.
chriseth Sep 25, 2024
8db0eba
Encapsulate temp dir in struct.
chriseth Sep 25, 2024
b47df41
Simplify compiler state.
chriseth Sep 25, 2024
bdc2d38
Error message.
chriseth Sep 25, 2024
8c23119
use unsafe extern C fn
chriseth Sep 25, 2024
9501ef9
use mebibytes.
chriseth Sep 25, 2024
1b44c1c
Match expressions.
chriseth Sep 24, 2024
3cf303b
test.
chriseth Sep 26, 2024
0d7df72
better match exprs
chriseth Sep 26, 2024
f22c2bb
comment
chriseth Sep 26, 2024
ed0c985
simple match test.
chriseth Sep 26, 2024
01dcb4b
match expressions for tuples.
chriseth Sep 26, 2024
48458fe
proper numbers
chriseth Sep 26, 2024
7f88008
more pat
chriseth Sep 26, 2024
e48c256
more pat
chriseth Sep 26, 2024
9a3242c
arrays
chriseth Sep 26, 2024
e79c610
docstring.
chriseth Sep 26, 2024
6a8462d
trigger change request
chriseth Sep 26, 2024
ca0ae6f
undo change.
chriseth Sep 26, 2024
c0fcc59
Merge remote-tracking branch 'origin/main' into match_expr
chriseth Sep 30, 2024
a8b1406
fix
chriseth Sep 30, 2024
b24ee27
Let statements
chriseth Sep 30, 2024
98c2ff6
Formatting.
chriseth Sep 30, 2024
c7cf68a
Merge remote-tracking branch 'origin/main' into let_statements_in_block
chriseth Oct 2, 2024
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
34 changes: 26 additions & 8 deletions jit-compiler/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use powdr_ast::{
analyzed::{Analyzed, Expression, FunctionValueDefinition, PolynomialReference, Reference},
parsed::{
display::quote,
types::{ArrayType, FunctionType, Type, TypeScheme},
types::{ArrayType, FunctionType, TupleType, Type, TypeScheme},
ArrayLiteral, BinaryOperation, BinaryOperator, BlockExpression, FunctionCall, IfExpression,
IndexAccess, LambdaExpression, MatchArm, MatchExpression, Number, Pattern,
StatementInsideBlock, UnaryOperation,
IndexAccess, LambdaExpression, LetStatementInsideBlock, MatchArm, MatchExpression, Number,
Pattern, StatementInsideBlock, UnaryOperation,
},
};
use powdr_number::{BigInt, BigUint, FieldElement, LargeInt};
Expand Down Expand Up @@ -261,7 +261,7 @@ impl<'a, T: FieldElement> CodeGenerator<'a, T> {
.join(", ")
)
}
Expression::String(_, s) => quote(s),
Expression::String(_, s) => format!("{}.to_string()", quote(s)),
Expression::Tuple(_, items) => format!(
"({})",
items
Expand Down Expand Up @@ -312,9 +312,27 @@ impl<'a, T: FieldElement> CodeGenerator<'a, T> {
}

fn format_statement(&mut self, s: &StatementInsideBlock<Expression>) -> Result<String, String> {
Err(format!(
"Compiling statements inside blocks is not yet implemented: {s}"
))
Ok(match s {
StatementInsideBlock::LetStatement(LetStatementInsideBlock { pattern, ty, value }) => {
let Some(value) = value else {
return Err(format!(
"Column creating 'let'-statements not yet supported: {s}"
));
};
let value = self.format_expr(value)?;
let var_name = "scrutinee__";
let ty = ty
.as_ref()
.map(|ty| format!(": {}", map_type(ty)))
.unwrap_or_default();

let (vars, code) = check_pattern(var_name, pattern)?;
// TODO if we want to explicitly specify the type, we need to exchange the non-captured
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a note in case we need it in the future if rust cannot determine the type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment. It is talking about the type of {vars} , I assume. But what are the non-captured parts that must be exchanged by ()?

// parts by `()`.
format!("let {vars} = (|{var_name}{ty}| {code})({value}).unwrap();",)
}
StatementInsideBlock::Expression(e) => format!("{};", self.format_expr(e)?),
})
}

/// Returns a string expression evaluating to the value of the symbol.
Expand Down Expand Up @@ -456,7 +474,7 @@ fn map_type(ty: &Type) -> String {
Type::String => "String".to_string(),
Type::Expr => "Expr".to_string(),
Type::Array(ArrayType { base, length: _ }) => format!("Vec<{}>", map_type(base)),
Type::Tuple(_) => todo!(),
Type::Tuple(TupleType { items }) => format!("({})", items.iter().map(map_type).join(", ")),
Type::Function(ft) => format!(
"fn({}) -> {}",
ft.params.iter().map(map_type).join(", "),
Expand Down
31 changes: 31 additions & 0 deletions jit-compiler/tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,34 @@ fn match_array() {
assert_eq!(f.call(5), 6);
assert_eq!(f.call(6), 7);
}

#[test]
fn let_simple() {
let f = compile(
r#"let f: int -> int = |x| {
let a = 1;
let b = a + 9;
b - 9 + x
};"#,
"f",
);

assert_eq!(f.call(0), 1);
assert_eq!(f.call(1), 2);
assert_eq!(f.call(2), 3);
assert_eq!(f.call(3), 4);
}

#[test]
fn let_complex() {
let f = compile(
r#"let f: int -> int = |x| {
let (a, b, (_, d)) = (1, 2, ("abc", [x, 5]));
a + b + d[0] + d[1]
};"#,
"f",
);

assert_eq!(f.call(0), 8);
assert_eq!(f.call(1), 9);
}
Loading