[linux-port] Enables the DXC main on Unix. (#1384)

This commit is contained in:
Ehsan 2018-06-27 15:00:26 -04:00 коммит произвёл GitHub
Родитель eacaf5a58c
Коммит e8d5eb0111
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 50 добавлений и 7 удалений

Просмотреть файл

@ -44,6 +44,12 @@
#define CoTaskMemAlloc malloc #define CoTaskMemAlloc malloc
#define CoTaskMemFree free #define CoTaskMemFree free
// Windows-specific heap functions. Use malloc/realloc/free instead.
#define HeapAlloc(hHeap,dwFlags, nBytes) malloc(nBytes)
#define HeapReAlloc(hHeap, dwFlags, voidPtr, nBytes) realloc(voidPtr, nBytes)
#define HeapFree(hHeap, dwFlags, voidPtr) free(voidPtr)
#define HeapCreate(flags, nBytes, maxSize) malloc(nBytes)
#define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0])) #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
#define _countof(a) (sizeof(a) / sizeof(*(a))) #define _countof(a) (sizeof(a) / sizeof(*(a)))

Просмотреть файл

@ -15,6 +15,7 @@
#endif #endif
#include "dxc/Support/WinIncludes.h" #include "dxc/Support/WinIncludes.h"
#include "dxc/Support/WinFunctions.h"
#include "llvm/Support/ThreadLocal.h" #include "llvm/Support/ThreadLocal.h"
#include <memory> #include <memory>

Просмотреть файл

@ -40,6 +40,7 @@
#include "dxc/Support/Global.h" #include "dxc/Support/Global.h"
#include "dxc/Support/Unicode.h" #include "dxc/Support/Unicode.h"
#include "dxc/Support/WinIncludes.h" #include "dxc/Support/WinIncludes.h"
#include "dxc/Support/WinFunctions.h"
#include <vector> #include <vector>
#include <string> #include <string>
@ -137,12 +138,20 @@ public:
HeapFree(m_Handle, 0, pv); HeapFree(m_Handle, 0, pv);
} }
virtual SIZE_T STDMETHODCALLTYPE GetSize( virtual SIZE_T STDMETHODCALLTYPE GetSize(
/* [annotation][in] */ /* [annotation][in] */
_In_opt_ _Post_writable_byte_size_(return) void *pv) _In_opt_ _Post_writable_byte_size_(return) void *pv)
{ {
#ifdef _WIN32
return HeapSize(m_Handle, 0, pv); return HeapSize(m_Handle, 0, pv);
#else
// Note: There is no way to get the size of the dynamically allocated memory
// from the pointer in Linux. Therefore, we'd need to add a member variable
// to this class to keep track of the size. Not needed yet.
assert(false &&
"Can't get the size of dynamically allocated memory from pointer.");
return 0;
#endif // _WIN32
} }
virtual int STDMETHODCALLTYPE DidAlloc( virtual int STDMETHODCALLTYPE DidAlloc(
@ -161,7 +170,6 @@ public:
inline bool wcseq(LPCWSTR a, LPCWSTR b) { inline bool wcseq(LPCWSTR a, LPCWSTR b) {
return (a == nullptr && b == nullptr) || (a != nullptr && b != nullptr && wcscmp(a, b) == 0); return (a == nullptr && b == nullptr) || (a != nullptr && b != nullptr && wcscmp(a, b) == 0);
} }
inline bool wcsieq(LPCWSTR a, LPCWSTR b) { return _wcsicmp(a, b) == 0; }
using namespace dxc; using namespace dxc;
using namespace llvm::opt; using namespace llvm::opt;
@ -182,9 +190,13 @@ private:
void WriteHeader(IDxcBlobEncoding *pDisassembly, IDxcBlob *pCode, void WriteHeader(IDxcBlobEncoding *pDisassembly, IDxcBlob *pCode,
llvm::Twine &pVariableName, LPCWSTR pPath); llvm::Twine &pVariableName, LPCWSTR pPath);
HRESULT ReadFileIntoPartContent(hlsl::DxilFourCC fourCC, LPCWSTR fileName, IDxcBlob **ppResult); HRESULT ReadFileIntoPartContent(hlsl::DxilFourCC fourCC, LPCWSTR fileName, IDxcBlob **ppResult);
// Dia is only supported on Windows.
#ifdef _WIN32
// TODO : Refactor two functions below. There are duplicate functions in DxcContext in dxa.cpp // TODO : Refactor two functions below. There are duplicate functions in DxcContext in dxa.cpp
HRESULT GetDxcDiaTable(IDxcLibrary *pLibrary, IDxcBlob *pTargetBlob, IDiaTable **ppTable, LPCWSTR tableName); HRESULT GetDxcDiaTable(IDxcLibrary *pLibrary, IDxcBlob *pTargetBlob, IDiaTable **ppTable, LPCWSTR tableName);
#endif // _WIN32
HRESULT FindModuleBlob(hlsl::DxilFourCC fourCC, IDxcBlob *pSource, IDxcLibrary *pLibrary, IDxcBlob **ppTargetBlob); HRESULT FindModuleBlob(hlsl::DxilFourCC fourCC, IDxcBlob *pSource, IDxcLibrary *pLibrary, IDxcBlob **ppTargetBlob);
void ExtractRootSignature(IDxcBlob *pBlob, IDxcBlob **ppResult); void ExtractRootSignature(IDxcBlob *pBlob, IDxcBlob **ppResult);
int VerifyRootSignature(); int VerifyRootSignature();
@ -475,7 +487,6 @@ HRESULT DxcContext::ReadFileIntoPartContent(hlsl::DxilFourCC fourCC, LPCWSTR fil
// Right now IDxcContainerBuilder assumes that we are building a full dxil container, // Right now IDxcContainerBuilder assumes that we are building a full dxil container,
// but we are building a container with only rootsignature part // but we are building a container with only rootsignature part
void DxcContext::ExtractRootSignature(IDxcBlob *pBlob, IDxcBlob **ppResult) { void DxcContext::ExtractRootSignature(IDxcBlob *pBlob, IDxcBlob **ppResult) {
DXASSERT_NOMSG(pBlob != nullptr && ppResult != nullptr); DXASSERT_NOMSG(pBlob != nullptr && ppResult != nullptr);
const hlsl::DxilContainerHeader *pHeader = (hlsl::DxilContainerHeader *)(pBlob->GetBufferPointer()); const hlsl::DxilContainerHeader *pHeader = (hlsl::DxilContainerHeader *)(pBlob->GetBufferPointer());
IFTBOOL(hlsl::IsValidDxilContainer(pHeader, pHeader->ContainerSizeInBytes), DXC_E_CONTAINER_INVALID); IFTBOOL(hlsl::IsValidDxilContainer(pHeader, pHeader->ContainerSizeInBytes), DXC_E_CONTAINER_INVALID);
@ -577,7 +588,14 @@ public:
HRESULT insertIncludeFile(_In_ LPCWSTR pFilename, _In_ IDxcBlobEncoding *pBlob, _In_ UINT32 dataLen) { HRESULT insertIncludeFile(_In_ LPCWSTR pFilename, _In_ IDxcBlobEncoding *pBlob, _In_ UINT32 dataLen) {
try { try {
#ifdef _WIN32
includeFiles.try_emplace(std::wstring(pFilename), pBlob); includeFiles.try_emplace(std::wstring(pFilename), pBlob);
#else
// Note: try_emplace is only available in C++17 on Linux.
// try_emplace does nothing if the key already exists in the map.
if (includeFiles.find(std::wstring(pFilename)) != includeFiles.end())
includeFiles.emplace(std::wstring(pFilename), pBlob);
#endif // _WIN32
} }
CATCH_CPP_RETURN_HRESULT() CATCH_CPP_RETURN_HRESULT()
return S_OK; return S_OK;
@ -597,6 +615,8 @@ public:
}; };
void DxcContext::Recompile(IDxcBlob *pSource, IDxcLibrary *pLibrary, IDxcCompiler *pCompiler, std::vector<LPCWSTR> &args, IDxcOperationResult **ppCompileResult) { void DxcContext::Recompile(IDxcBlob *pSource, IDxcLibrary *pLibrary, IDxcCompiler *pCompiler, std::vector<LPCWSTR> &args, IDxcOperationResult **ppCompileResult) {
// Recompile currently only supported on Windows
#ifdef _WIN32
CComPtr<IDxcBlob> pTargetBlob; CComPtr<IDxcBlob> pTargetBlob;
IFT(FindModuleBlob(hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL, pSource, pLibrary, &pTargetBlob)); IFT(FindModuleBlob(hlsl::DxilFourCC::DFCC_ShaderDebugInfoDXIL, pSource, pLibrary, &pTargetBlob));
// Retrieve necessary data from DIA symbols for recompiling // Retrieve necessary data from DIA symbols for recompiling
@ -747,6 +767,10 @@ void DxcContext::Recompile(IDxcBlob *pSource, IDxcLibrary *pLibrary, IDxcCompile
ConcatArgs.size(), ConcatDefines.data(), ConcatArgs.size(), ConcatDefines.data(),
ConcatDefines.size(), pIncludeHandler, &pResult)); ConcatDefines.size(), pIncludeHandler, &pResult));
*ppCompileResult = pResult.Detach(); *ppCompileResult = pResult.Detach();
#else
assert(false && "Recompile is currently only supported on Windows.");
*ppCompileResult = nullptr;
#endif // _WIN32
} }
int DxcContext::Compile() { int DxcContext::Compile() {
@ -957,6 +981,8 @@ HRESULT DxcContext::FindModuleBlob(hlsl::DxilFourCC fourCC, IDxcBlob *pSource, I
return E_INVALIDARG; return E_INVALIDARG;
} }
// This function is currently only supported on Windows due to usage of IDiaTable.
#ifdef _WIN32
// TODO : There is an identical code in DxaContext in Dxa.cpp. Refactor this function. // TODO : There is an identical code in DxaContext in Dxa.cpp. Refactor this function.
HRESULT DxcContext::GetDxcDiaTable(IDxcLibrary *pLibrary, IDxcBlob *pTargetBlob, IDiaTable **ppTable, LPCWSTR tableName) { HRESULT DxcContext::GetDxcDiaTable(IDxcLibrary *pLibrary, IDxcBlob *pTargetBlob, IDiaTable **ppTable, LPCWSTR tableName) {
if (!pLibrary || !pTargetBlob || !ppTable) if (!pLibrary || !pTargetBlob || !ppTable)
@ -988,8 +1014,10 @@ HRESULT DxcContext::GetDxcDiaTable(IDxcLibrary *pLibrary, IDxcBlob *pTargetBlob,
*ppTable = pTable.Detach(); *ppTable = pTable.Detach();
return S_OK; return S_OK;
} }
#endif // _WIN32
bool GetDLLFileVersionInfo(const char *dllPath, unsigned int *version) { bool GetDLLFileVersionInfo(const char *dllPath, unsigned int *version) {
#ifdef _WIN32
DWORD dwVerHnd = 0; DWORD dwVerHnd = 0;
DWORD size = GetFileVersionInfoSize(dllPath, &dwVerHnd); DWORD size = GetFileVersionInfoSize(dllPath, &dwVerHnd);
if (size == 0) return false; if (size == 0) return false;
@ -1009,6 +1037,11 @@ bool GetDLLFileVersionInfo(const char *dllPath, unsigned int *version) {
} }
} }
return false; return false;
#else
// This function is used to get version information from the DLL file.
// This information in is not available through a Unix interface.
return false;
#endif // _WIN32
} }
// Collects compiler/validator version info // Collects compiler/validator version info
@ -1083,14 +1116,17 @@ void DxcContext::GetCompilerVersionInfo(llvm::raw_string_ostream &OS) {
} }
} }
#ifdef _WIN32
int __cdecl wmain(int argc, const wchar_t **argv_) { int __cdecl wmain(int argc, const wchar_t **argv_) {
#else
int main(int argc, const char **argv_) {
#endif // _WIN32
const char *pStage = "Operation"; const char *pStage = "Operation";
int retVal = 0; int retVal = 0;
if (FAILED(DxcInitThreadMalloc())) return 1; if (FAILED(DxcInitThreadMalloc())) return 1;
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocOrDefault(nullptr);
try { try {
pStage = "Argument processing"; pStage = "Argument processing";
if (initHlslOptTable()) throw std::bad_alloc(); if (initHlslOptTable()) throw std::bad_alloc();
// Parse command line options. // Parse command line options.
@ -1107,7 +1143,7 @@ int __cdecl wmain(int argc, const wchar_t **argv_) {
ReadDxcOpts(optionTable, DxcFlags, argStrings, dxcOpts, errorStream); ReadDxcOpts(optionTable, DxcFlags, argStrings, dxcOpts, errorStream);
errorStream.flush(); errorStream.flush();
if (errorString.size()) { if (errorString.size()) {
fprintf(stderr, "dxc failed : %s", errorString.data()); fprintf(stderr, "dxc failed : %s\n", errorString.data());
} }
if (optResult != 0) { if (optResult != 0) {
return optResult; return optResult;
@ -1126,7 +1162,7 @@ int __cdecl wmain(int argc, const wchar_t **argv_) {
int dllResult = SetupDxcDllSupport(dxcOpts, dxcSupport, dllErrorStream); int dllResult = SetupDxcDllSupport(dxcOpts, dxcSupport, dllErrorStream);
dllErrorStream.flush(); dllErrorStream.flush();
if (dllErrorString.size()) { if (dllErrorString.size()) {
fprintf(stderr, "%s", dllErrorString.data()); fprintf(stderr, "%s\n", dllErrorString.data());
} }
if (dllResult) if (dllResult)
return dllResult; return dllResult;