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

v: -skip-unused cleanup #22877

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Changes from all commits
Commits
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
95 changes: 24 additions & 71 deletions vlib/v/markused/markused.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
} else {
byteptr_idx_str := '${ast.byteptr_type_idx}'
charptr_idx_str := '${ast.charptr_type_idx}'
u8_idx_str := '${ast.u8_type_idx}'
string_idx_str := '${ast.string_type_idx}'
array_idx_str := '${ast.array_type_idx}'
map_idx_str := '${ast.map_type_idx}'
ref_array_idx_str := '${int(ast.array_type.ref())}'
mut core_fns := [
'main.main',
//'format_sb',
'init_global_allocator', // needed for linux_bare and wasm_bare
'v_realloc', // needed for _STR
'malloc',
Expand All @@ -40,61 +38,34 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
'vstrlen',
'tos',
'tos2',
'_option_ok',
'_result_ok',
'error',
'builtin_init',
// byte. methods
u8_idx_str + '.str_escaped',
// string. methods
// string_idx_str + '.add',
// string_idx_str + '.trim_space',
// string_idx_str + '.replace',
// string_idx_str + '.trim',
// string_idx_str + '.index_kmp',
// string. ==, !=, etc...
// string_idx_str + '.eq',
// string_idx_str + '.ne',
// string_idx_str + '.lt',
// string_idx_str + '.gt',
// string_idx_str + '.le',
// string_idx_str + '.ge',
'fast_string_eq',
// other array methods
// array_idx_str + '.get_unsafe',
// array_idx_str + '.set_unsafe',
// array_idx_str + '.first',
// array_idx_str + '.last',
// array_idx_str + '.pointers', // TODO: handle generic methods calling array primitives more precisely in pool_test.v
// array_idx_str + '.reverse',
// array_idx_str + '.repeat_to_depth',
// reference array methods
// ref_array_idx_str + '.last',
// ref_array_idx_str + '.pop',
// ref_array_idx_str + '.insert_many',
// ref_array_idx_str + '.reverse',
// ref_array_idx_str + '.set_unsafe',
// TODO: process the _vinit const initializations automatically too
// 'main.nasserts',
'main.vtest_init',
'main.vtest_new_metainfo',
'main.vtest_new_filemetainfo',
// 'os.getwd',
// 'v.embed_file.find_index_entry_by_path',
]
$if debug_used_features ? {
dump(table.used_features)
}
panic_deps := [
'__new_array_with_default',
'str_intp',
ref_array_idx_str + '.push',
string_idx_str + '.substr',
array_idx_str + '.slice',
array_idx_str + '.get',
'v_fixed_index',
]
// real world apps
if table.used_features.builtin_types || table.used_features.as_cast
|| table.used_features.auto_str || table.used_features.option_or_result {
// println('used builtin')
|| table.used_features.auto_str {
core_fns << panic_deps
core_fns << '__new_array'
core_fns << '__new_array_with_default'
core_fns << '__new_array_with_multi_default'
core_fns << '__new_array_with_array_default'
core_fns << 'new_array_from_c_array'
core_fns << 'str_intp'
// byteptr and charptr
core_fns << byteptr_idx_str + '.vstring'
core_fns << byteptr_idx_str + '.vstring_with_len'
Expand All @@ -104,15 +75,11 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
core_fns << charptr_idx_str + '.vstring_literal'

if table.used_features.index {
core_fns << 'v_fixed_index'
core_fns << string_idx_str + '.at_with_check'
core_fns << string_idx_str + '.clone'
core_fns << string_idx_str + '.clone_static'
core_fns << string_idx_str + '.at'
core_fns << array_idx_str + '.slice'
core_fns << array_idx_str + '.get'
core_fns << array_idx_str + '.set'
core_fns << ref_array_idx_str + '.push'
core_fns << ref_array_idx_str + '.set'
core_fns << map_idx_str + '.get'
core_fns << map_idx_str + '.set'
Expand All @@ -124,7 +91,6 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
core_fns << array_idx_str + '.get_with_check' // used for `x := a[i] or {}`
core_fns << array_idx_str + '.clone_static_to_depth'
core_fns << array_idx_str + '.clone_to_depth'
core_fns << string_idx_str + '.substr'
}
if table.used_features.cast_ptr {
core_fns << 'ptr_str' // TODO: remove this. It is currently needed for the auto str methods for &u8, fn types, etc; See `./v -skip-unused vlib/builtin/int_test.v`
Expand All @@ -151,33 +117,20 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
} else {
// TODO: this *should not* depend on the used compiler, which is brittle, but only on info in the AST...
// hello world apps
if pref_.ccompiler_type == .tinyc {
// unused on tcc
all_fns.delete(ref_array_idx_str + '.ensure_cap')
all_fns.delete(u8_idx_str + '.str_escaped')
all_fns.delete(u8_idx_str + '.ascii_str')
allow_noscan = false
if pref_.ccompiler_type != .tinyc && 'no_backtrace' !in pref_.compile_defines {
// with backtrace on gcc/clang more code needs be generated
allow_noscan = true
core_fns << panic_deps
} else {
if 'no_backtrace' in pref_.compile_defines {
// with -d no_backtrace on gcc/clang
allow_noscan = false
all_fns.delete(ref_array_idx_str + '.slice')
all_fns.delete(ref_array_idx_str + '.ensure_cap')
all_fns.delete(array_idx_str + '.get_unsafe')
all_fns.delete(u8_idx_str + '.str_escaped')
all_fns.delete(u8_idx_str + '.ascii_str')
} else {
// with backtrace on gcc/clang
core_fns << ref_array_idx_str + '.push_many_noscan'
core_fns << '__new_array_with_default'
core_fns << '__new_array_with_default_noscan'
core_fns << 'str_intp'
core_fns << ref_array_idx_str + '.push'
core_fns << string_idx_str + '.substr'
core_fns << array_idx_str + '.slice'
core_fns << array_idx_str + '.get'
core_fns << 'v_fixed_index'
}
allow_noscan = false
}
}
if table.used_features.option_or_result {
core_fns << '_option_ok'
core_fns << '_result_ok'
if !allow_noscan {
core_fns << panic_deps
allow_noscan = true
}
}
if table.used_features.as_cast {
Expand Down
Loading