From 5a15d4162d1736442f9abbb61f0afd923de9ab41 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 22 Dec 2018 17:09:41 +0100 Subject: [PATCH] compiler: add optsize function attr to reduce binary size This is also added by Clang at -Oz and results in slightly smaller binaries. --- compiler/optimizer.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/compiler/optimizer.go b/compiler/optimizer.go index 08d759b1..cf4b9580 100644 --- a/compiler/optimizer.go +++ b/compiler/optimizer.go @@ -51,6 +51,16 @@ func (c *Compiler) Optimize(optLevel, sizeLevel int, inlinerThreshold uint) erro return 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 := c.ctx.CreateEnumAttribute(kind, 0) + for fn := c.mod.FirstFunction(); !fn.IsNil(); fn = llvm.NextFunction(fn) { + fn.AddFunctionAttr(attr) + } + } + // Run module passes. modPasses := llvm.NewPassManager() defer modPasses.Dispose()