Add bitcode writer to AMDGCN hsaco output

This commit is contained in:
Corbin Robeck
2023-08-29 10:07:53 -04:00
committed by Jason Furmanek
parent 320b1029da
commit 007bea9994

View File

@@ -13,6 +13,7 @@
#include "triton/Target/LLVMIR/LLVMIRTranslation.h"
#include "triton/Tools/Sys/GetEnv.hpp"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/IR/IRBuilder.h"
@@ -109,7 +110,8 @@ std::string generate_hsaco(llvm::Module *module, const std::string &triple,
const std::string &proc,
const std::string &features) {
auto machine = initialize_module(module, triple, proc, features);
std::string dump_path = ::triton::tools::getenv("AMDGCN_DUMP_PATH");
// create unique dir for kernel's binary and hsaco
std::error_code ec;
std::string kernel_name_base = "amd_triton_kernel";
@@ -125,9 +127,15 @@ std::string generate_hsaco(llvm::Module *module, const std::string &triple,
std::filesystem::path kernel_dir(unique_dir.data());
std::string kernel_name = kernel_dir.stem();
// Save GCN ISA binary.
std::filesystem::path isa_binary(kernel_name + ".o");
std::string isabin_path = (kernel_dir / isa_binary).string();
std::filesystem::path isa_binary(kernel_name + ".o");
std::string isabin_path;
if (!dump_path.empty())
isabin_path = (dump_path / isa_binary).string();
else
isabin_path = (kernel_dir / isa_binary).string();
std::unique_ptr<llvm::raw_fd_ostream> isabin_fs(
new llvm::raw_fd_ostream(isabin_path, ec, llvm::sys::fs::OF_Text));
if (ec) {
@@ -136,6 +144,23 @@ std::string generate_hsaco(llvm::Module *module, const std::string &triple,
<< ':' << ec.value() << '\n';
}
//Write out bitcode
std::filesystem::path bitcode_filename (kernel_name + ".bc");
std::string bitcode_path;
if (!dump_path.empty())
bitcode_path = (dump_path / bitcode_filename ).string();
else
bitcode_path = (kernel_dir / bitcode_filename ).string();
std::unique_ptr<llvm::raw_fd_ostream> bitecode_fs(
new llvm::raw_fd_ostream(bitcode_path, ec, llvm::sys::fs::OF_Text));
if (ec) {
llvm::errs() << bitcode_path
<< " was not created. error code: " << ec.category().name()
<< ':' << ec.value() << '\n';
}
llvm::WriteBitcodeToFile(*module, *bitecode_fs);
// emit
llvm::legacy::PassManager pass;
machine->addPassesToEmitFile(pass, *isabin_fs, nullptr,