Skip to content

Commit beef306

Browse files
adam-yangtex3d
authored andcommitted
Fixed a bug reading version string from PDB. Implemented IDxcVersionInfo3 for DxcCompiler. (#3570)
1 parent 6599661 commit beef306

4 files changed

Lines changed: 59 additions & 25 deletions

File tree

include/dxc/dxcapi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ struct IDxcVersionInfo2 : public IDxcVersionInfo {
576576
};
577577

578578
CROSS_PLATFORM_UUIDOF(IDxcVersionInfo3, "5e13e843-9d25-473c-9ad2-03b2d0b44b1e")
579-
struct IDxcVersionInfo3 : public IDxcVersionInfo2 {
579+
struct IDxcVersionInfo3 : public IUnknown {
580580
virtual HRESULT STDMETHODCALLTYPE GetCustomVersionString(
581581
_Outptr_result_z_ char **pVersionString // Custom version string for compiler. (Must be CoTaskMemFree()'d!)
582582
) = 0;

tools/clang/tools/dxcompiler/dxcompilerobj.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "dxillib.h"
5252
#include "dxcshadersourceinfo.h"
5353
#include "dxcompileradapter.h"
54+
#include "dxcversion.inc"
5455
#include <algorithm>
5556
#include <cfloat>
5657

@@ -97,6 +98,8 @@ struct CompilerVersionPartWriter {
9798
hlsl::DxilCompilerVersion m_Header = {};
9899
CComHeapPtr<char> m_CommitShaStorage;
99100
llvm::StringRef m_CommitSha = "";
101+
CComHeapPtr<char> m_CustomStringStorage;
102+
llvm::StringRef m_CustomString = "";
100103

101104
void Init(IDxcVersionInfo *pVersionInfo) {
102105
m_Header = {};
@@ -115,7 +118,14 @@ struct CompilerVersionPartWriter {
115118
IFT(pVersionInfo2->GetCommitInfo(&CommitCount, &m_CommitShaStorage));
116119
m_CommitSha = llvm::StringRef(m_CommitShaStorage.m_pData, strlen(m_CommitShaStorage.m_pData));
117120
m_Header.CommitCount = CommitCount;
118-
m_Header.VersionStringListSizeInBytes = m_CommitSha.size() + /*null term*/1 + /*another null term for the empty custom string*/1;
121+
m_Header.VersionStringListSizeInBytes += m_CommitSha.size() + /*null term*/1;
122+
}
123+
124+
CComPtr<IDxcVersionInfo3> pVersionInfo3;
125+
if (SUCCEEDED(pVersionInfo->QueryInterface(&pVersionInfo3))) {
126+
IFT(pVersionInfo3->GetCustomVersionString(&m_CustomStringStorage));
127+
m_CustomString = llvm::StringRef(m_CustomStringStorage, strlen(m_CustomStringStorage.m_pData));
128+
m_Header.VersionStringListSizeInBytes += m_CustomString.size() + /*null term*/1;
119129
}
120130
}
121131

@@ -150,7 +160,9 @@ struct CompilerVersionPartWriter {
150160
// Null terminator for the commit sha
151161
IFT(pStream->Write(&padByte, sizeof(padByte), &cbWritten));
152162

153-
// Null terminator for the empty version string
163+
// Write the custom version string.
164+
IFT(pStream->Write(m_CustomString.data(), m_CustomString.size(), &cbWritten));
165+
// Null terminator for the custom version string.
154166
IFT(pStream->Write(&padByte, sizeof(padByte), &cbWritten));
155167

156168
// Write padding
@@ -537,10 +549,9 @@ static void CreateDefineStrings(
537549
class DxcCompiler : public IDxcCompiler3,
538550
public IDxcLangExtensions2,
539551
public IDxcContainerEvent,
552+
public IDxcVersionInfo3,
540553
#ifdef SUPPORT_QUERY_GIT_COMMIT_INFO
541554
public IDxcVersionInfo2
542-
#else
543-
public IDxcVersionInfo
544555
#endif // SUPPORT_QUERY_GIT_COMMIT_INFO
545556
{
546557
private:
@@ -577,6 +588,7 @@ class DxcCompiler : public IDxcCompiler3,
577588
#ifdef SUPPORT_QUERY_GIT_COMMIT_INFO
578589
,IDxcVersionInfo2
579590
#endif // SUPPORT_QUERY_GIT_COMMIT_INFO
591+
,IDxcVersionInfo3
580592
>
581593
(this, iid, ppvObject);
582594
if (FAILED(hr)) {
@@ -1393,6 +1405,19 @@ class DxcCompiler : public IDxcCompiler3,
13931405
*pMinor = DXIL::kDxilMinor;
13941406
return S_OK;
13951407
}
1408+
HRESULT STDMETHODCALLTYPE GetCustomVersionString(
1409+
_Outptr_result_z_ char **pVersionString // Custom version string for compiler. (Must be CoTaskMemFree()'d!)
1410+
) override
1411+
{
1412+
size_t size = strlen(RC_FILE_VERSION);
1413+
char *const result = (char *)CoTaskMemAlloc(size + 1);
1414+
if (result == nullptr)
1415+
return E_OUTOFMEMORY;
1416+
std::strcpy(result, RC_FILE_VERSION);
1417+
*pVersionString = result;
1418+
return S_OK;
1419+
}
1420+
13961421
#ifdef SUPPORT_QUERY_GIT_COMMIT_INFO
13971422
HRESULT STDMETHODCALLTYPE GetCommitInfo(_Out_ UINT32 *pCommitCount,
13981423
_Out_ char **pCommitHash) override {
@@ -1410,6 +1435,7 @@ class DxcCompiler : public IDxcCompiler3,
14101435
return S_OK;
14111436
}
14121437
#endif // SUPPORT_QUERY_GIT_COMMIT_INFO
1438+
14131439
HRESULT STDMETHODCALLTYPE GetFlags(_Out_ UINT32 *pFlags) override {
14141440
if (pFlags == nullptr)
14151441
return E_INVALIDARG;

tools/clang/tools/dxcompiler/dxcpdbutils.cpp

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ static void ComputeFlagsBasedOnArgs(ArrayRef<std::wstring> args, std::vector<std
144144
}
145145
}
146146

147-
struct DxcPdbVersionInfo : public IDxcVersionInfo3 {
147+
struct DxcPdbVersionInfo :
148+
public IDxcVersionInfo2,
149+
public IDxcVersionInfo3
150+
{
148151
private:
149152
DXC_MICROCOM_TM_REF_FIELDS()
150153

@@ -453,31 +456,34 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
453456
m_HasVersionInfo = true;
454457

455458
const char *ptr = (const char *)(header+1);
456-
unsigned commitShaLength = 0;
457459
unsigned i = 0;
458460

459-
const char *commitSha = (const char *)(header+1) + i;
460-
for (; i < header->VersionStringListSizeInBytes; i++) {
461-
if (ptr[i] == 0) {
462-
commitShaLength = i;
463-
i++;
464-
break;
461+
{
462+
unsigned commitShaLength = 0;
463+
const char *commitSha = (const char *)(header+1) + i;
464+
for (; i < header->VersionStringListSizeInBytes; i++) {
465+
if (ptr[i] == 0) {
466+
i++;
467+
break;
468+
}
469+
commitShaLength++;
465470
}
471+
m_VersionCommitSha.assign(commitSha, commitShaLength);
466472
}
467473

468-
const char *versionString = (const char *)(header+1) + i;
469-
unsigned versionStringLength = 0;
470-
for (; i < header->VersionStringListSizeInBytes; i++) {
471-
if (ptr[i] == 0) {
472-
commitShaLength = i;
473-
i++;
474-
break;
474+
{
475+
const char *versionString = (const char *)(header+1) + i;
476+
unsigned versionStringLength = 0;
477+
for (; i < header->VersionStringListSizeInBytes; i++) {
478+
if (ptr[i] == 0) {
479+
i++;
480+
break;
481+
}
482+
versionStringLength++;
475483
}
484+
m_VersionString.assign(versionString, versionStringLength);
476485
}
477486

478-
m_VersionCommitSha.assign(commitSha, commitShaLength);
479-
m_VersionString.assign(versionString, versionStringLength);
480-
481487
} break;
482488

483489
case hlsl::DFCC_ShaderSourceInfo:

tools/clang/unittests/HLSL/CompilerTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,9 +1052,11 @@ static void VerifyPdbUtil(dxc::DxcDllSupport &dllSupport,
10521052
// IDxcVersionInfo3
10531053
CComHeapPtr<char> VersionString;
10541054
VERIFY_SUCCEEDED(pVersion3->GetCustomVersionString(&VersionString));
1055+
VERIFY_IS_TRUE(VersionString && strlen(VersionString) != 0);
10551056

1056-
CComPtr<IDxcVersionInfo3> pCompilerVersion3;
1057-
if (SUCCEEDED(pCompiler->QueryInterface(&pCompilerVersion3))) {
1057+
{
1058+
CComPtr<IDxcVersionInfo3> pCompilerVersion3;
1059+
VERIFY_SUCCEEDED(pCompiler->QueryInterface(&pCompilerVersion3));
10581060
CComHeapPtr<char> CompilerVersionString;
10591061
VERIFY_SUCCEEDED(pCompilerVersion3->GetCustomVersionString(&CompilerVersionString));
10601062
VERIFY_IS_TRUE(0 == strcmp(CompilerVersionString, VersionString));

0 commit comments

Comments
 (0)