mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-08 21:13:39 +00:00
compiler: refactor when the optsize attribute is set
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.
This commit is contained in:
committed by
Ron Evans
parent
1869efe954
commit
d7b7583e83
@@ -33,6 +33,7 @@ import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
@@ -84,7 +85,7 @@ type interfaceInfo struct {
|
||||
// should be seen as a regular function call (see LowerInterfaces).
|
||||
type lowerInterfacesPass struct {
|
||||
mod llvm.Module
|
||||
sizeLevel int // LLVM optimization size level, 1 means -opt=s and 2 means -opt=z
|
||||
config *compileopts.Config
|
||||
builder llvm.Builder
|
||||
ctx llvm.Context
|
||||
uintptrType llvm.Type
|
||||
@@ -97,10 +98,10 @@ type lowerInterfacesPass struct {
|
||||
// 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 LowerInterfaces(mod llvm.Module, sizeLevel int) error {
|
||||
func LowerInterfaces(mod llvm.Module, config *compileopts.Config) error {
|
||||
p := &lowerInterfacesPass{
|
||||
mod: mod,
|
||||
sizeLevel: sizeLevel,
|
||||
config: config,
|
||||
builder: mod.Context().NewBuilder(),
|
||||
ctx: mod.Context(),
|
||||
uintptrType: mod.Context().IntType(llvm.NewTargetData(mod.DataLayout()).PointerSize() * 8),
|
||||
@@ -343,9 +344,7 @@ func (p *lowerInterfacesPass) defineInterfaceImplementsFunc(fn llvm.Value, itf *
|
||||
fn.Param(0).SetName("actualType")
|
||||
fn.SetLinkage(llvm.InternalLinkage)
|
||||
fn.SetUnnamedAddr(true)
|
||||
if p.sizeLevel >= 2 {
|
||||
fn.AddFunctionAttr(p.ctx.CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
|
||||
}
|
||||
AddStandardAttributes(fn, p.config)
|
||||
|
||||
// Start the if/else chain at the entry block.
|
||||
entry := p.ctx.AddBasicBlock(fn, "entry")
|
||||
@@ -389,9 +388,7 @@ func (p *lowerInterfacesPass) defineInterfaceMethodFunc(fn llvm.Value, itf *inte
|
||||
parentHandle.SetName("parentHandle")
|
||||
fn.SetLinkage(llvm.InternalLinkage)
|
||||
fn.SetUnnamedAddr(true)
|
||||
if p.sizeLevel >= 2 {
|
||||
fn.AddFunctionAttr(p.ctx.CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
|
||||
}
|
||||
AddStandardAttributes(fn, p.config)
|
||||
|
||||
// TODO: debug info
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package transform_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"github.com/tinygo-org/tinygo/transform"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
@@ -10,7 +11,7 @@ import (
|
||||
func TestInterfaceLowering(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTransform(t, "testdata/interface", func(mod llvm.Module) {
|
||||
err := transform.LowerInterfaces(mod, 0)
|
||||
err := transform.LowerInterfaces(mod, &compileopts.Config{Options: &compileopts.Options{Opt: "2"}})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
@@ -22,7 +23,7 @@ import (
|
||||
// simply call the registered handlers. This might seem like it causes extra
|
||||
// overhead, but in fact inlining and const propagation will eliminate most if
|
||||
// not all of that.
|
||||
func LowerInterrupts(mod llvm.Module, sizeLevel int) []error {
|
||||
func LowerInterrupts(mod llvm.Module, config *compileopts.Config) []error {
|
||||
var errs []error
|
||||
|
||||
// Discover interrupts. The runtime/interrupt.Register call is a compiler
|
||||
@@ -174,9 +175,7 @@ func LowerInterrupts(mod llvm.Module, sizeLevel int) []error {
|
||||
// Create the wrapper function which is the actual interrupt handler
|
||||
// that is inserted in the interrupt vector.
|
||||
fn.SetUnnamedAddr(true)
|
||||
if sizeLevel >= 2 {
|
||||
fn.AddFunctionAttr(ctx.CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
|
||||
}
|
||||
AddStandardAttributes(fn, config)
|
||||
fn.SetSection(".text." + name)
|
||||
if isSoftwareVectored {
|
||||
fn.SetLinkage(llvm.InternalLinkage)
|
||||
|
||||
@@ -3,6 +3,7 @@ package transform_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"github.com/tinygo-org/tinygo/transform"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
@@ -12,7 +13,7 @@ func TestInterruptLowering(t *testing.T) {
|
||||
for _, subtest := range []string{"avr", "cortexm"} {
|
||||
t.Run(subtest, func(t *testing.T) {
|
||||
testTransform(t, "testdata/interrupt-"+subtest, func(mod llvm.Module) {
|
||||
errs := transform.LowerInterrupts(mod, 0)
|
||||
errs := transform.LowerInterrupts(mod, &compileopts.Config{Options: &compileopts.Options{Opt: "2"}})
|
||||
if len(errs) != 0 {
|
||||
t.Fail()
|
||||
for _, err := range errs {
|
||||
|
||||
@@ -68,12 +68,12 @@ func Optimize(mod llvm.Module, config *compileopts.Config, optLevel, sizeLevel i
|
||||
OptimizeStringToBytes(mod)
|
||||
OptimizeReflectImplements(mod)
|
||||
OptimizeAllocs(mod, nil, nil)
|
||||
err := LowerInterfaces(mod, sizeLevel)
|
||||
err := LowerInterfaces(mod, config)
|
||||
if err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
|
||||
errs := LowerInterrupts(mod, sizeLevel)
|
||||
errs := LowerInterrupts(mod, config)
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
@@ -97,7 +97,7 @@ func Optimize(mod llvm.Module, config *compileopts.Config, optLevel, sizeLevel i
|
||||
|
||||
} else {
|
||||
// Must be run at any optimization level.
|
||||
err := LowerInterfaces(mod, sizeLevel)
|
||||
err := LowerInterfaces(mod, config)
|
||||
if err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
@@ -105,7 +105,7 @@ func Optimize(mod llvm.Module, config *compileopts.Config, optLevel, sizeLevel i
|
||||
if config.FuncImplementation() == "switch" {
|
||||
LowerFuncValues(mod)
|
||||
}
|
||||
errs := LowerInterrupts(mod, sizeLevel)
|
||||
errs := LowerInterrupts(mod, config)
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
@@ -11,3 +11,18 @@
|
||||
// lowering pass, which replaces stub runtime calls to get an interface method
|
||||
// with the method implementation (either a direct call or a thunk).
|
||||
package transform
|
||||
|
||||
import (
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
// 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 AddStandardAttributes(fn llvm.Value, config *compileopts.Config) {
|
||||
_, sizeLevel, _ := config.OptLevels()
|
||||
if sizeLevel >= 2 {
|
||||
fn.AddFunctionAttr(fn.Type().Context().CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
|
||||
@@ -15,7 +16,7 @@ import (
|
||||
//
|
||||
// This pass can be enabled/disabled with the -wasm-abi flag, and is enabled by
|
||||
// default as of december 2019.
|
||||
func ExternalInt64AsPtr(mod llvm.Module) error {
|
||||
func ExternalInt64AsPtr(mod llvm.Module, config *compileopts.Config) error {
|
||||
ctx := mod.Context()
|
||||
builder := ctx.NewBuilder()
|
||||
defer builder.Dispose()
|
||||
@@ -79,10 +80,7 @@ func ExternalInt64AsPtr(mod llvm.Module) error {
|
||||
fn.SetName(name + "$i64wrap")
|
||||
externalFnType := llvm.FunctionType(returnType, paramTypes, fnType.IsFunctionVarArg())
|
||||
externalFn := llvm.AddFunction(mod, name, externalFnType)
|
||||
optsize := fn.GetEnumFunctionAttribute(llvm.AttributeKindID("optsize"))
|
||||
if !optsize.IsNil() {
|
||||
fn.AddFunctionAttr(optsize)
|
||||
}
|
||||
AddStandardAttributes(fn, config)
|
||||
|
||||
if fn.IsDeclaration() {
|
||||
// Just a declaration: the definition doesn't exist on the Go side
|
||||
|
||||
@@ -3,6 +3,7 @@ package transform_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tinygo-org/tinygo/compileopts"
|
||||
"github.com/tinygo-org/tinygo/transform"
|
||||
"tinygo.org/x/go-llvm"
|
||||
)
|
||||
@@ -11,7 +12,7 @@ func TestWasmABI(t *testing.T) {
|
||||
t.Parallel()
|
||||
testTransform(t, "testdata/wasm-abi", func(mod llvm.Module) {
|
||||
// Run ABI change pass.
|
||||
err := transform.ExternalInt64AsPtr(mod)
|
||||
err := transform.ExternalInt64AsPtr(mod, &compileopts.Config{Options: &compileopts.Options{Opt: "2"}})
|
||||
if err != nil {
|
||||
t.Errorf("failed to change wasm ABI: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user