From 05e94c67399dfb7924aa42f1f3f9a820ac1b16e9 Mon Sep 17 00:00:00 2001
From: Mark Poliakov <mark@alnyan.me>
Date: Mon, 16 Dec 2024 15:39:04 +0200
Subject: [PATCH 2/2] alnyan/yggdrasil: Yggdrasil host support

---
 clang/lib/Basic/Targets.cpp               |  3 +++
 clang/lib/Driver/Driver.cpp               |  7 +++++++
 clang/lib/Driver/ToolChains/Yggdrasil.cpp |  9 +++++++--
 libcxx/include/__config                   |  3 ++-
 libcxx/src/chrono.cpp                     |  2 +-
 lld/Common/Filesystem.cpp                 |  2 +-
 llvm/CMakeLists.txt                       |  2 +-
 llvm/include/llvm/ADT/GenericCycleImpl.h  |  4 ++++
 llvm/include/llvm/Support/FileSystem.h    | 12 ++++++------
 llvm/include/llvm/Support/Program.h       |  2 +-
 llvm/include/llvm/Support/thread.h        |  4 ++--
 llvm/lib/IR/Constants.cpp                 |  6 +++---
 llvm/lib/IR/DebugInfoMetadata.cpp         |  4 ++--
 llvm/lib/IR/Value.cpp                     |  6 +++---
 llvm/lib/Support/CMakeLists.txt           |  4 ++++
 llvm/lib/Support/COM.cpp                  |  2 +-
 llvm/lib/Support/Chrono.cpp               |  4 ++--
 llvm/lib/Support/CrashRecoveryContext.cpp | 16 ++++++++++++++--
 llvm/lib/Support/Memory.cpp               |  2 +-
 llvm/lib/Support/Path.cpp                 |  2 +-
 llvm/lib/Support/Process.cpp              |  2 +-
 llvm/lib/Support/Program.cpp              |  2 +-
 llvm/lib/Support/Signals.cpp              |  2 +-
 llvm/lib/Support/ThreadPool.cpp           |  2 ++
 llvm/lib/Support/Threading.cpp            |  2 +-
 llvm/lib/Support/Unix/Path.inc            |  5 ++++-
 llvm/lib/Support/Unix/Process.inc         |  4 +++-
 llvm/lib/Support/Unix/Program.inc         |  4 ++--
 llvm/lib/Support/Unix/Signals.inc         |  9 ++++++++-
 llvm/lib/Support/VirtualFileSystem.cpp    |  4 ++++
 llvm/lib/Support/Watchdog.cpp             |  2 +-
 llvm/lib/Support/raw_socket_stream.cpp    |  4 ++++
 llvm/lib/Support/regengine.inc            |  4 ++--
 llvm/lib/TargetParser/Host.cpp            |  2 +-
 34 files changed, 101 insertions(+), 43 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index b116c06e86bc..adb84d46305e 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -441,6 +441,9 @@ std::unique_ptr<TargetInfo> AllocateTarget(const llvm::Triple &Triple,
 
   case llvm::Triple::riscv64:
     switch (os) {
+    case llvm::Triple::Yggdrasil:
+      return std::make_unique<YggdrasilTargetInfo<RISCV64TargetInfo>>(Triple,
+                                                                      Opts);
     case llvm::Triple::FreeBSD:
       return std::make_unique<FreeBSDTargetInfo<RISCV64TargetInfo>>(Triple,
                                                                     Opts);
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 18604cc8ed75..0e68650219ab 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1966,10 +1966,17 @@ int Driver::ExecuteCompilation(
 
     // llvm/lib/Support/*/Signals.inc will exit with a special return code
     // for SIGPIPE. Do not print diagnostics for this case.
+#if defined(__yggdrasil__)
+    if (CommandRes == -1) {
+      Res = CommandRes;
+      continue;
+    }
+#else
     if (CommandRes == EX_IOERR) {
       Res = CommandRes;
       continue;
     }
+#endif
 
     // Print extra information about abnormal failures, if possible.
     //
diff --git a/clang/lib/Driver/ToolChains/Yggdrasil.cpp b/clang/lib/Driver/ToolChains/Yggdrasil.cpp
index d45945abbd99..0589a026a79d 100644
--- a/clang/lib/Driver/ToolChains/Yggdrasil.cpp
+++ b/clang/lib/Driver/ToolChains/Yggdrasil.cpp
@@ -113,7 +113,7 @@ void yggdrasil::Linker::ConstructJob(
         if (D.CCCIsCXX()) {
             if (ToolChain.ShouldLinkCXXStdlib(Args)) {
                 ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
-                // CmdArgs.push_back("-lm");
+                CmdArgs.push_back("-lm");
                 // CmdArgs.push_back("-lpthread");
             }
         }
@@ -139,6 +139,9 @@ Yggdrasil::Yggdrasil(
         llvm::sys::path::append(P, "lib");
         getFilePaths().push_back(std::string(P));
     }
+
+    getFilePaths().push_back(concat(getDriver().SysRoot, "/lib"));
+    getFilePaths().push_back(concat(getDriver().SysRoot, "/usr/lib"));
 }
 
 std::string Yggdrasil::ComputeEffectiveClangTriple(const ArgList &Args,
@@ -178,7 +181,7 @@ ToolChain::UnwindTableLevel Yggdrasil::getDefaultUnwindTableLevel(const llvm::op
 void Yggdrasil::addClangTargetOptions(const ArgList &DriverArgs,
                                     ArgStringList &CC1Args,
                                     Action::OffloadKind) const {
-    if (getArch() == llvm::Triple::x86) {
+    if (getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::riscv64) {
         CC1Args.push_back("-mstack-alignment=16");
     }
   // if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
@@ -214,6 +217,8 @@ void Yggdrasil::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
         llvm::sys::path::append(P, "include");
         addExternCSystemInclude(DriverArgs, CC1Args, P.str());
     }
+
+    addSystemInclude(DriverArgs, CC1Args, concat(getDriver().SysRoot, "/usr/include"));
 }
 
 void Yggdrasil::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
diff --git a/libcxx/include/__config b/libcxx/include/__config
index ecb21a705c51..af49b7e359fb 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -817,7 +817,8 @@ typedef __char32_t char32_t;
         defined(__APPLE__) ||                                                                                          \
         defined(__MVS__) ||                                                                                            \
         defined(_AIX) ||                                                                                               \
-        defined(__EMSCRIPTEN__)
+        defined(__EMSCRIPTEN__) ||                                                                                     \
+        defined(__yggdrasil__)
 // clang-format on
 #      define _LIBCPP_HAS_THREAD_API_PTHREAD
 #    elif defined(__Fuchsia__)
diff --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index 83e8a64504ae..23c041c3857f 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -33,7 +33,7 @@
 
 // OpenBSD does not have a fully conformant suite of POSIX timers, but
 // it does have clock_gettime and CLOCK_MONOTONIC which is all we need.
-#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
+#if defined(__APPLE__) || defined(__gnu_hurd__) || defined(__OpenBSD__) || defined(__yggdrasil__) || (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0)
 #  define _LIBCPP_HAS_CLOCK_GETTIME
 #endif
 
diff --git a/lld/Common/Filesystem.cpp b/lld/Common/Filesystem.cpp
index c2d3644191c9..3fb66c892f12 100644
--- a/lld/Common/Filesystem.cpp
+++ b/lld/Common/Filesystem.cpp
@@ -18,7 +18,7 @@
 #include "llvm/Support/Parallel.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/TimeProfiler.h"
-#if LLVM_ON_UNIX
+#if LLVM_ON_UNIX || defined(__yggdrasil__)
 #include <unistd.h>
 #endif
 #include <thread>
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index 12618966c4ad..cd310e22ef3b 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -1238,7 +1238,7 @@ if( LLVM_INCLUDE_UTILS )
   if( LLVM_INCLUDE_TESTS )
     set(LLVM_SUBPROJECT_TITLE "Third-Party/Google Test")
     add_subdirectory(${LLVM_THIRD_PARTY_DIR}/unittest ${CMAKE_CURRENT_BINARY_DIR}/third-party/unittest)
-    set(LLVM_SUBPROJECT_TITLE) 
+    set(LLVM_SUBPROJECT_TITLE)
   endif()
 else()
   if ( LLVM_INCLUDE_TESTS )
diff --git a/llvm/include/llvm/ADT/GenericCycleImpl.h b/llvm/include/llvm/ADT/GenericCycleImpl.h
index ab9c421a4469..1c3a23ba609d 100644
--- a/llvm/include/llvm/ADT/GenericCycleImpl.h
+++ b/llvm/include/llvm/ADT/GenericCycleImpl.h
@@ -393,7 +393,9 @@ void GenericCycleInfo<ContextT>::compute(FunctionT &F) {
                     << "\n");
   Compute.run(&F.front());
 
+#if !defined(NDEBUG)
   assert(validateTree());
+#endif
 }
 
 template <typename ContextT>
@@ -406,7 +408,9 @@ void GenericCycleInfo<ContextT>::splitCriticalEdge(BlockT *Pred, BlockT *Succ,
     return;
 
   addBlockToCycle(NewBlock, Cycle);
+#if !defined(NDEBUG)
   assert(validateTree());
+#endif
 }
 
 /// \brief Find the innermost cycle containing a given block.
diff --git a/llvm/include/llvm/Support/FileSystem.h b/llvm/include/llvm/Support/FileSystem.h
index 9cf53360b4e9..dbd25abf766b 100644
--- a/llvm/include/llvm/Support/FileSystem.h
+++ b/llvm/include/llvm/Support/FileSystem.h
@@ -135,7 +135,7 @@ inline perms operator~(perms x) {
 /// represents the information provided by Windows FileFirstFile/FindNextFile.
 class basic_file_status {
 protected:
-  #if defined(LLVM_ON_UNIX)
+  #if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
   time_t fs_st_atime = 0;
   time_t fs_st_mtime = 0;
   uint32_t fs_st_atime_nsec = 0;
@@ -159,7 +159,7 @@ public:
 
   explicit basic_file_status(file_type Type) : Type(Type) {}
 
-  #if defined(LLVM_ON_UNIX)
+  #if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
   basic_file_status(file_type Type, perms Perms, time_t ATime,
                     uint32_t ATimeNSec, time_t MTime, uint32_t MTimeNSec,
                     uid_t UID, gid_t GID, off_t Size)
@@ -198,7 +198,7 @@ public:
   /// same machine.
   TimePoint<> getLastModificationTime() const;
 
-  #if defined(LLVM_ON_UNIX)
+  #if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
   uint32_t getUser() const { return fs_st_uid; }
   uint32_t getGroup() const { return fs_st_gid; }
   uint64_t getSize() const { return fs_st_size; }
@@ -225,7 +225,7 @@ public:
 class file_status : public basic_file_status {
   friend bool equivalent(file_status A, file_status B);
 
-  #if defined(LLVM_ON_UNIX)
+  #if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
   dev_t fs_st_dev = 0;
   nlink_t fs_st_nlinks = 0;
   ino_t fs_st_ino = 0;
@@ -240,7 +240,7 @@ public:
 
   explicit file_status(file_type Type) : basic_file_status(Type) {}
 
-  #if defined(LLVM_ON_UNIX)
+  #if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
   file_status(file_type Type, perms Perms, dev_t Dev, nlink_t Links, ino_t Ino,
               time_t ATime, uint32_t ATimeNSec,
               time_t MTime, uint32_t MTimeNSec,
@@ -1209,7 +1209,7 @@ std::error_code unlockFile(int FD);
 /// means that the filesystem may have failed to perform some buffered writes.
 std::error_code closeFile(file_t &F);
 
-#ifdef LLVM_ON_UNIX
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 /// @brief Change ownership of a file.
 ///
 /// @param Owner The owner of the file to change to.
diff --git a/llvm/include/llvm/Support/Program.h b/llvm/include/llvm/Support/Program.h
index 9df94eb604c7..f9266dddf4d2 100644
--- a/llvm/include/llvm/Support/Program.h
+++ b/llvm/include/llvm/Support/Program.h
@@ -28,7 +28,7 @@ namespace sys {
 
   /// This is the OS-specific separator for PATH like environment variables:
   // a colon on Unix or a semicolon on Windows.
-#if defined(LLVM_ON_UNIX)
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
   const char EnvPathSeparator = ':';
 #elif defined (_WIN32)
   const char EnvPathSeparator = ';';
diff --git a/llvm/include/llvm/Support/thread.h b/llvm/include/llvm/Support/thread.h
index 69c06c050aac..ebb04812fae8 100644
--- a/llvm/include/llvm/Support/thread.h
+++ b/llvm/include/llvm/Support/thread.h
@@ -31,7 +31,7 @@ typedef PVOID HANDLE;
 
 namespace llvm {
 
-#if LLVM_ON_UNIX || _WIN32
+#if LLVM_ON_UNIX || defined(__yggdrasil__) || _WIN32
 
 /// LLVM thread following std::thread interface with added constructor to
 /// specify stack size.
@@ -46,7 +46,7 @@ class thread {
   }
 
 public:
-#if LLVM_ON_UNIX
+#if LLVM_ON_UNIX || defined(__yggdrasil__)
   using native_handle_type = pthread_t;
   using id = pthread_t;
   using start_routine_type = void *(*)(void *);
diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 70803c153d8c..3eb34d7d8fc2 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -716,7 +716,7 @@ static bool constantIsDead(const Constant *C, bool RemoveDeadUsers) {
     ReplaceableMetadataImpl::SalvageDebugInfo(*C);
     const_cast<Constant *>(C)->destroyConstant();
   }
-  
+
   return true;
 }
 
@@ -2239,10 +2239,10 @@ Constant *ConstantExpr::getPointerBitCastOrAddrSpaceCast(Constant *S,
 }
 
 Constant *ConstantExpr::getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced) {
-#ifndef NDEBUG
+// #ifndef NDEBUG
   bool fromVec = isa<VectorType>(C->getType());
   bool toVec = isa<VectorType>(Ty);
-#endif
+// #endif
   assert((fromVec == toVec) && "Cannot convert from scalar to/from vector");
   assert(C->getType()->isIntOrIntVectorTy() && "Trunc operand must be integer");
   assert(Ty->isIntOrIntVectorTy() && "Trunc produces only integral");
diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp b/llvm/lib/IR/DebugInfoMetadata.cpp
index 3440fcf711c7..e1e5e899d4c2 100644
--- a/llvm/lib/IR/DebugInfoMetadata.cpp
+++ b/llvm/lib/IR/DebugInfoMetadata.cpp
@@ -382,11 +382,11 @@ StringRef DIScope::getName() const {
   return "";
 }
 
-#ifndef NDEBUG
+// #ifndef NDEBUG
 static bool isCanonical(const MDString *S) {
   return !S || !S->getString().empty();
 }
-#endif
+// #endif
 
 dwarf::Tag GenericDINode::getTag() const { return (dwarf::Tag)SubclassData16; }
 GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp
index b2ee75811fbb..701c2d78fb88 100644
--- a/llvm/lib/IR/Value.cpp
+++ b/llvm/lib/IR/Value.cpp
@@ -465,7 +465,7 @@ void Value::assertModuleIsMaterializedImpl() const {
 #endif
 }
 
-#ifndef NDEBUG
+// #ifndef NDEBUG
 static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
                      Constant *C) {
   if (!Cache.insert(Expr).second)
@@ -498,7 +498,7 @@ static bool contains(Value *Expr, Value *V) {
   SmallPtrSet<ConstantExpr *, 4> Cache;
   return contains(Cache, CE, C);
 }
-#endif // NDEBUG
+// #endif // NDEBUG
 
 void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) {
   assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
@@ -827,7 +827,7 @@ bool Value::canBeFreed() const {
   // which is why we need the explicit opt in on a per collector basis.
   if (!F->hasGC())
     return true;
-  
+
   const auto &GCName = F->getGC();
   if (GCName == "statepoint-example") {
     auto *PT = cast<PointerType>(this->getType());
diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index f653379e3033..bbc934d1460b 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -37,6 +37,10 @@ if(LLVM_ENABLE_ZSTD)
   list(APPEND imported_libs ${zstd_target})
 endif()
 
+if (CMAKE_SYSTEM_NAME STREQUAL "yggdrasil")
+  set(system_libs ${system_libs} m)
+endif()
+
 if( WIN32 )
   # libuuid required for FOLDERID_Profile usage in lib/Support/Windows/Path.inc.
   # advapi32 required for CryptAcquireContextW in lib/Support/Windows/Path.inc.
diff --git a/llvm/lib/Support/COM.cpp b/llvm/lib/Support/COM.cpp
index f37b95ba8651..d3d80db52a70 100644
--- a/llvm/lib/Support/COM.cpp
+++ b/llvm/lib/Support/COM.cpp
@@ -15,7 +15,7 @@
 #include "llvm/Config/llvm-config.h"
 
 // Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 #include "Unix/COM.inc"
 #elif defined(_WIN32)
 #include "Windows/COM.inc"
diff --git a/llvm/lib/Support/Chrono.cpp b/llvm/lib/Support/Chrono.cpp
index 993d200675fe..6e00d5dd79b3 100644
--- a/llvm/lib/Support/Chrono.cpp
+++ b/llvm/lib/Support/Chrono.cpp
@@ -26,7 +26,7 @@ static inline struct tm getStructTM(TimePoint<> TP) {
   struct tm Storage;
   std::time_t OurTime = toTimeT(TP);
 
-#if defined(LLVM_ON_UNIX)
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
   struct tm *LT = ::localtime_r(&OurTime, &Storage);
   assert(LT);
   (void)LT;
@@ -44,7 +44,7 @@ static inline struct tm getStructTMUtc(UtcTime<> TP) {
   struct tm Storage;
   std::time_t OurTime = toTimeT(TP);
 
-#if defined(LLVM_ON_UNIX)
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
   struct tm *LT = ::gmtime_r(&OurTime, &Storage);
   assert(LT);
   (void)LT;
diff --git a/llvm/lib/Support/CrashRecoveryContext.cpp b/llvm/lib/Support/CrashRecoveryContext.cpp
index f53aea177d61..e8d97aedc0c1 100644
--- a/llvm/lib/Support/CrashRecoveryContext.cpp
+++ b/llvm/lib/Support/CrashRecoveryContext.cpp
@@ -344,8 +344,16 @@ static void uninstallExceptionOrSignalHandlers() {
 
 #include <signal.h>
 
-static const int Signals[] =
-    { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
+static const int Signals[] = {
+    SIGABRT,
+    SIGBUS,
+    SIGFPE,
+#if !defined(__yggdrasil__)
+    SIGILL,
+#endif
+    SIGSEGV,
+    SIGTRAP
+};
 static const unsigned NumSignals = std::size(Signals);
 static struct sigaction PrevActions[NumSignals];
 
@@ -372,10 +380,12 @@ static void CrashRecoverySignalHandler(int Signal) {
   }
 
   // Unblock the signal we received.
+#if !defined(__yggdrasil__)
   sigset_t SigMask;
   sigemptyset(&SigMask);
   sigaddset(&SigMask, Signal);
   sigprocmask(SIG_UNBLOCK, &SigMask, nullptr);
+#endif
 
   // Return the same error code as if the program crashed, as mentioned in the
   // section "Exit Status for Commands":
@@ -383,8 +393,10 @@ static void CrashRecoverySignalHandler(int Signal) {
   int RetCode = 128 + Signal;
 
   // Don't consider a broken pipe as a crash (see clang/lib/Driver/Driver.cpp)
+#if !defined(__yggdrasil__)
   if (Signal == SIGPIPE)
     RetCode = EX_IOERR;
+#endif
 
   if (CRCI)
     const_cast<CrashRecoveryContextImpl *>(CRCI)->HandleCrash(RetCode, Signal);
diff --git a/llvm/lib/Support/Memory.cpp b/llvm/lib/Support/Memory.cpp
index f1ba2d0cfe3a..ad8c64b31984 100644
--- a/llvm/lib/Support/Memory.cpp
+++ b/llvm/lib/Support/Memory.cpp
@@ -19,7 +19,7 @@
 #endif // ifndef NDEBUG
 
 // Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 #include "Unix/Memory.inc"
 #endif
 #ifdef _WIN32
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index 4db9bc80b415..ec8c656bf8e2 100644
--- a/llvm/lib/Support/Path.cpp
+++ b/llvm/lib/Support/Path.cpp
@@ -1196,7 +1196,7 @@ Error readNativeFileToEOF(file_t FileHandle, SmallVectorImpl<char> &Buffer,
 } // end namespace llvm
 
 // Include the truly platform-specific parts.
-#if defined(LLVM_ON_UNIX)
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 #include "Unix/Path.inc"
 #endif
 #if defined(_WIN32)
diff --git a/llvm/lib/Support/Process.cpp b/llvm/lib/Support/Process.cpp
index 54462f23c842..535057f7fd45 100644
--- a/llvm/lib/Support/Process.cpp
+++ b/llvm/lib/Support/Process.cpp
@@ -119,7 +119,7 @@ bool Process::AreCoreFilesPrevented() { return coreFilesPrevented; }
 }
 
 // Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 #include "Unix/Process.inc"
 #endif
 #ifdef _WIN32
diff --git a/llvm/lib/Support/Program.cpp b/llvm/lib/Support/Program.cpp
index 181f68cfbb8c..ac52cf126030 100644
--- a/llvm/lib/Support/Program.cpp
+++ b/llvm/lib/Support/Program.cpp
@@ -100,7 +100,7 @@ void sys::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
 }
 
 // Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 #include "Unix/Program.inc"
 #endif
 #ifdef _WIN32
diff --git a/llvm/lib/Support/Signals.cpp b/llvm/lib/Support/Signals.cpp
index 9f9030e79d10..cb68f8010d7e 100644
--- a/llvm/lib/Support/Signals.cpp
+++ b/llvm/lib/Support/Signals.cpp
@@ -273,7 +273,7 @@ static bool printMarkupStackTrace(StringRef Argv0, void **StackTrace, int Depth,
 }
 
 // Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 #include "Unix/Signals.inc"
 #endif
 #ifdef _WIN32
diff --git a/llvm/lib/Support/ThreadPool.cpp b/llvm/lib/Support/ThreadPool.cpp
index 27e0f220ac4e..edfc20fbcfee 100644
--- a/llvm/lib/Support/ThreadPool.cpp
+++ b/llvm/lib/Support/ThreadPool.cpp
@@ -158,8 +158,10 @@ void StdThreadPool::wait(ThreadPoolTaskGroup &Group) {
     return;
   }
   // Make sure to not deadlock waiting for oneself.
+#ifndef NDEBUG
   assert(CurrentThreadTaskGroups == nullptr ||
          !llvm::is_contained(*CurrentThreadTaskGroups, &Group));
+#endif
   // Handle the case of recursive call from another task in a different group,
   // in which case process tasks while waiting to keep the thread busy and avoid
   // possible deadlock.
diff --git a/llvm/lib/Support/Threading.cpp b/llvm/lib/Support/Threading.cpp
index 7cc7ba44cc72..0deae08a3962 100644
--- a/llvm/lib/Support/Threading.cpp
+++ b/llvm/lib/Support/Threading.cpp
@@ -65,7 +65,7 @@ unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
 }
 
 // Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 #include "Unix/Threading.inc"
 #endif
 #ifdef _WIN32
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index cf05db546e02..502408f1cb04 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -112,7 +112,7 @@ typedef uint_t uint;
 #endif
 
 #if defined(__NetBSD__) || defined(__DragonFly__) || defined(__GNU__) ||       \
-    defined(__MVS__)
+    defined(__MVS__) || defined(__yggdrasil__)
 #define STATVFS_F_FLAG(vfs) (vfs).f_flag
 #else
 #define STATVFS_F_FLAG(vfs) (vfs).f_flags
@@ -505,6 +505,9 @@ static bool is_local_impl(struct STATVFS &Vfs) {
 #elif defined(__Fuchsia__)
   // Fuchsia doesn't yet support remote filesystem mounts.
   return true;
+#elif defined(__yggdrasil__)
+  // Yggdrasil doesn't yet support remote filesystem mounts.
+  return true;
 #elif defined(__EMSCRIPTEN__)
   // Emscripten doesn't currently support remote filesystem mounts.
   return true;
diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc
index 84b10ff5d1d0..90edcb2ea177 100644
--- a/llvm/lib/Support/Unix/Process.inc
+++ b/llvm/lib/Support/Unix/Process.inc
@@ -92,7 +92,9 @@ Expected<unsigned> Process::getPageSize() {
 }
 
 size_t Process::GetMallocUsage() {
-#if defined(HAVE_MALLINFO2)
+#if defined(__yggdrasil__)
+  return 1;
+#elif defined(HAVE_MALLINFO2)
   struct mallinfo2 mi;
   mi = ::mallinfo2();
   return mi.uordblks;
diff --git a/llvm/lib/Support/Unix/Program.inc b/llvm/lib/Support/Unix/Program.inc
index 2742734bb11e..e6486805e3f5 100644
--- a/llvm/lib/Support/Unix/Program.inc
+++ b/llvm/lib/Support/Unix/Program.inc
@@ -426,7 +426,7 @@ ProcessInfo llvm::sys::Wait(const ProcessInfo &PI,
   // Parent process: Wait for the child process to terminate.
   int status = 0;
   ProcessInfo WaitResult;
-#ifndef __Fuchsia__
+#if !defined(__Fuchsia__)
   rusage Info;
   if (ProcStat)
     ProcStat->reset();
@@ -473,7 +473,7 @@ ProcessInfo llvm::sys::Wait(const ProcessInfo &PI,
     sigaction(SIGALRM, &Old, nullptr);
   }
 
-#ifndef __Fuchsia__
+#if !defined(__Fuchsia__) && !defined(__yggdrasil__)
   if (ProcStat) {
     std::chrono::microseconds UserT = toDuration(Info.ru_utime);
     std::chrono::microseconds KernelT = toDuration(Info.ru_stime);
diff --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc
index 298fde1a387c..a0bc42e70acc 100644
--- a/llvm/lib/Support/Unix/Signals.inc
+++ b/llvm/lib/Support/Unix/Signals.inc
@@ -213,7 +213,10 @@ static const int IntSigs[] = {SIGHUP, SIGINT, SIGTERM, SIGUSR2};
 
 /// Signals that represent that we have a bug, and our prompt termination has
 /// been ordered.
-static const int KillSigs[] = {SIGILL,
+static const int KillSigs[] = {
+#if !defined(__yggdrasil__)
+                               SIGILL,
+#endif
                                SIGTRAP,
                                SIGABRT,
                                SIGFPE,
@@ -437,7 +440,11 @@ void llvm::sys::SetOneShotPipeSignalFunction(void (*Handler)()) {
 
 void llvm::sys::DefaultOneShotPipeSignalHandler() {
   // Send a special return code that drivers can check for, from sysexits.h.
+#if defined(__yggdrasil__)
+  exit(-1);
+#else
   exit(EX_IOERR);
+#endif
 }
 
 // The public API
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index ce2bf2b4193a..469686a20616 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -2323,9 +2323,11 @@ RedirectingFileSystem::lookupPathImpl(
     sys::path::const_iterator Start, sys::path::const_iterator End,
     RedirectingFileSystem::Entry *From,
     llvm::SmallVectorImpl<Entry *> &Entries) const {
+#ifndef NDEBUG
   assert(!isTraversalComponent(*Start) &&
          !isTraversalComponent(From->getName()) &&
          "Paths should not contain traversal components");
+#endif
 
   StringRef FromName = From->getName();
 
@@ -2725,7 +2727,9 @@ void YAMLVFSWriter::addEntry(StringRef VirtualPath, StringRef RealPath,
                              bool IsDirectory) {
   assert(sys::path::is_absolute(VirtualPath) && "virtual path not absolute");
   assert(sys::path::is_absolute(RealPath) && "real path not absolute");
+#ifndef NDEBUG
   assert(!pathHasTraversal(VirtualPath) && "path traversal is not supported");
+#endif
   Mappings.emplace_back(VirtualPath, RealPath, IsDirectory);
 }
 
diff --git a/llvm/lib/Support/Watchdog.cpp b/llvm/lib/Support/Watchdog.cpp
index 246f3dc7a0ca..4f0f5b1db2d8 100644
--- a/llvm/lib/Support/Watchdog.cpp
+++ b/llvm/lib/Support/Watchdog.cpp
@@ -14,7 +14,7 @@
 #include "llvm/Config/llvm-config.h"
 
 // Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 #include "Unix/Watchdog.inc"
 #endif
 #ifdef _WIN32
diff --git a/llvm/lib/Support/raw_socket_stream.cpp b/llvm/lib/Support/raw_socket_stream.cpp
index 04b3233084a4..887eaed86f41 100644
--- a/llvm/lib/Support/raw_socket_stream.cpp
+++ b/llvm/lib/Support/raw_socket_stream.cpp
@@ -11,6 +11,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+#if !defined(__yggdrasil__)
+
 #include "llvm/Support/raw_socket_stream.h"
 #include "llvm/Config/config.h"
 #include "llvm/Support/Error.h"
@@ -339,3 +341,5 @@ ssize_t raw_socket_stream::read(char *Ptr, size_t Size,
   }
   return raw_fd_stream::read(Ptr, Size);
 }
+
+#endif // defined(__yggdrasil__)
diff --git a/llvm/lib/Support/regengine.inc b/llvm/lib/Support/regengine.inc
index f23993abc6e7..3fa0c28bb6d8 100644
--- a/llvm/lib/Support/regengine.inc
+++ b/llvm/lib/Support/regengine.inc
@@ -120,8 +120,8 @@ static char *pchar(int);
 #define	SP(t, s, c)	print(m, t, s, c, stdout)
 #define	AT(t, p1, p2, s1, s2)	at(m, t, p1, p2, s1, s2)
 #define	NOTE(str)	{ if (m->eflags&REG_TRACE) (void)printf("=%s\n", (str)); }
-static int nope = 0;
 #else
+static int nope = 0;
 #define	SP(t, s, c)	/* nothing */
 #define	AT(t, p1, p2, s1, s2)	/* nothing */
 #define	NOTE(s)	/* nothing */
@@ -1047,7 +1047,7 @@ print(struct match *m, const char *caption, states st, int ch, FILE *d)
 	(void)fprintf(d, "\n");
 }
 
-/* 
+/*
  - at - print current situation
  */
 static void
diff --git a/llvm/lib/TargetParser/Host.cpp b/llvm/lib/TargetParser/Host.cpp
index 865b6a44adbb..94d061504f26 100644
--- a/llvm/lib/TargetParser/Host.cpp
+++ b/llvm/lib/TargetParser/Host.cpp
@@ -23,7 +23,7 @@
 #include <string.h>
 
 // Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
+#if defined(LLVM_ON_UNIX) || defined(__yggdrasil__)
 #include "Unix/Host.inc"
 #include <sched.h>
 #endif
-- 
2.48.1

