Skip to content

Commit 9246899

Browse files
aykevldeadprogram
authored andcommitted
builder: move some code to transform package
The transform package is the more appropriate location for package-level optimizations, to match `transform.Optimize` for whole-program optimizations. This is just a refactor, to make later changes easier to read.
1 parent 04ace4d commit 9246899

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

builder/build.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -439,27 +439,7 @@ func Build(pkgName, outpath string, config *compileopts.Config, action func(Buil
439439
return errors.New("verification error after interpreting " + pkgInit.Name())
440440
}
441441

442-
// Run function passes for each function in the module.
443-
// These passes are intended to be run on each function right
444-
// after they're created to reduce IR size (and maybe also for
445-
// cache locality to improve performance), but for now they're
446-
// run here for each function in turn. Maybe this can be
447-
// improved in the future.
448-
builder := llvm.NewPassManagerBuilder()
449-
defer builder.Dispose()
450-
builder.SetOptLevel(optLevel)
451-
builder.SetSizeLevel(sizeLevel)
452-
funcPasses := llvm.NewFunctionPassManagerForModule(mod)
453-
defer funcPasses.Dispose()
454-
builder.PopulateFunc(funcPasses)
455-
funcPasses.InitializeFunc()
456-
for fn := mod.FirstFunction(); !fn.IsNil(); fn = llvm.NextFunction(fn) {
457-
if fn.IsDeclaration() {
458-
continue
459-
}
460-
funcPasses.RunFunc(fn)
461-
}
462-
funcPasses.FinalizeFunc()
442+
transform.OptimizePackage(mod, config)
463443

464444
// Serialize the LLVM module as a bitcode file.
465445
// Write to a temporary path that is renamed to the destination

transform/optimizer.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,34 @@ import (
1111
"tinygo.org/x/go-llvm"
1212
)
1313

14+
// OptimizePackage runs optimization passes over the LLVM module for the given
15+
// Go package.
16+
func OptimizePackage(mod llvm.Module, config *compileopts.Config) {
17+
optLevel, sizeLevel, _ := config.OptLevels()
18+
19+
// Run function passes for each function in the module.
20+
// These passes are intended to be run on each function right
21+
// after they're created to reduce IR size (and maybe also for
22+
// cache locality to improve performance), but for now they're
23+
// run here for each function in turn. Maybe this can be
24+
// improved in the future.
25+
builder := llvm.NewPassManagerBuilder()
26+
defer builder.Dispose()
27+
builder.SetOptLevel(optLevel)
28+
builder.SetSizeLevel(sizeLevel)
29+
funcPasses := llvm.NewFunctionPassManagerForModule(mod)
30+
defer funcPasses.Dispose()
31+
builder.PopulateFunc(funcPasses)
32+
funcPasses.InitializeFunc()
33+
for fn := mod.FirstFunction(); !fn.IsNil(); fn = llvm.NextFunction(fn) {
34+
if fn.IsDeclaration() {
35+
continue
36+
}
37+
funcPasses.RunFunc(fn)
38+
}
39+
funcPasses.FinalizeFunc()
40+
}
41+
1442
// Optimize runs a number of optimization and transformation passes over the
1543
// given module. Some passes are specific to TinyGo, others are generic LLVM
1644
// passes. You can set a preferred performance (0-3) and size (0-2) level and

0 commit comments

Comments
 (0)