Skip to content

Commit 7ab5243

Browse files
LLVM 16 changes: HSA platform.
1 parent 4222654 commit 7ab5243

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

src/cuda_platform.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
#include <thread>
1515

1616
#ifdef AnyDSL_runtime_HAS_LLVM_SUPPORT
17-
#include <llvm/Analysis/TargetTransformInfo.h>
1817
#include <llvm/IR/LLVMContext.h>
1918
#include <llvm/IR/LegacyPassManager.h>
20-
#include <llvm/Passes/PassBuilder.h>
2119
#include <llvm/IR/Module.h>
2220
#include <llvm/IRReader/IRReader.h>
2321
#include <llvm/Linker/Linker.h>
2422
#include <llvm/MC/TargetRegistry.h>
23+
#include <llvm/Passes/PassBuilder.h>
2524
#include <llvm/Support/CommandLine.h>
2625
#include <llvm/Support/SourceMgr.h>
2726
#include <llvm/Support/TargetSelect.h>
@@ -451,7 +450,7 @@ static std::string emit_nvptx(const std::string& program, const std::string& lib
451450

452451
machine->Options.MCOptions.AsmVerbose = true;
453452

454-
// Create the analysis managers.
453+
// create the analysis managers
455454
llvm::LoopAnalysisManager LAM;
456455
llvm::FunctionAnalysisManager FAM;
457456
llvm::CGSCCAnalysisManager CGAM;

src/hsa_platform.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
#ifdef AnyDSL_runtime_HAS_LLVM_SUPPORT
1414
#include <lld/Common/Driver.h>
15-
#include <llvm/Analysis/TargetTransformInfo.h>
1615
#include <llvm/IR/LLVMContext.h>
1716
#include <llvm/IR/LegacyPassManager.h>
1817
#include <llvm/IR/Module.h>
1918
#include <llvm/IRReader/IRReader.h>
2019
#include <llvm/Linker/Linker.h>
2120
#include <llvm/MC/TargetRegistry.h>
21+
#include <llvm/Passes/PassBuilder.h>
2222
#include <llvm/Support/raw_os_ostream.h>
2323
#include <llvm/Support/CommandLine.h>
2424
#include <llvm/Support/SourceMgr.h>
@@ -572,7 +572,7 @@ HSAPlatform::KernelInfo& HSAPlatform::load_kernel(DeviceId dev, const std::strin
572572
#define AnyDSL_runtime_HSA_BITCODE_SUFFIX ".bc"
573573
#endif
574574
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 {
576576
if (!llvm_amdgpu_initialized) {
577577
// 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"
578578
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&
617617
options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
618618
options.NoTrappingFPMath = true;
619619
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);
621621

622622
// link ocml.amdgcn and ocml config
623623
if (cpu.compare(0, 3, "gfx"))
@@ -666,30 +666,30 @@ std::string HSAPlatform::emit_gcn(const std::string& program, const std::string&
666666
error("Can't link config into module");
667667

668668
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;
671670

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;
674676

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);
681678

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);
683686

687+
MPM.run(*module, MAM);
688+
689+
llvm::legacy::PassManager module_pass_manager;
684690
llvm::SmallString<0> outstr;
685691
llvm::raw_svector_ostream llvm_stream(outstr);
686-
687692
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();
693693
module_pass_manager.run(*module);
694694

695695
if (print_ir) {
@@ -726,14 +726,14 @@ std::string HSAPlatform::emit_gcn(const std::string& program, const std::string&
726726
return runtime_->load_file(gcn_file);
727727
}
728728
#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 {
730730
error("Recompile runtime with LLVM enabled for gcn support.");
731731
}
732732
#endif
733733

734734
std::string HSAPlatform::compile_gcn(DeviceId dev, const std::string& filename, const std::string& program_string) const {
735735
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);
737737
}
738738

739739
const char* HSAPlatform::device_name(DeviceId dev) const {

src/hsa_platform.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include <hsa.h>
1313
#include <hsa_ext_amd.h>
1414

15+
namespace llvm {
16+
class OptimizationLevel;
17+
}
18+
1519
/// HSA platform. Has the same number of devices as that of the HSA implementation.
1620
class HSAPlatform : public Platform {
1721
public:
@@ -103,7 +107,7 @@ class HSAPlatform : public Platform {
103107
static hsa_status_t iterate_memory_pools_callback(hsa_amd_memory_pool_t, void*);
104108
KernelInfo& load_kernel(DeviceId, const std::string&, const std::string&);
105109
std::string compile_gcn(DeviceId, const std::string&, const std::string&) const;
106-
std::string emit_gcn(const std::string&, const std::string&, const std::string &, int) const;
110+
std::string emit_gcn(const std::string&, const std::string&, const std::string &, llvm::OptimizationLevel) const;
107111
};
108112

109113
#endif

0 commit comments

Comments
 (0)