|
| 1 | +package llvm |
| 2 | + |
| 3 | +/* |
| 4 | +#include "llvm-c/Transforms/PassBuilder.h" |
| 5 | +#include "llvm-c/Error.h" |
| 6 | +#include <stdlib.h> |
| 7 | +*/ |
| 8 | +import "C" |
| 9 | +import ( |
| 10 | + "errors" |
| 11 | + "unsafe" |
| 12 | +) |
| 13 | + |
| 14 | +type PassBuilderOptions struct { |
| 15 | + C C.LLVMPassBuilderOptionsRef |
| 16 | +} |
| 17 | + |
| 18 | +// NewPassBuilderOptions creates a PassBuilderOptions which can be used |
| 19 | +// to specify pass options for RunPasses. |
| 20 | +func NewPassBuilderOptions() (pbo PassBuilderOptions) { |
| 21 | + pbo.C = C.LLVMCreatePassBuilderOptions() |
| 22 | + return |
| 23 | +} |
| 24 | + |
| 25 | +// RunPasses runs the specified optimization passes on the functions in the module. |
| 26 | +// `passes` is a comma separated list of pass names in the same format as llvm's |
| 27 | +// `opt -passes=...` command. Running `opt -print-passes` can list the available |
| 28 | +// passes. |
| 29 | +// |
| 30 | +// Some notable passes include: |
| 31 | +// |
| 32 | +// default<O0> -- run the default -O0 passes |
| 33 | +// default<O1> -- run the default -O1 passes |
| 34 | +// default<O2> -- run the default -O2 passes |
| 35 | +// default<O3> -- run the default -O3 passes |
| 36 | +// default<Os> -- run the default -Os passes, like -O2 but size concious |
| 37 | +// default<Oz> -- run the default -Oz passes, optimizing for size above all else |
| 38 | +func (mod Module) RunPasses(passes string, tm TargetMachine, options PassBuilderOptions) error { |
| 39 | + cpasses := C.CString(passes) |
| 40 | + defer C.free(unsafe.Pointer(cpasses)) |
| 41 | + |
| 42 | + err := C.LLVMRunPasses(mod.C, cpasses, tm.C, options.C) |
| 43 | + if err != nil { |
| 44 | + cstr := C.LLVMGetErrorMessage(err) |
| 45 | + gstr := C.GoString(cstr) |
| 46 | + C.LLVMDisposeErrorMessage(cstr) |
| 47 | + |
| 48 | + return errors.New(gstr) |
| 49 | + } |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +// SetVerifyEach toggles adding a VerifierPass to the PassBuilder, |
| 54 | +// ensuring all functions inside the module are valid. |
| 55 | +func (pbo PassBuilderOptions) SetVerifyEach(verifyEach bool) { |
| 56 | + C.LLVMPassBuilderOptionsSetVerifyEach(pbo.C, boolToLLVMBool(verifyEach)) |
| 57 | +} |
| 58 | + |
| 59 | +func (pbo PassBuilderOptions) SetDebugLogging(debugLogging bool) { |
| 60 | + C.LLVMPassBuilderOptionsSetDebugLogging(pbo.C, boolToLLVMBool(debugLogging)) |
| 61 | +} |
| 62 | + |
| 63 | +func (pbo PassBuilderOptions) SetLoopInterleaving(loopInterleaving bool) { |
| 64 | + C.LLVMPassBuilderOptionsSetLoopInterleaving(pbo.C, boolToLLVMBool(loopInterleaving)) |
| 65 | +} |
| 66 | + |
| 67 | +func (pbo PassBuilderOptions) SetLoopVectorization(loopVectorization bool) { |
| 68 | + C.LLVMPassBuilderOptionsSetLoopVectorization(pbo.C, boolToLLVMBool(loopVectorization)) |
| 69 | +} |
| 70 | + |
| 71 | +func (pbo PassBuilderOptions) SetSLPVectorization(slpVectorization bool) { |
| 72 | + C.LLVMPassBuilderOptionsSetSLPVectorization(pbo.C, boolToLLVMBool(slpVectorization)) |
| 73 | +} |
| 74 | + |
| 75 | +func (pbo PassBuilderOptions) SetLoopUnrolling(loopUnrolling bool) { |
| 76 | + C.LLVMPassBuilderOptionsSetLoopUnrolling(pbo.C, boolToLLVMBool(loopUnrolling)) |
| 77 | +} |
| 78 | + |
| 79 | +func (pbo PassBuilderOptions) SetForgetAllSCEVInLoopUnroll(forgetSCEV bool) { |
| 80 | + C.LLVMPassBuilderOptionsSetForgetAllSCEVInLoopUnroll(pbo.C, boolToLLVMBool(forgetSCEV)) |
| 81 | +} |
| 82 | + |
| 83 | +func (pbo PassBuilderOptions) SetLicmMssaOptCap(optCap uint) { |
| 84 | + C.LLVMPassBuilderOptionsSetLicmMssaOptCap(pbo.C, C.unsigned(optCap)) |
| 85 | +} |
| 86 | + |
| 87 | +func (pbo PassBuilderOptions) SetLicmMssaNoAccForPromotionCap(promotionCap uint) { |
| 88 | + C.LLVMPassBuilderOptionsSetLicmMssaNoAccForPromotionCap(pbo.C, C.unsigned(promotionCap)) |
| 89 | +} |
| 90 | + |
| 91 | +func (pbo PassBuilderOptions) SetCallGraphProfile(cgProfile bool) { |
| 92 | + C.LLVMPassBuilderOptionsSetCallGraphProfile(pbo.C, boolToLLVMBool(cgProfile)) |
| 93 | +} |
| 94 | + |
| 95 | +func (pbo PassBuilderOptions) SetMergeFunctions(mergeFuncs bool) { |
| 96 | + C.LLVMPassBuilderOptionsSetMergeFunctions(pbo.C, boolToLLVMBool(mergeFuncs)) |
| 97 | +} |
| 98 | + |
| 99 | +func (pbo PassBuilderOptions) Dispose() { |
| 100 | + C.LLVMDisposePassBuilderOptions(pbo.C) |
| 101 | +} |
0 commit comments