|
12 | 12 |
|
13 | 13 | #ifdef AnyDSL_runtime_HAS_LLVM_SUPPORT |
14 | 14 | #include <lld/Common/Driver.h> |
15 | | -#include <llvm/Analysis/TargetTransformInfo.h> |
16 | 15 | #include <llvm/IR/LLVMContext.h> |
17 | 16 | #include <llvm/IR/LegacyPassManager.h> |
18 | 17 | #include <llvm/IR/Module.h> |
19 | 18 | #include <llvm/IRReader/IRReader.h> |
20 | 19 | #include <llvm/Linker/Linker.h> |
21 | 20 | #include <llvm/MC/TargetRegistry.h> |
| 21 | +#include <llvm/Passes/PassBuilder.h> |
22 | 22 | #include <llvm/Support/raw_os_ostream.h> |
23 | 23 | #include <llvm/Support/CommandLine.h> |
24 | 24 | #include <llvm/Support/SourceMgr.h> |
@@ -572,7 +572,7 @@ HSAPlatform::KernelInfo& HSAPlatform::load_kernel(DeviceId dev, const std::strin |
572 | 572 | #define AnyDSL_runtime_HSA_BITCODE_SUFFIX ".bc" |
573 | 573 | #endif |
574 | 574 | bool llvm_amdgpu_initialized = false; |
575 | | -std::string HSAPlatform::emit_gcn(const std::string& program, const std::string& cpu, const std::string &filename, int opt) const { |
| 575 | +std::string HSAPlatform::emit_gcn(const std::string& program, const std::string& cpu, const std::string &filename, llvm::OptimizationLevel OptLevel) const { |
576 | 576 | if (!llvm_amdgpu_initialized) { |
577 | 577 | // ANYDSL_LLVM_ARGS="-amdgpu-sroa -amdgpu-load-store-vectorizer -amdgpu-scalarize-global-loads -amdgpu-internalize-symbols -amdgpu-early-inline-all -amdgpu-sdwa-peephole -amdgpu-dpp-combine -enable-amdgpu-aa -amdgpu-late-structurize=0 -amdgpu-function-calls -amdgpu-simplify-libcall -amdgpu-ir-lower-kernel-arguments -amdgpu-atomic-optimizations -amdgpu-mode-register" |
578 | 578 | const char* env_var = std::getenv("ANYDSL_LLVM_ARGS"); |
@@ -617,7 +617,7 @@ std::string HSAPlatform::emit_gcn(const std::string& program, const std::string& |
617 | 617 | options.AllowFPOpFusion = llvm::FPOpFusion::Fast; |
618 | 618 | options.NoTrappingFPMath = true; |
619 | 619 | std::string attrs = "-trap-handler"; |
620 | | - std::unique_ptr<llvm::TargetMachine> machine(target->createTargetMachine(triple_str, cpu, attrs, options, llvm::Reloc::PIC_, llvm::CodeModel::Small, llvm::CodeGenOpt::Aggressive)); |
| 620 | + llvm::TargetMachine* machine = target->createTargetMachine(triple_str, cpu, attrs, options, llvm::Reloc::PIC_, llvm::CodeModel::Small, llvm::CodeGenOpt::Aggressive); |
621 | 621 |
|
622 | 622 | // link ocml.amdgcn and ocml config |
623 | 623 | if (cpu.compare(0, 3, "gfx")) |
@@ -666,30 +666,30 @@ std::string HSAPlatform::emit_gcn(const std::string& program, const std::string& |
666 | 666 | error("Can't link config into module"); |
667 | 667 |
|
668 | 668 | auto run_pass_manager = [&] (std::unique_ptr<llvm::Module> module, llvm::CodeGenFileType cogen_file_type, std::string out_filename, bool print_ir=false) { |
669 | | - llvm::legacy::FunctionPassManager function_pass_manager(module.get()); |
670 | | - llvm::legacy::PassManager module_pass_manager; |
| 669 | + machine->Options.MCOptions.AsmVerbose = true; |
671 | 670 |
|
672 | | - module_pass_manager.add(llvm::createTargetTransformInfoWrapperPass(machine->getTargetIRAnalysis())); |
673 | | - function_pass_manager.add(llvm::createTargetTransformInfoWrapperPass(machine->getTargetIRAnalysis())); |
| 671 | + // create the analysis managers |
| 672 | + llvm::LoopAnalysisManager LAM; |
| 673 | + llvm::FunctionAnalysisManager FAM; |
| 674 | + llvm::CGSCCAnalysisManager CGAM; |
| 675 | + llvm::ModuleAnalysisManager MAM; |
674 | 676 |
|
675 | | - llvm::PassManagerBuilder builder; |
676 | | - builder.OptLevel = opt; |
677 | | - builder.Inliner = llvm::createFunctionInliningPass(builder.OptLevel, 0, false); |
678 | | - machine->adjustPassManager(builder); |
679 | | - builder.populateFunctionPassManager(function_pass_manager); |
680 | | - builder.populateModulePassManager(module_pass_manager); |
| 677 | + llvm::PassBuilder PB(machine); |
681 | 678 |
|
682 | | - machine->Options.MCOptions.AsmVerbose = true; |
| 679 | + PB.registerModuleAnalyses(MAM); |
| 680 | + PB.registerCGSCCAnalyses(CGAM); |
| 681 | + PB.registerFunctionAnalyses(FAM); |
| 682 | + PB.registerLoopAnalyses(LAM); |
| 683 | + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); |
| 684 | + |
| 685 | + llvm::ModulePassManager MPM = PB.buildPerModuleDefaultPipeline(OptLevel); |
683 | 686 |
|
| 687 | + MPM.run(*module, MAM); |
| 688 | + |
| 689 | + llvm::legacy::PassManager module_pass_manager; |
684 | 690 | llvm::SmallString<0> outstr; |
685 | 691 | llvm::raw_svector_ostream llvm_stream(outstr); |
686 | | - |
687 | 692 | machine->addPassesToEmitFile(module_pass_manager, llvm_stream, nullptr, cogen_file_type, true); |
688 | | - |
689 | | - function_pass_manager.doInitialization(); |
690 | | - for (auto func = module->begin(); func != module->end(); ++func) |
691 | | - function_pass_manager.run(*func); |
692 | | - function_pass_manager.doFinalization(); |
693 | 693 | module_pass_manager.run(*module); |
694 | 694 |
|
695 | 695 | if (print_ir) { |
@@ -726,14 +726,14 @@ std::string HSAPlatform::emit_gcn(const std::string& program, const std::string& |
726 | 726 | return runtime_->load_file(gcn_file); |
727 | 727 | } |
728 | 728 | #else |
729 | | -std::string HSAPlatform::emit_gcn(const std::string&, const std::string&, const std::string &, int) const { |
| 729 | +std::string HSAPlatform::emit_gcn(const std::string&, const std::string&, const std::string &, llvm::OptimizationLevel) const { |
730 | 730 | error("Recompile runtime with LLVM enabled for gcn support."); |
731 | 731 | } |
732 | 732 | #endif |
733 | 733 |
|
734 | 734 | std::string HSAPlatform::compile_gcn(DeviceId dev, const std::string& filename, const std::string& program_string) const { |
735 | 735 | debug("Compiling AMDGPU to GCN using amdgpu for '%' on HSA device %", filename, dev); |
736 | | - return emit_gcn(program_string, devices_[dev].isa, filename, 3); |
| 736 | + return emit_gcn(program_string, devices_[dev].isa, filename, llvm::OptimizationLevel::O3); |
737 | 737 | } |
738 | 738 |
|
739 | 739 | const char* HSAPlatform::device_name(DeviceId dev) const { |
|
0 commit comments