aboutsummaryrefslogtreecommitdiffhomepage
path: root/transform/optimizer.go
AgeCommit message (Collapse)Author
2024-07-31transform: use thinlto-pre-link passesAyke van Laethem
This improves compilation performance by about 5% in my quick test, while increasing binary size on average by 0.13% when comparing the smoke tests in the drivers repo (and about two thirds of that 0.13% is actually caused by a single smoke test). I think this is a good idea because it aligns the TinyGo optimization sequence with what ThinLTO expects.
2024-05-24LLVM 18 supportAyke van Laethem
2024-04-19Run Nick G's spellchecker github.com/client9/misspell, carefuly fix what it ↵dkegel-fastly
found (#4235)
2024-03-19all: move -panic=trap support to the compiler/runtimeAyke van Laethem
Support for `-panic=trap` was previously a pass in the optimization pipeline. This change moves it to the compiler and runtime, which in my opinion is a much better place. As a side effect, it also fixes https://github.com/tinygo-org/tinygo/issues/4161 by trapping inside runtime.runtimePanicAt and not just runtime.runtimePanic. This change also adds a test for the list of imported functions. This is a more generic test where it's easy to add more tests for WebAssembly file properties, such as exported functions.
2024-01-31Allow larger systems to have a larger max stack allocDamian Gryski
Fixes #3331
2023-10-04all: use the new LLVM pass managerAyke van Laethem
The old LLVM pass manager is deprecated and should not be used anymore. Moreover, the pass manager builder (which we used to set up a pass pipeline) is actually removed from LLVM entirely in LLVM 17: https://reviews.llvm.org/D145387 https://reviews.llvm.org/D145835 The new pass manager does change the binary size in many cases: both growing and shrinking it. However, on average the binary size remains more or less the same. This is needed as a preparation for LLVM 17.
2023-02-17all: refactor reflect packageAyke van Laethem
This is a big commit that changes the way runtime type information is stored in the binary. Instead of compressing it and storing it in a number of sidetables, it is stored similar to how the Go compiler toolchain stores it (but still more compactly). This has a number of advantages: * It is much easier to add new features to reflect support. They can simply be added to these structs without requiring massive changes (especially in the reflect lowering pass). * It removes the reflect lowering pass, which was a large amount of hard to understand and debug code. * The reflect lowering pass also required merging all LLVM IR into one module, which is terrible for performance especially when compiling large amounts of code. See issue 2870 for details. * It is (probably!) easier to reason about for the compiler. The downside is that it increases code size a bit, especially when reflect is involved. I hope to fix some of that in later patches.
2022-06-01gc: drop support for 'precise' globalsAyke van Laethem
Precise globals require a whole program optimization pass that is hard to support when building packages separately. This patch removes support for these globals by converting the last use (Linux) to use linker-defined symbols instead. For details, see: https://github.com/tinygo-org/tinygo/issues/2870
2022-05-30transform: run OptimizeMaps during package optimizationsAyke van Laethem
This shrinks transform.Optimize() a little bit, working towards the goal of https://github.com/tinygo-org/tinygo/issues/2870. I ran the smoke tests and there is no practical downside: one test got smaller (??) and one had a different .hex hash, but other than that there was no difference. This should also make TinyGo a liiitle bit faster but it's probably not even measurable.
2022-05-30builder: move some code to transform packageAyke van Laethem
The transform package is the more appropriate location for package-level optimizations, to match `transform.Optimize` for whole-program optimizations. This is just a refactor, to make later changes easier to read.
2022-03-12all: add support for ThinLTOAyke van Laethem
ThinLTO optimizes across LLVM modules at link time. This means that optimizations (such as inlining and const-propagation) are possible between C and Go. This makes this change especially useful for CGo, but not just for CGo. By doing some optimizations at link time, the linker can discard some unused functions and this leads to a size reduction on average. It does increase code size in some cases, but that's true for most optimizations. I've excluded a number of targets for now (wasm, avr, xtensa, windows, macos). They can probably be supported with some more work, but that should be done in separate PRs. Overall, this change results in an average 3.24% size reduction over all the tinygo.org/x/drivers smoke tests. TODO: this commit runs part of the pass pipeline twice. We should set the PrepareForThinLTO flag in the PassManagerBuilder for even further reduced code size (0.7%) and improved compilation speed.
2022-01-19transform: remove switched func loweringNia Waldvogel
The switched func lowering was mainly necessary for coroutines. With coroutines removed, this is no longer necessary.
2022-01-19internal/task: remove coroutinesNia Waldvogel
2022-01-09all: switch to LLVM 13Ayke van Laethem
This adds support for building with `-tags=llvm13` and switches to LLVM 13 for tinygo binaries that are statically linked against LLVM. Some notes on this commit: * Added `-mfloat-abi=soft` to all Cortex-M targets because otherwise nrfx would complain that floating point was enabled on Cortex-M0. That's not the case, but with `-mfloat-abi=soft` the `__SOFTFP__` macro is defined which silences this warning. See: https://reviews.llvm.org/D100372 * Changed from `--sysroot=<root>` to `-nostdlib -isystem <root>` for musl because with Clang 13, even with `--sysroot` some system libraries are used which we don't want. * Changed all `-Xclang -internal-isystem -Xclang` to simply `-isystem`, for consistency with the above change. It appears to have the same effect. * Moved WebAssembly function declarations to the top of the file in task_asyncify_wasm.S because (apparently) the assembler has become more strict.
2021-11-14internal/task: use asyncify on webassemblyNia Waldvogel
This change implements a new "scheduler" for WebAssembly using binaryen's asyncify transform. This is more reliable than the current "coroutines" transform, and works with non-Go code in the call stack. runtime (js/wasm): handle scheduler nesting If WASM calls into JS which calls back into WASM, it is possible for the scheduler to nest. The event from the callback must be handled immediately, so the task cannot simply be deferred to the outer scheduler. This creates a minimal scheduler loop which is used to handle such nesting.
2021-11-06transform: refactor interrupt loweringAyke van Laethem
Instead of doing everything in the interrupt lowering pass, generate some more code in gen-device to declare interrupt handler functions and do some work in the compiler so that interrupt lowering becomes a lot simpler. This has several benefits: - Overall code is smaller, in particular the interrupt lowering pass. - The code should be a bit less "magical" and instead a bit easier to read. In particular, instead of having a magic runtime.callInterruptHandler (that is fully written by the interrupt lowering pass), the runtime calls a generated function like device/sifive.InterruptHandler where this switch already exists in code. - Debug information is improved. This can be helpful during actual debugging but is also useful for other uses of DWARF debug information. For an example on debug information improvement, this is what a backtrace might look like before this commit: Breakpoint 1, 0x00000b46 in UART0_IRQHandler () (gdb) bt #0 0x00000b46 in UART0_IRQHandler () #1 <signal handler called> [..etc] Notice that the debugger doesn't see the source code location where it has stopped. After this commit, breaking at the same line might look like this: Breakpoint 1, (*machine.UART).handleInterrupt (arg1=..., uart=<optimized out>) at /home/ayke/src/github.com/tinygo-org/tinygo/src/machine/machine_nrf.go:200 200 uart.Receive(byte(nrf.UART0.RXD.Get())) (gdb) bt #0 (*machine.UART).handleInterrupt (arg1=..., uart=<optimized out>) at /home/ayke/src/github.com/tinygo-org/tinygo/src/machine/machine_nrf.go:200 #1 UART0_IRQHandler () at /home/ayke/src/github.com/tinygo-org/tinygo/src/device/nrf/nrf51.go:176 #2 <signal handler called> [..etc] By now, the debugger sees an actual source location for UART0_IRQHandler (in the generated file) and an inlined function.
2021-11-03compiler: refactor when the optsize attribute is setAyke van Laethem
This commit has a few related changes: * It sets the optsize attribute immediately in the compiler instead of adding it to each function afterwards in a loop. This seems to me like the more appropriate way to do it. * It centralizes setting the optsize attribute in the transform package, to make later changes easier. * It sets the optsize in a few more places: to runtime.initAll and to WebAssembly i64 wrappers. This commit does not affect the binary size of any of the smoke tests, so should be risk-free.
2021-05-03transform: split interface and reflect loweringAyke van Laethem
These two passes are related, but can definitely work independently. Which is what this change does: it splits the two passes. This should make it easier to change these two new passes in the future. This change now also enables slightly better testing by testing these two passes independently. In particular, the reflect lowering pass got some actual tests: it was barely unit-tested before. I have verified that this doesn't really change code size, at least not on the microbit target. Two tests do change, but in a very minor way (and in opposite direction).
2021-04-22main: add -print-allocs flag that lets you print all heap allocationsAyke van Laethem
This flag, if set, is a regexp for function names. If there are heap allocations in the matching function names, these heap allocations will be printed with an explanation why the heap allocation exists (and why the object can't be stack allocated).
2021-04-08transform: use IPSCCP pass instead of the constant propagation passAyke van Laethem
The constant propagation pass is removed in LLVM 12, so this pass needs to be replaced anyway. The direct replacement would be the SCCP (sparse conditional constant propagation) pass, but perhaps a better replacement is the IPSCCP pass, which is an interprocedural version of the SCCP pass and propagates constants across function calls if possible. This is not always a code size reduction, but it appears to reduce code size in a majority of cases. It certainly reduces code size in almost all WebAssembly tests I did.
2021-04-08builder: run function passes per packageAyke van Laethem
This should result in a small compile time reduction for incremental builds, somewhere around 5-9%. This commit, while small, required many previous commits to not regress binary size. Right now binary size is basically identical with very few changes in size (the only baremetal program that changed in size did so with a 4 byte increase). This commit is one extra step towards doing as much work as possible in the parallel and cached package build step, out of the serial LTO phase. Later improvements in this area have this change as a prerequisite.
2021-04-08builder: add optsize attribute while building the packageAyke van Laethem
This simplifies future changes. While the move itself is very simple, it required some other changes to a few transforms that create new functions to add the optsize attribute manually. It also required abstracting away the optimization level flags (based on the -opt flag) so that it can easily be retrieved from the config object. This commit does not impact binary size on baremetal and WebAssembly. I've seen a few tests on linux/amd64 grow slightly in size, but I'm not too worried about those.
2021-03-28transform: optimize reflect.Type Implements() methodAyke van Laethem
This commit adds a new transform that converts reflect Implements() calls to runtime.interfaceImplements. At the moment, the Implements() method is not yet implemented (how ironic) but if the value passed to Implements is known at compile time the method call can be optimized to runtime.interfaceImplements to make it a regular interface assert. This commit is the last change necessary to add basic support for the encoding/json package. The json package is certainly not yet fully supported, but some trivial objects can be converted to JSON.
2021-03-18transform: optimize string comparisons against ""Ayke van Laethem
This optimizes a common pattern like: if s != "" { ... } to: if len(s) != 0 { ... } This avoids a runtime call and thus produces slightly better code.
2021-03-15builder: add support for -opt=0Ayke van Laethem
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.
2021-01-29compiler: move settings to a separate Config structAyke van Laethem
Moving settings to a separate config struct has two benefits: - It decouples the compiler a bit from other packages, most importantly the compileopts package. Decoupling is generally a good thing. - Perhaps more importantly, it precisely specifies which settings are used while compiling and affect the resulting LLVM module. This will be necessary for caching the LLVM module. While it would have been possible to cache without this refactor, it would have been very easy to miss a setting and thus let the compiler work with invalid/stale data.
2020-07-13all: fix -gc=noneAyke van Laethem
This option was broken for a long time, in part because we didn't test for it. This commit fixes that and adds a test to make sure it won't break again unnoticed.
2020-03-27compiler, transform: remove runtime.isnil hackAyke van Laethem
This hack was originally introduced in https://github.com/tinygo-org/tinygo/pull/251 to fix an escape analysis regression after https://github.com/tinygo-org/tinygo/pull/222 introduced nil checks. Since a new optimization in LLVM (see https://reviews.llvm.org/D60047) this hack is not necessary anymore and can be removed. I've compared all regular tests and smoke tests before and after to check the size. In most cases this change was an improvement although there are a few regressions.
2020-03-24transform: replace panics with source locationsAyke van Laethem
Panics are bad for usability: whenever something breaks, the user is shown a (not very informative) backtrace. Replace it with real error messages instead, that even try to display the Go source location.
2020-03-21compiler: move Optimizer function to transform packageAyke van Laethem
This refactor is a prerequisite to much larger refactors in the compiler.