From 4799c6ce50eca8cd1e8a5ff27f8bace6b3f77f5f Mon Sep 17 00:00:00 2001 From: Ehsan Date: Mon, 25 Jun 2018 20:15:54 -0400 Subject: [PATCH] [linux-port] Enable some code. Disable some code. (#1372) Some code originally in llvm/clang was commented out, and was replaced with Windows-specific implementation. In this change we bring back some of those. Also disabled some code that is not relevant any more. And a few minor improvements towards cross-platform compilation. --- include/dxc/Support/HLSLOptions.h | 1 + include/dxc/dxcisense.h | 4 +- include/llvm/PassRegistry.h | 5 ++- include/llvm/Support/Mutex.h | 6 +-- lib/DxcSupport/HLSLOptions.cpp | 11 ++++++ lib/DxcSupport/dxcapi.use.cpp | 9 ++++- lib/IR/PassRegistry.cpp | 38 +++++++++++++++---- lib/Support/ErrorHandling.cpp | 6 +-- lib/Support/assert.cpp | 13 +++++++ .../include/clang/Frontend/FrontendActions.h | 2 + tools/clang/lib/Frontend/FrontendActions.cpp | 3 +- .../clang/tools/dxcompiler/dxcfilesystem.cpp | 13 ++++++- .../clang/tools/libclang/dxcrewriteunused.cpp | 7 +--- 13 files changed, 93 insertions(+), 25 deletions(-) diff --git a/include/dxc/Support/HLSLOptions.h b/include/dxc/Support/HLSLOptions.h index d370053f9..650bdb73c 100644 --- a/include/dxc/Support/HLSLOptions.h +++ b/include/dxc/Support/HLSLOptions.h @@ -186,6 +186,7 @@ public: MainArgs() = default; MainArgs(int argc, const wchar_t **argv, int skipArgCount = 1); + MainArgs(int argc, const char **argv, int skipArgCount = 1); MainArgs(llvm::ArrayRef args); MainArgs& operator=(const MainArgs &other); llvm::ArrayRef getArrayRef() const { diff --git a/include/dxc/dxcisense.h b/include/dxc/dxcisense.h index 036044627..271adf24e 100644 --- a/include/dxc/dxcisense.h +++ b/include/dxc/dxcisense.h @@ -134,8 +134,8 @@ typedef enum DxcDiagnosticDisplayOptions // Display the category name associated with this diagnostic, if any. DxcDiagnostic_DisplayCategoryName = 0x20, - // Display the severity of the diagnostic message. - DxcDiagnostic_DisplaySeverity = 0x200 + // Display the severity of the diagnostic message. + DxcDiagnostic_DisplaySeverity = 0x200 } DxcDiagnosticDisplayOptions; typedef enum DxcTranslationUnitFlags diff --git a/include/llvm/PassRegistry.h b/include/llvm/PassRegistry.h index 72b4be89b..a4201fddd 100644 --- a/include/llvm/PassRegistry.h +++ b/include/llvm/PassRegistry.h @@ -39,7 +39,10 @@ struct PassRegistrationListener; /// threads simultaneously, you will need to use a separate PassRegistry on /// each thread. class PassRegistry { - //mutable sys::SmartRWMutex Lock; // HLSL Change - no lock needed + #ifndef LLVM_ON_WIN32 + // HLSL Change - no lock needed for Windows, as it will use its own mechanism defined in PassRegistry.cpp. + mutable sys::SmartRWMutex Lock; + #endif /// PassInfoMap - Keep track of the PassInfo object for each registered pass. typedef DenseMap MapType; diff --git a/include/llvm/Support/Mutex.h b/include/llvm/Support/Mutex.h index fefa3fca5..158f48fe3 100644 --- a/include/llvm/Support/Mutex.h +++ b/include/llvm/Support/Mutex.h @@ -71,10 +71,10 @@ namespace llvm /// @{ private: #if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0 -#if 0 // HLSL Change - void* data_; ///< We don't know what the data will be -#else +#if LLVM_ON_WIN32 // HLSL Change char data_[sizeof(void*) == 8 ? 40 : 24]; // C_ASSERT this is CRITICAL_SECTION-sized +#else + void* data_; ///< We don't know what the data will be #endif // HLSL Change #endif diff --git a/lib/DxcSupport/HLSLOptions.cpp b/lib/DxcSupport/HLSLOptions.cpp index 0a5b1c25c..d1adbe16b 100644 --- a/lib/DxcSupport/HLSLOptions.cpp +++ b/lib/DxcSupport/HLSLOptions.cpp @@ -147,6 +147,17 @@ MainArgs::MainArgs(int argc, const wchar_t **argv, int skipArgCount) { } } +MainArgs::MainArgs(int argc, const char **argv, int skipArgCount) { + if (argc > skipArgCount) { + Utf8StringVector.reserve(argc - skipArgCount); + Utf8CharPtrVector.reserve(argc - skipArgCount); + for (int i = skipArgCount; i < argc; ++i) { + Utf8StringVector.emplace_back(argv[i]); + Utf8CharPtrVector.push_back(Utf8StringVector.back().data()); + } + } +} + MainArgs::MainArgs(llvm::ArrayRef args) { Utf8StringVector.reserve(args.size()); Utf8CharPtrVector.reserve(args.size()); diff --git a/lib/DxcSupport/dxcapi.use.cpp b/lib/DxcSupport/dxcapi.use.cpp index e32afdba2..bea683346 100644 --- a/lib/DxcSupport/dxcapi.use.cpp +++ b/lib/DxcSupport/dxcapi.use.cpp @@ -13,6 +13,7 @@ #include "dxc/Support/dxcapi.use.h" #include "dxc/Support/Global.h" #include "dxc/Support/Unicode.h" +#include "dxc/Support/WinFunctions.h" namespace dxc { @@ -37,6 +38,12 @@ static std::string GetWin32ErrorMessage(DWORD err) { } return std::string(); } +#else +static std::string GetWin32ErrorMessage(DWORD err) { + // Since we use errno for handling messages, we use strerror to get the error + // message. + return std::string(std::strerror(err)); +} #endif // _WIN32 void IFT_Data(HRESULT hr, LPCWSTR data) { @@ -160,7 +167,7 @@ void WriteUtf8ToConsoleSizeT(_In_opt_count_(charCount) const char *pText, return; } - int charCountInt; + int charCountInt = 0; IFT(SizeTToInt(charCount, &charCountInt)); WriteUtf8ToConsole(pText, charCountInt, streamType); } diff --git a/lib/IR/PassRegistry.cpp b/lib/IR/PassRegistry.cpp index 75a7a298c..b77d2bf36 100644 --- a/lib/IR/PassRegistry.cpp +++ b/lib/IR/PassRegistry.cpp @@ -22,6 +22,7 @@ using namespace llvm; +#ifdef LLVM_ON_WIN32 // HLSL Change Starts - managed statics are tied to DLL lifetime // Passes exist only in dxcompiler.dll or in a tool like opt that is updated. // @@ -43,6 +44,7 @@ static void CheckThreadId() { "else updating PassRegistry from incorrect thread"); } // HLSL Change Ends +#endif // FIXME: We use ManagedStatic to erase the pass registrar on shutdown. // Unfortunately, passes are registered with static ctors, and having @@ -61,13 +63,17 @@ PassRegistry *PassRegistry::getPassRegistry() { PassRegistry::~PassRegistry() {} const PassInfo *PassRegistry::getPassInfo(const void *TI) const { - // sys::SmartScopedReader Guard(Lock); // HLSL Change + #ifndef LLVM_ON_WIN32 // HLSL Change + sys::SmartScopedReader Guard(Lock); + #endif MapType::const_iterator I = PassInfoMap.find(TI); return I != PassInfoMap.end() ? I->second : nullptr; } const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const { - // sys::SmartScopedReader Guard(Lock); // HLSL Change + #ifndef LLVM_ON_WIN32 // HLSL Change + sys::SmartScopedReader Guard(Lock); + #endif StringMapType::const_iterator I = PassInfoStringMap.find(Arg); return I != PassInfoStringMap.end() ? I->second : nullptr; } @@ -77,7 +83,11 @@ const PassInfo *PassRegistry::getPassInfo(StringRef Arg) const { // void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) { - CheckThreadId(); // sys::SmartScopedReader Guard(Lock); // HLSL Change + #ifdef LLVM_ON_WIN32 // HLSL Change + CheckThreadId(); + #else + sys::SmartScopedReader Guard(Lock); + #endif bool Inserted = PassInfoMap.insert(std::make_pair(PI.getTypeInfo(), &PI)).second; assert(Inserted && "Pass registered multiple times!"); @@ -93,7 +103,9 @@ void PassRegistry::registerPass(const PassInfo &PI, bool ShouldFree) { } void PassRegistry::enumerateWith(PassRegistrationListener *L) { - // sys::SmartScopedReader Guard(Lock); // HLSL Change + #ifndef LLVM_ON_WIN32 // HLSL Change + sys::SmartScopedReader Guard(Lock); + #endif for (auto PassInfoPair : PassInfoMap) L->passEnumerate(PassInfoPair.second); } @@ -117,7 +129,11 @@ void PassRegistry::registerAnalysisGroup(const void *InterfaceID, assert(ImplementationInfo && "Must register pass before adding to AnalysisGroup!"); - CheckThreadId(); // sys::SmartScopedReader Guard(Lock); // HLSL Change + #ifdef LLVM_ON_WIN32 // HLSL Change + CheckThreadId(); + #else + sys::SmartScopedReader Guard(Lock); + #endif // Make sure we keep track of the fact that the implementation implements // the interface. @@ -140,12 +156,20 @@ void PassRegistry::registerAnalysisGroup(const void *InterfaceID, } void PassRegistry::addRegistrationListener(PassRegistrationListener *L) { - CheckThreadId(); // sys::SmartScopedReader Guard(Lock); // HLSL Change + #ifdef LLVM_ON_WIN32 // HLSL Change + CheckThreadId(); + #else + sys::SmartScopedReader Guard(Lock); + #endif Listeners.push_back(L); } void PassRegistry::removeRegistrationListener(PassRegistrationListener *L) { - CheckThreadId(); // sys::SmartScopedReader Guard(Lock); // HLSL Change + #ifdef LLVM_ON_WIN32 // HLSL Change + CheckThreadId(); + #else + sys::SmartScopedReader Guard(Lock); + #endif auto I = std::find(Listeners.begin(), Listeners.end(), L); Listeners.erase(I); diff --git a/lib/Support/ErrorHandling.cpp b/lib/Support/ErrorHandling.cpp index a939fa758..10239a507 100644 --- a/lib/Support/ErrorHandling.cpp +++ b/lib/Support/ErrorHandling.cpp @@ -87,7 +87,7 @@ void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { handlerData = ErrorHandlerUserData; } -#if 0 // HLSL Change - unwind if necessary, but don't terminate the process +#ifndef LLVM_ON_WIN32 // HLSL Change - unwind if necessary, but don't terminate the process if (handler) { handler(handlerData, Reason.str(), GenCrashDiag); } else { @@ -98,7 +98,7 @@ void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) { raw_svector_ostream OS(Buffer); OS << "LLVM ERROR: " << Reason << "\n"; StringRef MessageStr = OS.str(); - ssize_t written = ::_write(2, MessageStr.data(), MessageStr.size()); + ssize_t written = ::write(2, MessageStr.data(), MessageStr.size()); (void)written; // If something went wrong, we deliberately just give up. } @@ -127,7 +127,7 @@ void llvm::llvm_unreachable_internal(const char *msg, const char *file, if (file) dbgs() << " at " << file << ":" << line; dbgs() << "!\n"; -#if 0 // HLSL Change - unwind if necessary, but don't terminate the process +#ifndef LLVM_ON_WIN32 // HLSL Change - unwind if necessary, but don't terminate the process abort(); #else RaiseException(STATUS_LLVM_UNREACHABLE, 0, 0, 0); diff --git a/lib/Support/assert.cpp b/lib/Support/assert.cpp index 2d1acba3e..13e0263d1 100644 --- a/lib/Support/assert.cpp +++ b/lib/Support/assert.cpp @@ -8,6 +8,9 @@ /////////////////////////////////////////////////////////////////////////////// #include "assert.h" + +#ifdef _WIN32 + #include "windows.h" #include "dxc/Support/Global.h" @@ -18,3 +21,13 @@ void llvm_assert(_In_z_ const char *_Message, OutputDebugFormatA("Error: assert(%s)\nFile:\n%s(%d)\nFunc:\t%s\n", _Message, _File, _Line, _Function); RaiseException(STATUS_LLVM_ASSERT, 0, 0, 0); } + +#else + +#include + +void llvm_assert(const char* message, const char*, unsigned) { + assert(false && message); +} + +#endif diff --git a/tools/clang/include/clang/Frontend/FrontendActions.h b/tools/clang/include/clang/Frontend/FrontendActions.h index bc5324dec..741a6a689 100644 --- a/tools/clang/include/clang/Frontend/FrontendActions.h +++ b/tools/clang/include/clang/Frontend/FrontendActions.h @@ -137,6 +137,7 @@ public: bool hasCodeCompletionSupport() const override { return true; } }; +#if 0 // HLSL change - no support for modules or PCH /// \brief Dump information about the given module file, to be used for /// basic debugging and discovery. class DumpModuleInfoAction : public ASTFrontendAction { @@ -162,6 +163,7 @@ protected: public: bool hasCodeCompletionSupport() const override { return false; } }; +#endif // HLSL changes /** * \brief Frontend action adaptor that merges ASTs together. diff --git a/tools/clang/lib/Frontend/FrontendActions.cpp b/tools/clang/lib/Frontend/FrontendActions.cpp index 0fa13e876..536bef784 100644 --- a/tools/clang/lib/Frontend/FrontendActions.cpp +++ b/tools/clang/lib/Frontend/FrontendActions.cpp @@ -408,6 +408,7 @@ SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { return llvm::make_unique(); } +#if 0 // HLSL Change Starts - no support for modules or PCH std::unique_ptr DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { @@ -419,8 +420,6 @@ VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { return llvm::make_unique(); } -#if 0 // HLSL Change Starts - no support for modules or PCH - void VerifyPCHAction::ExecuteAction() { CompilerInstance &CI = getCompilerInstance(); bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; diff --git a/tools/clang/tools/dxcompiler/dxcfilesystem.cpp b/tools/clang/tools/dxcompiler/dxcfilesystem.cpp index c531c32a4..5bd447515 100644 --- a/tools/clang/tools/dxcompiler/dxcfilesystem.cpp +++ b/tools/clang/tools/dxcompiler/dxcfilesystem.cpp @@ -37,6 +37,7 @@ namespace { #endif #endif +#ifdef _WIN32 #ifdef DBG // This should be improved with global enabled mask rather than a compile-time mask. @@ -56,6 +57,10 @@ namespace { #define DXTRACE_FMT_APIFS(...) #endif // DBG +#else // _WIN32 +#define DXTRACE_FMT_APIFS(...) +#endif // _WIN32 + enum class HandleKind { @@ -138,6 +143,13 @@ bool IsAbsoluteOrCurDirRelativeW(LPCWSTR Path) { return Path[1] == L'\\'; } + #ifndef _WIN32 + // Absolute paths on unix systems start with '/' + if (Path[0] == L'/') { + return TRUE; + } + #endif + // // NOTE: there are a number of cases we don't handle, as they don't play well with the simple // file system abstraction we use: @@ -470,7 +482,6 @@ public: lpFileInformation->nFileIndexHigh = 1; return TRUE; } - SetLastError(ERROR_INVALID_HANDLE); return FALSE; } diff --git a/tools/clang/tools/libclang/dxcrewriteunused.cpp b/tools/clang/tools/libclang/dxcrewriteunused.cpp index 3c7369745..1ed649598 100644 --- a/tools/clang/tools/libclang/dxcrewriteunused.cpp +++ b/tools/clang/tools/libclang/dxcrewriteunused.cpp @@ -100,7 +100,7 @@ static void raw_string_ostream_to_CoString(raw_string_ostream &o, _Outptr_result *pResult = (LPSTR)CoTaskMemAlloc(s.size() + 1); if (*pResult == nullptr) throw std::bad_alloc(); - strcpy_s(*pResult, s.size() + 1, s.c_str()); + strncpy(*pResult, s.c_str(), s.size() + 1); } static @@ -574,7 +574,6 @@ public: ::llvm::sys::fs::MSFileSystem* msfPtr; IFT(CreateMSFileSystemForDisk(&msfPtr)); std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr); - ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get()); IFTLLVM(pts.error_code()); @@ -617,7 +616,6 @@ public: ::llvm::sys::fs::MSFileSystem* msfPtr; IFT(CreateMSFileSystemForDisk(&msfPtr)); std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr); - ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get()); IFTLLVM(pts.error_code()); @@ -666,7 +664,6 @@ public: try { dxcutil::DxcArgsFileSystem *msfPtr = dxcutil::CreateDxcArgsFileSystem(utf8Source, pSourceName, pIncludeHandler); std::unique_ptr<::llvm::sys::fs::MSFileSystem> msf(msfPtr); - ::llvm::sys::fs::AutoPerThreadSystem pts(msf.get()); IFTLLVM(pts.error_code()); @@ -698,7 +695,7 @@ public: defineStr += "#define "; defineStr += utf8Name; defineStr += " "; - defineStr += utf8Value ? utf8Value : "1"; + defineStr += utf8Value ? utf8Value.m_psz : "1"; defineStr += "\n"; }