Browse Source

compiler: add minsize attribute for -Oz

This matches the behavior of Clang, which uses optsize for -Os and adds
minsize for -Oz.

The code size change is all over the map, but using a hacked together
size comparison tool I've found that there is a slight reduction in
binary size overall (-1.6% with the tinygo smoke tests and -0.8% for the
drivers smoke test).
pull/2031/head
Ayke van Laethem 3 years ago
committed by Ron Evans
parent
commit
7c24925aa7
  1. 11
      compiler/symbol.go
  2. 4
      stacksize/stacksize.go
  3. 5
      transform/transform.go

11
compiler/symbol.go

@ -327,13 +327,20 @@ func getParams(sig *types.Signature) []*types.Var {
// addStandardDeclaredAttributes adds attributes that are set for any function,
// whether declared or defined.
func (c *compilerContext) addStandardDeclaredAttributes(llvmFn llvm.Value) {
if c.SizeLevel >= 2 {
if c.SizeLevel >= 1 {
// Set the "optsize" attribute to make slightly smaller binaries at the
// cost of some performance.
// cost of minimal performance loss (-Os in Clang).
kind := llvm.AttributeKindID("optsize")
attr := c.ctx.CreateEnumAttribute(kind, 0)
llvmFn.AddFunctionAttr(attr)
}
if c.SizeLevel >= 2 {
// Set the "minsize" attribute to reduce code size even further,
// regardless of performance loss (-Oz in Clang).
kind := llvm.AttributeKindID("minsize")
attr := c.ctx.CreateEnumAttribute(kind, 0)
llvmFn.AddFunctionAttr(attr)
}
}
// addStandardDefinedAttributes adds the set of attributes that are added to

4
stacksize/stacksize.go

@ -180,8 +180,8 @@ func CallGraph(f *elf.File, callsIndirectFunction []string) (map[string][]*CallN
// used for getting a function pointer
isCall = false
case elf.R_ARM_ABS32:
// used in the reset vector for pointers
isCall = false
// when compiling with -Oz (minsize), used for calling
isCall = true
default:
return nil, fmt.Errorf("unknown relocation: %s", relocType)
}

5
transform/transform.go

@ -22,7 +22,10 @@ import (
// the -opt= compiler flag.
func AddStandardAttributes(fn llvm.Value, config *compileopts.Config) {
_, sizeLevel, _ := config.OptLevels()
if sizeLevel >= 2 {
if sizeLevel >= 1 {
fn.AddFunctionAttr(fn.Type().Context().CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
}
if sizeLevel >= 2 {
fn.AddFunctionAttr(fn.Type().Context().CreateEnumAttribute(llvm.AttributeKindID("minsize"), 0))
}
}

Loading…
Cancel
Save