Skip to content

Commit

Permalink
Merge pull request #875 from cpunion/fix-alloc-in-loop
Browse files Browse the repository at this point in the history
Reduce alloca
  • Loading branch information
xushiwei authored Nov 25, 2024
2 parents e042aad + 43c1bc8 commit ed83522
Show file tree
Hide file tree
Showing 74 changed files with 9,698 additions and 21,833 deletions.
1,375 changes: 487 additions & 888 deletions cl/_testdata/print/out.ll

Large diffs are not rendered by default.

40 changes: 13 additions & 27 deletions cl/_testdata/utf8/out.ll
Original file line number Diff line number Diff line change
Expand Up @@ -52,41 +52,27 @@ _llgo_0:
br label %_llgo_3

_llgo_1: ; preds = %_llgo_3
%2 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 0
store ptr @0, ptr %3, align 8
%4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %2, i32 0, i32 1
store i64 7, ptr %4, align 4
%5 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %2, align 8
%6 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %5, 1
%7 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.StringSlice"(%"github.com/goplus/llgo/internal/runtime.String" %5, i64 %15, i64 %6)
%8 = call { i32, i64 } @"unicode/utf8.DecodeRuneInString"(%"github.com/goplus/llgo/internal/runtime.String" %7)
%9 = extractvalue { i32, i64 } %8, 0
%10 = extractvalue { i32, i64 } %8, 1
%11 = add i64 %15, %10
%12 = sext i32 %9 to i64
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %12)
%2 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.StringSlice"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 7 }, i64 %10, i64 7)
%3 = call { i32, i64 } @"unicode/utf8.DecodeRuneInString"(%"github.com/goplus/llgo/internal/runtime.String" %2)
%4 = extractvalue { i32, i64 } %3, 0
%5 = extractvalue { i32, i64 } %3, 1
%6 = add i64 %10, %5
%7 = sext i32 %4 to i64
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %7)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
br label %_llgo_3

_llgo_2: ; preds = %_llgo_3
%13 = call i8 @main.index(i8 2)
%14 = icmp eq i8 %13, 3
call void @"github.com/goplus/llgo/internal/runtime.PrintBool"(i1 %14)
%8 = call i8 @main.index(i8 2)
%9 = icmp eq i8 %8, 3
call void @"github.com/goplus/llgo/internal/runtime.PrintBool"(i1 %9)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret i32 0

_llgo_3: ; preds = %_llgo_1, %_llgo_0
%15 = phi i64 [ 0, %_llgo_0 ], [ %11, %_llgo_1 ]
%16 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%17 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 0
store ptr @0, ptr %17, align 8
%18 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %16, i32 0, i32 1
store i64 7, ptr %18, align 4
%19 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %16, align 8
%20 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %19, 1
%21 = icmp slt i64 %15, %20
br i1 %21, label %_llgo_1, label %_llgo_2
%10 = phi i64 [ 0, %_llgo_0 ], [ %6, %_llgo_1 ]
%11 = icmp slt i64 %10, 7
br i1 %11, label %_llgo_1, label %_llgo_2
}

declare void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1)
Expand Down
73 changes: 23 additions & 50 deletions cl/_testdata/vargs/out.ll
Original file line number Diff line number Diff line change
Expand Up @@ -36,40 +36,23 @@ _llgo_0:
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 48)
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %2, i64 0
%4 = load ptr, ptr @_llgo_int, align 8
%5 = alloca %"github.com/goplus/llgo/internal/runtime.eface", align 8
%6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %5, i32 0, i32 0
store ptr %4, ptr %6, align 8
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %5, i32 0, i32 1
store ptr inttoptr (i64 1 to ptr), ptr %7, align 8
%8 = load %"github.com/goplus/llgo/internal/runtime.eface", ptr %5, align 8
store %"github.com/goplus/llgo/internal/runtime.eface" %8, ptr %3, align 8
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %2, i64 1
%10 = load ptr, ptr @_llgo_int, align 8
%11 = alloca %"github.com/goplus/llgo/internal/runtime.eface", align 8
%12 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %11, i32 0, i32 0
store ptr %10, ptr %12, align 8
%13 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %11, i32 0, i32 1
store ptr inttoptr (i64 2 to ptr), ptr %13, align 8
%14 = load %"github.com/goplus/llgo/internal/runtime.eface", ptr %11, align 8
store %"github.com/goplus/llgo/internal/runtime.eface" %14, ptr %9, align 8
%15 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %2, i64 2
%16 = load ptr, ptr @_llgo_int, align 8
%17 = alloca %"github.com/goplus/llgo/internal/runtime.eface", align 8
%18 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %17, i32 0, i32 0
store ptr %16, ptr %18, align 8
%19 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %17, i32 0, i32 1
store ptr inttoptr (i64 3 to ptr), ptr %19, align 8
%20 = load %"github.com/goplus/llgo/internal/runtime.eface", ptr %17, align 8
store %"github.com/goplus/llgo/internal/runtime.eface" %20, ptr %15, align 8
%21 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
%22 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %21, i32 0, i32 0
store ptr %2, ptr %22, align 8
%23 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %21, i32 0, i32 1
store i64 3, ptr %23, align 4
%24 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %21, i32 0, i32 2
store i64 3, ptr %24, align 4
%25 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %21, align 8
call void @main.test(%"github.com/goplus/llgo/internal/runtime.Slice" %25)
%5 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %4, 0
%6 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %5, ptr inttoptr (i64 1 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %6, ptr %3, align 8
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %2, i64 1
%8 = load ptr, ptr @_llgo_int, align 8
%9 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %8, 0
%10 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %9, ptr inttoptr (i64 2 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %10, ptr %7, align 8
%11 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %2, i64 2
%12 = load ptr, ptr @_llgo_int, align 8
%13 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %12, 0
%14 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %13, ptr inttoptr (i64 3 to ptr), 1
store %"github.com/goplus/llgo/internal/runtime.eface" %14, ptr %11, align 8
%15 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" undef, ptr %2, 0
%16 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %15, i64 3, 1
%17 = insertvalue %"github.com/goplus/llgo/internal/runtime.Slice" %16, i64 3, 2
call void @main.test(%"github.com/goplus/llgo/internal/runtime.Slice" %17)
ret i32 0
}

Expand Down Expand Up @@ -108,22 +91,12 @@ _llgo_4: ; preds = %_llgo_2
br label %_llgo_1

_llgo_5: ; preds = %_llgo_2
%18 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%19 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %18, i32 0, i32 0
store ptr @1, ptr %19, align 8
%20 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %18, i32 0, i32 1
store i64 21, ptr %20, align 4
%21 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %18, align 8
%22 = load ptr, ptr @_llgo_string, align 8
%23 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 16)
store %"github.com/goplus/llgo/internal/runtime.String" %21, ptr %23, align 8
%24 = alloca %"github.com/goplus/llgo/internal/runtime.eface", align 8
%25 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %24, i32 0, i32 0
store ptr %22, ptr %25, align 8
%26 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.eface", ptr %24, i32 0, i32 1
store ptr %23, ptr %26, align 8
%27 = load %"github.com/goplus/llgo/internal/runtime.eface", ptr %24, align 8
call void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface" %27)
%18 = load ptr, ptr @_llgo_string, align 8
%19 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 16)
store %"github.com/goplus/llgo/internal/runtime.String" { ptr @1, i64 21 }, ptr %19, align 8
%20 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" undef, ptr %18, 0
%21 = insertvalue %"github.com/goplus/llgo/internal/runtime.eface" %20, ptr %19, 1
call void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplus/llgo/internal/runtime.eface" %21)
unreachable
}

Expand Down
17 changes: 17 additions & 0 deletions cl/_testgo/allocinloop/in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

func Foo(s string) int {
return len(s)
}

func Test() {
j := 0
for i := 0; i < 10000000; i++ {
j += Foo("hello")
}
println(j)
}

func main() {
Test()
}
66 changes: 66 additions & 0 deletions cl/_testgo/allocinloop/out.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
; ModuleID = 'main'
source_filename = "main"

%"github.com/goplus/llgo/internal/runtime.String" = type { ptr, i64 }

@"main.init$guard" = global i1 false, align 1
@0 = private unnamed_addr constant [5 x i8] c"hello", align 1
@__llgo_argc = global i32 0, align 4
@__llgo_argv = global ptr null, align 8

define i64 @main.Foo(%"github.com/goplus/llgo/internal/runtime.String" %0) {
_llgo_0:
%1 = extractvalue %"github.com/goplus/llgo/internal/runtime.String" %0, 1
ret i64 %1
}

define void @main.Test() {
_llgo_0:
br label %_llgo_3

_llgo_1: ; preds = %_llgo_3
%0 = call i64 @main.Foo(%"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 5 })
%1 = add i64 %3, %0
%2 = add i64 %4, 1
br label %_llgo_3

_llgo_2: ; preds = %_llgo_3
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %3)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
ret void

_llgo_3: ; preds = %_llgo_1, %_llgo_0
%3 = phi i64 [ 0, %_llgo_0 ], [ %1, %_llgo_1 ]
%4 = phi i64 [ 0, %_llgo_0 ], [ %2, %_llgo_1 ]
%5 = icmp slt i64 %4, 10000000
br i1 %5, label %_llgo_1, label %_llgo_2
}

define void @main.init() {
_llgo_0:
%0 = load i1, ptr @"main.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1

_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"main.init$guard", align 1
br label %_llgo_2

_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}

define i32 @main(i32 %0, ptr %1) {
_llgo_0:
store i32 %0, ptr @__llgo_argc, align 4
store ptr %1, ptr @__llgo_argv, align 8
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
call void @main.Test()
ret i32 0
}

declare void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64)

declare void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8)

declare void @"github.com/goplus/llgo/internal/runtime.init"()
59 changes: 14 additions & 45 deletions cl/_testgo/closure/out.ll
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,24 @@ _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init()
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 16)
%3 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %3, i32 0, i32 0
store ptr @0, ptr %4, align 8
%5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %3, i32 0, i32 1
store i64 3, ptr %5, align 4
%6 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %3, align 8
store %"github.com/goplus/llgo/internal/runtime.String" %6, ptr %2, align 8
%7 = alloca %main.T, align 8
%8 = getelementptr inbounds %main.T, ptr %7, i32 0, i32 0
store ptr @"__llgo_stub.main.main$1", ptr %8, align 8
%9 = getelementptr inbounds %main.T, ptr %7, i32 0, i32 1
store ptr null, ptr %9, align 8
%10 = load %main.T, ptr %7, align 8
%11 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8)
%12 = getelementptr inbounds { ptr }, ptr %11, i32 0, i32 0
store ptr %2, ptr %12, align 8
%13 = alloca { ptr, ptr }, align 8
%14 = getelementptr inbounds { ptr, ptr }, ptr %13, i32 0, i32 0
store ptr @"main.main$2", ptr %14, align 8
%15 = getelementptr inbounds { ptr, ptr }, ptr %13, i32 0, i32 1
store ptr %11, ptr %15, align 8
%16 = load { ptr, ptr }, ptr %13, align 8
%17 = alloca %main.T, align 8
store { ptr, ptr } %16, ptr %17, align 8
%18 = load %main.T, ptr %17, align 8
%19 = extractvalue %main.T %10, 1
%20 = extractvalue %main.T %10, 0
call void %20(ptr %19, i64 100)
%21 = extractvalue %main.T %18, 1
%22 = extractvalue %main.T %18, 0
call void %22(ptr %21, i64 200)
store %"github.com/goplus/llgo/internal/runtime.String" { ptr @0, i64 3 }, ptr %2, align 8
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64 8)
%4 = getelementptr inbounds { ptr }, ptr %3, i32 0, i32 0
store ptr %2, ptr %4, align 8
%5 = insertvalue { ptr, ptr } { ptr @"main.main$2", ptr undef }, ptr %3, 1
%6 = alloca %main.T, align 8
store { ptr, ptr } %5, ptr %6, align 8
%7 = load %main.T, ptr %6, align 8
call void @"__llgo_stub.main.main$1"(ptr null, i64 100)
%8 = extractvalue %main.T %7, 1
%9 = extractvalue %main.T %7, 0
call void %9(ptr %8, i64 200)
ret i32 0
}

define void @"main.main$1"(i64 %0) {
_llgo_0:
%1 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %1, i32 0, i32 0
store ptr @1, ptr %2, align 8
%3 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %1, i32 0, i32 1
store i64 4, ptr %3, align 4
%4 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %1, align 8
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %4)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @1, i64 4 })
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %0)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 10)
Expand All @@ -85,13 +60,7 @@ _llgo_0:
%2 = load { ptr }, ptr %0, align 8
%3 = extractvalue { ptr } %2, 0
%4 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %3, align 8
%5 = alloca %"github.com/goplus/llgo/internal/runtime.String", align 8
%6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %5, i32 0, i32 0
store ptr @2, ptr %6, align 8
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.String", ptr %5, i32 0, i32 1
store i64 7, ptr %7, align 4
%8 = load %"github.com/goplus/llgo/internal/runtime.String", ptr %5, align 8
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %8)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" { ptr @2, i64 7 })
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 %1)
call void @"github.com/goplus/llgo/internal/runtime.PrintByte"(i8 32)
Expand Down
Loading

0 comments on commit ed83522

Please sign in to comment.