diff --git a/mlir/lib/Dialect/GPU/CMakeLists.txt b/mlir/lib/Dialect/GPU/CMakeLists.txt --- a/mlir/lib/Dialect/GPU/CMakeLists.txt +++ b/mlir/lib/Dialect/GPU/CMakeLists.txt @@ -128,35 +128,16 @@ 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.MLIRGPUOps 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.MLIRGPUOps 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 --- a/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp +++ b/mlir/lib/Dialect/GPU/Transforms/SerializeToHsaco.cpp @@ -28,19 +28,19 @@ #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" #include "llvm/Support/WithColor.h" + #include "llvm/Target/TargetOptions.h" #include "lld/Common/Driver.h" -#include "hip/hip_version.h" - #include using namespace mlir; @@ -49,12 +49,36 @@ class SerializeToHsacoPass : public PassWrapper { public: - SerializeToHsacoPass(StringRef triple, StringRef arch, StringRef features); + // Needed to make options work + SerializeToHsacoPass(); + SerializeToHsacoPass(const SerializeToHsacoPass &other) { + if (other.triple.hasValue()) { + this->triple = other.triple; + } + if (other.chip.hasValue()) { + this->chip = other.chip; + } + if (other.features.hasValue()) { + this->features = other.features; + } + if (other.rocmPath.hasValue()) { + this->rocmPath = other.rocmPath; + } + this->optLevel = other.optLevel; + }; + + SerializeToHsacoPass(StringRef triple, StringRef arch, StringRef features, + int optLevel); + StringRef getArgument() const override { return "gpu-to-hsaco"; } StringRef getDescription() const override { return "Lower GPU kernel function to HSACO binary annotations"; } +protected: + Option rocmPath{*this, "rocm-path", + llvm::cl::desc("Path to ROCm install")}; + private: void getDependentDialects(DialectRegistry ®istry) const override; @@ -62,67 +86,35 @@ std::unique_ptr> serializeISA(const std::string &isa) override; + // Overload to allow linking in device libs + std::unique_ptr + translateToLLVMIR(llvm::LLVMContext &llvmContext) override; + + /// Adds LLVM optimization passes + LogicalResult optimizeLlvm(llvm::Module &llvmModule, + llvm::TargetMachine &targetMachine) override; + std::unique_ptr> assembleIsa(const std::string &isa); std::unique_ptr> createHsaco(const SmallVectorImpl &isaBinary); -}; -} // namespace - -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; - } + std::string getRocmPath(); - // 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; + int optLevel; +}; +} // end namespace + +/// 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(); } - 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(); + if (auto env = llvm::sys::Process::GetEnv("ROCM_PATH")) { + return env.getValue(); } - - return kDefaultChip; + return __DEFAULT_ROCM_PATH__; } // Sets the 'option' to 'value' unless it already has a value.