Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created April 27, 2022 22:51
Show Gist options
  • Save andrewrk/fa349667588753e79dfc9fcf64cfa394 to your computer and use it in GitHub Desktop.
Save andrewrk/fa349667588753e79dfc9fcf64cfa394 to your computer and use it in GitHub Desktop.
most common ZIR tags in the zig codebase
[nix-shell:~/Downloads/zig/build]$ ./zig fmt --ast-check ../src/ ../lib/std/
as_node: 571082
dbg_stmt: 325704
int: 266542
extended: 212003
param_type: 185337
int_big: 177048
call: 138602
field_call_bind: 105334
decl_val: 99774
break: 95248
break_inline: 93384
array_type: 92802
field_val: 88145
array_init: 86592
block: 64823
condbr: 63897
ref: 62863
field_ptr: 52090
store_node: 51806
enum_literal: 51690
ret_node: 47587
ensure_result_used: 47161
is_non_err: 41190
err_union_payload_unsafe: 40942
block_inline: 40516
err_union_code: 39679
dbg_var_val: 36395
load: 35334
param: 29922
str: 29211
store_to_block_ptr: 27680
decl_ref: 24870
field_type: 19274
ptr_type_simple: 17702
alloc_inferred: 15339
dbg_var_ptr: 12207
ret_load: 11127
add: 10323
elem_ptr_imm: 10127
elem_val: 9724
store: 9277
typeof: 9199
cmp_eq: 8835
func: 8264
ret_tok: 7065
alloc_mut: 5881
resolve_inferred_alloc: 5799
float: 5586
struct_init_anon: 5431
negate: 5330
alloc_inferred_mut: 5087
store_to_inferred_ptr: 4756
elem_ptr: 4692
validate_struct_init: 4464
switch_block: 4456
switch_cond: 4436
validate_struct_init_ty: 4086
array_init_anon: 3910
cmp_lt: 3905
field_base_ptr: 3817
loop: 3697
repeat: 3697
as: 3622
is_non_null: 3549
sub: 3505
optional_payload_unsafe: 3434
optional_type: 3422
cmp_neq: 3394
alloc: 3239
func_inferred: 3054
coerce_result_ptr: 2963
optional_payload_safe: 2888
typeof_log2_int_type: 2867
unreachable: 2846
ret_err_value: 2774
bool_not: 2689
int_cast: 2636
import: 2611
array_cat: 2360
mul: 2226
int_type: 2216
ensure_result_non_error: 2214
indexable_ptr_len: 2176
struct_init: 2051
array_init_ref: 2008
slice_end: 1981
error_union_type: 1976
bitcast: 1973
enum_to_int: 1944
slice_start: 1891
closure_get: 1851
bool_br_and: 1824
switch_capture: 1627
error_value: 1624
bit_or: 1568
shl: 1550
bit_and: 1441
shr: 1314
param_comptime: 1292
validate_struct_init_comptime: 1259
struct_init_empty: 1252
ptr_type: 1200
array_init_anon_ref: 1140
make_ptr_const: 1095
bool_br_or: 1093
cmp_gt: 1074
validate_array_init: 1013
param_anytype: 937
truncate: 812
cmp_gte: 785
div: 783
typeof_builtin: 769
size_of: 739
xor: 679
ptr_cast: 672
ptr_to_int: 635
type_info: 594
cmp_lte: 569
compile_error: 549
panic: 510
addwrap: 466
error_set_decl: 455
condbr_inline: 427
repeat_inline: 427
array_mul: 412
alloc_comptime_mut: 390
validate_array_init_ty: 379
elem_type: 379
bool_to_int: 370
closure_capture: 348
optional_payload_safe_ptr: 345
export_value: 328
int_to_ptr: 322
type_name: 298
tag_name: 285
mod_rem: 280
is_non_null_ptr: 269
optional_payload_unsafe_ptr: 269
merge_error_sets: 244
mulwrap: 234
float128: 229
subwrap: 222
align_of: 221
set_runtime_safety: 206
bit_not: 202
int_to_enum: 186
error_name: 185
bit_size_of: 175
field_parent_ptr: 147
struct_init_ref: 129
array_base_ptr: 128
export: 128
field_val_named: 119
has_decl: 107
alloc_inferred_comptime_mut: 105
align_cast: 103
array_type_sentinel: 94
alloc_inferred_comptime: 94
builtin_call: 92
vector_type: 91
div_exact: 89
int_to_float: 66
has_field: 59
float_to_int: 59
set_cold: 58
switch_capture_multi: 53
param_anytype_comptime: 51
atomic_rmw: 50
is_non_err_ptr: 49
err_union_payload_unsafe_ptr: 49
err_union_code_ptr: 49
switch_capture_ref: 48
float_cast: 46
shuffle: 46
slice_sentinel: 44
splat: 43
set_eval_branch_quota: 41
await: 41
clz: 39
byte_swap: 38
memset: 37
field_ptr_named: 34
reify: 33
maximum: 32
validate_array_init_comptime: 31
suspend_block: 30
ctz: 28
atomic_load: 28
div_trunc: 27
atomic_store: 24
pop_count: 23
minimum: 23
div_floor: 21
mod: 21
memcpy: 21
switch_cond_ref: 20
bit_reverse: 17
cmpxchg_strong: 14
offset_of: 14
embed_file: 12
sub_sat: 12
rem: 12
resume: 12
frame_type: 11
sqrt: 10
mul_add: 9
field_type_ref: 8
union_init: 8
floor: 8
ceil: 8
reduce: 8
sin: 7
cos: 7
exp: 7
exp2: 7
log: 7
log2: 7
log10: 7
fabs: 7
round: 7
trunc: 7
anyframe_type: 7
negate_wrap: 6
select: 6
mul_sat: 6
log2_int_type: 5
struct_init_anon_ref: 4
breakpoint: 4
err_set_cast: 4
builtin_async_call: 4
set_align_stack: 4
shr_exact: 3
error_to_int: 3
int_to_error: 3
cmpxchg_weak: 3
array_init_sent_ref: 3
add_sat: 3
array_init_sent: 2
await_nosuspend: 2
shl_sat: 2
set_float_mode: 1
field_call_bind_named: 1
fence: 1
switch_capture_multi_ref: 1
diff --git a/src/main.zig b/src/main.zig
index e47ff0e27..ba46d55c7 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -3797,6 +3797,7 @@ pub const usage_fmt =
\\
;
+const Zir = @import("Zir.zig");
const Fmt = struct {
seen: SeenMap,
any_error: bool,
@@ -3805,6 +3806,7 @@ const Fmt = struct {
gpa: Allocator,
arena: Allocator,
out_buffer: std.ArrayList(u8),
+ tag_counts: std.AutoArrayHashMap(Zir.Inst.Tag, u32),
const SeenMap = std.AutoHashMap(fs.File.INode, void);
};
@@ -3946,9 +3948,11 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
.check_ast = check_ast_flag,
.color = color,
.out_buffer = std.ArrayList(u8).init(gpa),
+ .tag_counts = std.AutoArrayHashMap(Zir.Inst.Tag, u32).init(gpa),
};
defer fmt.seen.deinit();
defer fmt.out_buffer.deinit();
+ defer fmt.tag_counts.deinit();
// Mark any excluded files/directories as already seen,
// so that they are skipped later during actual processing
@@ -3966,6 +3970,24 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
if (fmt.any_error) {
process.exit(1);
}
+ var sorted_tags = std.ArrayList(Zir.Inst.Tag).init(gpa);
+ defer sorted_tags.deinit();
+
+ try sorted_tags.appendSlice(fmt.tag_counts.keys());
+ const Sort = struct {
+ fmt: *Fmt,
+
+ pub fn lessThan(sort: @This(), a: Zir.Inst.Tag, b: Zir.Inst.Tag) bool {
+ const a_count = sort.fmt.tag_counts.get(a) orelse 0;
+ const b_count = sort.fmt.tag_counts.get(b) orelse 0;
+ return a_count > b_count;
+ }
+ };
+ std.sort.sort(Zir.Inst.Tag, sorted_tags.items, Sort{ .fmt = &fmt }, Sort.lessThan);
+ for (sorted_tags.items) |tag| {
+ const count = fmt.tag_counts.get(tag) orelse 0;
+ std.debug.print("{s}: {d}\n", .{ @tagName(tag), count });
+ }
}
const FmtError = error{
@@ -4128,6 +4150,12 @@ fn fmtPathFile(
}
fmt.any_error = true;
}
+
+ for (file.zir.instructions.items(.tag)) |tag| {
+ const gop = try fmt.tag_counts.getOrPut(tag);
+ if (!gop.found_existing) gop.value_ptr.* = 0;
+ gop.value_ptr.* += 1;
+ }
}
// As a heuristic, we make enough capacity for the same as the input source.
@@ -4712,7 +4740,6 @@ pub fn cmdAstCheck(
) !void {
const Module = @import("Module.zig");
const AstGen = @import("AstGen.zig");
- const Zir = @import("Zir.zig");
var color: Color = .auto;
var want_output_text = false;
@@ -4880,7 +4907,6 @@ pub fn cmdChangelist(
) !void {
const Module = @import("Module.zig");
const AstGen = @import("AstGen.zig");
- const Zir = @import("Zir.zig");
const old_source_file = args[0];
const new_source_file = args[1];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment