Skip to content

Commit c0f84ba

Browse files
committed
[COMGR][Cache] Get an input-stable -cuid when compiling hip
When compiling hip, compute the hash of the input files contents use it as the cuid. Otherwise a cuid that is computed from the input file paths (which depend on /tmp/comgr-xxxxx) is generated. This path changes at every run which causes a cache miss since the command-line changes. co-authored by anjenner and jmmartinez Change-Id: I6cbd3248df9c6bb8ac440c7c80ca3cb1dbe6722c
1 parent 441c3bc commit c0f84ba

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

amd/comgr/src/comgr-cache-command.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,14 @@ void addString(CachedCommandAdaptor::HashAlgorithm &H, StringRef S) {
7676
H.update(S);
7777
}
7878

79-
void addFileContents(CachedCommandAdaptor::HashAlgorithm &H, StringRef Buf) {
80-
// this is a workaround temporary paths getting in the output files of the
81-
// different commands in #line directives in preprocessed files, and the
82-
// ModuleID or source_filename in the bitcode.
83-
while (!Buf.empty()) {
84-
std::optional<size_t> ComgrTmpPos = searchComgrTmpModel(Buf);
85-
if (!ComgrTmpPos) {
86-
addString(H, Buf);
87-
break;
88-
}
89-
90-
StringRef ToHash = Buf.substr(0, *ComgrTmpPos);
91-
addString(H, ToHash);
92-
Buf = Buf.substr(ToHash.size() + StringRef("comgr-xxxxxx").size());
93-
}
94-
}
95-
9679
Error addFile(CachedCommandAdaptor::HashAlgorithm &H, StringRef Path) {
9780
auto BufOrError = MemoryBuffer::getFile(Path);
9881
if (std::error_code EC = BufOrError.getError()) {
9982
return errorCodeToError(EC);
10083
}
10184
StringRef Buf = BufOrError.get()->getBuffer();
10285

103-
addFileContents(H, Buf);
86+
CachedCommandAdaptor::addFileContents(H, Buf);
10487

10588
return Error::success();
10689
}
@@ -150,6 +133,24 @@ bool isSourceCodeInput(const driver::InputInfo &II) {
150133
}
151134
} // namespace
152135

136+
void CachedCommandAdaptor::addFileContents(
137+
CachedCommandAdaptor::HashAlgorithm &H, StringRef Buf) {
138+
// this is a workaround temporary paths getting in the output files of the
139+
// different commands in #line directives in preprocessed files, and the
140+
// ModuleID or source_filename in the bitcode.
141+
while (!Buf.empty()) {
142+
std::optional<size_t> ComgrTmpPos = searchComgrTmpModel(Buf);
143+
if (!ComgrTmpPos) {
144+
addString(H, Buf);
145+
break;
146+
}
147+
148+
StringRef ToHash = Buf.substr(0, *ComgrTmpPos);
149+
addString(H, ToHash);
150+
Buf = Buf.substr(ToHash.size() + StringRef("comgr-xxxxxx").size());
151+
}
152+
}
153+
153154
Expected<CachedCommandAdaptor::Identifier>
154155
CachedCommandAdaptor::getIdentifier() const {
155156
CachedCommandAdaptor::HashAlgorithm H;

amd/comgr/src/comgr-cache-command.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class CachedCommandAdaptor {
7171

7272
virtual ~CachedCommandAdaptor() = default;
7373

74+
// helper to work around the comgr-xxxxx string appearing in files
75+
static void addFileContents(HashAlgorithm &H, llvm::StringRef Buf);
76+
7477
protected:
7578
virtual ActionClass getClass() const = 0;
7679
virtual void addOptionsIdentifier(HashAlgorithm &) const = 0;

amd/comgr/src/comgr-compiler.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "clang/Frontend/FrontendDiagnostic.h"
5656
#include "clang/Frontend/TextDiagnosticPrinter.h"
5757
#include "clang/FrontendTool/Utils.h"
58+
#include "llvm/ADT/StringExtras.h"
5859
#include "llvm/Bitcode/BitcodeWriter.h"
5960
#include "llvm/IR/LLVMContext.h"
6061
#include "llvm/IR/Module.h"
@@ -721,6 +722,15 @@ amd_comgr_status_t executeCommand(const Command &Job, raw_ostream &LogS,
721722
return AMD_COMGR_STATUS_SUCCESS;
722723
}
723724

725+
std::string getStableCUID(const DataSet *InSet) {
726+
using Hash = CachedCommandAdaptor::HashAlgorithm;
727+
Hash H;
728+
for (const DataObject *Input : InSet->DataObjects) {
729+
CachedCommandAdaptor::addFileContents(H,
730+
StringRef{Input->Data, Input->Size});
731+
}
732+
return toHex(H.final());
733+
}
724734
} // namespace
725735

726736
amd_comgr_status_t
@@ -1034,6 +1044,10 @@ amd_comgr_status_t AMDGPUCompiler::addCompilationFlags() {
10341044
case AMD_COMGR_LANGUAGE_HIP:
10351045
Args.push_back("hip");
10361046
Args.push_back("--offload-device-only");
1047+
// Pass a cuid that depends on the input files
1048+
// Otherwise, a random (which depends on the /tmp/comgr-xxxxx path) cuid is
1049+
// generated which causes a cache miss on every run.
1050+
Args.push_back(Saver.save("-cuid=" + getStableCUID(InSet)).data());
10371051
break;
10381052
default:
10391053
return AMD_COMGR_STATUS_ERROR_INVALID_ARGUMENT;

0 commit comments

Comments
 (0)