Skip to content

Commit 7c24925

Browse files
aykevldeadprogram
authored andcommitted
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).
1 parent d7b7583 commit 7c24925

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

compiler/symbol.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,13 +327,20 @@ func getParams(sig *types.Signature) []*types.Var {
327327
// addStandardDeclaredAttributes adds attributes that are set for any function,
328328
// whether declared or defined.
329329
func (c *compilerContext) addStandardDeclaredAttributes(llvmFn llvm.Value) {
330-
if c.SizeLevel >= 2 {
330+
if c.SizeLevel >= 1 {
331331
// Set the "optsize" attribute to make slightly smaller binaries at the
332-
// cost of some performance.
332+
// cost of minimal performance loss (-Os in Clang).
333333
kind := llvm.AttributeKindID("optsize")
334334
attr := c.ctx.CreateEnumAttribute(kind, 0)
335335
llvmFn.AddFunctionAttr(attr)
336336
}
337+
if c.SizeLevel >= 2 {
338+
// Set the "minsize" attribute to reduce code size even further,
339+
// regardless of performance loss (-Oz in Clang).
340+
kind := llvm.AttributeKindID("minsize")
341+
attr := c.ctx.CreateEnumAttribute(kind, 0)
342+
llvmFn.AddFunctionAttr(attr)
343+
}
337344
}
338345

339346
// addStandardDefinedAttributes adds the set of attributes that are added to

stacksize/stacksize.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ func CallGraph(f *elf.File, callsIndirectFunction []string) (map[string][]*CallN
180180
// used for getting a function pointer
181181
isCall = false
182182
case elf.R_ARM_ABS32:
183-
// used in the reset vector for pointers
184-
isCall = false
183+
// when compiling with -Oz (minsize), used for calling
184+
isCall = true
185185
default:
186186
return nil, fmt.Errorf("unknown relocation: %s", relocType)
187187
}

transform/transform.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ import (
2222
// the -opt= compiler flag.
2323
func AddStandardAttributes(fn llvm.Value, config *compileopts.Config) {
2424
_, sizeLevel, _ := config.OptLevels()
25-
if sizeLevel >= 2 {
25+
if sizeLevel >= 1 {
2626
fn.AddFunctionAttr(fn.Type().Context().CreateEnumAttribute(llvm.AttributeKindID("optsize"), 0))
2727
}
28+
if sizeLevel >= 2 {
29+
fn.AddFunctionAttr(fn.Type().Context().CreateEnumAttribute(llvm.AttributeKindID("minsize"), 0))
30+
}
2831
}

0 commit comments

Comments
 (0)