authorgravatar for minyihh@uci.eduMin-Yih Hsu <minyihh@uci.edu> 2021-04-05 13:26:15-07:00
committergravatar for minyihh@uci.eduMin-Yih Hsu <minyihh@uci.edu> 2021-04-09 16:51:35-07:00
log6b3eaa62e86d2652cf0b0802d704ab16d148c2ce
tree3e150f815fea01fc581b92aa9beb306d71d6b386
parent000c0845b1ac30fe57690d71cb84847b42ad762a

Revert "Revert back to the old LLVM PassManager"

This reverts commit 09008125e7944ae01bb907f2eb8dbff41d7a0715.

1 files changed, 135 insertions(+), 96 deletions(-)

src/zig_llvm.cpp+135-96
......@@ -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,154 @@ 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;
249279
280 // Passes for either debug or release build
250281 if (is_debug) {
251 PMBuilder->Inliner = createAlwaysInlinerLegacyPass(false);
282 // NOTE: Always inliner will go away (in debug build)
283 // when the self-hosted compiler becomes mature.
284 pass_builder.registerPipelineStartEPCallback(
285 [](ModulePassManager &module_pm, OptimizationLevel OL) {
286 module_pm.addPass(AlwaysInlinerPass());
287 });
252288 } else {
253 target_machine->adjustPassManager(*PMBuilder);
254
255 PMBuilder->addExtension(PassManagerBuilder::EP_EarlyAsPossible, addDiscriminatorsPass);
256 PMBuilder->Inliner = createFunctionInliningPass(PMBuilder->OptLevel, PMBuilder->SizeLevel, false);
289 pass_builder.registerPipelineStartEPCallback(
290 [](ModulePassManager &module_pm, OptimizationLevel OL) {
291 module_pm.addPass(
292 createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
293 });
257294 }
258295
296 // Thread sanitizer
259297 if (tsan) {
260 PMBuilder->addExtension(PassManagerBuilder::EP_OptimizerLast, addThreadSanitizerPass);
261 PMBuilder->addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0, addThreadSanitizerPass);
298 pass_builder.registerOptimizerLastEPCallback(
299 [](ModulePassManager &module_pm, OptimizationLevel level) {
300 module_pm.addPass(ThreadSanitizerPass());
301 });
262302 }
263303
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());
304 ModulePassManager module_pm;
305 OptimizationLevel opt_level;
306 // Setting up the optimization level
307 if (is_debug)
308 opt_level = OptimizationLevel::O0;
309 else if (is_small)
310 opt_level = OptimizationLevel::Oz;
311 else
312 opt_level = OptimizationLevel::O3;
313
314 // Initialize the PassManager
315 if (lto) {
316 module_pm = pass_builder.buildLTOPreLinkDefaultPipeline(opt_level);
317 module_pm.addPass(CanonicalizeAliasesPass());
318 module_pm.addPass(NameAnonGlobalPass());
319 } else {
320 module_pm = pass_builder.buildPerModuleDefaultPipeline(opt_level);
271321 }
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();
300322
301 MPM.run(*module);
323 // Unfortunately we don't have new PM for code generation
324 legacy::PassManager codegen_pm;
325 codegen_pm.add(
326 createTargetTransformInfoWrapperPass(target_machine.getTargetIRAnalysis()));
302327
303 if (llvm_ir_filename) {
304 if (LLVMPrintModuleToFile(module_ref, llvm_ir_filename, error_message)) {
305 return true;
306 }
328 if (dest_bin && !lto) {
329 if (target_machine.addPassesToEmitFile(codegen_pm, *dest_bin, nullptr, CGFT_ObjectFile)) {
330 *error_message = strdup("TargetMachine can't emit an object file");
331 return true;
307332 }
308 if (dest_bin && lto) {
309 WriteBitcodeToFile(*module, *dest_bin);
333 }
334 if (dest_asm) {
335 if (target_machine.addPassesToEmitFile(codegen_pm, *dest_asm, nullptr, CGFT_AssemblyFile)) {
336 *error_message = strdup("TargetMachine can't emit an assembly file");
337 return true;
310338 }
339 }
311340
312 if (time_report) {
313 TimerGroup::printAll(errs());
341 // Optimization phase
342 module_pm.run(module, module_am);
343
344 // Code generation phase
345 codegen_pm.run(module);
346
347 if (llvm_ir_filename) {
348 if (LLVMPrintModuleToFile(module_ref, llvm_ir_filename, error_message)) {
349 return true;
314350 }
351 }
315352
316 // MPM goes out of scope and writes to the out streams
353 if (dest_bin && lto) {
354 WriteBitcodeToFile(module, *dest_bin);
317355 }
318356
319 delete dest_asm;
320 delete dest_bin;
357 if (time_report) {
358 TimerGroup::printAll(errs());
359 }
321360
322361 return false;
323362}