compiler: move optimizer into the binary

This commit is contained in:
Ayke van Laethem
2018-09-04 18:55:47 +02:00
parent c109ec0955
commit 0746d61639
3 changed files with 14 additions and 10 deletions
+1 -6
View File
@@ -94,14 +94,9 @@ build/tgo: *.go
go build -o build/tgo -i .
# Build IR with the Go compiler.
build/%.bc: src/examples/% src/examples/%/*.go build/tgo src/runtime/*.go build/runtime-$(TARGET)-combined.bc
build/%.o: src/examples/% src/examples/%/*.go build/tgo src/runtime/*.go build/runtime-$(TARGET)-combined.bc
./build/tgo $(TGOFLAGS) -runtime build/runtime-$(TARGET)-combined.bc -o $@ $(subst src/,,$<)
# Compile and optimize bitcode file.
build/%.o: build/%.bc
$(OPT) -Oz -enable-coroutines -o $< $<
$(LLC) -filetype=obj -o $@ $<
# Compile C sources for the runtime.
build/%.bc: src/runtime/%.c src/runtime/*.h
@mkdir -p build
+10 -3
View File
@@ -2787,21 +2787,28 @@ func (c *Compiler) ApplyFunctionSections() {
}
}
func (c *Compiler) Optimize(optLevel, sizeLevel int) {
func (c *Compiler) Optimize(optLevel, sizeLevel int, inlinerThreshold uint) {
builder := llvm.NewPassManagerBuilder()
defer builder.Dispose()
builder.SetOptLevel(optLevel)
builder.SetSizeLevel(sizeLevel)
builder.UseInlinerWithThreshold(200) // TODO depend on opt level, and -Os
builder.UseInlinerWithThreshold(inlinerThreshold)
builder.AddCoroutinePassesToExtensionPoints()
// Run function passes for each function.
funcPasses := llvm.NewFunctionPassManagerForModule(c.mod)
defer funcPasses.Dispose()
builder.PopulateFunc(funcPasses)
funcPasses.InitializeFunc()
for fn := c.mod.FirstFunction(); !fn.IsNil(); fn = llvm.NextFunction(fn) {
funcPasses.RunFunc(fn)
}
funcPasses.FinalizeFunc()
// Run module passes.
modPasses := llvm.NewPassManager()
defer modPasses.Dispose()
builder.Populate(modPasses)
modPasses.Run(c.mod)
}
+3 -1
View File
@@ -59,7 +59,9 @@ func Compile(pkgName, runtimePath, outpath, target string, printIR, dumpSSA bool
if err := c.Verify(); err != nil {
return err
}
//c.Optimize(2, 1) // -O2 -Os
// TODO: provide a flag to disable (most) optimizations.
c.Optimize(2, 2, 5) // -Oz params
if err := c.Verify(); err != nil {
return err
}