Skip to content

Commit

Permalink
Revert "Remove obsolete ListLenUsize"
Browse files Browse the repository at this point in the history
This reverts commit ad1bca4.
  • Loading branch information
rtfeldman committed Feb 17, 2024
1 parent 87d4760 commit 739565e
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/compiler/alias_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ fn lowlevel_spec<'a>(
// just dream up a unit value
builder.add_make_tuple(block, &[])
}
ListLen => {
ListLenUsize | ListLenU64 => {
// TODO should this touch the heap cell?
// just dream up a unit value
builder.add_make_tuple(block, &[])
Expand Down
3 changes: 2 additions & 1 deletion crates/compiler/can/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ map_symbol_to_lowlevel_and_arity! {
StrWithCapacity; STR_WITH_CAPACITY; 1,
StrReleaseExcessCapacity; STR_RELEASE_EXCESS_CAPACITY; 1,

ListLen; LIST_LEN; 1,
ListLenUsize; LIST_LEN_USIZE; 1,
ListLenU64; LIST_LEN_U64; 1,
ListWithCapacity; LIST_WITH_CAPACITY; 1,
ListReserve; LIST_RESERVE; 2,
ListIsUnique; LIST_IS_UNIQUE; 1,
Expand Down
7 changes: 6 additions & 1 deletion crates/compiler/gen_dev/src/generic64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2830,7 +2830,12 @@ impl<
}
}

fn build_list_len(&mut self, dst: &Symbol, list: &Symbol) {
fn build_list_len_usize(&mut self, dst: &Symbol, list: &Symbol) {
self.storage_manager
.list_len_usize(&mut self.buf, dst, list);
}

fn build_list_len_u64(&mut self, dst: &Symbol, list: &Symbol) {
self.storage_manager.list_len_u64(&mut self.buf, dst, list);
}

Expand Down
19 changes: 15 additions & 4 deletions crates/compiler/gen_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1484,13 +1484,21 @@ trait Backend<'a> {

self.build_fn_call(sym, intrinsic.to_string(), args, arg_layouts, ret_layout)
}
LowLevel::ListLen => {
LowLevel::ListLenU64 => {
debug_assert_eq!(
1,
args.len(),
"ListLenU64: expected to have exactly one argument"
);
self.build_list_len(sym, &args[0])
self.build_list_len_u64(sym, &args[0])
}
LowLevel::ListLenUsize => {
debug_assert_eq!(
1,
args.len(),
"ListLenUsize: expected to have exactly one argument"
);
self.build_list_len_usize(sym, &args[0])
}
LowLevel::ListWithCapacity => {
debug_assert_eq!(
Expand Down Expand Up @@ -2374,8 +2382,11 @@ trait Backend<'a> {
/// build_sqrt stores the result of `sqrt(src)` into dst.
fn build_num_sqrt(&mut self, dst: Symbol, src: Symbol, float_width: FloatWidth);

/// build_list_len returns the length of a list after casting it from usize to u64.
fn build_list_len(&mut self, dst: &Symbol, list: &Symbol);
/// build_list_len_usize returns the length of a list as a usize. This is for internal use only.
fn build_list_len_usize(&mut self, dst: &Symbol, list: &Symbol);

/// build_list_len_u64 returns the length of a list and casts it from usize to u64. This is for the public List.len.
fn build_list_len_u64(&mut self, dst: &Symbol, list: &Symbol);

/// generate a call to a higher-order lowlevel
fn build_higher_order_lowlevel(
Expand Down
8 changes: 7 additions & 1 deletion crates/compiler/gen_llvm/src/llvm/lowlevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ pub(crate) fn run_low_level<'a, 'ctx>(
bitcode::STR_WITH_CAPACITY,
)
}
ListLen => {
ListLenU64 => {
// List.len : List * -> U64
arguments!(list);

Expand All @@ -618,6 +618,12 @@ pub(crate) fn run_low_level<'a, 'ctx>(
.new_build_int_cast(len_usize, env.context.i64_type(), "usize_to_u64")
.into()
}
ListLenUsize => {
// List.lenUsize : List * -> usize # used internally, not exposed
arguments!(list);

list_len_usize(env.builder, list.into_struct_value()).into()
}
ListGetCapacity => {
// List.capacity: List a -> U64
arguments!(list);
Expand Down
3 changes: 2 additions & 1 deletion crates/compiler/gen_wasm/src/low_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,14 @@ impl<'a> LowLevelCall<'a> {
StrWithCapacity => self.load_args_and_call_zig(backend, bitcode::STR_WITH_CAPACITY),

// List
ListLen => {
ListLenU64 => {
self.load_list_len_usize(backend);

// Length is stored as 32 bits in memory on wasm32,
// but List.len always returns U64
backend.code_builder.i64_extend_u_i32();
}
ListLenUsize => self.load_list_len_usize(backend),

ListGetCapacity => self.load_args_and_call_zig(backend, bitcode::LIST_CAPACITY),

Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/load_internal/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4703,7 +4703,7 @@ fn synth_list_len_type(subs: &mut Subs) -> Variable {
Content::Structure(FlatType::Apply(Symbol::LIST_LIST, a_slice)),
);
let fn_var = synth_import(subs, Content::Error);
let solved_list_len = UnionLabels::insert_into_subs(subs, [(Symbol::LIST_LEN, [])]);
let solved_list_len = UnionLabels::insert_into_subs(subs, [(Symbol::LIST_LEN_U64, [])]);
let clos_list_len = synth_import(
subs,
Content::LambdaSet(LambdaSet {
Expand Down Expand Up @@ -4757,7 +4757,7 @@ pub fn add_imports(
// Num needs List.len, but List imports Num.
let list_len_type_var = synth_list_len_type(subs);
let list_len_type_index = constraints.push_variable(list_len_type_var);
def_types.push((Symbol::LIST_LEN, Loc::at_zero(list_len_type_index)));
def_types.push((Symbol::LIST_LEN_U64, Loc::at_zero(list_len_type_index)));
import_variables.push(list_len_type_var);
}

Expand Down
6 changes: 4 additions & 2 deletions crates/compiler/module/src/low_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub enum LowLevel {
StrReserve,
StrWithCapacity,
StrReleaseExcessCapacity,
ListLen,
ListLenUsize,
ListLenU64,
ListWithCapacity,
ListReserve,
ListReleaseExcessCapacity,
Expand Down Expand Up @@ -268,7 +269,8 @@ map_symbol_to_lowlevel! {
StrToNum <= STR_TO_NUM;
StrWithCapacity <= STR_WITH_CAPACITY;
StrReleaseExcessCapacity <= STR_RELEASE_EXCESS_CAPACITY;
ListLen <= LIST_LEN;
ListLenU64 <= LIST_LEN_U64;
ListLenUsize <= LIST_LEN_USIZE;
ListGetCapacity <= LIST_CAPACITY;
ListWithCapacity <= LIST_WITH_CAPACITY;
ListReserve <= LIST_RESERVE;
Expand Down
3 changes: 2 additions & 1 deletion crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ define_builtins! {
3 LIST_SET: "set"
4 LIST_APPEND: "append"
5 LIST_MAP: "map"
6 LIST_LEN: "len"
6 LIST_LEN_U64: "len"
7 LIST_WALK_BACKWARDS: "walkBackwards"
8 LIST_CONCAT: "concat"
9 LIST_FIRST: "first"
Expand Down Expand Up @@ -1424,6 +1424,7 @@ define_builtins! {
85 LIST_PREPEND_IF_OK: "prependIfOk"
86 LIST_WALK_WITH_INDEX_UNTIL: "walkWithIndexUntil"
87 LIST_CLONE: "clone"
88 LIST_LEN_USIZE: "lenUsize"
}
7 RESULT: "Result" => {
0 RESULT_RESULT: "Result" exposed_type=true // the Result.Result type alias
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/mono/src/code_gen_help/equality.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,8 @@ fn eq_list<'a>(

let len_1 = root.create_symbol(ident_ids, "len_1");
let len_2 = root.create_symbol(ident_ids, "len_2");
let len_1_stmt = |next| let_lowlevel(arena, Layout::U64, len_1, ListLen, &[ARG_1], next);
let len_2_stmt = |next| let_lowlevel(arena, Layout::U64, len_2, ListLen, &[ARG_2], next);
let len_1_stmt = |next| let_lowlevel(arena, layout_isize, len_1, ListLenUsize, &[ARG_1], next);
let len_2_stmt = |next| let_lowlevel(arena, layout_isize, len_2, ListLenUsize, &[ARG_2], next);

let eq_len = root.create_symbol(ident_ids, "eq_len");
let eq_len_stmt = |next| let_lowlevel(arena, LAYOUT_BOOL, eq_len, Eq, &[len_1, len_2], next);
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/mono/src/code_gen_help/refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,12 +926,12 @@ fn refcount_list<'a>(
//

let len = root.create_symbol(ident_ids, "len");
let len_stmt = |next| let_lowlevel(arena, Layout::U64, len, ListLen, &[structure], next);
let len_stmt = |next| let_lowlevel(arena, layout_isize, len, ListLenUsize, &[structure], next);

// let zero = 0
let zero = root.create_symbol(ident_ids, "zero");
let zero_expr = Expr::Literal(Literal::Int(0i128.to_ne_bytes()));
let zero_stmt = |next| Stmt::Let(zero, zero_expr, Layout::U64, next);
let zero_stmt = |next| Stmt::Let(zero, zero_expr, layout_isize, next);

// let is_empty = lowlevel Eq len zero
let is_empty = root.create_symbol(ident_ids, "is_empty");
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/mono/src/drop_specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,8 +1533,8 @@ fn low_level_no_rc(lowlevel: &LowLevel) -> RC {

match lowlevel {
Unreachable => RC::Uknown,
ListLen | StrIsEmpty | StrCountUtf8Bytes | ListGetCapacity | ListWithCapacity
| StrWithCapacity => RC::NoRc,
ListLenU64 | ListLenUsize | StrIsEmpty | StrCountUtf8Bytes | ListGetCapacity
| ListWithCapacity | StrWithCapacity => RC::NoRc,
ListReplaceUnsafe => RC::Rc,
StrGetUnsafe | ListGetUnsafe => RC::NoRc,
ListConcat => RC::Rc,
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/mono/src/inc_dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ fn lowlevel_borrow_signature(arena: &Bump, op: LowLevel) -> &[Ownership] {
match op {
Unreachable => arena.alloc_slice_copy(&[irrelevant]),
DictPseudoSeed => arena.alloc_slice_copy(&[irrelevant]),
ListLen | StrIsEmpty | StrCountUtf8Bytes | ListGetCapacity => {
ListLenU64 | ListLenUsize | StrIsEmpty | StrCountUtf8Bytes | ListGetCapacity => {
arena.alloc_slice_copy(&[borrowed])
}
ListWithCapacity | StrWithCapacity => arena.alloc_slice_copy(&[irrelevant]),
Expand Down
19 changes: 13 additions & 6 deletions crates/compiler/mono/src/ir/decision_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,7 @@ fn test_to_comparison<'a>(
LayoutRepr::Builtin(Builtin::List(_elem_layout)) => {
let real_len_expr = Expr::Call(Call {
call_type: CallType::LowLevel {
op: LowLevel::ListLen,
op: LowLevel::ListLenUsize,
update_mode: env.next_update_mode_id(),
},
arguments: env.arena.alloc([list_sym]),
Expand All @@ -1779,8 +1779,10 @@ fn test_to_comparison<'a>(
let real_len = env.unique_symbol();
let test_len = env.unique_symbol();

stores.push((real_len, Layout::U64, real_len_expr));
stores.push((test_len, Layout::U64, test_len_expr));
let usize_layout = Layout::usize(env.target_info);

stores.push((real_len, usize_layout, real_len_expr));
stores.push((test_len, usize_layout, test_len_expr));

let comparison = match bound {
ListLenBound::Exact => (real_len, Comparator::Eq, test_len),
Expand Down Expand Up @@ -2335,7 +2337,7 @@ fn decide_to_branching<'a>(
let len_symbol = env.unique_symbol();

let switch = Stmt::Switch {
cond_layout: Layout::U64,
cond_layout: Layout::usize(env.target_info),
cond_symbol: len_symbol,
branches: branches.into_bump_slice(),
default_branch: (default_branch_info, env.arena.alloc(default_branch)),
Expand All @@ -2344,13 +2346,18 @@ fn decide_to_branching<'a>(

let len_expr = Expr::Call(Call {
call_type: CallType::LowLevel {
op: LowLevel::ListLen,
op: LowLevel::ListLenUsize,
update_mode: env.next_update_mode_id(),
},
arguments: env.arena.alloc([inner_cond_symbol]),
});

Stmt::Let(len_symbol, len_expr, Layout::U64, env.arena.alloc(switch))
Stmt::Let(
len_symbol,
len_expr,
Layout::usize(env.target_info),
env.arena.alloc(switch),
)
} else {
Stmt::Switch {
cond_layout: inner_cond_layout,
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/mono/src/ir/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ pub(crate) fn build_list_index_probe<'a>(
let len_sym = env.unique_symbol();
let len_expr = Expr::Call(Call {
call_type: CallType::LowLevel {
op: LowLevel::ListLen,
op: LowLevel::ListLenU64,
update_mode: env.next_update_mode_id(),
},
arguments: env.arena.alloc([list_sym]),
Expand Down Expand Up @@ -1567,7 +1567,7 @@ fn store_list_rest<'a>(
call_type: CallType::LowLevel {
// Must use ListLenU64 here because we're using it with List.sublist,
// which takes U64s for start and len.
op: LowLevel::ListLen,
op: LowLevel::ListLenU64,
update_mode: env.next_update_mode_id(),
},
arguments: env.arena.alloc([list_sym]),
Expand Down
1 change: 1 addition & 0 deletions crates/compiler/mono/src/low_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ enum FirstOrder {
StrRepeat,
StrFromFloat,
ListLenU64,
ListLenUsize,
ListGetUnsafe,
ListSublist,
ListDropAt,
Expand Down

0 comments on commit 739565e

Please sign in to comment.