@@ -225,8 +225,9 @@ func use(goos, goarch string, wasiThreads, forceEspClang bool) (export Export, e
225225 "-Wl,--error-limit=0" ,
226226 "-fuse-ld=lld" ,
227227 // Enable ICF (Identical Code Folding) to reduce binary size
228- "-Xlinker" ,
229- "--icf=safe" ,
228+ "-Wl,--icf=safe" ,
229+ // Enable ThinLTO, Using default lto kind(thinlto).
230+ "-Wl,--lto-O0" ,
230231 }
231232 if clangRoot != "" {
232233 clangLib := filepath .Join (clangRoot , "lib" )
@@ -249,6 +250,7 @@ func use(goos, goarch string, wasiThreads, forceEspClang bool) (export Export, e
249250 export .CCFLAGS = []string {
250251 "-Qunused-arguments" ,
251252 "-Wno-unused-command-line-argument" ,
253+ "-flto=thin" ,
252254 }
253255
254256 // Add sysroot for macOS only
@@ -499,6 +501,13 @@ func UseTarget(targetName string) (export Export, err error) {
499501 expandedCFlags := env .ExpandEnvSlice (config .CFlags , envs )
500502 cflags = append (cflags , expandedCFlags ... )
501503
504+ if config .Linker == "ld.lld" {
505+ // Enable ThinLTO, Using default lto kind(thinlto).
506+ ldflags = append (ldflags , "--lto-O0" )
507+ cflags = append (cflags , "-flto=thin" )
508+ ccflags = append (ccflags , "-flto=thin" )
509+ }
510+
502511 // The following parameters are inspired by tinygo/builder/library.go
503512 // Handle CPU configuration
504513 if cpu != "" {
@@ -545,6 +554,8 @@ func UseTarget(targetName string) (export Export, err error) {
545554 ccflags = append (ccflags , "-fforce-enable-int128" )
546555 case "riscv64" :
547556 ccflags = append (ccflags , "-march=rv64gc" )
557+ // codegen option should be added to ldflags for lto
558+ ldflags = append (ldflags , "-mllvm" , "-march=rv64gc" )
548559 case "mips" :
549560 ccflags = append (ccflags , "-fno-pic" )
550561 }
@@ -574,9 +585,13 @@ func UseTarget(targetName string) (export Export, err error) {
574585 // Handle code generation configuration
575586 if config .CodeModel != "" {
576587 ccflags = append (ccflags , "-mcmodel=" + config .CodeModel )
588+ // codegen option should be added to ldflags for lto
589+ ldflags = append (ldflags , "-mllvm" , "-code-model=" + config .CodeModel )
577590 }
578591 if config .TargetABI != "" {
579592 ccflags = append (ccflags , "-mabi=" + config .TargetABI )
593+ // codegen option should be added to ldflags for lto
594+ ldflags = append (ldflags , "-mllvm" , "-target-abi=" + config .TargetABI )
580595 }
581596 if config .RelocationModel != "" {
582597 switch config .RelocationModel {
0 commit comments