purego_passes.go raw
1 //go:build purego
2
3 package llvm
4
5 import (
6 "errors"
7 "runtime"
8 "unsafe"
9
10 "github.com/ebitengine/purego"
11 )
12
13 type PassBuilderOptions struct {
14 C uintptr
15 }
16
17 var (
18 symCreatePassBuilderOptions uintptr
19 symDisposePassBuilderOptions uintptr
20 symRunPasses uintptr
21 symGetErrorMessage uintptr
22 symDisposeErrorMessage uintptr
23 symPBOSetVerifyEach uintptr
24 symPBOSetDebugLogging uintptr
25 symPBOSetLoopInterleaving uintptr
26 symPBOSetLoopVectorization uintptr
27 symPBOSetSLPVectorization uintptr
28 symPBOSetLoopUnrolling uintptr
29 symPBOSetMergeFunctions uintptr
30 )
31
32 func registerPassSymbols() {
33 symCreatePassBuilderOptions = mustSym(libLLVM, "LLVMCreatePassBuilderOptions")
34 symDisposePassBuilderOptions = mustSym(libLLVM, "LLVMDisposePassBuilderOptions")
35 symRunPasses = mustSym(libLLVM, "LLVMRunPasses")
36 symGetErrorMessage = mustSym(libLLVM, "LLVMGetErrorMessage")
37 symDisposeErrorMessage = mustSym(libLLVM, "LLVMDisposeErrorMessage")
38 symPBOSetVerifyEach = mustSym(libLLVM, "LLVMPassBuilderOptionsSetVerifyEach")
39 symPBOSetDebugLogging = mustSym(libLLVM, "LLVMPassBuilderOptionsSetDebugLogging")
40 symPBOSetLoopInterleaving = mustSym(libLLVM, "LLVMPassBuilderOptionsSetLoopInterleaving")
41 symPBOSetLoopVectorization = mustSym(libLLVM, "LLVMPassBuilderOptionsSetLoopVectorization")
42 symPBOSetSLPVectorization = mustSym(libLLVM, "LLVMPassBuilderOptionsSetSLPVectorization")
43 symPBOSetLoopUnrolling = mustSym(libLLVM, "LLVMPassBuilderOptionsSetLoopUnrolling")
44 symPBOSetMergeFunctions = mustSym(libLLVM, "LLVMPassBuilderOptionsSetMergeFunctions")
45 }
46
47 func NewPassBuilderOptions() (pbo PassBuilderOptions) {
48 r, _, _ := purego.SyscallN(symCreatePassBuilderOptions)
49 pbo.C = r
50 return
51 }
52
53 func (mod Module) RunPasses(passes string, tm TargetMachine, options PassBuilderOptions) error {
54 cpasses := cString(passes)
55 r, _, _ := purego.SyscallN(symRunPasses, mod.C, cpasses, tm.C, options.C)
56 runtime.KeepAlive(passes)
57 if r != 0 {
58 cstr, _, _ := purego.SyscallN(symGetErrorMessage, r)
59 s := goString(cstr)
60 purego.SyscallN(symDisposeErrorMessage, cstr)
61 return errors.New(s)
62 }
63 return nil
64 }
65
66 func (pbo PassBuilderOptions) SetVerifyEach(v bool) {
67 purego.SyscallN(symPBOSetVerifyEach, pbo.C, boolToInt(v))
68 }
69
70 func (pbo PassBuilderOptions) SetDebugLogging(v bool) {
71 purego.SyscallN(symPBOSetDebugLogging, pbo.C, boolToInt(v))
72 }
73
74 func (pbo PassBuilderOptions) SetLoopInterleaving(v bool) {
75 purego.SyscallN(symPBOSetLoopInterleaving, pbo.C, boolToInt(v))
76 }
77
78 func (pbo PassBuilderOptions) SetLoopVectorization(v bool) {
79 purego.SyscallN(symPBOSetLoopVectorization, pbo.C, boolToInt(v))
80 }
81
82 func (pbo PassBuilderOptions) SetSLPVectorization(v bool) {
83 purego.SyscallN(symPBOSetSLPVectorization, pbo.C, boolToInt(v))
84 }
85
86 func (pbo PassBuilderOptions) SetLoopUnrolling(v bool) {
87 purego.SyscallN(symPBOSetLoopUnrolling, pbo.C, boolToInt(v))
88 }
89
90 func (pbo PassBuilderOptions) SetMergeFunctions(v bool) {
91 purego.SyscallN(symPBOSetMergeFunctions, pbo.C, boolToInt(v))
92 }
93
94 func (pbo PassBuilderOptions) Dispose() {
95 purego.SyscallN(symDisposePassBuilderOptions, pbo.C)
96 }
97
98 func (pbo PassBuilderOptions) SetForgetAllSCEVInLoopUnroll(v bool) {}
99 func (pbo PassBuilderOptions) SetLicmMssaOptCap(optCap uint) {}
100 func (pbo PassBuilderOptions) SetLicmMssaNoAccForPromotionCap(cap uint) {}
101 func (pbo PassBuilderOptions) SetCallGraphProfile(v bool) {}
102
103 func init() {
104 _ = unsafe.Pointer(nil)
105 }
106