[MLIR][GPU] Make the path to ROCm a runtime option

Our current build assumes that the path to ROCm we find at build time
will be the path at which ROCm is located when the built code is
executed. This commit adds a --rocm-path option to SerializeToHsaco,
and removes the HIP dependency that the SerializeToHsaco previously had.

Depends on D114113

(though the dependency is to ensure the diffs apply cleanly and to capture the dependency on D114107)

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D114114
This commit is contained in:
Krzysztof Drewniak 2021-11-18 21:42:42 +00:00
parent 3fcdd182e9
commit 20f79f8caa
2 changed files with 17 additions and 79 deletions

View File

@ -128,35 +128,16 @@ if(MLIR_ENABLE_ROCM_RUNNER)
message(SEND_ERROR "lld is not enabled. Please revise LLVM_ENABLE_PROJECTS")
endif()
# Configure ROCm support.
if (NOT DEFINED ROCM_PATH)
if (NOT DEFINED ENV{ROCM_PATH})
set(ROCM_PATH "/opt/rocm" CACHE PATH "Path to which ROCm has been installed")
else()
set(ROCM_PATH $ENV{ROCM_PATH} CACHE PATH "Path to which ROCm has been installed")
endif()
set(HIP_PATH "${ROCM_PATH}/hip" CACHE PATH " Path to which HIP has been installed")
endif()
set(CMAKE_MODULE_PATH "${HIP_PATH}/cmake" ${CMAKE_MODULE_PATH})
find_package(HIP)
if (NOT HIP_FOUND)
message(SEND_ERROR "Building mlir with ROCm support requires a working ROCm and HIP install")
else()
message(STATUS "ROCm HIP version: ${HIP_VERSION}")
endif()
set(DEFAULT_ROCM_PATH "/opt/rocm" CACHE PATH "Fallback path to search for ROCm installs")
target_compile_definitions(obj.MLIRGPUTransforms
PRIVATE
__HIP_PLATFORM_HCC__
__ROCM_PATH__="${ROCM_PATH}"
__DEFAULT_ROCM_PATH__="${DEFAULT_ROCM_PATH}"
MLIR_GPU_TO_HSACO_PASS_ENABLE=1
)
target_include_directories(obj.MLIRGPUTransforms
PRIVATE
${MLIR_SOURCE_DIR}/../lld/include
${HIP_PATH}/include
${ROCM_PATH}/include
)
target_link_libraries(MLIRGPUOps

View File

@ -30,10 +30,10 @@
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileUtilities.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
@ -44,8 +44,6 @@
#include "lld/Common/Driver.h"
#include "hip/hip_version.h"
#include <mutex>
using namespace mlir;
@ -68,6 +66,9 @@ protected:
llvm::cl::desc("Optimization level for HSACO compilation"),
llvm::cl::init(2)};
Option<std::string> rocmPath{*this, "rocm-path",
llvm::cl::desc("Path to ROCm install")};
/// Adds LLVM optimization passes
LogicalResult optimizeLlvm(llvm::Module &llvmModule,
llvm::TargetMachine &targetMachine) override;
@ -82,66 +83,22 @@ private:
std::unique_ptr<SmallVectorImpl<char>> assembleIsa(const std::string &isa);
std::unique_ptr<std::vector<char>>
createHsaco(const SmallVectorImpl<char> &isaBinary);
std::string getRocmPath();
};
} // namespace
} // end namespace
SerializeToHsacoPass::SerializeToHsacoPass(const SerializeToHsacoPass &other)
: PassWrapper<SerializeToHsacoPass, gpu::SerializeToBlobPass>(other) {}
static std::string getDefaultChip() {
const char kDefaultChip[] = "gfx900";
// Locate rocm_agent_enumerator.
const char kRocmAgentEnumerator[] = "rocm_agent_enumerator";
llvm::ErrorOr<std::string> rocmAgentEnumerator = llvm::sys::findProgramByName(
kRocmAgentEnumerator, {__ROCM_PATH__ "/bin"});
if (!rocmAgentEnumerator) {
llvm::WithColor::warning(llvm::errs())
<< kRocmAgentEnumerator << "couldn't be located under " << __ROCM_PATH__
<< "/bin\n";
return kDefaultChip;
}
/// Get a user-specified path to ROCm
// Tries, in order, the --rocm-path option, the ROCM_PATH environment variable
// and a compile-time default
std::string SerializeToHsacoPass::getRocmPath() {
if (rocmPath.getNumOccurrences() > 0)
return rocmPath.getValue();
// Prepare temp file to hold the outputs.
int tempFd = -1;
SmallString<128> tempFilename;
if (llvm::sys::fs::createTemporaryFile("rocm_agent", "txt", tempFd,
tempFilename)) {
llvm::WithColor::warning(llvm::errs())
<< "temporary file for " << kRocmAgentEnumerator << " creation error\n";
return kDefaultChip;
}
llvm::FileRemover cleanup(tempFilename);
// Invoke rocm_agent_enumerator.
std::string errorMessage;
SmallVector<StringRef, 2> args{"-t", "GPU"};
Optional<StringRef> redirects[3] = {{""}, tempFilename.str(), {""}};
int result =
llvm::sys::ExecuteAndWait(rocmAgentEnumerator.get(), args, llvm::None,
redirects, 0, 0, &errorMessage);
if (result) {
llvm::WithColor::warning(llvm::errs())
<< kRocmAgentEnumerator << " invocation error: " << errorMessage
<< "\n";
return kDefaultChip;
}
// Load and parse the result.
auto gfxIsaList = openInputFile(tempFilename);
if (!gfxIsaList) {
llvm::WithColor::error(llvm::errs())
<< "read ROCm agent list temp file error\n";
return kDefaultChip;
}
for (llvm::line_iterator lines(*gfxIsaList); !lines.is_at_end(); ++lines) {
// Skip the line with content "gfx000".
if (*lines == "gfx000")
continue;
// Use the first ISA version found.
return lines->str();
}
return kDefaultChip;
return __DEFAULT_ROCM_PATH__;
}
// Sets the 'option' to 'value' unless it already has a value.