Merge pull request #84 from xushiwei/q

llgo/ssa: Slice, IndexAddr bugfix
This commit is contained in:
xushiwei
2024-04-30 16:22:01 +08:00
committed by GitHub
6 changed files with 34 additions and 10 deletions

View File

@@ -31,7 +31,7 @@ _llgo_0:
store i64 3, ptr %3, align 4 store i64 3, ptr %3, align 4
%4 = getelementptr inbounds i64, ptr %0, i64 3 %4 = getelementptr inbounds i64, ptr %0, i64 3
store i64 4, ptr %4, align 4 store i64 4, ptr %4, align 4
%5 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(i64 4, i64 4) %5 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr %0, i64 4, i64 4)
%6 = call i64 @main.sum(%"github.com/goplus/llgo/internal/runtime.Slice" %5) %6 = call i64 @main.sum(%"github.com/goplus/llgo/internal/runtime.Slice" %5)
%7 = call i32 (ptr, ...) @printf(ptr @0, i64 %6) %7 = call i32 (ptr, ...) @printf(ptr @0, i64 %6)
ret void ret void
@@ -43,16 +43,17 @@ _llgo_0:
br label %_llgo_1 br label %_llgo_1
_llgo_1: ; preds = %_llgo_2, %_llgo_0 _llgo_1: ; preds = %_llgo_2, %_llgo_0
%2 = phi i64 [ 0, %_llgo_0 ], [ %8, %_llgo_2 ] %2 = phi i64 [ 0, %_llgo_0 ], [ %9, %_llgo_2 ]
%3 = phi i64 [ -1, %_llgo_0 ], [ %4, %_llgo_2 ] %3 = phi i64 [ -1, %_llgo_0 ], [ %4, %_llgo_2 ]
%4 = add i64 %3, 1 %4 = add i64 %3, 1
%5 = icmp slt i64 %4, %1 %5 = icmp slt i64 %4, %1
br i1 %5, label %_llgo_2, label %_llgo_3 br i1 %5, label %_llgo_2, label %_llgo_3
_llgo_2: ; preds = %_llgo_1 _llgo_2: ; preds = %_llgo_1
%6 = getelementptr inbounds i64, %"github.com/goplus/llgo/internal/runtime.Slice" %0, i64 %4 %6 = call ptr @"github.com/goplus/llgo/internal/runtime.SliceData"(%"github.com/goplus/llgo/internal/runtime.Slice" %0)
%7 = load i64, %"github.com/goplus/llgo/internal/runtime.Slice" %6, align 4 %7 = getelementptr inbounds i64, ptr %6, i64 %4
%8 = add i64 %2, %7 %8 = load i64, ptr %7, align 4
%9 = add i64 %2, %8
br label %_llgo_1 br label %_llgo_1
_llgo_3: ; preds = %_llgo_1 _llgo_3: ; preds = %_llgo_1
@@ -66,3 +67,5 @@ declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llg
declare i32 @printf(ptr, ...) declare i32 @printf(ptr, ...)
declare i64 @"github.com/goplus/llgo/internal/runtime.SliceLen"(%"github.com/goplus/llgo/internal/runtime.Slice") declare i64 @"github.com/goplus/llgo/internal/runtime.SliceLen"(%"github.com/goplus/llgo/internal/runtime.Slice")
declare ptr @"github.com/goplus/llgo/internal/runtime.SliceData"(%"github.com/goplus/llgo/internal/runtime.Slice")

View File

@@ -36,7 +36,7 @@ _llgo_0:
store i64 3, ptr %3, align 4 store i64 3, ptr %3, align 4
%4 = getelementptr inbounds i64, ptr %0, i64 3 %4 = getelementptr inbounds i64, ptr %0, i64 3
store i64 4, ptr %4, align 4 store i64 4, ptr %4, align 4
%5 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(i64 4, i64 4) %5 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr %0, i64 4, i64 4)
%6 = call i64 @"github.com/goplus/llgo/internal/runtime.SliceLen"(%"github.com/goplus/llgo/internal/runtime.Slice" %5) %6 = call i64 @"github.com/goplus/llgo/internal/runtime.SliceLen"(%"github.com/goplus/llgo/internal/runtime.Slice" %5)
%7 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) %7 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16)
%8 = getelementptr inbounds i64, ptr %7, i64 0 %8 = getelementptr inbounds i64, ptr %7, i64 0
@@ -47,7 +47,7 @@ _llgo_0:
store i64 3, ptr %10, align 4 store i64 3, ptr %10, align 4
%11 = getelementptr inbounds i64, ptr %7, i64 3 %11 = getelementptr inbounds i64, ptr %7, i64 3
store i64 4, ptr %11, align 4 store i64 4, ptr %11, align 4
%12 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(i64 4, i64 4) %12 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr %7, i64 4, i64 4)
%13 = call i64 @"github.com/goplus/llgo/internal/runtime.SliceLen"(%"github.com/goplus/llgo/internal/runtime.Slice" %12) %13 = call i64 @"github.com/goplus/llgo/internal/runtime.SliceLen"(%"github.com/goplus/llgo/internal/runtime.Slice" %12)
ret void ret void
} }

View File

@@ -140,8 +140,7 @@ func Do(args []string, conf *Config) {
} }
} }
if nErr > 0 { if nErr > 0 {
fmt.Fprintf(os.Stderr, "%d errors occurred\n", nErr) os.Exit(nErr)
os.Exit(1)
} }
} }
} }

View File

@@ -198,6 +198,15 @@ _llgo_0:
ret %"github.com/goplus/llgo/internal/runtime.Slice" %4 ret %"github.com/goplus/llgo/internal/runtime.Slice" %4
} }
define ptr @"github.com/goplus/llgo/internal/runtime.SliceData"(%"github.com/goplus/llgo/internal/runtime.Slice" %0) {
_llgo_0:
%1 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
store %"github.com/goplus/llgo/internal/runtime.Slice" %0, ptr %1, align 8
%2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %1, i32 0, i32 0
%3 = load ptr, ptr %2, align 8
ret ptr %3
}
define i64 @"github.com/goplus/llgo/internal/runtime.SliceLen"(%"github.com/goplus/llgo/internal/runtime.Slice" %0) { define i64 @"github.com/goplus/llgo/internal/runtime.SliceLen"(%"github.com/goplus/llgo/internal/runtime.Slice" %0) {
_llgo_0: _llgo_0:
%1 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8 %1 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8

View File

@@ -44,4 +44,9 @@ func SliceLen(s Slice) int {
return s.len return s.len
} }
// SliceData returns the data pointer of a slice.
func SliceData(s Slice) unsafe.Pointer {
return s.data
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@@ -424,6 +424,14 @@ func (b Builder) IndexAddr(x, idx Expr) Expr {
prog := b.Prog prog := b.Prog
telem := prog.Index(x.Type) telem := prog.Index(x.Type)
pt := prog.Pointer(telem) pt := prog.Pointer(telem)
switch x.t.Underlying().(type) {
case *types.Slice:
pkg := b.fn.pkg
ptr := b.InlineCall(pkg.rtFunc("SliceData"), x)
indices := []llvm.Value{idx.impl}
return Expr{llvm.CreateInBoundsGEP(b.impl, telem.ll, ptr.impl, indices), pt}
}
// case *types.Pointer:
indices := []llvm.Value{idx.impl} indices := []llvm.Value{idx.impl}
return Expr{llvm.CreateInBoundsGEP(b.impl, telem.ll, x.impl, indices), pt} return Expr{llvm.CreateInBoundsGEP(b.impl, telem.ll, x.impl, indices), pt}
} }
@@ -458,7 +466,7 @@ func (b Builder) Slice(x, low, high, max Expr) (ret Expr) {
ret.Type = prog.Type(types.NewSlice(te.Elem())) ret.Type = prog.Type(types.NewSlice(te.Elem()))
if low.IsNil() && high.IsNil() && max.IsNil() { if low.IsNil() && high.IsNil() && max.IsNil() {
n := prog.Val(int(te.Len())) n := prog.Val(int(te.Len()))
ret.impl = b.InlineCall(pkg.rtFunc("NewSlice"), n, n).impl ret.impl = b.InlineCall(pkg.rtFunc("NewSlice"), x, n, n).impl
return ret return ret
} }
} }