aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2021-11-01 18:37:00 +0100
committerRon Evans <[email protected]>2021-11-03 13:40:13 +0100
commit7c24925aa793c11ed5748b641f804a795f9029ed (patch)
tree4f9a1562571cdc668b1b19664308b8cccccc0aeb
parentd7b7583e83b877031c78cf9a727bfd338cf65892 (diff)
downloadtinygo-7c24925aa793c11ed5748b641f804a795f9029ed.tar.gz
tinygo-7c24925aa793c11ed5748b641f804a795f9029ed.zip
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).
-rw-r--r--compiler/symbol.go11
-rw-r--r--stacksize/stacksize.go4
-rw-r--r--transform/transform.go5
3 files changed, 15 insertions, 5 deletions
diff --git a/compiler/symbol.go b/compiler/symbol.go
index b4f47d26f..469066af0 100644
--- a/compiler/symbol.go
+++ b/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
diff --git a/stacksize/stacksize.go b/stacksize/stacksize.go
index 2b111ddcf..ee5ca472a 100644
--- a/stacksize/stacksize.go
+++ b/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)
}
diff --git a/transform/transform.go b/transform/transform.go
index 441f9efa7..3027f913c 100644
--- a/transform/transform.go
+++ b/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))
+ }
}