Skip to content

Commit

Permalink
fix: list index union operation
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Jul 17, 2024
1 parent 7a22b93 commit 41d1642
Show file tree
Hide file tree
Showing 64 changed files with 640 additions and 320 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build-test-macos-arm64.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Build and Test on MacOS ARCH64

env:
MACOSX_DEPLOYMENT_TARGET: '10.13'
on: ["push", "pull_request"]
jobs:
build-and-test:
Expand Down
1 change: 0 additions & 1 deletion kclvm/ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,6 @@ pub struct ConfigEntry {
pub key: Option<NodeRef<Expr>>,
pub value: NodeRef<Expr>,
pub operation: ConfigEntryOperation,
pub insert_index: isize,
}

/// CheckExpr, e.g.
Expand Down
3 changes: 0 additions & 3 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,6 @@ impl<'p> Printer<'p> {
match &item.node.key {
Some(key) => {
let print_right_brace_count = self.write_config_key(key);
if item.node.insert_index >= 0 {
self.write(&format!("[{}]", item.node.insert_index));
}
if !matches!(item.node.operation, ast::ConfigEntryOperation::Union) {
self.write_space();
}
Expand Down
40 changes: 29 additions & 11 deletions kclvm/compiler/src/codegen/llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,11 +998,13 @@ impl<'ctx> DerivedValueCalculationMethods for LLVMCodeGenContext<'ctx> {
key: &str,
value: Self::Value,
op: i32,
insert_index: i32,
insert_index: Option<i32>,
) {
let name = self.native_global_string(key, "").into();
let op = self.native_int_value(op);
let insert_index = self.native_int_value(insert_index);
let has_insert_index = insert_index.is_some();
let has_insert_index = self.native_i8_value(if has_insert_index { 1 } else { 0 });
let insert_index = self.native_int_value(insert_index.unwrap_or(-1));
self.build_void_call(
&ApiFunc::kclvm_dict_insert.name(),
&[
Expand All @@ -1012,6 +1014,7 @@ impl<'ctx> DerivedValueCalculationMethods for LLVMCodeGenContext<'ctx> {
value,
op,
insert_index,
has_insert_index,
],
);
}
Expand All @@ -1025,10 +1028,12 @@ impl<'ctx> DerivedValueCalculationMethods for LLVMCodeGenContext<'ctx> {
key: Self::Value,
value: Self::Value,
op: i32,
insert_index: i32,
insert_index: Option<i32>,
) {
let op = self.native_int_value(op);
let insert_index = self.native_int_value(insert_index);
let has_insert_index = insert_index.is_some();
let has_insert_index = self.native_i8_value(if has_insert_index { 1 } else { 0 });
let insert_index = self.native_int_value(insert_index.unwrap_or(-1));
self.build_void_call(
&ApiFunc::kclvm_dict_insert_value.name(),
&[
Expand All @@ -1038,6 +1043,7 @@ impl<'ctx> DerivedValueCalculationMethods for LLVMCodeGenContext<'ctx> {
value,
op,
insert_index,
has_insert_index,
],
);
}
Expand Down Expand Up @@ -1576,6 +1582,12 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
i8_type.const_int(v as u64, false)
}

/// Get LLVM i8 zero value
pub fn native_i8_value(&self, v: i8) -> BasicValueEnum<'ctx> {
let i8_type = self.context.i8_type();
i8_type.const_int(v as u64, false).into()
}

/// Construct a LLVM int value using i32
pub fn native_int_value(&self, v: i32) -> BasicValueEnum<'ctx> {
let i32_type = self.context.i32_type();
Expand Down Expand Up @@ -2356,14 +2368,14 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
}
// Deal scalars
for scalar in scalars.iter() {
self.dict_safe_insert(global_dict, SCALAR_KEY, *scalar, 0, -1);
self.dict_safe_insert(global_dict, SCALAR_KEY, *scalar, 0, None);
}
// Deal global variables
for (name, ptr) in globals.iter() {
let value = self.builder.build_load(*ptr, "");
let value_dict = self.dict_value();
self.dict_safe_insert(value_dict, name.as_str(), value, 0, -1);
self.dict_safe_insert(global_dict, SCALAR_KEY, value_dict, 0, -1);
self.dict_safe_insert(value_dict, name.as_str(), value, 0, None);
self.dict_safe_insert(global_dict, SCALAR_KEY, value_dict, 0, None);
}
// Plan result to json string.
self.build_call(
Expand All @@ -2386,11 +2398,13 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
key: &str,
value: BasicValueEnum<'ctx>,
op: i32,
insert_index: i32,
insert_index: Option<i32>,
) {
let name = self.native_global_string(key, "").into();
let op = self.native_int_value(op);
let insert_index = self.native_int_value(insert_index);
let has_insert_index = insert_index.is_some();
let has_insert_index = self.native_i8_value(if has_insert_index { 1 } else { 0 });
let insert_index = self.native_int_value(insert_index.unwrap_or(-1));
self.build_void_call(
&ApiFunc::kclvm_dict_safe_insert.name(),
&[
Expand All @@ -2400,6 +2414,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
value,
op,
insert_index,
has_insert_index,
],
);
}
Expand All @@ -2413,11 +2428,13 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
key: &str,
value: BasicValueEnum<'ctx>,
op: i32,
insert_index: i32,
insert_index: Option<i32>,
) {
let name = self.native_global_string(key, "").into();
let op = self.native_int_value(op);
let insert_index = self.native_int_value(insert_index);
let has_insert_index = insert_index.is_some();
let has_insert_index = self.native_i8_value(if has_insert_index { 1 } else { 0 });
let insert_index = self.native_int_value(insert_index.unwrap_or(-1));
self.build_void_call(
&ApiFunc::kclvm_dict_merge.name(),
&[
Expand All @@ -2427,6 +2444,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
value,
op,
insert_index,
has_insert_index,
],
);
}
Expand Down
31 changes: 20 additions & 11 deletions kclvm/compiler/src/codegen/llvm/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,10 +1538,10 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
&fn_name.name(),
&[self.current_runtime_ctx_ptr(), org_value, value],
);
self.dict_merge(schema_value, name, value, 1, -1);
self.dict_merge(schema_value, name, value, 1, None);
}
// Assign
_ => self.dict_merge(schema_value, name, value, 1, -1),
_ => self.dict_merge(schema_value, name, value, 1, None),
}
}
self.br(is_not_override_else_block);
Expand Down Expand Up @@ -1595,10 +1595,10 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
&fn_name.name(),
&[self.current_runtime_ctx_ptr(), org_value, value],
);
self.dict_merge(schema_value, name, value, 1, -1);
self.dict_merge(schema_value, name, value, 1, None);
}
// Assign
_ => self.dict_merge(schema_value, name, value, 1, -1),
_ => self.dict_merge(schema_value, name, value, 1, None),
}
}
self.br(end_block);
Expand Down Expand Up @@ -1781,7 +1781,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
} else {
self.none_value()
};
self.dict_insert(dict_value, name.node.as_str(), value, 0, -1);
self.dict_insert(dict_value, name.node.as_str(), value, 0, None);
}
let pkgpath = self.native_global_string_value(&self.current_pkgpath());
let is_in_schema = self.is_in_schema() || self.is_in_schema_expr();
Expand Down Expand Up @@ -2053,7 +2053,7 @@ impl<'ctx> TypedResultWalker<'ctx> for LLVMCodeGenContext<'ctx> {
} else {
self.none_value()
};
self.dict_insert(dict_value, name.node.as_str(), value, 0, -1);
self.dict_insert(dict_value, name.node.as_str(), value, 0, None);
}
let pkgpath = self.native_global_string_value(&self.current_pkgpath());
let schema = self.build_call(
Expand Down Expand Up @@ -2597,7 +2597,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
} else {
self.none_value()
};
self.dict_insert(dict_value, name.node.as_str(), value, 0, -1);
self.dict_insert(dict_value, name.node.as_str(), value, 0, None);
}
let name = match &decorator.func.node {
ast::Expr::Identifier(ident) if ident.names.len() == 1 => ident.names[0].clone(),
Expand Down Expand Up @@ -2808,7 +2808,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
key,
self.value_deep_copy(value),
op.value(),
-1,
None,
);
}
}
Expand Down Expand Up @@ -2843,19 +2843,28 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
for item in items {
let value = self.walk_expr(&item.node.value)?;
if let Some(key) = &item.node.key {
let mut insert_index = -1;
let mut insert_index = None;
let optional_name = match &key.node {
ast::Expr::Identifier(identifier) => Some(identifier.names[0].node.clone()),
ast::Expr::StringLit(string_lit) => Some(string_lit.value.clone()),
ast::Expr::Subscript(subscript) => {
let mut name = None;
if let ast::Expr::Identifier(identifier) = &subscript.value.node {
if let Some(index_node) = &subscript.index {
// Insert index
if let ast::Expr::NumberLit(number) = &index_node.node {
if let ast::NumberLitValue::Int(v) = number.value {
insert_index = v;
insert_index = Some(v as i32);
name = Some(identifier.names[0].node.clone())
}
} else if let ast::Expr::Unary(unary_expr) = &index_node.node {
// Negative insert index
if let ast::Expr::NumberLit(number) = &unary_expr.operand.node {
if let ast::NumberLitValue::Int(v) = number.value {
insert_index = Some(-v as i32);
name = Some(identifier.names[0].node.clone())
}
}
}
}
}
Expand All @@ -2873,7 +2882,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
key,
value,
item.node.operation.value(),
insert_index as i32,
insert_index,
);
if let Some(name) = &optional_name {
let value =
Expand Down
10 changes: 5 additions & 5 deletions kclvm/compiler/src/codegen/llvm/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
match &stmt.node {
ast::Stmt::Unification(unification_stmt) => {
let name = &unification_stmt.target.node.names[0].node;
self.dict_merge(schema_value, name, value, 0, -1);
self.dict_merge(schema_value, name, value, 0, None);
if is_in_if {
in_if_names.push(name.to_string());
} else {
Expand All @@ -105,7 +105,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
ast::Stmt::Assign(assign_stmt) => {
for target in &assign_stmt.targets {
let name = &target.node.names[0].node;
self.dict_merge(schema_value, name, value, 0, -1);
self.dict_merge(schema_value, name, value, 0, None);
if is_in_if {
in_if_names.push(name.to_string());
} else {
Expand All @@ -122,7 +122,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
ast::Stmt::AugAssign(aug_assign_stmt) => {
let target = &aug_assign_stmt.target;
let name = &target.node.names[0].node;
self.dict_merge(schema_value, name, value, 0, -1);
self.dict_merge(schema_value, name, value, 0, None);
if is_in_if {
in_if_names.push(name.to_string());
} else {
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
}
ast::Stmt::SchemaAttr(schema_attr) => {
let name = schema_attr.name.node.as_str();
self.dict_merge(schema_value, name, value, 0, -1);
self.dict_merge(schema_value, name, value, 0, None);
if is_in_if {
in_if_names.push(name.to_string());
} else {
Expand Down Expand Up @@ -324,7 +324,7 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
let config_value = phi.as_basic_value();
if self.scope_level() >= INNER_LEVEL && !self.local_vars.borrow().contains(name) {
if let Some(value) = value {
self.dict_merge(schema_value, name, value, 1, -1);
self.dict_merge(schema_value, name, value, 1, None);
}
self.value_union(schema_value, config_value);
let cal_map = self
Expand Down
6 changes: 3 additions & 3 deletions kclvm/compiler/src/codegen/traits/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub trait DerivedValueCalculationMethods: ValueMethods + ValueCalculationMethods
key: Self::Value,
value: Self::Value,
op: i32,
insert_index: i32,
insert_index: Option<i32>,
);
/// Insert a dict entry including key, value, op and insert_index into the dict,
/// and the type of key is `&str`
Expand All @@ -175,13 +175,13 @@ pub trait DerivedValueCalculationMethods: ValueMethods + ValueCalculationMethods
key: &str,
value: Self::Value,
op: i32,
insert_index: i32,
insert_index: Option<i32>,
) {
self.dict_insert_with_key_value(dict, self.string_value(key), value, op, insert_index);
}
/// Insert a dict entry with the override = attribute operator including key, value into the dict.
fn dict_insert_override_item(&self, dict: Self::Value, key: &str, value: Self::Value) {
self.dict_insert(dict, key, value, 1, -1);
self.dict_insert(dict, key, value, 1, None);
}
/// Dict contains key.
fn dict_contains_key(&self, dict: Self::Value, key: &str) -> Self::Value {
Expand Down
21 changes: 15 additions & 6 deletions kclvm/evaluator/src/calculation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ impl<'ctx> Evaluator<'ctx> {
key: &str,
value: &ValueRef,
op: &ast::ConfigEntryOperation,
insert_index: i32,
insert_index: Option<i32>,
) {
let op = match op {
ast::ConfigEntryOperation::Union => ConfigEntryOperationKind::Union,
ast::ConfigEntryOperation::Override => ConfigEntryOperationKind::Override,
ast::ConfigEntryOperation::Insert => ConfigEntryOperationKind::Insert,
};
self.dict_merge_key_value_pair(dict, key, value, op, insert_index, false);
self.dict_merge_key_value_pair(dict, key, value, op, insert_index, true);
}

/// Insert a dict entry including key, value, op and insert_index into the dict,
Expand All @@ -228,7 +228,7 @@ impl<'ctx> Evaluator<'ctx> {
key: &str,
value: &ValueRef,
op: &ast::ConfigEntryOperation,
insert_index: i32,
insert_index: Option<i32>,
) {
let op = match op {
ast::ConfigEntryOperation::Union => ConfigEntryOperationKind::Union,
Expand Down Expand Up @@ -259,7 +259,14 @@ impl<'ctx> Evaluator<'ctx> {
/// Insert an entry including key and value into the dict, and merge the original entry.
#[inline]
pub(crate) fn dict_insert_merge_value(&self, dict: &mut ValueRef, key: &str, value: &ValueRef) {
self.dict_merge_key_value_pair(dict, key, value, ConfigEntryOperationKind::Union, -1, true);
self.dict_merge_key_value_pair(
dict,
key,
value,
ConfigEntryOperationKind::Union,
None,
true,
);
}

/// Set dict key with the value. When the dict is a schema and resolve schema validations.
Expand Down Expand Up @@ -292,14 +299,16 @@ impl<'ctx> Evaluator<'ctx> {
key: &str,
v: &ValueRef,
op: ConfigEntryOperationKind,
insert_index: i32,
insert_index: Option<i32>,
idempotent_check: bool,
) {
if p.is_config() {
let mut dict: DictValue = Default::default();
dict.values.insert(key.to_string(), v.clone());
dict.ops.insert(key.to_string(), op);
dict.insert_indexs.insert(key.to_string(), insert_index);
if let Some(index) = insert_index {
dict.insert_indexs.insert(key.to_string(), index);
}
union_entry(
self,
p,
Expand Down
Loading

0 comments on commit 41d1642

Please sign in to comment.