| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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 |
|
| 74 |
|
| 75 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
LLVMAddScalarReplAggregatesPass(mgr->passmgr); |
| 96 |
LLVMAddEarlyCSEPass(mgr->passmgr); |
| 97 |
LLVMAddCFGSimplificationPass(mgr->passmgr); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 119 |
|
| 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 |
|
| 146 |
|
| 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 |
|
| 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 |
|
| 176 |
|
| 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 |
} |