From 20f79f8caa3a333a34021f0028e828f97d79c2a1 Mon Sep 17 00:00:00 2001 From: Krzysztof Drewniak Date: Thu, 18 Nov 2021 21:42:42 +0000 Subject: [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 --- mlir/lib/Dialect/GPU/CMakeLists.txt | 23 +------ .../Dialect/GPU/Transforms/SerializeToHsaco.cpp | 73 +++++----------------- 2 files changed, 17 insertions(+), 79 deletions(-) diff --git a/mlir/lib/Dialect/GPU/CMakeLists.txt b/mlir/lib/Dialect/GPU/CMakeLists.txt index 1243f4e540ab..285bc0f82a27 100644 --- a/mlir/lib/Dialect/GPU/CMakeLists.txt +++ b/mlir/lib/Dialect/GPU/CMakeLists.txt @@ -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 diff --git a/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp b/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp index 16949a70a810..023ac93f7a68 100644 --- a/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp +++ b/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp @@ -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 using namespace mlir; @@ -68,6 +66,9 @@ protected: llvm::cl::desc("Optimization level for HSACO compilation"), llvm::cl::init(2)}; + Option 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> assembleIsa(const std::string &isa); std::unique_ptr> createHsaco(const SmallVectorImpl &isaBinary); + + std::string getRocmPath(); }; -} // namespace +} // end namespace SerializeToHsacoPass::SerializeToHsacoPass(const SerializeToHsacoPass &other) : PassWrapper(other) {} -static std::string getDefaultChip() { - const char kDefaultChip[] = "gfx900"; - - // Locate rocm_agent_enumerator. - const char kRocmAgentEnumerator[] = "rocm_agent_enumerator"; - llvm::ErrorOr 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; - } - // 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 args{"-t", "GPU"}; - Optional 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(); - } +/// 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(); - return kDefaultChip; + return __DEFAULT_ROCM_PATH__; } // Sets the 'option' to 'value' unless it already has a value. -- cgit v1.2.1