Skip to content

Commit

Permalink
Fixed pdbutils recompilation failure when the main file is in a relat…
Browse files Browse the repository at this point in the history
…ive path. (microsoft#3486)
  • Loading branch information
adam-yang authored Feb 23, 2021
1 parent 81eb980 commit b63b179
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
3 changes: 3 additions & 0 deletions include/dxc/Support/dxcfilesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "dxc/dxcapi.h"
#include "llvm/Support/MSFileSystem.h"
#include <string>

namespace clang {
class CompilerInstance;
Expand Down Expand Up @@ -46,4 +47,6 @@ DxcArgsFileSystem *
CreateDxcArgsFileSystem(_In_ IDxcBlobUtf8 *pSource, _In_ LPCWSTR pSourceName,
_In_opt_ IDxcIncludeHandler *pIncludeHandler);

void MakeAbsoluteOrCurDirRelativeW(LPCWSTR &Path, std::wstring &PathStorage);

} // namespace dxcutil
7 changes: 4 additions & 3 deletions tools/clang/tools/dxcompiler/dxcfilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ bool IsAbsoluteOrCurDirRelativeW(LPCWSTR Path) {
return FALSE;
}

}

namespace dxcutil {

void MakeAbsoluteOrCurDirRelativeW(LPCWSTR &Path, std::wstring &PathStorage) {
if (IsAbsoluteOrCurDirRelativeW(Path)) {
return;
Expand All @@ -191,9 +195,6 @@ void MakeAbsoluteOrCurDirRelativeW(LPCWSTR &Path, std::wstring &PathStorage) {
}
}

}

namespace dxcutil {
/// File system based on API arguments. Support being added incrementally.
///
/// DxcArgsFileSystem emulates a file system to clang/llvm based on API
Expand Down
6 changes: 5 additions & 1 deletion tools/clang/tools/dxcompiler/dxcpdbutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "dxc/Support/HLSLOptions.h"

#include "dxcshadersourceinfo.h"
#include "dxc/Support/dxcfilesystem.h"

#include <vector>
#include <locale>
Expand Down Expand Up @@ -79,6 +80,8 @@ static HRESULT CopyWstringToBSTR(const std::wstring &str, BSTR *pResult) {
}

static std::wstring NormalizePath(const WCHAR *path) {
std::wstring PathStorage;
dxcutil::MakeAbsoluteOrCurDirRelativeW(path, PathStorage);
std::string FilenameStr8 = Unicode::UTF16ToUTF8StringOrThrow(path);
llvm::SmallString<128> NormalizedPath;
llvm::sys::path::native(FilenameStr8, NormalizedPath);
Expand Down Expand Up @@ -223,7 +226,8 @@ struct PdbRecompilerIncludeHandler : public IDxcIncludeHandler {
return E_POINTER;
*ppIncludeSource = nullptr;

auto it = m_FileMap.find(NormalizePath(pFilename));
std::wstring Filename = NormalizePath(pFilename);
auto it = m_FileMap.find(Filename);
if (it == m_FileMap.end())
return E_FAIL;

Expand Down
53 changes: 53 additions & 0 deletions tools/clang/unittests/HLSL/CompilerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class CompilerTest : public ::testing::Test {
TEST_METHOD(CompileThenTestPdbUtils)
TEST_METHOD(CompileThenTestPdbUtilsStripped)
TEST_METHOD(CompileThenTestPdbUtilsEmptyEntry)
TEST_METHOD(CompileThenTestPdbUtilsRelativePath)
TEST_METHOD(CompileWithRootSignatureThenStripRootSignature)

TEST_METHOD(CompileWhenIncludeThenLoadInvoked)
Expand Down Expand Up @@ -1536,6 +1537,58 @@ TEST_F(CompilerTest, CompileThenTestPdbUtils) {
TestPdbUtils(/*bSlim*/true, /*bSourceInDebugModule*/false, /*strip*/true); // Slim PDB, where source info is stored in its own part, and debug module is NOT present
}

TEST_F(CompilerTest, CompileThenTestPdbUtilsRelativePath) {
std::string main_source = R"x(
#include "helper.h"
cbuffer MyCbuffer : register(b1) {
float4 my_cbuf_foo;
}
[RootSignature("CBV(b1)")]
float4 main() : SV_Target {
return my_cbuf_foo;
}
)x";

CComPtr<IDxcCompiler3> pCompiler;
VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));

DxcBuffer SourceBuf = {};
SourceBuf.Ptr = main_source.c_str();
SourceBuf.Size = main_source.size();
SourceBuf.Encoding = CP_UTF8;

std::vector<const WCHAR *> args;
args.push_back(L"/Tps_6_0");
args.push_back(L"/Zi");
args.push_back(L"/Qsource_only_debug");
args.push_back(L"shaders/Shader.hlsl");

CComPtr<TestIncludeHandler> pInclude;
std::string included_File = "#define ZERO 0";
pInclude = new TestIncludeHandler(m_dllSupport);
pInclude->CallResults.emplace_back(included_File.c_str());

CComPtr<IDxcResult> pResult;
VERIFY_SUCCEEDED(pCompiler->Compile(&SourceBuf, args.data(), args.size(), pInclude, IID_PPV_ARGS(&pResult)));

CComPtr<IDxcBlob> pPdb;
CComPtr<IDxcBlobUtf16> pPdbName;
VERIFY_SUCCEEDED(pResult->GetOutput(DXC_OUT_PDB, IID_PPV_ARGS(&pPdb), &pPdbName));

CComPtr<IDxcPdbUtils> pPdbUtils;
VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcPdbUtils, &pPdbUtils));

VERIFY_SUCCEEDED(pPdbUtils->Load(pPdb));

CComPtr<IDxcBlob> pFullPdb;
VERIFY_SUCCEEDED(pPdbUtils->GetFullPDB(&pFullPdb));

VERIFY_SUCCEEDED(pPdbUtils->Load(pFullPdb));
VERIFY_IS_TRUE(pPdbUtils->IsFullPDB());
}


TEST_F(CompilerTest, CompileThenTestPdbUtilsEmptyEntry) {
std::string main_source = R"x(
cbuffer MyCbuffer : register(b1) {
Expand Down

0 comments on commit b63b179

Please sign in to comment.