Skip to content

Commit ad955d8

Browse files
adam-yangtex3d
authored andcommitted
Fixed arg pairs not correct for old source in module pdbs (#3599)
(cherry picked from commit e8372b9)
1 parent 5a31765 commit ad955d8

4 files changed

Lines changed: 69 additions & 67 deletions

File tree

include/dxc/Support/FileIOHelper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ HRESULT DxcCreateBlobWithEncodingFromPinned(
190190
_In_bytecount_(size) LPCVOID pText, UINT32 size, UINT32 codePage,
191191
_COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) throw();
192192

193+
HRESULT DxcCreateBlobFromPinned(
194+
_In_bytecount_(size) LPCVOID pText, UINT32 size,
195+
_COM_Outptr_ IDxcBlob **pBlob) throw();
196+
193197
HRESULT
194198
DxcCreateBlobWithEncodingFromStream(
195199
IStream *pStream, bool newInstanceAlways, UINT32 codePage,

lib/DxcSupport/FileIOHelper.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,14 @@ HRESULT DxcCreateBlobWithEncodingFromPinned(LPCVOID pText, UINT32 size,
778778
return DxcCreateBlob(pText, size, true, false, true, codePage, nullptr, pBlobEncoding);
779779
}
780780

781+
HRESULT DxcCreateBlobFromPinned(
782+
_In_bytecount_(size) LPCVOID pText, UINT32 size,
783+
_COM_Outptr_ IDxcBlob **pBlob) throw() {
784+
CComPtr<IDxcBlobEncoding> pBlobEncoding;
785+
DxcCreateBlob(pText, size, true, false, false, CP_ACP, nullptr, &pBlobEncoding);
786+
return pBlobEncoding.QueryInterface(pBlob);
787+
}
788+
781789
_Use_decl_annotations_
782790
HRESULT
783791
DxcCreateBlobWithEncodingFromStream(IStream *pStream, bool newInstanceAlways,

tools/clang/tools/dxcompiler/dxcpdbutils.cpp

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -96,52 +96,31 @@ static bool IsBitcode(const void *ptr, size_t size) {
9696
return !memcmp(ptr, pattern, _countof(pattern));
9797
}
9898

99-
static void ComputeFlagsBasedOnArgs(ArrayRef<std::wstring> args, std::vector<std::wstring> *outFlags, std::vector<std::wstring> *outDefines, std::wstring *outTargetProfile, std::wstring *outEntryPoint) {
99+
static std::vector<std::pair<std::wstring, std::wstring> > ComputeArgPairs(ArrayRef<std::string> args) {
100+
std::vector<std::pair<std::wstring, std::wstring> > ret;
101+
100102
const llvm::opt::OptTable *optionTable = hlsl::options::getHlslOptTable();
101103
assert(optionTable);
102104
if (optionTable) {
103-
std::vector<std::string> argUtf8List;
104-
for (unsigned i = 0; i < args.size(); i++) {
105-
argUtf8List.push_back(ToUtf8String(args[i]));
106-
}
107-
108105
std::vector<const char *> argPointerList;
109-
for (unsigned i = 0; i < argUtf8List.size(); i++) {
110-
argPointerList.push_back(argUtf8List[i].c_str());
106+
for (unsigned i = 0; i < args.size(); i++) {
107+
argPointerList.push_back(args[i].c_str());
111108
}
112109

113110
unsigned missingIndex = 0;
114111
unsigned missingCount = 0;
115112
llvm::opt::InputArgList argList = optionTable->ParseArgs(argPointerList, missingIndex, missingCount);
116113
for (llvm::opt::Arg *arg : argList) {
117-
if (arg->getOption().matches(hlsl::options::OPT_D)) {
118-
std::wstring def = ToWstring(arg->getValue());
119-
if (outDefines)
120-
outDefines->push_back(def);
121-
continue;
122-
}
123-
else if (arg->getOption().matches(hlsl::options::OPT_target_profile)) {
124-
if (outTargetProfile)
125-
*outTargetProfile = ToWstring(arg->getValue());
126-
continue;
127-
}
128-
else if (arg->getOption().matches(hlsl::options::OPT_entrypoint)) {
129-
if (outEntryPoint)
130-
*outEntryPoint = ToWstring(arg->getValue());
131-
continue;
114+
std::pair<std::wstring, std::wstring> newPair;
115+
newPair.first = ToWstring( arg->getOption().getName() );
116+
if (arg->getNumValues() > 0) {
117+
newPair.second = ToWstring( arg->getValue() );
132118
}
133119

134-
if (outFlags) {
135-
llvm::StringRef Name = arg->getOption().getName();
136-
if (Name.size()) {
137-
outFlags->push_back(std::wstring(L"-") + ToWstring(Name));
138-
}
139-
if (arg->getNumValues() > 0) {
140-
outFlags->push_back(ToWstring(arg->getValue()));
141-
}
142-
}
120+
ret.push_back(std::move(newPair));
143121
}
144122
}
123+
return ret;
145124
}
146125

147126
struct DxcPdbVersionInfo :
@@ -246,10 +225,13 @@ struct PdbRecompilerIncludeHandler : public IDxcIncludeHandler {
246225
if (it == m_FileMap.end())
247226
return E_FAIL;
248227

249-
CComPtr<IDxcBlobEncoding> pEncoding;
250-
IFR(m_pPdbUtils->GetSource(it->second, &pEncoding));
228+
CComPtr<IDxcBlobEncoding> pSource;
229+
IFR(m_pPdbUtils->GetSource(it->second, &pSource));
251230

252-
return pEncoding.QueryInterface(ppIncludeSource);
231+
CComPtr<IDxcBlobEncoding> pOutBlob;
232+
IFR(hlsl::DxcCreateBlobEncodingFromBlob(pSource, 0, pSource->GetBufferSize(), /*encoding Known*/true, CP_UTF8, m_pMalloc, &pOutBlob));
233+
234+
return pOutBlob.QueryInterface(ppIncludeSource);
253235
}
254236
};
255237

@@ -260,7 +242,7 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
260242

261243
struct Source_File {
262244
std::wstring Name;
263-
CComPtr<IDxcBlobEncoding> Content;
245+
CComPtr<IDxcBlob> Content;
264246
};
265247

266248
CComPtr<IDxcBlob> m_InputBlob;
@@ -384,26 +366,14 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
384366
file.Name = ToWstring(md_name->getString());
385367

386368
// File content
387-
IFR(hlsl::DxcCreateBlobWithEncodingOnHeapCopy(
369+
IFR(hlsl::DxcCreateBlobOnHeapCopy(
388370
md_content->getString().data(),
389371
md_content->getString().size(),
390-
CP_ACP, // NOTE: ACP instead of UTF8 because it's possible for compiler implementations to
391-
// inject non-UTF8 data here.
392372
&file.Content));
393373

394374
m_SourceFiles.push_back(std::move(file));
395375
}
396376
}
397-
// dx.source.defines
398-
else if (node_name == hlsl::DxilMDHelper::kDxilSourceDefinesMDName ||
399-
node_name == hlsl::DxilMDHelper::kDxilSourceDefinesOldMDName)
400-
{
401-
MDTuple *tup = cast<MDTuple>(node.getOperand(0));
402-
for (unsigned i = 0; i < tup->getNumOperands(); i++) {
403-
StringRef define = cast<MDString>(tup->getOperand(i))->getString();
404-
m_Defines.push_back(ToWstring(define));
405-
}
406-
}
407377
// dx.source.mainFileName
408378
else if (node_name == hlsl::DxilMDHelper::kDxilSourceMainFileNameMDName ||
409379
node_name == hlsl::DxilMDHelper::kDxilSourceMainFileNameOldMDName)
@@ -417,13 +387,20 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
417387
node_name == hlsl::DxilMDHelper::kDxilSourceArgsOldMDName)
418388
{
419389
MDTuple *tup = cast<MDTuple>(node.getOperand(0));
390+
std::vector<std::string> args;
420391
// Args
421392
for (unsigned i = 0; i < tup->getNumOperands(); i++) {
422393
StringRef arg = cast<MDString>(tup->getOperand(i))->getString();
423-
m_Args.push_back(ToWstring(arg));
394+
args.push_back(arg.str());
424395
}
425396

426-
ComputeFlagsBasedOnArgs(m_Args, &m_Flags, nullptr, nullptr, nullptr);
397+
std::vector<std::pair<std::wstring, std::wstring> > Pairs = ComputeArgPairs(args);
398+
for (std::pair<std::wstring, std::wstring> &p : Pairs) {
399+
ArgPair newPair;
400+
newPair.Name = std::move(p.first);
401+
newPair.Value = std::move(p.second);
402+
AddArgPair(std::move(newPair));
403+
}
427404
}
428405
}
429406

@@ -522,11 +499,9 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
522499

523500
Source_File source;
524501
source.Name = ToWstring(source_data.Name);
525-
IFR(hlsl::DxcCreateBlobWithEncodingOnHeapCopy(
502+
IFR(hlsl::DxcCreateBlobOnHeapCopy(
526503
source_data.Content.data(),
527504
source_data.Content.size(),
528-
CP_ACP, // NOTE: ACP instead of UTF8 because it's possible for compiler implementations to
529-
// inject non-UTF8 data here.
530505
&source.Content));
531506

532507
// First file is the main file
@@ -556,8 +531,8 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
556531
{
557532
const hlsl::DxilProgramHeader *program_header = (const hlsl::DxilProgramHeader *)(part+1);
558533

559-
CComPtr<IDxcBlobEncoding> pProgramHeaderBlob;
560-
IFR(hlsl::DxcCreateBlobWithEncodingFromPinned(program_header, program_header->SizeInUint32*sizeof(UINT32), CP_ACP, &pProgramHeaderBlob));
534+
CComPtr<IDxcBlob> pProgramHeaderBlob;
535+
IFR(hlsl::DxcCreateBlobFromPinned(program_header, program_header->SizeInUint32*sizeof(UINT32), &pProgramHeaderBlob));
561536
IFR(pProgramHeaderBlob.QueryInterface(ppDebugProgramBlob));
562537

563538
} break; // hlsl::DFCC_ShaderDebugInfoDXIL
@@ -657,10 +632,11 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
657632
}
658633
// DXIL program header or bitcode
659634
else {
660-
CComPtr<IDxcBlobEncoding> pProgramHeaderBlob;
661-
IFR(hlsl::DxcCreateBlobWithEncodingFromPinned(
635+
CComPtr<IDxcBlob> pProgramHeaderBlob;
636+
IFR(hlsl::DxcCreateBlobFromPinned(
662637
(hlsl::DxilProgramHeader *)pPdbOrDxil->GetBufferPointer(),
663-
pPdbOrDxil->GetBufferSize(), CP_ACP, &pProgramHeaderBlob));
638+
pPdbOrDxil->GetBufferSize(), &pProgramHeaderBlob));
639+
664640
IFR(pProgramHeaderBlob.QueryInterface(&m_pDebugProgramBlob));
665641
IFR(PopulateSourcesFromProgramHeaderOrBitcode(m_pDebugProgramBlob));
666642
}
@@ -812,11 +788,15 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
812788
if (m_pCachedRecompileResult)
813789
return m_pCachedRecompileResult.QueryInterface(ppResult);
814790

791+
DxcThreadMalloc TM(m_pMalloc);
792+
793+
// Fail early if there are no source files.
794+
if (m_SourceFiles.empty())
795+
return E_FAIL;
796+
815797
if (!m_pCompiler)
816798
IFR(DxcCreateInstance2(m_pMalloc, CLSID_DxcCompiler, IID_PPV_ARGS(&m_pCompiler)));
817799

818-
DxcThreadMalloc TM(m_pMalloc);
819-
820800
std::vector<std::wstring> new_args_storage;
821801
for (unsigned i = 0; i < m_ArgPairs.size(); i++) {
822802
std::wstring name = m_ArgPairs[i].Name;
@@ -844,9 +824,6 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
844824
if (m_MainFileName.size())
845825
new_args.push_back(m_MainFileName.c_str());
846826

847-
if (m_SourceFiles.empty())
848-
return E_FAIL;
849-
850827
CComPtr<PdbRecompilerIncludeHandler> pIncludeHandler = CreateOnMalloc<PdbRecompilerIncludeHandler>(m_pMalloc);
851828
if (!pIncludeHandler)
852829
return E_OUTOFMEMORY;
@@ -857,13 +834,12 @@ struct DxcPdbUtils : public IDxcPdbUtils, public IDxcPixDxilDebugInfoFactory
857834
pIncludeHandler->m_FileMap.insert(std::pair<std::wstring, unsigned>(NormalizedName, i));
858835
}
859836

860-
IDxcBlobEncoding *main_file = m_SourceFiles[0].Content;
837+
IDxcBlob *main_file = m_SourceFiles[0].Content;
861838

862839
DxcBuffer source_buf = {};
863840
source_buf.Ptr = main_file->GetBufferPointer();
864841
source_buf.Size = main_file->GetBufferSize();
865-
BOOL bEndodingKnown = FALSE;
866-
IFR(main_file->GetEncoding(&bEndodingKnown, &source_buf.Encoding));
842+
source_buf.Encoding = CP_UTF8;
867843

868844
CComPtr<IDxcResult> pResult;
869845
IFR(m_pCompiler->Compile(&source_buf, new_args.data(), new_args.size(), pIncludeHandler, IID_PPV_ARGS(&m_pCachedRecompileResult)));

tools/clang/unittests/HLSL/CompilerTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,20 @@ static void VerifyPdbUtil(dxc::DxcDllSupport &dllSupport,
12821282
CComPtr<IDxcPixDxilDebugInfo> pDebugInfo;
12831283
VERIFY_SUCCEEDED(pFactory->NewDxcPixDxilDebugInfo(&pDebugInfo));
12841284
VERIFY_ARE_NOT_EQUAL(pDebugInfo, nullptr);
1285+
1286+
// Recompile when it's a full PDB anyway.
1287+
{
1288+
CComPtr<IDxcResult> pResult;
1289+
VERIFY_SUCCEEDED(pPdbUtils->CompileForFullPDB(&pResult));
1290+
1291+
HRESULT compileStatus = S_OK;
1292+
VERIFY_SUCCEEDED(pResult->GetStatus(&compileStatus));
1293+
VERIFY_SUCCEEDED(compileStatus);
1294+
1295+
CComPtr<IDxcBlob> pRecompiledPdbBlob;
1296+
VERIFY_SUCCEEDED(pResult->GetOutput(DXC_OUT_PDB, IID_PPV_ARGS(&pRecompiledPdbBlob), nullptr));
1297+
}
1298+
12851299
}
12861300
else {
12871301
VERIFY_IS_FALSE(pPdbUtils->IsFullPDB());

0 commit comments

Comments
 (0)