diff --git a/Makefile b/Makefile index c5bd5c05..ed56bef2 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/compiler.go b/compiler.go index 51516dad..4a3467c1 100644 --- a/compiler.go +++ b/compiler.go @@ -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) } diff --git a/main.go b/main.go index e2a9a137..b98f567c 100644 --- a/main.go +++ b/main.go @@ -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 }