Skip to content

Commit b549c2d

Browse files
committed
use llvm EmitToFile for native object emission
1 parent 66693e8 commit b549c2d

4 files changed

Lines changed: 20 additions & 24 deletions

File tree

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@ require (
2828
)
2929

3030
replace github.com/goplus/llgo/runtime => ./runtime
31+
32+
replace github.com/goplus/llvm => github.com/cpunion/llvm v0.8.9-0.20260429084913-40fdafa22ac4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/cpunion/llvm v0.8.9-0.20260429084913-40fdafa22ac4 h1:R4uLegmI8JT2Dg3BBsHfGtfXVIm0HvGEGuG4RJLqvEU=
2+
github.com/cpunion/llvm v0.8.9-0.20260429084913-40fdafa22ac4/go.mod h1:PeVK8GgzxwAYCiMiUAJb5wJR6xbhj989tu9oulKLLT4=
13
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
24
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
35
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -10,8 +12,6 @@ github.com/goplus/gogen v1.22.0 h1:clM2eMVWQZz7tYUU/u9xwKFCBLcyVbcstzdiZrgRdfA=
1012
github.com/goplus/gogen v1.22.0/go.mod h1:Y7ulYW3wonQ3d9er00b0uGFEV/IUZa6okWJZh892ACQ=
1113
github.com/goplus/lib v0.3.1 h1:Xws4DBVvgOMu58awqB972wtvTacDbk3nqcbHjdx9KSg=
1214
github.com/goplus/lib v0.3.1/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
13-
github.com/goplus/llvm v0.8.8 h1:02GfPE54wVlYtahedzXXFl0JHND8Y+kH6Ng3iHL5BOo=
14-
github.com/goplus/llvm v0.8.8/go.mod h1:PeVK8GgzxwAYCiMiUAJb5wJR6xbhj989tu9oulKLLT4=
1515
github.com/goplus/mod v0.20.2 h1:YX72E6AhhRLvlkVnI9cBK6PZvUwtge2hwROh7w9N6Yk=
1616
github.com/goplus/mod v0.20.2/go.mod h1:lWW62tH7L3Vm42Lr6wlUMYHvsm5w3TkEpE2ulKTgmU8=
1717
github.com/goplus/plan9asm v0.2.1 h1:XI9OoxyBmjhE/R81m3jHnhkZDMdl+t2OHn9iwozcXV8=

internal/build/build.go

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,17 +1438,17 @@ func buildPkg(ctx *context, aPkg *aPackage, verbose bool) error {
14381438
}
14391439

14401440
func exportObject(ctx *context, pkgPath string, exportFile string, pkg llssa.Package) (string, error) {
1441-
if useInMemoryNativeCodegen(ctx) {
1441+
if useNativeLLVMCodegen(ctx) {
14421442
return exportObjectInMemory(ctx, pkgPath, exportFile, pkg)
14431443
}
14441444
return exportObjectWithClang(ctx, pkgPath, exportFile, []byte(pkg.String()))
14451445
}
14461446

1447-
func useInMemoryNativeCodegen(ctx *context) bool {
1448-
return useInMemoryNativeCodegenConf(ctx.buildConf)
1447+
func useNativeLLVMCodegen(ctx *context) bool {
1448+
return useNativeLLVMCodegenConf(ctx.buildConf)
14491449
}
14501450

1451-
func useInMemoryNativeCodegenConf(conf *Config) bool {
1451+
func useNativeLLVMCodegenConf(conf *Config) bool {
14521452
return conf != nil && !conf.GenLL &&
14531453
conf.Target == "" &&
14541454
conf.Goos == runtime.GOOS &&
@@ -1500,28 +1500,22 @@ func exportObjectInMemory(ctx *context, pkgPath string, exportFile string, pkg l
15001500
return "", err
15011501
}
15021502
}
1503-
buf, err := ctx.prog.TargetMachine().EmitToMemoryBuffer(pkg.Module(), gllvm.ObjectFile)
1504-
if err != nil {
1505-
return "", err
1506-
}
1507-
defer buf.Dispose()
15081503

15091504
base := filepath.Base(exportFile)
15101505
objFile, err := os.CreateTemp("", base+"-*.o")
15111506
if err != nil {
15121507
return "", err
15131508
}
15141509
objFileName := objFile.Name()
1515-
if ctx.shouldPrintCommands(false) {
1516-
fmt.Fprintf(os.Stderr, "# compiling %s for pkg: %s\n", objFileName, pkgPath)
1517-
fmt.Fprintln(os.Stderr, "# using in-memory LLVM object emission")
1518-
}
1519-
if _, err := objFile.Write(buf.Bytes()); err != nil {
1520-
objFile.Close()
1510+
if err := objFile.Close(); err != nil {
15211511
os.Remove(objFileName)
15221512
return "", err
15231513
}
1524-
if err := objFile.Close(); err != nil {
1514+
if ctx.shouldPrintCommands(false) {
1515+
fmt.Fprintf(os.Stderr, "# compiling %s for pkg: %s\n", objFileName, pkgPath)
1516+
fmt.Fprintln(os.Stderr, "# using direct LLVM object emission")
1517+
}
1518+
if err := ctx.prog.TargetMachine().EmitToFile(pkg.Module(), objFileName, gllvm.ObjectFile); err != nil {
15251519
os.Remove(objFileName)
15261520
return "", err
15271521
}

internal/build/llvm_emit_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import (
55
"testing"
66
)
77

8-
func TestUseInMemoryNativeCodegenConf(t *testing.T) {
8+
func TestUseNativeLLVMCodegenConf(t *testing.T) {
99
t.Run("native host", func(t *testing.T) {
1010
conf := &Config{Goos: runtime.GOOS, Goarch: runtime.GOARCH}
11-
if !useInMemoryNativeCodegenConf(conf) {
12-
t.Fatal("expected native host build to use in-memory native codegen")
11+
if !useNativeLLVMCodegenConf(conf) {
12+
t.Fatal("expected native host build to use native LLVM codegen")
1313
}
1414
})
1515

1616
t.Run("embedded target", func(t *testing.T) {
1717
conf := &Config{Goos: runtime.GOOS, Goarch: runtime.GOARCH, Target: "rp2040"}
18-
if useInMemoryNativeCodegenConf(conf) {
18+
if useNativeLLVMCodegenConf(conf) {
1919
t.Fatal("expected embedded target build to keep using clang")
2020
}
2121
})
@@ -34,14 +34,14 @@ func TestUseInMemoryNativeCodegenConf(t *testing.T) {
3434
goarch = "amd64"
3535
}
3636
conf := &Config{Goos: goos, Goarch: goarch}
37-
if useInMemoryNativeCodegenConf(conf) {
37+
if useNativeLLVMCodegenConf(conf) {
3838
t.Fatal("expected host mismatch to keep using clang")
3939
}
4040
})
4141

4242
t.Run("wasm", func(t *testing.T) {
4343
conf := &Config{Goos: "wasip1", Goarch: "wasm"}
44-
if useInMemoryNativeCodegenConf(conf) {
44+
if useNativeLLVMCodegenConf(conf) {
4545
t.Fatal("expected wasm target to keep using clang")
4646
}
4747
})

0 commit comments

Comments
 (0)