Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created August 4, 2021 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewrk/011e9b7801595054f63be6779270af7b to your computer and use it in GitHub Desktop.
Save andrewrk/011e9b7801595054f63be6779270af7b to your computer and use it in GitHub Desktop.
stage2 generics demo
const std = @import("std");
const expect = std.testing.expect;
test "example" {
var x: usize = 0;
x += checkSize(i32);
x += checkSize(bool);
try expect(x == 5);
}
fn checkSize(comptime T: type) usize {
return @sizeOf(T);
}
# Source bytes: 245B
# Tokens: 68 (364B)
# AST Nodes: 37 (601B)
# Total ZIR bytes: 1.240234375KiB
# Instructions: 62 (558B)
# String Table Bytes: 44B
# Extra Data Items: 151 (604B)
%61 = extended(struct_decl(parent, Auto, {
[120] std line(0) hash(c9cf6ee7a5ad2804a9114568f721d663): %0 = block_inline({
%1 = import("std") token_offset:1:21
%2 = break_inline(%0, %1)
}) node_offset:1:1
[127] expect line(1) hash(e54c2f3027b16e0fb665e9d21a2cac97): %3 = block_inline({
%4 = decl_ref("std") token_offset:2:16
%5 = field_val(%4, "testing") node_offset:2:19
%6 = field_val(%5, "expect") node_offset:2:27
%7 = break_inline(%3, %6)
}) node_offset:2:1
[134] test example line(3) hash(dedd7fa9acbc1e2f491958e24495c353): %8 = block_inline({
%49 = extended(func(test, @Ref.void_type, inferror, {
%9 = dbg_stmt(4, 4)
%10 = alloc_mut(@Ref.usize_type) node_offset:5:5
%11 = store_node(%10, @Ref.zero) node_offset:5:20
%12 = dbg_stmt(5, 4)
%13 = load(%10) node_offset:6:7
%14 = typeof(%13) node_offset:6:7
%15 = decl_val("checkSize") token_offset:6:10
%16 = param_type(%15, 0)
%17 = as_node(%16, @Ref.i32_type) node_offset:6:20
%18 = call(%15, [%17]) node_offset:6:19
%19 = as_node(%14, %18) node_offset:6:19
%20 = add(%13, %19) node_offset:6:7
%21 = store(%10, %20)
%22 = dbg_stmt(6, 4)
%23 = load(%10) node_offset:7:7
%24 = typeof(%23) node_offset:7:7
%25 = decl_val("checkSize") token_offset:7:10
%26 = param_type(%25, 0)
%27 = as_node(%26, @Ref.bool_type) node_offset:7:20
%28 = call(%25, [%27]) node_offset:7:19
%29 = as_node(%24, %28) node_offset:7:19
%30 = add(%23, %29) node_offset:7:7
%31 = store(%10, %30)
%32 = dbg_stmt(7, 4)
%42 = block({
%33 = decl_val("expect") token_offset:8:9
%34 = param_type(%33, 0)
%35 = load(%10) node_offset:8:16
%36 = int(5)
%37 = cmp_eq(%35, %36) node_offset:8:18
%38 = as_node(%34, %37) node_offset:8:18
%39 = call(%33, [%38]) node_offset:8:15
%40 = is_non_err(%39) node_offset:8:5
%41 = condbr(%40, {
%43 = err_union_payload_unsafe(%39) node_offset:8:5
%46 = break(%42, %43)
}, {
%44 = err_union_code(%39) node_offset:8:5
%45 = ret_node(%44) node_offset:8:5
}) node_offset:8:5
}) node_offset:8:5
%47 = ensure_result_used(%42) node_offset:8:5
%48 = ret_coerce(@Ref.void_value) token_offset:9:1
}) (lbrace=7:4,rbrace=8:0) node_offset:4:1
%50 = break_inline(%8, %49)
}) node_offset:4:1
[141] checkSize line(10) hash(53a27a02cd639458dcc91efe1330b62a): %51 = block_inline({
%52 = param_comptime("T", @Ref.type_type) token_offset:11:23
%59 = func(@Ref.usize_type, {
%53 = dbg_stmt(11, 4)
%54 = extended(ret_type()) node_offset:12:5
%55 = as_node(@Ref.type_type, %52) node_offset:12:20
%56 = size_of(%55) node_offset:12:12
%57 = as_node(%54, %56) node_offset:12:12
%58 = ret_node(%57) node_offset:12:5
}) (lbrace=11:4,rbrace=12:0) node_offset:11:1
%60 = break_inline(%51, %59)
}) node_offset:11:1
}, {}, {})
Imports:
@import("std") token_abs:1:21
# Begin Function AIR: test.example:
# Total AIR+Liveness bytes: 854B
# AIR Instructions: 50 (450B)
# AIR Extra Data: 37 (148B)
# AIR Values Bytes: 12 (96B)
# Liveness tomb_bits: 32B
# Liveness Extra Data: 2 (8B)
# Liveness special table: 1 (8B)
%2 = constant(usize, 0)
%6 = const_ty(*const fn(type) callconv(.Unspecified) usize)
%7 = constant(*const fn(type) callconv(.Unspecified) usize, (decl ref))
%8 = const_ty(fn(type) callconv(.Unspecified) usize)
%9 = constant(fn(type) callconv(.Unspecified) usize, (function 'checkSize'))
%10 = const_ty(*const fn() callconv(.Unspecified) usize)
%11 = constant(*const fn() callconv(.Unspecified) usize, (decl ref))
%12 = const_ty(fn() callconv(.Unspecified) usize)
%13 = constant(fn() callconv(.Unspecified) usize, (function 'checkSize__anon_2'))
%19 = const_ty(*const fn() callconv(.Unspecified) usize)
%20 = constant(*const fn() callconv(.Unspecified) usize, (decl ref))
%21 = const_ty(fn() callconv(.Unspecified) usize)
%22 = constant(fn() callconv(.Unspecified) usize, (function 'checkSize__anon_3'))
%28 = const_ty(*const fn(bool) callconv(.Unspecified) (inferred error set of expect)!void)
%29 = constant(*const fn(bool) callconv(.Unspecified) (inferred error set of expect)!void, (decl ref))
%30 = const_ty(fn(bool) callconv(.Unspecified) (inferred error set of expect)!void)
%31 = constant(fn(bool) callconv(.Unspecified) (inferred error set of expect)!void, (function 'expect'))
%33 = constant(comptime_int, 5)
%34 = const_ty(u64)
%36 = const_ty(u64)
%37 = constant(u64, 5)
%43 = const_ty((inferred error set of expect))
%47 = const_ty((inferred error set of test.example)!void)
%48 = constant((inferred error set of test.example)!void, error_union_val({}))
%0!= dbg_stmt(5:5)
%1 = alloc(*usize)
%3!= store(%1, %2!)
%4!= dbg_stmt(6:5)
%5 = load(usize, %1)
%14 = call(%13!, [])
%15 = add(%5!, %14!)
%16!= store(%1, %15!)
%17!= dbg_stmt(7:5)
%18 = load(usize, %1)
%23 = call(%22!, [])
%24 = add(%18!, %23!)
%25!= store(%1, %24!)
%26!= dbg_stmt(8:5)
%27!= block({
%32 = load(usize, %1!)
%35 = intcast(u64, %32!)
%38 = cmp_eq(%35!, %37!)
%39 = call(%31!, [%38!])
%40 = is_non_err(%39)
%46!= cond_br(%40!, {
%41 = unwrap_errunion_payload(void, %39!)
%42!= br(%27, %41!)
}, {
%44 = unwrap_errunion_err((inferred error set of expect), %39!)
%45!= ret(%44!)
})
})
%49!= ret(%48!)
# End Function AIR: test.example:
# Begin Function AIR: checkSize__anon_2:
# Total AIR+Liveness bytes: 205B
# AIR Instructions: 5 (45B)
# AIR Extra Data: 4 (16B)
# AIR Values Bytes: 3 (24B)
# Liveness tomb_bits: 8B
# Liveness Extra Data: 0 (0B)
# Liveness special table: 0 (0B)
%0 = constant(type, i32)
%2 = constant(comptime_int, 4)
%3 = constant(usize, 4)
%1!= dbg_stmt(12:5)
%4!= ret(%3!)
# End Function AIR: checkSize__anon_2:
# Begin Function AIR: checkSize__anon_3:
# Total AIR+Liveness bytes: 205B
# AIR Instructions: 5 (45B)
# AIR Extra Data: 4 (16B)
# AIR Values Bytes: 3 (24B)
# Liveness tomb_bits: 8B
# Liveness Extra Data: 0 (0B)
# Liveness special table: 0 (0B)
%0 = constant(type, bool)
%2 = constant(comptime_int, 1)
%3 = constant(usize, 1)
%1!= dbg_stmt(12:5)
%4!= ret(%3!)
# End Function AIR: checkSize__anon_3:
; ModuleID = 'test'
source_filename = "test"
@test_functions = constant { { { i8*, i64 }, i16 ()*, { i64, i1 } }*, i64 } { { { i8*, i64 }, i16 ()*, { i64, i1 } }* getelementptr inbounds ([1 x { { i8*, i64 }, i16 ()*, { i64, i1 } }], [1 x { { i8*, i64 }, i16 ()*, { i64, i1 } }]* @test_functions__anon_6, i32 0, i32 0), i64 1 }
@test_functions__anon_6__anon_7 = constant [12 x i8] c"test.example"
@test_functions__anon_6 = constant [1 x { { i8*, i64 }, i16 ()*, { i64, i1 } }] [{ { i8*, i64 }, i16 ()*, { i64, i1 } } { { i8*, i64 } { i8* getelementptr inbounds ([12 x i8], [12 x i8]* @test_functions__anon_6__anon_7, i32 0, i32 0), i64 12 }, i16 ()* @test.example, { i64, i1 } zeroinitializer }]
@_start = alias void (), void ()* @_start2
define internal i16 @test.example() unnamed_addr {
Entry:
%0 = alloca i64, align 8
store i64 0, i64* %0, align 4
%1 = load i64, i64* %0, align 4
%2 = call i64 @checkSize__anon_2()
%3 = add nuw i64 %1, %2
store i64 %3, i64* %0, align 4
%4 = load i64, i64* %0, align 4
%5 = call i64 @checkSize__anon_3()
%6 = add nuw i64 %4, %5
store i64 %6, i64* %0, align 4
%7 = load i64, i64* %0, align 4
%8 = icmp eq i64 %7, 5
%9 = call i16 @expect(i1 %8)
%10 = icmp eq i16 %9, 0
br i1 %10, label %Then, label %Else
Then: ; preds = %Entry
br label %Block
Else: ; preds = %Entry
ret i16 %9
Block: ; preds = %Then
ret i16 0
}
define internal i64 @checkSize__anon_2() unnamed_addr {
Entry:
ret i64 4
}
define internal i64 @checkSize__anon_3() unnamed_addr {
Entry:
ret i64 1
}
define internal i16 @expect(i1 %0) unnamed_addr {
Entry:
%1 = alloca i1, align 1
store i1 %0, i1* %1, align 1
%2 = load i1, i1* %1, align 1
%3 = xor i1 %2, true
br i1 %3, label %Then, label %Else
Then: ; preds = %Entry
ret i16 1
Else: ; preds = %Entry
br label %Block
Block: ; preds = %Else
ret i16 0
}
; Function Attrs: noreturn
define dso_local void @_start2() #0 {
Entry:
call void @main()
call void @exit2(i64 0)
unreachable
}
define internal void @main() unnamed_addr {
Entry:
%0 = call i16 @main2()
%1 = icmp eq i16 %0, 0
br i1 %1, label %Then, label %Else
Then: ; preds = %Entry
br label %Block
Else: ; preds = %Entry
call void @llvm.debugtrap()
unreachable
Block: ; preds = %Then
ret void
}
; Function Attrs: noreturn
define internal void @exit2(i64 %0) unnamed_addr #0 {
Entry:
%1 = alloca i64, align 8
store i64 %0, i64* %1, align 4
%2 = load i64, i64* %1, align 4
call void asm sideeffect "syscall", "{rax},{rdi},~{rcx},~{r11},~{memory}"(i64 231, i64 %2)
call void @llvm.debugtrap()
unreachable
}
define internal i16 @main2() unnamed_addr {
Entry:
%0 = alloca i64, align 8
%1 = load { { { i8*, i64 }, i16 ()*, { i64, i1 } }*, i64 }, { { { i8*, i64 }, i16 ()*, { i64, i1 } }*, i64 }* @test_functions, align 8
%2 = extractvalue { { { i8*, i64 }, i16 ()*, { i64, i1 } }*, i64 } %1, 1
store i64 0, i64* %0, align 4
br label %Loop
Loop: ; preds = %Block3, %Entry
%3 = load i64, i64* %0, align 4
%4 = icmp slt i64 %3, %2
br i1 %4, label %Then, label %Else
Then: ; preds = %Loop
%5 = load { { { i8*, i64 }, i16 ()*, { i64, i1 } }*, i64 }, { { { i8*, i64 }, i16 ()*, { i64, i1 } }*, i64 }* @test_functions, align 8
%6 = extractvalue { { { i8*, i64 }, i16 ()*, { i64, i1 } }*, i64 } %5, 0
%7 = getelementptr inbounds { { i8*, i64 }, i16 ()*, { i64, i1 } }, { { i8*, i64 }, i16 ()*, { i64, i1 } }* %6, i64 %3
%8 = load { { i8*, i64 }, i16 ()*, { i64, i1 } }, { { i8*, i64 }, i16 ()*, { i64, i1 } }* %7, align 8
%9 = extractvalue { { i8*, i64 }, i16 ()*, { i64, i1 } } %8, 1
%10 = call i16 %9()
%11 = icmp eq i16 %10, 0
br i1 %11, label %Then1, label %Else2
Else: ; preds = %Loop
br label %Block4
Then1: ; preds = %Then
br label %Block
Else2: ; preds = %Then
ret i16 %10
Block: ; preds = %Then1
br label %Block3
Block3: ; preds = %Block
%12 = load i64, i64* %0, align 4
%13 = add nuw i64 %12, 1
store i64 %13, i64* %0, align 4
br label %Loop
Block4: ; preds = %Else
ret i16 0
}
; Function Attrs: nounwind
declare void @llvm.debugtrap() #1
attributes #0 = { noreturn }
attributes #1 = { nounwind }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment