Debian Codegraph @

src/gallium/auxiliary/gallivm/lp_bld_passmgr.c

raw symbols SCIP
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_debug.h"
29 #include "util/u_memory.h"
30 #include "util/os_time.h"
31 #include "lp_bld_debug.h"
32 #include "lp_bld_passmgr.h"
33
34 #define USE_NEW_PASS (LLVM_VERSION_MAJOR >= 15)
35
36 #if USE_NEW_PASS == 1
37 #include <llvm-c/Transforms/PassBuilder.h>
38 #else
39 #include <llvm-c/Transforms/Scalar.h>
40 #include <llvm-c/Transforms/Utils.h>
41 #if LLVM_VERSION_MAJOR <= 8 && (DETECT_ARCH_AARCH64 || DETECT_ARCH_ARM || DETECT_ARCH_S390 || DETECT_ARCH_MIPS64)
42 #include <llvm-c/Transforms/IPO.h>
43 #endif
44 #include <llvm-c/Transforms/Coroutines.h>
45 #endif
46
47 #if USE_NEW_PASS == 0
48 struct lp_passmgr {
49 LLVMPassManagerRef passmgr;
50 LLVMPassManagerRef cgpassmgr;
51 };
52 #else
53 struct lp_passmgr;
54 #endif
55
56 bool
57 lp_passmgr_create(LLVMModuleRef module, struct lp_passmgr **mgr_p)
58 {
59 struct lp_passmgr *mgr = NULL;
60 #if USE_NEW_PASS == 0
61 mgr = CALLOC_STRUCT(lp_passmgr);
62 if (!mgr)
63 return false;
64
65 mgr->passmgr = LLVMCreateFunctionPassManagerForModule(module);
66 if (!mgr->passmgr) {
67 free(mgr);
68 return false;
69 }
70
71 mgr->cgpassmgr = LLVMCreatePassManager();
72 /*
73 * TODO: some per module pass manager with IPO passes might be helpful -
74 * the generated texture functions may benefit from inlining if they are
75 * simple, or constant propagation into them, etc.
76 */
77
78 #if LLVM_VERSION_MAJOR <= 8 && (DETECT_ARCH_AARCH64 || DETECT_ARCH_ARM || DETECT_ARCH_S390 || DETECT_ARCH_MIPS64)
79 LLVMAddArgumentPromotionPass(mgr->cgpassmgr);
80 LLVMAddFunctionAttrsPass(mgr->cgpassmgr);
81 #endif
82 LLVMAddCoroEarlyPass(mgr->cgpassmgr);
83 LLVMAddCoroSplitPass(mgr->cgpassmgr);
84 LLVMAddCoroElidePass(mgr->cgpassmgr);
85
86 if ((gallivm_perf & GALLIVM_PERF_NO_OPT) == 0) {
87 /*
88 * TODO: Evaluate passes some more - keeping in mind
89 * both quality of generated code and compile times.
90 */
91 /*
92 * NOTE: if you change this, don't forget to change the output
93 * with GALLIVM_DEBUG_DUMP_BC in gallivm_compile_module.
94 */
95 LLVMAddScalarReplAggregatesPass(mgr->passmgr);
96 LLVMAddEarlyCSEPass(mgr->passmgr);
97 LLVMAddCFGSimplificationPass(mgr->passmgr);
98 /*
99 * FIXME: LICM is potentially quite useful. However, for some
100 * rather crazy shaders the compile time can reach _hours_ per shader,
101 * due to licm implying lcssa (since llvm 3.5), which can take forever.
102 * Even for sane shaders, the cost of licm is rather high (and not just
103 * due to lcssa, licm itself too), though mostly only in cases when it
104 * can actually move things, so having to disable it is a pity.
105 * LLVMAddLICMPass(mgr->passmgr);
106 */
107 LLVMAddReassociatePass(mgr->passmgr);
108 LLVMAddPromoteMemoryToRegisterPass(mgr->passmgr);
109 #if LLVM_VERSION_MAJOR <= 11
110 LLVMAddConstantPropagationPass(mgr->passmgr);
111 #else
112 LLVMAddInstructionSimplifyPass(mgr->passmgr);
113 #endif
114 LLVMAddInstructionCombiningPass(mgr->passmgr);
115 LLVMAddGVNPass(mgr->passmgr);
116 }
117 else {
118 /* We need at least this pass to prevent the backends to fail in
119 * unexpected ways.
120 */
121 LLVMAddPromoteMemoryToRegisterPass(mgr->passmgr);
122 }
123 LLVMAddCoroCleanupPass(mgr->passmgr);
124 #endif
125 *mgr_p = mgr;
126 return true;
127 }
128
129 void
130 lp_passmgr_run(struct lp_passmgr *mgr,
131 LLVMModuleRef module,
132 LLVMTargetMachineRef tm,
133 const char *module_name)
134 {
135 int64_t time_begin;
136
137 if (gallivm_debug & GALLIVM_DEBUG_PERF)
138 time_begin = os_time_get();
139
140 #if USE_NEW_PASS == 1
141 char passes[1024];
142 passes[0] = 0;
143
144 /*
145 * there should be some way to combine these two pass runs but I'm not seeing it,
146 * at the time of writing.
147 */
148 strcpy(passes, "default<O0>");
149
150 LLVMPassBuilderOptionsRef opts = LLVMCreatePassBuilderOptions();
151 LLVMRunPasses(module, passes, tm, opts);
152
153 if (!(gallivm_perf & GALLIVM_PERF_NO_OPT))
154 #if LLVM_VERSION_MAJOR >= 18
155 strcpy(passes, "sroa,early-cse,simplifycfg,reassociate,mem2reg,instsimplify,instcombine<no-verify-fixpoint>");
156 #else
157 strcpy(passes, "sroa,early-cse,simplifycfg,reassociate,mem2reg,instsimplify,instcombine");
158 #endif
159 else
160 strcpy(passes, "mem2reg");
161
162 LLVMRunPasses(module, passes, tm, opts);
163 LLVMDisposePassBuilderOptions(opts);
164 #else
165 LLVMRunPassManager(mgr->cgpassmgr, module);
166 /* Run optimization passes */
167 LLVMInitializeFunctionPassManager(mgr->passmgr);
168 LLVMValueRef func;
169 func = LLVMGetFirstFunction(module);
170 while (func) {
171 if (0) {
172 debug_printf("optimizing func %s...\n", LLVMGetValueName(func));
173 }
174
175 /* Disable frame pointer omission on debug/profile builds */
176 /* XXX: And workaround http://llvm.org/PR21435 */
177 #if MESA_DEBUG || defined(PROFILE) || DETECT_ARCH_X86 || DETECT_ARCH_X86_64
178 LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim", "true");
179 LLVMAddTargetDependentFunctionAttr(func, "no-frame-pointer-elim-non-leaf", "true");
180 #endif
181
182 LLVMRunFunctionPassManager(mgr->passmgr, func);
183 func = LLVMGetNextFunction(func);
184 }
185 LLVMFinalizeFunctionPassManager(mgr->passmgr);
186 #endif
187
188 if (gallivm_debug & GALLIVM_DEBUG_PERF) {
189 int64_t time_end = os_time_get();
190 int time_msec = (int)((time_end - time_begin) / 1000);
191 assert(module_name);
192 debug_printf("optimizing module %s took %d msec\n",
193 module_name, time_msec);
194 }
195 }
196
197 void
198 lp_passmgr_dispose(struct lp_passmgr *mgr)
199 {
200 #if USE_NEW_PASS == 0
201 if (mgr->passmgr) {
202 LLVMDisposePassManager(mgr->passmgr);
203 mgr->passmgr = NULL;
204 }
205
206 if (mgr->cgpassmgr) {
207 LLVMDisposePassManager(mgr->cgpassmgr);
208 mgr->cgpassmgr = NULL;
209 }
210 FREE(mgr);
211 #endif
212 }