builder: add optsize attribute while building the package

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.
This commit is contained in:
Ayke van Laethem
2021-04-04 17:50:47 +02:00
committed by Ron Evans
parent fa6c1b69ce
commit 49ec3eb58e
9 changed files with 66 additions and 33 deletions
+9 -1
View File
@@ -133,6 +133,7 @@ func (itf *interfaceInfo) id() string {
// 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
builder llvm.Builder
ctx llvm.Context
uintptrType llvm.Type
@@ -145,9 +146,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) error {
func LowerInterfaces(mod llvm.Module, sizeLevel int) error {
p := &lowerInterfacesPass{
mod: mod,
sizeLevel: sizeLevel,
builder: mod.Context().NewBuilder(),
ctx: mod.Context(),
uintptrType: mod.Context().IntType(llvm.NewTargetData(mod.DataLayout()).PointerSize() * 8),
@@ -594,6 +596,9 @@ func (p *lowerInterfacesPass) createInterfaceImplementsFunc(itf *interfaceInfo)
fn := itf.assertFunc
fn.SetLinkage(llvm.InternalLinkage)
fn.SetUnnamedAddr(true)
if p.sizeLevel >= 2 {
fn.AddFunctionAttr(p.ctx.CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
}
// TODO: debug info
@@ -653,6 +658,9 @@ func (p *lowerInterfacesPass) createInterfaceMethodFunc(itf *interfaceInfo, sign
fn := itf.methodFuncs[signature]
fn.SetLinkage(llvm.InternalLinkage)
fn.SetUnnamedAddr(true)
if p.sizeLevel >= 2 {
fn.AddFunctionAttr(p.ctx.CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
}
// TODO: debug info
+1 -1
View File
@@ -9,7 +9,7 @@ import (
func TestInterfaceLowering(t *testing.T) {
t.Parallel()
testTransform(t, "testdata/interface", func(mod llvm.Module) {
err := LowerInterfaces(mod)
err := LowerInterfaces(mod, 0)
if err != nil {
t.Error(err)
}
+4 -1
View File
@@ -22,7 +22,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) []error {
func LowerInterrupts(mod llvm.Module, sizeLevel int) []error {
var errs []error
// Discover interrupts. The runtime/interrupt.Register call is a compiler
@@ -182,6 +182,9 @@ func LowerInterrupts(mod llvm.Module) []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))
}
fn.SetSection(".text." + name)
if isSoftwareVectored {
fn.SetLinkage(llvm.InternalLinkage)
+1 -1
View File
@@ -11,7 +11,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 := LowerInterrupts(mod)
errs := LowerInterrupts(mod, 0)
if len(errs) != 0 {
t.Fail()
for _, err := range errs {
+4 -14
View File
@@ -76,12 +76,12 @@ func Optimize(mod llvm.Module, config *compileopts.Config, optLevel, sizeLevel i
OptimizeStringToBytes(mod)
OptimizeReflectImplements(mod)
OptimizeAllocs(mod)
err := LowerInterfaces(mod)
err := LowerInterfaces(mod, sizeLevel)
if err != nil {
return []error{err}
}
errs := LowerInterrupts(mod)
errs := LowerInterrupts(mod, sizeLevel)
if len(errs) > 0 {
return errs
}
@@ -102,14 +102,14 @@ func Optimize(mod llvm.Module, config *compileopts.Config, optLevel, sizeLevel i
} else {
// Must be run at any optimization level.
err := LowerInterfaces(mod)
err := LowerInterfaces(mod, sizeLevel)
if err != nil {
return []error{err}
}
if config.FuncImplementation() == "switch" {
LowerFuncValues(mod)
}
errs := LowerInterrupts(mod)
errs := LowerInterrupts(mod, sizeLevel)
if len(errs) > 0 {
return errs
}
@@ -153,16 +153,6 @@ func Optimize(mod llvm.Module, config *compileopts.Config, optLevel, sizeLevel i
return []error{errors.New("optimizations caused a verification failure")}
}
if sizeLevel >= 2 {
// Set the "optsize" attribute to make slightly smaller binaries at the
// cost of some performance.
kind := llvm.AttributeKindID("optsize")
attr := mod.Context().CreateEnumAttribute(kind, 0)
for fn := mod.FirstFunction(); !fn.IsNil(); fn = llvm.NextFunction(fn) {
fn.AddFunctionAttr(attr)
}
}
// After TinyGo-specific transforms have finished, undo exporting these functions.
for _, name := range getFunctionsUsedInTransforms(config) {
fn := mod.NamedFunction(name)
+4
View File
@@ -73,6 +73,10 @@ 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)
}
if fn.IsDeclaration() {
// Just a declaration: the definition doesn't exist on the Go side