From 5a4dcfb367b6d6bb37d8be199db97085a932a2ea Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 5 Mar 2021 14:28:18 +0100 Subject: [PATCH] builder: add support for -opt=0 This optimization level wasn't working before because some passes expect some globals to be cleaned up afterwards. Cleaning these globals is easy, just add the pass necessary for it. This shouldn't reduce the usefulness of the -opt=0 build flag as most optimizations are still skipped. --- Makefile | 2 ++ builder/build.go | 11 ++--------- transform/optimizer.go | 6 ++++++ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index aebf6354..98ff2ede 100644 --- a/Makefile +++ b/Makefile @@ -391,6 +391,8 @@ endif @$(MD5SUM) test.hex $(TINYGO) build -o test.nro -target=nintendoswitch examples/serial @$(MD5SUM) test.nro + $(TINYGO) build -size short -o test.hex -target=pca10040 -opt=0 ./testdata/stdlib.go + @$(MD5SUM) test.hex wasmtest: $(GO) test ./tests/wasm diff --git a/builder/build.go b/builder/build.go index bdeccad5..e5977770 100644 --- a/builder/build.go +++ b/builder/build.go @@ -411,15 +411,8 @@ func compileWholeProgram(pkgName string, config *compileopts.Config, compilerCon // exactly. errs = nil switch config.Options.Opt { - /* - Currently, turning optimizations off causes compile failures. - We rely on the optimizer removing some dead symbols. - Avoid providing an option that does not work right now. - In the future once everything has been fixed we can re-enable this. - - case "none", "0": - errs = transform.Optimize(mod, config, 0, 0, 0) // -O0 - */ + case "none", "0": + errs = transform.Optimize(mod, config, 0, 0, 0) // -O0 case "1": errs = transform.Optimize(mod, config, 1, 0, 0) // -O1 case "2": diff --git a/transform/optimizer.go b/transform/optimizer.go index dc9e1ffb..e330d1af 100644 --- a/transform/optimizer.go +++ b/transform/optimizer.go @@ -111,6 +111,12 @@ func Optimize(mod llvm.Module, config *compileopts.Config, optLevel, sizeLevel i if len(errs) > 0 { return errs } + + // Clean up some leftover symbols of the previous transformations. + goPasses := llvm.NewPassManager() + defer goPasses.Dispose() + goPasses.AddGlobalDCEPass() + goPasses.Run(mod) } // Lower async implementations.