authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-09 17:21:06-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-04-09 17:21:06-07:00
log86b31394c9d73b0f753918cea4f08ce8d7a26119
tree05adee310cd34a85fd6ced7777d669e7442f6817
parent000c0845b1ac30fe57690d71cb84847b42ad762a
parent52d871844c643f396a2bddee0753d24ff71ca3bb
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #8482 from mshockwave/dev-llvm-newpm

Switch to new LLVM PassManager

1 files changed, 130 insertions(+), 98 deletions(-)

src/zig_llvm.cpp+130-98
......@@ -20,6 +20,7 @@
2020#pragma GCC diagnostic ignored "-Winit-list-lifetime"
2121#endif
2222
23#include <llvm/Analysis/AliasAnalysis.h>
2324#include <llvm/Analysis/TargetLibraryInfo.h>
2425#include <llvm/Analysis/TargetTransformInfo.h>
2526#include <llvm/Bitcode/BitcodeWriter.h>
......@@ -30,9 +31,12 @@
3031#include <llvm/IR/Instructions.h>
3132#include <llvm/IR/LegacyPassManager.h>
3233#include <llvm/IR/Module.h>
34#include <llvm/IR/PassManager.h>
3335#include <llvm/IR/Verifier.h>
3436#include <llvm/InitializePasses.h>
3537#include <llvm/MC/SubtargetFeature.h>
38#include <llvm/Passes/PassBuilder.h>
39#include <llvm/Passes/StandardInstrumentations.h>
3640#include <llvm/Object/Archive.h>
3741#include <llvm/Object/ArchiveWriter.h>
3842#include <llvm/Object/COFF.h>
......@@ -54,6 +58,9 @@
5458#include <llvm/Transforms/Instrumentation/ThreadSanitizer.h>
5559#include <llvm/Transforms/Scalar.h>
5660#include <llvm/Transforms/Utils.h>
61#include <llvm/Transforms/Utils/AddDiscriminators.h>
62#include <llvm/Transforms/Utils/CanonicalizeAliases.h>
63#include <llvm/Transforms/Utils/NameAnonGlobals.h>
5764
5865#include <lld/Common/Driver.h>
5966
......@@ -91,14 +98,6 @@ char *ZigLLVMGetNativeFeatures(void) {
9198 return strdup((const char *)StringRef(features.getString()).bytes_begin());
9299}
93100
94static void addDiscriminatorsPass(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) {
95 PM.add(createAddDiscriminatorsPass());
96}
97
98static void addThreadSanitizerPass(const PassManagerBuilder &Builder, legacy::PassManagerBase &PM) {
99 PM.add(createThreadSanitizerLegacyPassPass());
100}
101
102101#ifndef NDEBUG
103102static const bool assertions_on = true;
104103#else
......@@ -193,14 +192,16 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
193192 bool is_small, bool time_report, bool tsan, bool lto,
194193 const char *asm_filename, const char *bin_filename, const char *llvm_ir_filename)
195194{
195 // TODO: Maybe we should collect time trace rather than using timer
196 // to get a more hierarchical timeline view
196197 TimePassesIsEnabled = time_report;
197198
198 raw_fd_ostream *dest_asm = nullptr;
199 raw_fd_ostream *dest_bin = nullptr;
199 raw_fd_ostream *dest_asm_ptr = nullptr;
200 raw_fd_ostream *dest_bin_ptr = nullptr;
200201
201202 if (asm_filename) {
202203 std::error_code EC;
203 dest_asm = new(std::nothrow) raw_fd_ostream(asm_filename, EC, sys::fs::F_None);
204 dest_asm_ptr = new(std::nothrow) raw_fd_ostream(asm_filename, EC, sys::fs::F_None);
204205 if (EC) {
205206 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
206207 return true;
......@@ -208,116 +209,147 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
208209 }
209210 if (bin_filename) {
210211 std::error_code EC;
211 dest_bin = new(std::nothrow) raw_fd_ostream(bin_filename, EC, sys::fs::F_None);
212 dest_bin_ptr = new(std::nothrow) raw_fd_ostream(bin_filename, EC, sys::fs::F_None);
212213 if (EC) {
213214 *error_message = strdup((const char *)StringRef(EC.message()).bytes_begin());
214215 return true;
215216 }
216217 }
217218
218 TargetMachine* target_machine = reinterpret_cast<TargetMachine*>(targ_machine_ref);
219 target_machine->setO0WantsFastISel(true);
220
221 Module* module = unwrap(module_ref);
222
223 PassManagerBuilder *PMBuilder = new(std::nothrow) PassManagerBuilder();
224 if (PMBuilder == nullptr) {
225 *error_message = strdup("memory allocation failure");
226 return true;
219 std::unique_ptr<raw_fd_ostream> dest_asm(dest_asm_ptr),
220 dest_bin(dest_bin_ptr);
221
222 TargetMachine &target_machine = *reinterpret_cast<TargetMachine*>(targ_machine_ref);
223 target_machine.setO0WantsFastISel(true);
224
225 Module &module = *unwrap(module_ref);
226
227 // Pipeline configurations
228 PipelineTuningOptions pipeline_opts;
229 pipeline_opts.LoopUnrolling = !is_debug;
230 pipeline_opts.SLPVectorization = !is_debug;
231 pipeline_opts.LoopVectorization = !is_debug;
232 pipeline_opts.LoopInterleaving = !is_debug;
233 pipeline_opts.MergeFunctions = !is_debug;
234
235 // Instrumentations
236 PassInstrumentationCallbacks instr_callbacks;
237 StandardInstrumentations std_instrumentations(false);
238 std_instrumentations.registerCallbacks(instr_callbacks);
239
240 PassBuilder pass_builder(false, &target_machine, pipeline_opts,
241 None, &instr_callbacks);
242 using OptimizationLevel = typename PassBuilder::OptimizationLevel;
243
244 LoopAnalysisManager loop_am;
245 FunctionAnalysisManager function_am;
246 CGSCCAnalysisManager cgscc_am;
247 ModuleAnalysisManager module_am;
248
249 // Register the AA manager first so that our version is the one used
250 function_am.registerPass([&] {
251 return pass_builder.buildDefaultAAPipeline();
252 });
253
254 Triple target_triple(module.getTargetTriple());
255 auto tlii = std::make_unique<TargetLibraryInfoImpl>(target_triple);
256 function_am.registerPass([&] { return TargetLibraryAnalysis(*tlii); });
257
258 // Initialize the AnalysisManagers
259 pass_builder.registerModuleAnalyses(module_am);
260 pass_builder.registerCGSCCAnalyses(cgscc_am);
261 pass_builder.registerFunctionAnalyses(function_am);
262 pass_builder.registerLoopAnalyses(loop_am);
263 pass_builder.crossRegisterProxies(loop_am, function_am,
264 cgscc_am, module_am);
265
266 // IR verification
267 if (assertions_on) {
268 // Verify the input
269 pass_builder.registerPipelineStartEPCallback(
270 [](ModulePassManager &module_pm, OptimizationLevel OL) {
271 module_pm.addPass(VerifierPass());
272 });
273 // Verify the output
274 pass_builder.registerOptimizerLastEPCallback(
275 [](ModulePassManager &module_pm, OptimizationLevel OL) {
276 module_pm.addPass(VerifierPass());
277 });
227278 }
228 PMBuilder->OptLevel = target_machine->getOptLevel();
229 PMBuilder->SizeLevel = is_small ? 2 : 0;
230
231 PMBuilder->DisableTailCalls = is_debug;
232 PMBuilder->DisableUnrollLoops = is_debug;
233 PMBuilder->SLPVectorize = !is_debug;
234 PMBuilder->LoopVectorize = !is_debug;
235 PMBuilder->LoopsInterleaved = !PMBuilder->DisableUnrollLoops;
236 PMBuilder->RerollLoops = !is_debug;
237 // Leaving NewGVN as default (off) because when on it caused issue #673
238 //PMBuilder->NewGVN = !is_debug;
239 PMBuilder->DisableGVNLoadPRE = is_debug;
240 PMBuilder->VerifyInput = assertions_on;
241 PMBuilder->VerifyOutput = assertions_on;
242 PMBuilder->MergeFunctions = !is_debug;
243 PMBuilder->PrepareForLTO = lto;
244 PMBuilder->PrepareForThinLTO = false;
245 PMBuilder->PerformThinLTO = false;
246
247 TargetLibraryInfoImpl tlii(Triple(module->getTargetTriple()));
248 PMBuilder->LibraryInfo = &tlii;
249
250 if (is_debug) {
251 PMBuilder->Inliner = createAlwaysInlinerLegacyPass(false);
252 } else {
253 target_machine->adjustPassManager(*PMBuilder);
254279
255 PMBuilder->addExtension(PassManagerBuilder::EP_EarlyAsPossible, addDiscriminatorsPass);
256 PMBuilder->Inliner = createFunctionInliningPass(PMBuilder->OptLevel, PMBuilder->SizeLevel, false);
280 // Passes specific for release build
281 if (!is_debug) {
282 pass_builder.registerPipelineStartEPCallback(
283 [](ModulePassManager &module_pm, OptimizationLevel OL) {
284 module_pm.addPass(
285 createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
286 });
257287 }
258288
289 // Thread sanitizer
259290 if (tsan) {
260 PMBuilder->addExtension(PassManagerBuilder::EP_OptimizerLast, addThreadSanitizerPass);
261 PMBuilder->addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, addThreadSanitizerPass);
291 pass_builder.registerOptimizerLastEPCallback(
292 [](ModulePassManager &module_pm, OptimizationLevel level) {
293 module_pm.addPass(ThreadSanitizerPass());
294 });
262295 }
263296
264 // Set up the per-function pass manager.
265 legacy::FunctionPassManager FPM = legacy::FunctionPassManager(module);
266 auto tliwp = new(std::nothrow) TargetLibraryInfoWrapperPass(tlii);
267 FPM.add(tliwp);
268 FPM.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis()));
269 if (assertions_on) {
270 FPM.add(createVerifierPass());
297 ModulePassManager module_pm;
298 OptimizationLevel opt_level;
299 // Setting up the optimization level
300 if (is_debug)
301 opt_level = OptimizationLevel::O0;
302 else if (is_small)
303 opt_level = OptimizationLevel::Oz;
304 else
305 opt_level = OptimizationLevel::O3;
306
307 // Initialize the PassManager
308 if (opt_level == OptimizationLevel::O0) {
309 module_pm = pass_builder.buildO0DefaultPipeline(opt_level, lto);
310 } else if (lto) {
311 module_pm = pass_builder.buildLTOPreLinkDefaultPipeline(opt_level);
312 } else {
313 module_pm = pass_builder.buildPerModuleDefaultPipeline(opt_level);
271314 }
272 PMBuilder->populateFunctionPassManager(FPM);
273
274 {
275 // Set up the per-module pass manager.
276 legacy::PassManager MPM;
277 MPM.add(createTargetTransformInfoWrapperPass(target_machine->getTargetIRAnalysis()));
278 PMBuilder->populateModulePassManager(MPM);
279
280 // Set output passes.
281 if (dest_bin && !lto) {
282 if (target_machine->addPassesToEmitFile(MPM, *dest_bin, nullptr, CGFT_ObjectFile)) {
283 *error_message = strdup("TargetMachine can't emit an object file");
284 return true;
285 }
286 }
287 if (dest_asm) {
288 if (target_machine->addPassesToEmitFile(MPM, *dest_asm, nullptr, CGFT_AssemblyFile)) {
289 *error_message = strdup("TargetMachine can't emit an assembly file");
290 return true;
291 }
292 }
293
294 // run per function optimization passes
295 FPM.doInitialization();
296 for (Function &F : *module)
297 if (!F.isDeclaration())
298 FPM.run(F);
299 FPM.doFinalization();
300315
301 MPM.run(*module);
316 // Unfortunately we don't have new PM for code generation
317 legacy::PassManager codegen_pm;
318 codegen_pm.add(
319 createTargetTransformInfoWrapperPass(target_machine.getTargetIRAnalysis()));
302320
303 if (llvm_ir_filename) {
304 if (LLVMPrintModuleToFile(module_ref, llvm_ir_filename, error_message)) {
305 return true;
306 }
321 if (dest_bin && !lto) {
322 if (target_machine.addPassesToEmitFile(codegen_pm, *dest_bin, nullptr, CGFT_ObjectFile)) {
323 *error_message = strdup("TargetMachine can't emit an object file");
324 return true;
307325 }
308 if (dest_bin && lto) {
309 WriteBitcodeToFile(*module, *dest_bin);
326 }
327 if (dest_asm) {
328 if (target_machine.addPassesToEmitFile(codegen_pm, *dest_asm, nullptr, CGFT_AssemblyFile)) {
329 *error_message = strdup("TargetMachine can't emit an assembly file");
330 return true;
310331 }
332 }
311333
312 if (time_report) {
313 TimerGroup::printAll(errs());
334 // Optimization phase
335 module_pm.run(module, module_am);
336
337 // Code generation phase
338 codegen_pm.run(module);
339
340 if (llvm_ir_filename) {
341 if (LLVMPrintModuleToFile(module_ref, llvm_ir_filename, error_message)) {
342 return true;
314343 }
344 }
315345
316 // MPM goes out of scope and writes to the out streams
346 if (dest_bin && lto) {
347 WriteBitcodeToFile(module, *dest_bin);
317348 }
318349
319 delete dest_asm;
320 delete dest_bin;
350 if (time_report) {
351 TimerGroup::printAll(errs());
352 }
321353
322354 return false;
323355}