diff --git a/cl/compile.go b/cl/compile.go index d3977318..ec18c06f 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -25,7 +25,6 @@ import ( "log" "os" "sort" - "strings" "github.com/goplus/llgo/cl/blocks" "github.com/goplus/llgo/internal/typepatch" @@ -91,20 +90,6 @@ func (p *context) pkgNoInit(pkg *types.Package) bool { return false } -func ignoreName(name string) bool { - /* TODO(xsw): confirm this is not needed more - if name == "unsafe.init" { - return true - } - */ - return strings.HasPrefix(name, "internal/") || strings.HasPrefix(name, "crypto/") || - strings.HasPrefix(name, "arena.") || strings.HasPrefix(name, "maps.") || - strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "syscall.") || - strings.HasPrefix(name, "os.") || strings.HasPrefix(name, "plugin.") || - strings.HasPrefix(name, "reflect.") || strings.HasPrefix(name, "errors.") || - strings.HasPrefix(name, "runtime/") -} - // ----------------------------------------------------------------------------- type instrOrValue interface { diff --git a/cl/import.go b/cl/import.go index 21979310..fbbf4408 100644 --- a/cl/import.go +++ b/cl/import.go @@ -490,6 +490,20 @@ func replaceGoName(v string, pos int) string { return v } +func ignoreName(name string) bool { + /* TODO(xsw): confirm this is not needed more + if name == "unsafe.init" { + return true + } + */ + return strings.HasPrefix(name, "internal/") || strings.HasPrefix(name, "crypto/") || + strings.HasPrefix(name, "arena.") || strings.HasPrefix(name, "maps.") || + strings.HasPrefix(name, "time.") || strings.HasPrefix(name, "syscall.") || + strings.HasPrefix(name, "os.") || strings.HasPrefix(name, "plugin.") || + strings.HasPrefix(name, "reflect.") || strings.HasPrefix(name, "errors.") || + strings.HasPrefix(name, "runtime/") +} + // ----------------------------------------------------------------------------- const ( diff --git a/cmd/internal/base/base.go b/cmd/internal/base/base.go index 440cdf4a..e23061cf 100644 --- a/cmd/internal/base/base.go +++ b/cmd/internal/base/base.go @@ -52,7 +52,7 @@ type Command struct { // Llgo command var Llgo = &Command{ UsageLine: "llgo", - Short: `llgo is a Go compiler based on LLVM in order to better integrate Go with the C ecosystem.`, + Short: `llgo is a Go compiler based on LLVM in order to better integrate Go with the C ecosystem including Python.`, // Commands initialized in package main } diff --git a/internal/build/build.go b/internal/build/build.go index 247546c1..d115f7c6 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -19,6 +19,7 @@ package build import ( "archive/zip" "fmt" + "go/constant" "go/token" "go/types" "io" @@ -147,7 +148,7 @@ func Do(args []string, conf *Config) { progSSA := ssa.NewProgram(initial[0].Fset, ssaBuildMode) patches := make(cl.Patches, len(altPkgPaths)) - altSSAPkgs(progSSA, patches, altPkgs[1:]) + altSSAPkgs(progSSA, patches, altPkgs[1:], verbose) ctx := &context{progSSA, prog, dedup, patches, make(map[string]none), mode, verbose} pkgs := buildAllPkgs(ctx, initial) @@ -234,9 +235,8 @@ func buildAllPkgs(ctx *context, initial []*packages.Package) (pkgs []*aPackage) // and set no export file pkg.ExportFile = "" case cl.PkgLinkIR, cl.PkgLinkExtern, cl.PkgPyModule: - pkgPath := pkg.PkgPath - if isPkgInLLGo(pkgPath) { - pkg.ExportFile = concatPkgLinkFiles(pkgPath) + if isPkgInLLGo(pkg.PkgPath) { + pkg.ExportFile = concatPkgLinkFiles(pkg, ctx.verbose) } else { // panic("todo") // TODO(xsw): support packages out of llgo @@ -292,18 +292,18 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, runtimeFiles []string, if app == "" { app = filepath.Join(conf.BinPath, name+conf.AppExt) } - const N = 6 + const N = 5 args := make([]string, N, len(pkg.Imports)+len(runtimeFiles)+(N+1)) args[0] = "-o" args[1] = app args[2] = "-Wno-override-module" - args[3] = "-fuse-ld=lld" - args[4] = "-Xlinker" + args[3] = "-Xlinker" if runtime.GOOS == "darwin" { // ld64.lld (macOS) - args[5] = "-dead_strip" + args[4] = "-dead_strip" } else { // ld.lld (Unix), lld-link (Windows), wasm-ld (WebAssembly) - args[5] = "--gc-sections" + args[4] = "--gc-sections" } + //args[5] = "-fuse-ld=lld" // TODO(xsw): to check lld exists or not //args[6] = "-O2" needRuntime := false needPyInit := false @@ -399,25 +399,6 @@ func buildPkg(ctx *context, aPkg *aPackage) { aPkg.LPkg = ret } -func canSkipToBuild(pkgPath string) bool { - switch pkgPath { - case "unsafe", "errors", "runtime", "sync": // TODO(xsw): remove it - return true - default: - return strings.HasPrefix(pkgPath, "internal/") || - strings.HasPrefix(pkgPath, "runtime/internal/") - } -} - -type none struct{} - -var hasAltPkg = map[string]none{ - "math": {}, - "sync": {}, - "sync/atomic": {}, - "runtime": {}, -} - const ( altPkgPathPrefix = "github.com/goplus/llgo/internal/lib/" ) @@ -433,14 +414,14 @@ func altPkgs(initial []*packages.Package, alts ...string) []string { return alts } -func altSSAPkgs(prog *ssa.Program, patches cl.Patches, alts []*packages.Package) { +func altSSAPkgs(prog *ssa.Program, patches cl.Patches, alts []*packages.Package, verbose bool) { packages.Visit(alts, nil, func(p *packages.Package) { if p.Types != nil && !p.IllTyped { pkgSSA := prog.CreatePackage(p.Types, p.Syntax, p.TypesInfo, true) if strings.HasPrefix(p.PkgPath, altPkgPathPrefix) { path := p.PkgPath[len(altPkgPathPrefix):] patches[path] = pkgSSA - if debugBuild { + if debugBuild || verbose { log.Println("==> Patching", path) } } @@ -462,13 +443,16 @@ func allPkgs(ctx *context, initial []*packages.Package) (all []*aPackage, errs [ built := ctx.built packages.Visit(initial, nil, func(p *packages.Package) { if p.Types != nil && !p.IllTyped { - if _, ok := built[p.PkgPath]; ok { + pkgPath := p.PkgPath + if _, ok := built[pkgPath]; ok { return } var altPkg *packages.Cached var ssaPkg = createSSAPkg(prog, p, verbose) - if _, ok := hasAltPkg[p.PkgPath]; ok { - altPkg = ctx.dedup.Check(altPkgPathPrefix + p.PkgPath) + if _, ok := hasAltPkg[pkgPath]; ok { + if altPkg = ctx.dedup.Check(altPkgPathPrefix + pkgPath); altPkg == nil { + return + } } all = append(all, &aPackage{p, ssaPkg, altPkg, nil}) } else { @@ -579,11 +563,11 @@ func isSingleLinkFile(ret string) bool { return len(ret) > 0 && ret[0] != ' ' } -func concatPkgLinkFiles(pkgPath string) string { +func concatPkgLinkFiles(pkg *packages.Package, verbose bool) string { var b strings.Builder var ret string var n int - llgoPkgLinkFiles(pkgPath, "", func(linkFile string) { + llgoPkgLinkFiles(pkg, "", func(linkFile string) { if n == 0 { ret = linkFile } else { @@ -591,7 +575,7 @@ func concatPkgLinkFiles(pkgPath string) string { b.WriteString(linkFile) } n++ - }) + }, verbose) if n > 1 { b.WriteByte(' ') b.WriteString(ret) @@ -600,7 +584,39 @@ func concatPkgLinkFiles(pkgPath string) string { return ret } -func llgoPkgLinkFiles(pkgPath string, llFile string, procFile func(linkFile string)) { +// const LLGoFiles = "file1; file2; ..." +func llgoPkgLinkFiles(pkg *packages.Package, llFile string, procFile func(linkFile string), verbose bool) { + if o := pkg.Types.Scope().Lookup("LLGoFiles"); o != nil { + val := o.(*types.Const).Val() + if val.Kind() == constant.String { + clFiles(constant.StringVal(val), pkg, procFile, verbose) + } + } + unzipPkgLinkFiles(pkg.PkgPath, llFile, procFile) +} + +// files = "file1; file2; ..." +func clFiles(files string, pkg *packages.Package, procFile func(linkFile string), verbose bool) { + dir := filepath.Dir(pkg.CompiledGoFiles[0]) + expFile := pkg.ExportFile + for _, file := range strings.Split(files, ";") { + cFile := filepath.Join(dir, strings.TrimSpace(file)) + clFile(cFile, expFile, procFile, verbose) + } +} + +func clFile(cFile, expFile string, procFile func(linkFile string), verbose bool) { + llFile := expFile + filepath.Base(cFile) + ".ll" + args := []string{"-emit-llvm", "-S", "-o", llFile, "-c", cFile} + if verbose { + fmt.Fprintln(os.Stderr, "clang", args) + } + err := clang.New("").Exec(args...) + check(err) + procFile(llFile) +} + +func unzipPkgLinkFiles(pkgPath string, llFile string, procFile func(linkFile string)) { dir := llgoRoot() + pkgPath[len(llgoModPath):] + "/" if llFile == "" { llFile = "llgo_autogen.ll" @@ -685,6 +701,25 @@ func decodeFile(outFile string, zipf *zip.File) (err error) { return } +func canSkipToBuild(pkgPath string) bool { + switch pkgPath { + case "unsafe", "errors": // TODO(xsw): remove it + return true + default: + return strings.HasPrefix(pkgPath, "internal/") || + strings.HasPrefix(pkgPath, "runtime/internal/") + } +} + +type none struct{} + +var hasAltPkg = map[string]none{ + "math": {}, + "sync": {}, + "sync/atomic": {}, + "runtime": {}, +} + func check(err error) { if err != nil { panic(err) diff --git a/py/llgo.cfg b/py/llgo.cfg deleted file mode 100644 index dcc7c16d..00000000 --- a/py/llgo.cfg +++ /dev/null @@ -1,7 +0,0 @@ -{ - "cl": [ - "clang -emit-llvm -S -o module.ll -c _pyg/module.c", - "llgen .", - "rm llgo_autogen.lla; zip llgo_autogen.lla llgo_autogen.ll module.ll", - ] -} diff --git a/py/llgo_autogen.lla b/py/llgo_autogen.lla index 36aace83..0bbf8a00 100644 Binary files a/py/llgo_autogen.lla and b/py/llgo_autogen.lla differ diff --git a/py/module.ll b/py/module.ll deleted file mode 100644 index d11c7450..00000000 --- a/py/module.ll +++ /dev/null @@ -1,82 +0,0 @@ -; ModuleID = '_pyg/module.c' -source_filename = "_pyg/module.c" - -%struct.PyObject = type opaque - -; Function Attrs: noinline nounwind optnone ssp uwtable(sync) -define void @llgoLoadPyModSyms(%struct.PyObject* noundef %0, ...) #0 { - %2 = alloca %struct.PyObject*, align 8 - %3 = alloca i8*, align 8 - %4 = alloca i8*, align 8 - %5 = alloca i8*, align 8 - %6 = alloca %struct.PyObject**, align 8 - %7 = alloca %struct.PyObject**, align 8 - store %struct.PyObject* %0, %struct.PyObject** %2, align 8 - %8 = bitcast i8** %3 to i8* - call void @llvm.va_start(i8* %8) - br label %9 - -9: ; preds = %26, %1 - %10 = va_arg i8** %3, i8* - store i8* %10, i8** %5, align 8 - %11 = load i8*, i8** %5, align 8 - store i8* %11, i8** %4, align 8 - %12 = load i8*, i8** %4, align 8 - %13 = icmp eq i8* %12, null - br i1 %13, label %14, label %15 - -14: ; preds = %9 - br label %27 - -15: ; preds = %9 - %16 = va_arg i8** %3, %struct.PyObject** - store %struct.PyObject** %16, %struct.PyObject*** %7, align 8 - %17 = load %struct.PyObject**, %struct.PyObject*** %7, align 8 - store %struct.PyObject** %17, %struct.PyObject*** %6, align 8 - %18 = load %struct.PyObject**, %struct.PyObject*** %6, align 8 - %19 = load %struct.PyObject*, %struct.PyObject** %18, align 8 - %20 = icmp eq %struct.PyObject* %19, null - br i1 %20, label %21, label %26 - -21: ; preds = %15 - %22 = load %struct.PyObject*, %struct.PyObject** %2, align 8 - %23 = load i8*, i8** %4, align 8 - %24 = call %struct.PyObject* @PyObject_GetAttrString(%struct.PyObject* noundef %22, i8* noundef %23) - %25 = load %struct.PyObject**, %struct.PyObject*** %6, align 8 - store %struct.PyObject* %24, %struct.PyObject** %25, align 8 - br label %26 - -26: ; preds = %21, %15 - br label %9 - -27: ; preds = %14 - %28 = bitcast i8** %3 to i8* - call void @llvm.va_end(i8* %28) - ret void -} - -; Function Attrs: nocallback nofree nosync nounwind willreturn -declare void @llvm.va_start(i8*) #1 - -declare %struct.PyObject* @PyObject_GetAttrString(%struct.PyObject* noundef, i8* noundef) #2 - -; Function Attrs: nocallback nofree nosync nounwind willreturn -declare void @llvm.va_end(i8*) #1 - -attributes #0 = { noinline nounwind optnone ssp uwtable(sync) "frame-pointer"="non-leaf" "min-legal-vector-width"="0" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.5a,+zcm,+zcz" } -attributes #1 = { nocallback nofree nosync nounwind willreturn } -attributes #2 = { "frame-pointer"="non-leaf" "no-trapping-math"="true" "probe-stack"="__chkstk_darwin" "stack-protector-buffer-size"="8" "target-cpu"="apple-m1" "target-features"="+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.5a,+zcm,+zcz" } - -!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7, !8} -!llvm.ident = !{!9} - -!0 = !{i32 2, !"SDK Version", [2 x i32] [i32 13, i32 3]} -!1 = !{i32 1, !"wchar_size", i32 4} -!2 = !{i32 8, !"branch-target-enforcement", i32 0} -!3 = !{i32 8, !"sign-return-address", i32 0} -!4 = !{i32 8, !"sign-return-address-all", i32 0} -!5 = !{i32 8, !"sign-return-address-with-bkey", i32 0} -!6 = !{i32 7, !"PIC Level", i32 2} -!7 = !{i32 7, !"uwtable", i32 1} -!8 = !{i32 7, !"frame-pointer", i32 1} -!9 = !{!"Apple clang version 14.0.3 (clang-1403.0.22.14.1)"} diff --git a/py/python.go b/py/python.go index 78330505..0d20a685 100644 --- a/py/python.go +++ b/py/python.go @@ -23,6 +23,7 @@ import ( ) const ( + LLGoFiles = "_pyg/module.c" LLGoPackage = "link: $LLGO_LIB_PYTHON; $(pkg-config --libs python3-embed)" )