Skip to content

Commit 4881747

Browse files
aykevldeadprogram
authored andcommitted
builder: remove non-ThinLTO build mode
All targets now support ThinLTO so let's remove the old unused code.
1 parent 201592d commit 4881747

File tree

7 files changed

+40
-127
lines changed

7 files changed

+40
-127
lines changed

builder/build.go

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,7 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
594594
defer llvmBuf.Dispose()
595595
return result, os.WriteFile(outpath, llvmBuf.Bytes(), 0666)
596596
case ".bc":
597-
var buf llvm.MemoryBuffer
598-
if config.UseThinLTO() {
599-
buf = llvm.WriteThinLTOBitcodeToMemoryBuffer(mod)
600-
} else {
601-
buf = llvm.WriteBitcodeToMemoryBuffer(mod)
602-
}
597+
buf := llvm.WriteThinLTOBitcodeToMemoryBuffer(mod)
603598
defer buf.Dispose()
604599
return result, os.WriteFile(outpath, buf.Bytes(), 0666)
605600
case ".ll":
@@ -621,16 +616,7 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
621616
dependencies: []*compileJob{programJob},
622617
result: objfile,
623618
run: func(*compileJob) error {
624-
var llvmBuf llvm.MemoryBuffer
625-
if config.UseThinLTO() {
626-
llvmBuf = llvm.WriteThinLTOBitcodeToMemoryBuffer(mod)
627-
} else {
628-
var err error
629-
llvmBuf, err = machine.EmitToMemoryBuffer(mod, llvm.ObjectFile)
630-
if err != nil {
631-
return err
632-
}
633-
}
619+
llvmBuf := llvm.WriteThinLTOBitcodeToMemoryBuffer(mod)
634620
defer llvmBuf.Dispose()
635621
return os.WriteFile(objfile, llvmBuf.Bytes(), 0666)
636622
},
@@ -664,7 +650,7 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
664650
job := &compileJob{
665651
description: "compile extra file " + path,
666652
run: func(job *compileJob) error {
667-
result, err := compileAndCacheCFile(abspath, tmpdir, config.CFlags(), config.UseThinLTO(), config.Options.PrintCommands)
653+
result, err := compileAndCacheCFile(abspath, tmpdir, config.CFlags(), config.Options.PrintCommands)
668654
job.result = result
669655
return err
670656
},
@@ -682,7 +668,7 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
682668
job := &compileJob{
683669
description: "compile CGo file " + abspath,
684670
run: func(job *compileJob) error {
685-
result, err := compileAndCacheCFile(abspath, tmpdir, pkg.CFlags, config.UseThinLTO(), config.Options.PrintCommands)
671+
result, err := compileAndCacheCFile(abspath, tmpdir, pkg.CFlags, config.Options.PrintCommands)
686672
job.result = result
687673
return err
688674
},
@@ -741,36 +727,34 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
741727
}
742728
ldflags = append(ldflags, dependency.result)
743729
}
744-
if config.UseThinLTO() {
745-
ldflags = append(ldflags, "-mllvm", "-mcpu="+config.CPU())
746-
if config.GOOS() == "windows" {
747-
// Options for the MinGW wrapper for the lld COFF linker.
748-
ldflags = append(ldflags,
749-
"-Xlink=/opt:lldlto="+strconv.Itoa(optLevel),
750-
"--thinlto-cache-dir="+filepath.Join(cacheDir, "thinlto"))
751-
} else if config.GOOS() == "darwin" {
752-
// Options for the ld64-compatible lld linker.
753-
ldflags = append(ldflags,
754-
"--lto-O"+strconv.Itoa(optLevel),
755-
"-cache_path_lto", filepath.Join(cacheDir, "thinlto"))
756-
} else {
757-
// Options for the ELF linker.
758-
ldflags = append(ldflags,
759-
"--lto-O"+strconv.Itoa(optLevel),
760-
"--thinlto-cache-dir="+filepath.Join(cacheDir, "thinlto"),
761-
)
762-
}
763-
if config.CodeModel() != "default" {
764-
ldflags = append(ldflags,
765-
"-mllvm", "-code-model="+config.CodeModel())
766-
}
767-
if sizeLevel >= 2 {
768-
// Workaround with roughly the same effect as
769-
// https://reviews.llvm.org/D119342.
770-
// Can hopefully be removed in LLVM 15.
771-
ldflags = append(ldflags,
772-
"-mllvm", "--rotation-max-header-size=0")
773-
}
730+
ldflags = append(ldflags, "-mllvm", "-mcpu="+config.CPU())
731+
if config.GOOS() == "windows" {
732+
// Options for the MinGW wrapper for the lld COFF linker.
733+
ldflags = append(ldflags,
734+
"-Xlink=/opt:lldlto="+strconv.Itoa(optLevel),
735+
"--thinlto-cache-dir="+filepath.Join(cacheDir, "thinlto"))
736+
} else if config.GOOS() == "darwin" {
737+
// Options for the ld64-compatible lld linker.
738+
ldflags = append(ldflags,
739+
"--lto-O"+strconv.Itoa(optLevel),
740+
"-cache_path_lto", filepath.Join(cacheDir, "thinlto"))
741+
} else {
742+
// Options for the ELF linker.
743+
ldflags = append(ldflags,
744+
"--lto-O"+strconv.Itoa(optLevel),
745+
"--thinlto-cache-dir="+filepath.Join(cacheDir, "thinlto"),
746+
)
747+
}
748+
if config.CodeModel() != "default" {
749+
ldflags = append(ldflags,
750+
"-mllvm", "-code-model="+config.CodeModel())
751+
}
752+
if sizeLevel >= 2 {
753+
// Workaround with roughly the same effect as
754+
// https://reviews.llvm.org/D119342.
755+
// Can hopefully be removed in LLVM 15.
756+
ldflags = append(ldflags,
757+
"-mllvm", "--rotation-max-header-size=0")
774758
}
775759
if config.Options.PrintCommands != nil {
776760
config.Options.PrintCommands(config.Target.Linker, ldflags...)
@@ -1069,10 +1053,6 @@ func optimizeProgram(mod llvm.Module, config *compileopts.Config) error {
10691053
}
10701054
}
10711055

1072-
if config.GOOS() != "darwin" && !config.UseThinLTO() {
1073-
transform.ApplyFunctionSections(mod) // -ffunction-sections
1074-
}
1075-
10761056
// Insert values from -ldflags="-X ..." into the IR.
10771057
err = setGlobalValues(mod, config.Options.GlobalValues)
10781058
if err != nil {

builder/cc.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import (
5656
// depfile but without invalidating its name. For this reason, the depfile is
5757
// written on each new compilation (even when it seems unnecessary). However, it
5858
// could in rare cases lead to a stale file fetched from the cache.
59-
func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool, printCommands func(string, ...string)) (string, error) {
59+
func compileAndCacheCFile(abspath, tmpdir string, cflags []string, printCommands func(string, ...string)) (string, error) {
6060
// Hash input file.
6161
fileHash, err := hashFile(abspath)
6262
if err != nil {
@@ -67,11 +67,6 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
6767
unlock := lock(filepath.Join(goenv.Get("GOCACHE"), fileHash+".c.lock"))
6868
defer unlock()
6969

70-
ext := ".o"
71-
if thinlto {
72-
ext = ".bc"
73-
}
74-
7570
// Create cache key for the dependencies file.
7671
buf, err := json.Marshal(struct {
7772
Path string
@@ -104,7 +99,7 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
10499
}
105100

106101
// Obtain hashes of all the files listed as a dependency.
107-
outpath, err := makeCFileCachePath(dependencies, depfileNameHash, ext)
102+
outpath, err := makeCFileCachePath(dependencies, depfileNameHash)
108103
if err == nil {
109104
if _, err := os.Stat(outpath); err == nil {
110105
return outpath, nil
@@ -117,7 +112,7 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
117112
return "", err
118113
}
119114

120-
objTmpFile, err := os.CreateTemp(goenv.Get("GOCACHE"), "tmp-*"+ext)
115+
objTmpFile, err := os.CreateTemp(goenv.Get("GOCACHE"), "tmp-*.bc")
121116
if err != nil {
122117
return "", err
123118
}
@@ -127,11 +122,8 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
127122
return "", err
128123
}
129124
depTmpFile.Close()
130-
flags := append([]string{}, cflags...) // copy cflags
131-
flags = append(flags, "-MD", "-MV", "-MTdeps", "-MF", depTmpFile.Name()) // autogenerate dependencies
132-
if thinlto {
133-
flags = append(flags, "-flto=thin")
134-
}
125+
flags := append([]string{}, cflags...) // copy cflags
126+
flags = append(flags, "-MD", "-MV", "-MTdeps", "-MF", depTmpFile.Name(), "-flto=thin") // autogenerate dependencies
135127
flags = append(flags, "-c", "-o", objTmpFile.Name(), abspath)
136128
if strings.ToLower(filepath.Ext(abspath)) == ".s" {
137129
// If this is an assembly file (.s or .S, lowercase or uppercase), then
@@ -189,7 +181,7 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
189181
}
190182

191183
// Move temporary object file to final location.
192-
outpath, err := makeCFileCachePath(dependencySlice, depfileNameHash, ext)
184+
outpath, err := makeCFileCachePath(dependencySlice, depfileNameHash)
193185
if err != nil {
194186
return "", err
195187
}
@@ -204,7 +196,7 @@ func compileAndCacheCFile(abspath, tmpdir string, cflags []string, thinlto bool,
204196
// Create a cache path (a path in GOCACHE) to store the output of a compiler
205197
// job. This path is based on the dep file name (which is a hash of metadata
206198
// including compiler flags) and the hash of all input files in the paths slice.
207-
func makeCFileCachePath(paths []string, depfileNameHash, ext string) (string, error) {
199+
func makeCFileCachePath(paths []string, depfileNameHash string) (string, error) {
208200
// Hash all input files.
209201
fileHashes := make(map[string]string, len(paths))
210202
for _, path := range paths {
@@ -229,7 +221,7 @@ func makeCFileCachePath(paths []string, depfileNameHash, ext string) (string, er
229221
outFileNameBuf := sha512.Sum512_224(buf)
230222
cacheKey := hex.EncodeToString(outFileNameBuf[:])
231223

232-
outpath := filepath.Join(goenv.Get("GOCACHE"), "obj-"+cacheKey+ext)
224+
outpath := filepath.Join(goenv.Get("GOCACHE"), "obj-"+cacheKey+".bc")
233225
return outpath, nil
234226
}
235227

compileopts/config.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,6 @@ func (c *Config) StackSize() uint64 {
191191
return c.Target.DefaultStackSize
192192
}
193193

194-
// UseThinLTO returns whether ThinLTO should be used for the given target.
195-
func (c *Config) UseThinLTO() bool {
196-
// All architectures support ThinLTO now. However, this code is kept for the
197-
// time being in case there are regressions. The non-ThinLTO code support
198-
// should be removed when it is proven to work reliably.
199-
return true
200-
}
201-
202194
// RP2040BootPatch returns whether the RP2040 boot patch should be applied that
203195
// calculates and patches in the checksum for the 2nd stage bootloader.
204196
func (c *Config) RP2040BootPatch() bool {

transform/globals.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

transform/globals_test.go

Lines changed: 0 additions & 15 deletions
This file was deleted.

transform/testdata/globals-function-sections.ll

Lines changed: 0 additions & 8 deletions
This file was deleted.

transform/testdata/globals-function-sections.out.ll

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)