Optimize Kernel Invariant on Unwrap and Tuple #261

Merged
sb10q merged 1 commits from optimize_unwrap_kernelinvariant into master 2024-08-17 17:37:19 +08:00
Collaborator

fixes #244

for this code:

@nac3
class A:
    a: KernelInvariant[int32]
    def __init__(self, a: int32):
        self.a = a


@nac3
class Demo:
    core: KernelInvariant[Core]
    i: KernelInvariant[int32]
    o: KernelInvariant[Option[int32]]
    k: KernelInvariant[Option[A]]
    d: KernelInvariant[Option[int32]]
    t: KernelInvariant[Option[Option[tuple[Option[int32]]]]]
    p: KernelInvariant[A]

    def __init__(self):
        self.core = Core()
        self.i = 123
        self.o = Some(234)
        self.k = Some(A(999))
        self.d = none
        self.t = Some(Some((Some(555), )))
        self.p = A(888)

    @kernel
    def run(self):
        print_int32(self.i)
        print_int32(self.o.unwrap())
        print_int32(self.k.unwrap().a)
        print_int32(self.t.unwrap().unwrap()[0].unwrap())
        print_int32(self.p.a)
        
        # unwrap on kernel invariant none, directly raise exception
        print_int32(self.d.unwrap())
        print_int32(0)
optmized IR before this PR
; ModuleID = 'main'
source_filename = "main"

%__main__.A = type { i32 }
%min_artiq.Core = type { double }
%__main__.Demo = type { %min_artiq.Core*, i32, i32*, %__main__.A**, i32*, { i32* }**, %__main__.A* }
%Exception = type { i32, %str, i32, i32, %str, %str, i64, i64, i64 }
%str = type { i8*, i64 }

@const = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@const.1 = private unnamed_addr constant [69 x i8] c"/home/cresc/code/nac3/work1/nac3/my_expr/artiq/./kernel_invariant.py\00", align 1
@const.2 = private unnamed_addr constant [20 x i8] c"__main__.Demo.run.0\00", align 1
@"140094400693920_option" = global i32 234
@"140094400718736_option" = global %__main__.A* @"140094400593056"
@"140094399590800_option" = global i32 555
@"140094399611088_option" = global { i32* } { i32* @"140094399590800_option" }
@"140092118420400_option" = global { i32* }* @"140094399611088_option"
@"140094400701776" = global %__main__.A { i32 888 }
@"140094400593056" = global %__main__.A { i32 999 }
@"140094400701680" = global %min_artiq.Core { double 1.000000e-09 }
@"140094400704416" = local_unnamed_addr global %__main__.Demo { %min_artiq.Core* @"140094400701680", i32 123, i32* @"140094400693920_option", %__main__.A** @"140094400718736_option", i32* null, { i32* }** @"140092118420400_option", %__main__.A* @"140094400701776" }

; Function Attrs: noreturn
define private fastcc void @__main__.Demo.run.0() unnamed_addr #0 personality i32 (...)* @__nac3_personality {
init:
  tail call void @print_int32(i32 123)
  %unwrap_some = load i32, i32* @"140094400693920_option", align 4
  tail call void @print_int32(i32 %unwrap_some)
  %unwrap_some16 = load %__main__.A*, %__main__.A** @"140094400718736_option", align 8
  %gep = getelementptr %__main__.A, %__main__.A* %unwrap_some16, i64 0, i32 0
  %load = load i32, i32* %gep, align 4
  tail call void @print_int32(i32 %load)
  %unwrap_some30 = load { i32* }*, { i32* }** @"140092118420400_option", align 8
  %unwrap_not_null.not = icmp eq { i32* }* %unwrap_some30, null
  br i1 %unwrap_not_null.not, label %fail33, label %succ32, !prof !0

succ32:                                           ; preds = %init
  %0 = getelementptr { i32* }, { i32* }* %unwrap_some30, i64 0, i32 0
  %unwrap_some44.unpack = load i32*, i32** %0, align 8
  %unwrap_not_null45.not = icmp eq i32* %unwrap_some44.unpack, null
  br i1 %unwrap_not_null45.not, label %fail48, label %succ47, !prof !0

fail33:                                           ; preds = %init
  %alloca34 = alloca %Exception, align 8
  %exn.id35 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 0
  store i32 10, i32* %exn.id35, align 8
  %exn.msg36.repack = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 5, i32 0
  store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @const, i64 0, i64 0), i8** %exn.msg36.repack, align 8
  %exn.msg36.repack1 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 5, i32 1
  %file_ptr40.repack = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 1, i32 0
  %1 = bitcast i64* %exn.msg36.repack1 to i8*
  call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 8 dereferenceable(32) %1, i8 0, i64 32, i1 false)
  store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @const.1, i64 0, i64 0), i8** %file_ptr40.repack, align 8
  %file_ptr40.repack2 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 1, i32 1
  store i64 68, i64* %file_ptr40.repack2, align 8
  %row_ptr41 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 2
  store i32 54, i32* %row_ptr41, align 8
  %col_ptr42 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 3
  store i32 43, i32* %col_ptr42, align 4
  %name_ptr43.repack = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 4, i32 0
  store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @const.2, i64 0, i64 0), i8** %name_ptr43.repack, align 8
  %name_ptr43.repack3 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 4, i32 1
  store i64 19, i64* %name_ptr43.repack3, align 8
  call void @__nac3_raise(%Exception* nonnull %alloca34)
  unreachable

succ47:                                           ; preds = %succ32
  %unwrap_some59 = load i32, i32* %unwrap_some44.unpack, align 4
  tail call void @print_int32(i32 %unwrap_some59)
  tail call void @print_int32(i32 888)
  %alloca63 = alloca %Exception, align 8
  %exn.id64 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 0
  store i32 10, i32* %exn.id64, align 8
  %exn.msg65.repack = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 5, i32 0
  store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @const, i64 0, i64 0), i8** %exn.msg65.repack, align 8
  %exn.msg65.repack8 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 5, i32 1
  %file_ptr69.repack = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 1, i32 0
  %2 = bitcast i64* %exn.msg65.repack8 to i8*
  call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 8 dereferenceable(32) %2, i8 0, i64 32, i1 false)
  store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @const.1, i64 0, i64 0), i8** %file_ptr69.repack, align 8
  %file_ptr69.repack9 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 1, i32 1
  store i64 68, i64* %file_ptr69.repack9, align 8
  %row_ptr70 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 2
  store i32 58, i32* %row_ptr70, align 8
  %col_ptr71 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 3
  store i32 34, i32* %col_ptr71, align 4
  %name_ptr72.repack = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 4, i32 0
  store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @const.2, i64 0, i64 0), i8** %name_ptr72.repack, align 8
  %name_ptr72.repack10 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 4, i32 1
  store i64 19, i64* %name_ptr72.repack10, align 8
  call void @__nac3_raise(%Exception* nonnull %alloca63)
  unreachable

fail48:                                           ; preds = %succ32
  %alloca49 = alloca %Exception, align 8
  %exn.id50 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 0
  store i32 10, i32* %exn.id50, align 8
  %exn.msg51.repack = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 5, i32 0
  store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @const, i64 0, i64 0), i8** %exn.msg51.repack, align 8
  %exn.msg51.repack5 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 5, i32 1
  %file_ptr55.repack = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 1, i32 0
  %3 = bitcast i64* %exn.msg51.repack5 to i8*
  call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 8 dereferenceable(32) %3, i8 0, i64 32, i1 false)
  store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @const.1, i64 0, i64 0), i8** %file_ptr55.repack, align 8
  %file_ptr55.repack6 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 1, i32 1
  store i64 68, i64* %file_ptr55.repack6, align 8
  %row_ptr56 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 2
  store i32 54, i32* %row_ptr56, align 8
  %col_ptr57 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 3
  store i32 55, i32* %col_ptr57, align 4
  %name_ptr58.repack = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 4, i32 0
  store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @const.2, i64 0, i64 0), i8** %name_ptr58.repack, align 8
  %name_ptr58.repack7 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 4, i32 1
  store i64 19, i64* %name_ptr58.repack7, align 8
  call void @__nac3_raise(%Exception* nonnull %alloca49)
  unreachable
}

declare i32 @__nac3_personality(...)

declare void @print_int32(i32) local_unnamed_addr

; Function Attrs: noreturn
declare void @__nac3_raise(%Exception*) local_unnamed_addr #0

; Function Attrs: noreturn
define void @__modinit__() local_unnamed_addr #0 personality i32 (...)* @__nac3_personality {
init:
  tail call fastcc void @__main__.Demo.run.0()
  unreachable
}

; Function Attrs: argmemonly nofree nounwind willreturn writeonly
declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { noreturn }
attributes #1 = { argmemonly nofree nounwind willreturn writeonly }

!0 = !{!"branch_weights", i32 1, i32 2000}
optmized IR after this PR
; ModuleID = 'main'
source_filename = "main"

%__main__.A = type { i32 }
%min_artiq.Core = type { double }
%__main__.Demo = type { %min_artiq.Core*, i32, i32*, %__main__.A**, i32*, { i32* }**, %__main__.A* }
%Exception = type { i32, %str, i32, i32, %str, %str, i64, i64, i64 }
%str = type { i8*, i64 }

@const = private unnamed_addr constant [1 x i8] zeroinitializer, align 1
@const.1 = private unnamed_addr constant [69 x i8] c"/home/cresc/code/nac3/work1/nac3/my_expr/artiq/./kernel_invariant.py\00", align 1
@const.2 = private unnamed_addr constant [20 x i8] c"__main__.Demo.run.0\00", align 1
@"139655879902880_option" = global i32 234
@"139655879927696_option" = global %__main__.A* @"139655879802016"
@"139655878799760_option" = global i32 555
@"139655878820048_option" = global { i32* } { i32* @"139655878799760_option" }
@"139653597629360_option" = global { i32* }* @"139655878820048_option"
@"139655879910640" = global %min_artiq.Core { double 1.000000e-09 }
@"139655879802016" = global %__main__.A { i32 999 }
@"139655879910736" = global %__main__.A { i32 888 }
@"139655879913376" = local_unnamed_addr global %__main__.Demo { %min_artiq.Core* @"139655879910640", i32 123, i32* @"139655879902880_option", %__main__.A** @"139655879927696_option", i32* null, { i32* }** @"139653597629360_option", %__main__.A* @"139655879910736" }

; Function Attrs: noreturn
define private fastcc void @__main__.Demo.run.0() unnamed_addr #0 personality i32 (...)* @__nac3_personality {
init:
  tail call void @print_int32(i32 123)
  tail call void @print_int32(i32 234)
  tail call void @print_int32(i32 999)
  tail call void @print_int32(i32 555)
  tail call void @print_int32(i32 888)
  %alloca = alloca %Exception, align 8
  %exn.id = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 0
  store i32 10, i32* %exn.id, align 8
  %.fca.0.gep = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 5, i32 0
  store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @const, i64 0, i64 0), i8** %.fca.0.gep, align 8
  %.fca.1.gep = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 5, i32 1
  %.fca.0.gep3 = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 1, i32 0
  %0 = bitcast i64* %.fca.1.gep to i8*
  call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 8 dereferenceable(32) %0, i8 0, i64 32, i1 false)
  store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @const.1, i64 0, i64 0), i8** %.fca.0.gep3, align 8
  %.fca.1.gep4 = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 1, i32 1
  store i64 68, i64* %.fca.1.gep4, align 8
  %row_ptr = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 2
  store i32 58, i32* %row_ptr, align 8
  %col_ptr = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 3
  store i32 21, i32* %col_ptr, align 4
  %.fca.0.gep5 = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 4, i32 0
  store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @const.2, i64 0, i64 0), i8** %.fca.0.gep5, align 8
  %.fca.1.gep6 = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 4, i32 1
  store i64 19, i64* %.fca.1.gep6, align 8
  call void @__nac3_raise(%Exception* nonnull %alloca)
  unreachable
}

declare i32 @__nac3_personality(...)

declare void @print_int32(i32) local_unnamed_addr

; Function Attrs: noreturn
declare void @__nac3_raise(%Exception*) local_unnamed_addr #0

; Function Attrs: noreturn
define void @__modinit__() local_unnamed_addr #0 personality i32 (...)* @__nac3_personality {
init:
  tail call fastcc void @__main__.Demo.run.0()
  unreachable
}

; Function Attrs: argmemonly nofree nounwind willreturn writeonly
declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg) #1

attributes #0 = { noreturn }
attributes #1 = { argmemonly nofree nounwind willreturn writeonly }
fixes #244 for this code: ```python @nac3 class A: a: KernelInvariant[int32] def __init__(self, a: int32): self.a = a @nac3 class Demo: core: KernelInvariant[Core] i: KernelInvariant[int32] o: KernelInvariant[Option[int32]] k: KernelInvariant[Option[A]] d: KernelInvariant[Option[int32]] t: KernelInvariant[Option[Option[tuple[Option[int32]]]]] p: KernelInvariant[A] def __init__(self): self.core = Core() self.i = 123 self.o = Some(234) self.k = Some(A(999)) self.d = none self.t = Some(Some((Some(555), ))) self.p = A(888) @kernel def run(self): print_int32(self.i) print_int32(self.o.unwrap()) print_int32(self.k.unwrap().a) print_int32(self.t.unwrap().unwrap()[0].unwrap()) print_int32(self.p.a) # unwrap on kernel invariant none, directly raise exception print_int32(self.d.unwrap()) print_int32(0) ``` <details> <summary>optmized IR before this PR</summary> ```llvm ; ModuleID = 'main' source_filename = "main" %__main__.A = type { i32 } %min_artiq.Core = type { double } %__main__.Demo = type { %min_artiq.Core*, i32, i32*, %__main__.A**, i32*, { i32* }**, %__main__.A* } %Exception = type { i32, %str, i32, i32, %str, %str, i64, i64, i64 } %str = type { i8*, i64 } @const = private unnamed_addr constant [1 x i8] zeroinitializer, align 1 @const.1 = private unnamed_addr constant [69 x i8] c"/home/cresc/code/nac3/work1/nac3/my_expr/artiq/./kernel_invariant.py\00", align 1 @const.2 = private unnamed_addr constant [20 x i8] c"__main__.Demo.run.0\00", align 1 @"140094400693920_option" = global i32 234 @"140094400718736_option" = global %__main__.A* @"140094400593056" @"140094399590800_option" = global i32 555 @"140094399611088_option" = global { i32* } { i32* @"140094399590800_option" } @"140092118420400_option" = global { i32* }* @"140094399611088_option" @"140094400701776" = global %__main__.A { i32 888 } @"140094400593056" = global %__main__.A { i32 999 } @"140094400701680" = global %min_artiq.Core { double 1.000000e-09 } @"140094400704416" = local_unnamed_addr global %__main__.Demo { %min_artiq.Core* @"140094400701680", i32 123, i32* @"140094400693920_option", %__main__.A** @"140094400718736_option", i32* null, { i32* }** @"140092118420400_option", %__main__.A* @"140094400701776" } ; Function Attrs: noreturn define private fastcc void @__main__.Demo.run.0() unnamed_addr #0 personality i32 (...)* @__nac3_personality { init: tail call void @print_int32(i32 123) %unwrap_some = load i32, i32* @"140094400693920_option", align 4 tail call void @print_int32(i32 %unwrap_some) %unwrap_some16 = load %__main__.A*, %__main__.A** @"140094400718736_option", align 8 %gep = getelementptr %__main__.A, %__main__.A* %unwrap_some16, i64 0, i32 0 %load = load i32, i32* %gep, align 4 tail call void @print_int32(i32 %load) %unwrap_some30 = load { i32* }*, { i32* }** @"140092118420400_option", align 8 %unwrap_not_null.not = icmp eq { i32* }* %unwrap_some30, null br i1 %unwrap_not_null.not, label %fail33, label %succ32, !prof !0 succ32: ; preds = %init %0 = getelementptr { i32* }, { i32* }* %unwrap_some30, i64 0, i32 0 %unwrap_some44.unpack = load i32*, i32** %0, align 8 %unwrap_not_null45.not = icmp eq i32* %unwrap_some44.unpack, null br i1 %unwrap_not_null45.not, label %fail48, label %succ47, !prof !0 fail33: ; preds = %init %alloca34 = alloca %Exception, align 8 %exn.id35 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 0 store i32 10, i32* %exn.id35, align 8 %exn.msg36.repack = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 5, i32 0 store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @const, i64 0, i64 0), i8** %exn.msg36.repack, align 8 %exn.msg36.repack1 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 5, i32 1 %file_ptr40.repack = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 1, i32 0 %1 = bitcast i64* %exn.msg36.repack1 to i8* call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 8 dereferenceable(32) %1, i8 0, i64 32, i1 false) store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @const.1, i64 0, i64 0), i8** %file_ptr40.repack, align 8 %file_ptr40.repack2 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 1, i32 1 store i64 68, i64* %file_ptr40.repack2, align 8 %row_ptr41 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 2 store i32 54, i32* %row_ptr41, align 8 %col_ptr42 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 3 store i32 43, i32* %col_ptr42, align 4 %name_ptr43.repack = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 4, i32 0 store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @const.2, i64 0, i64 0), i8** %name_ptr43.repack, align 8 %name_ptr43.repack3 = getelementptr inbounds %Exception, %Exception* %alloca34, i64 0, i32 4, i32 1 store i64 19, i64* %name_ptr43.repack3, align 8 call void @__nac3_raise(%Exception* nonnull %alloca34) unreachable succ47: ; preds = %succ32 %unwrap_some59 = load i32, i32* %unwrap_some44.unpack, align 4 tail call void @print_int32(i32 %unwrap_some59) tail call void @print_int32(i32 888) %alloca63 = alloca %Exception, align 8 %exn.id64 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 0 store i32 10, i32* %exn.id64, align 8 %exn.msg65.repack = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 5, i32 0 store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @const, i64 0, i64 0), i8** %exn.msg65.repack, align 8 %exn.msg65.repack8 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 5, i32 1 %file_ptr69.repack = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 1, i32 0 %2 = bitcast i64* %exn.msg65.repack8 to i8* call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 8 dereferenceable(32) %2, i8 0, i64 32, i1 false) store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @const.1, i64 0, i64 0), i8** %file_ptr69.repack, align 8 %file_ptr69.repack9 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 1, i32 1 store i64 68, i64* %file_ptr69.repack9, align 8 %row_ptr70 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 2 store i32 58, i32* %row_ptr70, align 8 %col_ptr71 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 3 store i32 34, i32* %col_ptr71, align 4 %name_ptr72.repack = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 4, i32 0 store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @const.2, i64 0, i64 0), i8** %name_ptr72.repack, align 8 %name_ptr72.repack10 = getelementptr inbounds %Exception, %Exception* %alloca63, i64 0, i32 4, i32 1 store i64 19, i64* %name_ptr72.repack10, align 8 call void @__nac3_raise(%Exception* nonnull %alloca63) unreachable fail48: ; preds = %succ32 %alloca49 = alloca %Exception, align 8 %exn.id50 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 0 store i32 10, i32* %exn.id50, align 8 %exn.msg51.repack = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 5, i32 0 store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @const, i64 0, i64 0), i8** %exn.msg51.repack, align 8 %exn.msg51.repack5 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 5, i32 1 %file_ptr55.repack = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 1, i32 0 %3 = bitcast i64* %exn.msg51.repack5 to i8* call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 8 dereferenceable(32) %3, i8 0, i64 32, i1 false) store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @const.1, i64 0, i64 0), i8** %file_ptr55.repack, align 8 %file_ptr55.repack6 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 1, i32 1 store i64 68, i64* %file_ptr55.repack6, align 8 %row_ptr56 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 2 store i32 54, i32* %row_ptr56, align 8 %col_ptr57 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 3 store i32 55, i32* %col_ptr57, align 4 %name_ptr58.repack = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 4, i32 0 store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @const.2, i64 0, i64 0), i8** %name_ptr58.repack, align 8 %name_ptr58.repack7 = getelementptr inbounds %Exception, %Exception* %alloca49, i64 0, i32 4, i32 1 store i64 19, i64* %name_ptr58.repack7, align 8 call void @__nac3_raise(%Exception* nonnull %alloca49) unreachable } declare i32 @__nac3_personality(...) declare void @print_int32(i32) local_unnamed_addr ; Function Attrs: noreturn declare void @__nac3_raise(%Exception*) local_unnamed_addr #0 ; Function Attrs: noreturn define void @__modinit__() local_unnamed_addr #0 personality i32 (...)* @__nac3_personality { init: tail call fastcc void @__main__.Demo.run.0() unreachable } ; Function Attrs: argmemonly nofree nounwind willreturn writeonly declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg) #1 attributes #0 = { noreturn } attributes #1 = { argmemonly nofree nounwind willreturn writeonly } !0 = !{!"branch_weights", i32 1, i32 2000} ``` </details> <details> <summary>optmized IR after this PR</summary> ```llvm ; ModuleID = 'main' source_filename = "main" %__main__.A = type { i32 } %min_artiq.Core = type { double } %__main__.Demo = type { %min_artiq.Core*, i32, i32*, %__main__.A**, i32*, { i32* }**, %__main__.A* } %Exception = type { i32, %str, i32, i32, %str, %str, i64, i64, i64 } %str = type { i8*, i64 } @const = private unnamed_addr constant [1 x i8] zeroinitializer, align 1 @const.1 = private unnamed_addr constant [69 x i8] c"/home/cresc/code/nac3/work1/nac3/my_expr/artiq/./kernel_invariant.py\00", align 1 @const.2 = private unnamed_addr constant [20 x i8] c"__main__.Demo.run.0\00", align 1 @"139655879902880_option" = global i32 234 @"139655879927696_option" = global %__main__.A* @"139655879802016" @"139655878799760_option" = global i32 555 @"139655878820048_option" = global { i32* } { i32* @"139655878799760_option" } @"139653597629360_option" = global { i32* }* @"139655878820048_option" @"139655879910640" = global %min_artiq.Core { double 1.000000e-09 } @"139655879802016" = global %__main__.A { i32 999 } @"139655879910736" = global %__main__.A { i32 888 } @"139655879913376" = local_unnamed_addr global %__main__.Demo { %min_artiq.Core* @"139655879910640", i32 123, i32* @"139655879902880_option", %__main__.A** @"139655879927696_option", i32* null, { i32* }** @"139653597629360_option", %__main__.A* @"139655879910736" } ; Function Attrs: noreturn define private fastcc void @__main__.Demo.run.0() unnamed_addr #0 personality i32 (...)* @__nac3_personality { init: tail call void @print_int32(i32 123) tail call void @print_int32(i32 234) tail call void @print_int32(i32 999) tail call void @print_int32(i32 555) tail call void @print_int32(i32 888) %alloca = alloca %Exception, align 8 %exn.id = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 0 store i32 10, i32* %exn.id, align 8 %.fca.0.gep = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 5, i32 0 store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @const, i64 0, i64 0), i8** %.fca.0.gep, align 8 %.fca.1.gep = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 5, i32 1 %.fca.0.gep3 = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 1, i32 0 %0 = bitcast i64* %.fca.1.gep to i8* call void @llvm.memset.p0i8.i64(i8* noundef nonnull align 8 dereferenceable(32) %0, i8 0, i64 32, i1 false) store i8* getelementptr inbounds ([69 x i8], [69 x i8]* @const.1, i64 0, i64 0), i8** %.fca.0.gep3, align 8 %.fca.1.gep4 = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 1, i32 1 store i64 68, i64* %.fca.1.gep4, align 8 %row_ptr = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 2 store i32 58, i32* %row_ptr, align 8 %col_ptr = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 3 store i32 21, i32* %col_ptr, align 4 %.fca.0.gep5 = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 4, i32 0 store i8* getelementptr inbounds ([20 x i8], [20 x i8]* @const.2, i64 0, i64 0), i8** %.fca.0.gep5, align 8 %.fca.1.gep6 = getelementptr inbounds %Exception, %Exception* %alloca, i64 0, i32 4, i32 1 store i64 19, i64* %.fca.1.gep6, align 8 call void @__nac3_raise(%Exception* nonnull %alloca) unreachable } declare i32 @__nac3_personality(...) declare void @print_int32(i32) local_unnamed_addr ; Function Attrs: noreturn declare void @__nac3_raise(%Exception*) local_unnamed_addr #0 ; Function Attrs: noreturn define void @__modinit__() local_unnamed_addr #0 personality i32 (...)* @__nac3_personality { init: tail call fastcc void @__main__.Demo.run.0() unreachable } ; Function Attrs: argmemonly nofree nounwind willreturn writeonly declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg) #1 attributes #0 = { noreturn } attributes #1 = { argmemonly nofree nounwind willreturn writeonly } ``` </details>
sb10q reviewed 2022-04-10 18:18:18 +08:00
@ -1374,0 +1365,4 @@
let err_msg = ctx.gen_string(generator, "");
let current_fun = ctx.builder.get_insert_block().unwrap().get_parent().unwrap();
let unreachable_block = ctx.ctx.append_basic_block(current_fun, "succ");
let exn_block = ctx.ctx.append_basic_block(current_fun, "fail");
Owner

Names should be more specific. See also #131

Names should be more specific. See also https://git.m-labs.hk/M-Labs/nac3/issues/131
Author
Collaborator

force-pused to update the names of the basic blocks to be unwrap_none_unreachable_bb and unwrap_none_exception_bb.

regarding #131, would it be ok that I look into it after the debug information issue and the field initialization issue?

force-pused to update the names of the basic blocks to be `unwrap_none_unreachable_bb` and `unwrap_none_exception_bb`. regarding #131, would it be ok that I look into it after the debug information issue and the field initialization issue?
Owner

Isn't _bb redundant? I think from the context you can always tell when a name is a basic block, no?

Isn't ``_bb`` redundant? I think from the context you can always tell when a name is a basic block, no?
Author
Collaborator

Oh yes, I think the _bb is redundant as we can easily tell whether a name is a lable to a basic block. I will remove these postfixes now.

Oh yes, I think the `_bb` is redundant as we can easily tell whether a name is a lable to a basic block. I will remove these postfixes now.
Author
Collaborator

updated block names to unwrap_none_unreachable and unwrap_none_exception

updated block names to `unwrap_none_unreachable` and `unwrap_none_exception`
ychenfo force-pushed optimize_unwrap_kernelinvariant from 47b5cb204f to 3223724c72 2022-04-10 20:31:14 +08:00 Compare
Contributor

I guess it would make more sense to name the function get_tuple_element instead of get_tuple_index?

I guess it would make more sense to name the function `get_tuple_element` instead of `get_tuple_index`?
ychenfo force-pushed optimize_unwrap_kernelinvariant from 3223724c72 to c037724a34 2022-04-10 21:57:54 +08:00 Compare
Author
Collaborator

I guess it would make more sense to name the function get_tuple_element instead of get_tuple_index?

force-pushed to update the name to get_tuple_element

> I guess it would make more sense to name the function get_tuple_element instead of get_tuple_index? force-pushed to update the name to `get_tuple_element`
ychenfo force-pushed optimize_unwrap_kernelinvariant from c037724a34 to 722e3df086 2022-04-11 14:59:02 +08:00 Compare
sb10q merged commit 722e3df086 into master 2022-04-12 09:08:34 +08:00
sb10q deleted branch optimize_unwrap_kernelinvariant 2022-04-12 09:08:35 +08:00
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#261
No description provided.