transform

package
v0.31.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2024 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package transform contains transformation passes for the TinyGo compiler. These transformation passes may be optimization passes or lowering passes.

Optimization passes transform the IR in such a way that they increase the performance of the generated code and/or help the LLVM optimizer better do its job by simplifying the IR. This usually means that certain TinyGo-specific runtime calls are removed or replaced with something simpler if that is a valid operation.

Lowering passes are usually required to run. One example is the interface lowering pass, which replaces stub runtime calls to get an interface method with the method implementation (either a direct call or a thunk).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddStandardAttributes added in v0.21.0

func AddStandardAttributes(fn llvm.Value, config *compileopts.Config)

AddStandardAttributes is a helper function to add standard function attributes to a function. For example, it adds optsize when requested from the -opt= compiler flag.

func CreateStackSizeLoads added in v0.15.0

func CreateStackSizeLoads(mod llvm.Module, config *compileopts.Config) []string

CreateStackSizeLoads replaces internal/task.getGoroutineStackSize calls with loads from internal/task.stackSizes that will be updated after linking. This way the stack sizes are loaded from a separate section and can easily be modified after linking.

func LowerInterfaces added in v0.10.0

func LowerInterfaces(mod llvm.Module, config *compileopts.Config) error

LowerInterfaces lowers all intermediate interface calls and globals that are emitted by the compiler as higher-level intrinsics. They need some lowering before LLVM can work on them. This is done so that a few cleanup passes can run before assigning the final type codes.

func LowerInterrupts added in v0.12.0

func LowerInterrupts(mod llvm.Module) []error

LowerInterrupts creates interrupt handlers for the interrupts created by runtime/interrupt.New.

The operation is as follows. The compiler creates the following during IR generation:

  • calls to runtime/interrupt.callHandlers with an interrupt number.
  • runtime/interrupt.handle objects that store the (constant) interrupt ID and interrupt handler func value.

This pass then replaces those callHandlers calls with calls to the actual interrupt handlers. If there are no interrupt handlers for the given call, the interrupt handler is removed. For hardware vectoring, that means that the entire function is removed. For software vectoring, that means that the call is replaced with an 'unreachable' instruction. This might seem like it causes extra overhead, but in fact inlining and const propagation will eliminate most if not all of that.

func MakeGCStackSlots added in v0.10.0

func MakeGCStackSlots(mod llvm.Module) bool

MakeGCStackSlots converts all calls to runtime.trackPointer to explicit stores to stack slots that are scannable by the GC.

func Optimize added in v0.13.0

func Optimize(mod llvm.Module, config *compileopts.Config) []error

Optimize runs a number of optimization and transformation passes over the given module. Some passes are specific to TinyGo, others are generic LLVM passes.

Please note that some optimizations are not optional, thus Optimize must alwasy be run before emitting machine code.

func OptimizeAllocs

func OptimizeAllocs(mod llvm.Module, printAllocs *regexp.Regexp, maxStackAlloc uint64, logger func(token.Position, string))

OptimizeAllocs tries to replace heap allocations with stack allocations whenever possible. It relies on the LLVM 'nocapture' flag for interprocedural escape analysis, and within a function looks whether an allocation can escape to the heap. If printAllocs is non-nil, it indicates the regexp of functions for which a heap allocation explanation should be printed (why the object can't be stack allocated).

func OptimizeMaps

func OptimizeMaps(mod llvm.Module)

OptimizeMaps eliminates created but unused maps.

In the future, this should statically allocate created but never modified maps. This has not yet been implemented, however.

func OptimizePackage added in v0.24.0

func OptimizePackage(mod llvm.Module, config *compileopts.Config)

OptimizePackage runs optimization passes over the LLVM module for the given Go package.

func OptimizeReflectImplements added in v0.18.0

func OptimizeReflectImplements(mod llvm.Module)

OptimizeReflectImplements optimizes the following code:

implements := someType.Implements(someInterfaceType)

where someType is an arbitrary reflect.Type and someInterfaceType is a reflect.Type of interface kind, to the following code:

_, implements := someType.(interfaceType)

if the interface type is known at compile time (that is, someInterfaceType is a LLVM constant aggregate). This optimization is especially important for the encoding/json package, which uses this method.

As of this writing, the (reflect.Type).Interface method has not yet been implemented so this optimization is critical for the encoding/json package.

func OptimizeStringEqual added in v0.18.0

func OptimizeStringEqual(mod llvm.Module)

OptimizeStringEqual transforms runtime.stringEqual(...) calls into simple integer comparisons if at least one of the sides of the comparison is zero. Ths converts str == "" into len(str) == 0 and "" == "" into false.

func OptimizeStringToBytes added in v0.9.0

func OptimizeStringToBytes(mod llvm.Module)

OptimizeStringToBytes transforms runtime.stringToBytes(...) calls into const []byte slices whenever possible. This optimizes the following pattern:

w.Write([]byte("foo"))

where Write does not store to the slice.

func ReplacePanicsWithTrap added in v0.10.0

func ReplacePanicsWithTrap(mod llvm.Module)

ReplacePanicsWithTrap replaces each call to panic (or similar functions) with calls to llvm.trap, to reduce code size. This is the -panic=trap command-line option.

Types

type ErrMissingIntrinsic added in v0.13.0

type ErrMissingIntrinsic struct {
	Name string
}

ErrMissingIntrinsic is an error indicating that a required intrinsic was not found in the module.

func (ErrMissingIntrinsic) Error added in v0.13.0

func (err ErrMissingIntrinsic) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL