Author: Michael Liao Date: 2021-01-06T17:00:14-05:00 New Revision: 2a29ce303451375bbf1de7c971296553ef5d9beb
URL: https://github.com/llvm/llvm-project/commit/2a29ce303451375bbf1de7c971296553ef5d9beb DIFF: https://github.com/llvm/llvm-project/commit/2a29ce303451375bbf1de7c971296553ef5d9beb.diff LOG: [hip] Fix HIP version parsing. - Need trimming before parsing major or minor version numbers. This's required due to the different line ending on Windows. - In addition, the integer conversion may fail due to invalid char. Return that parsing function return `true` when the parsing fails. Differential Revision: https://reviews.llvm.org/D93587 Added: Modified: clang/lib/Driver/ToolChains/AMDGPU.cpp clang/lib/Driver/ToolChains/ROCm.h clang/test/Driver/Inputs/rocm/bin/.hipVersion Removed: ################################################################################ diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp index 565a77e07fd8..0971a2da62a3 100644 --- a/clang/lib/Driver/ToolChains/AMDGPU.cpp +++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp @@ -88,23 +88,30 @@ void RocmInstallationDetector::scanLibDevicePath(llvm::StringRef Path) { } } -void RocmInstallationDetector::ParseHIPVersionFile(llvm::StringRef V) { +// Parse and extract version numbers from `.hipVersion`. Return `true` if +// the parsing fails. +bool RocmInstallationDetector::parseHIPVersionFile(llvm::StringRef V) { SmallVector<StringRef, 4> VersionParts; V.split(VersionParts, '\n'); - unsigned Major; - unsigned Minor; + unsigned Major = ~0U; + unsigned Minor = ~0U; for (auto Part : VersionParts) { - auto Splits = Part.split('='); - if (Splits.first == "HIP_VERSION_MAJOR") - Splits.second.getAsInteger(0, Major); - else if (Splits.first == "HIP_VERSION_MINOR") - Splits.second.getAsInteger(0, Minor); - else if (Splits.first == "HIP_VERSION_PATCH") + auto Splits = Part.rtrim().split('='); + if (Splits.first == "HIP_VERSION_MAJOR") { + if (Splits.second.getAsInteger(0, Major)) + return true; + } else if (Splits.first == "HIP_VERSION_MINOR") { + if (Splits.second.getAsInteger(0, Minor)) + return true; + } else if (Splits.first == "HIP_VERSION_PATCH") VersionPatch = Splits.second.str(); } + if (Major == ~0U || Minor == ~0U) + return true; VersionMajorMinor = llvm::VersionTuple(Major, Minor); DetectedVersion = (Twine(Major) + "." + Twine(Minor) + "." + VersionPatch).str(); + return false; } // For candidate specified by --rocm-path we do not do strict check. @@ -290,7 +297,8 @@ void RocmInstallationDetector::detectHIPRuntime() { continue; if (HIPVersionArg.empty() && VersionFile) - ParseHIPVersionFile((*VersionFile)->getBuffer()); + if (parseHIPVersionFile((*VersionFile)->getBuffer())) + continue; HasHIPRuntime = true; return; diff --git a/clang/lib/Driver/ToolChains/ROCm.h b/clang/lib/Driver/ToolChains/ROCm.h index 27c7d8b0ee54..21e62a465d7b 100644 --- a/clang/lib/Driver/ToolChains/ROCm.h +++ b/clang/lib/Driver/ToolChains/ROCm.h @@ -103,7 +103,7 @@ class RocmInstallationDetector { } void scanLibDevicePath(llvm::StringRef Path); - void ParseHIPVersionFile(llvm::StringRef V); + bool parseHIPVersionFile(llvm::StringRef V); SmallVector<Candidate, 4> getInstallationPathCandidates(); public: diff --git a/clang/test/Driver/Inputs/rocm/bin/.hipVersion b/clang/test/Driver/Inputs/rocm/bin/.hipVersion index 48ee6f10c3e4..677293c09139 100644 --- a/clang/test/Driver/Inputs/rocm/bin/.hipVersion +++ b/clang/test/Driver/Inputs/rocm/bin/.hipVersion @@ -1,4 +1,6 @@ # Auto-generated by cmake -HIP_VERSION_MAJOR=3 +# NOTE: The trailing whitespace is added on purpose to verify that these +# whitespaces are trimmed before paring. +HIP_VERSION_MAJOR=3 HIP_VERSION_MINOR=6 HIP_VERSION_PATCH=20214-a2917cd _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits