r336231 - Factor out Clang's desired 8MB stack size constant from the various

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 3 14:34:13 PDT 2018


Author: rsmith
Date: Tue Jul  3 14:34:13 2018
New Revision: 336231

URL: http://llvm.org/viewvc/llvm-project?rev=336231&view=rev
Log:
Factor out Clang's desired 8MB stack size constant from the various
places we hardcode it.

Added:
    cfe/trunk/include/clang/Basic/Stack.h
Modified:
    cfe/trunk/lib/Frontend/CompilerInstance.cpp
    cfe/trunk/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
    cfe/trunk/tools/driver/cc1_main.cpp
    cfe/trunk/tools/libclang/CIndex.cpp

Added: cfe/trunk/include/clang/Basic/Stack.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Stack.h?rev=336231&view=auto
==============================================================================
--- cfe/trunk/include/clang/Basic/Stack.h (added)
+++ cfe/trunk/include/clang/Basic/Stack.h Tue Jul  3 14:34:13 2018
@@ -0,0 +1,25 @@
+//===--- Stack.h - Utilities for dealing with stack space -------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Defines utilities for dealing with stack allocation and stack space.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_BASIC_STACK_H
+#define LLVM_CLANG_BASIC_STACK_H
+
+namespace clang {
+  /// The amount of stack space that Clang would like to be provided with.
+  /// If less than this much is available, we may be unable to reach our
+  /// template instantiation depth limit and other similar limits.
+  constexpr size_t DesiredStackSize = 8 << 20;
+} // end namespace clang
+
+#endif // LLVM_CLANG_BASIC_STACK_H

Modified: cfe/trunk/lib/Frontend/CompilerInstance.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInstance.cpp?rev=336231&r1=336230&r2=336231&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/CompilerInstance.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInstance.cpp Tue Jul  3 14:34:13 2018
@@ -16,6 +16,7 @@
 #include "clang/Basic/FileManager.h"
 #include "clang/Basic/MemoryBufferCache.h"
 #include "clang/Basic/SourceManager.h"
+#include "clang/Basic/Stack.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Basic/Version.h"
 #include "clang/Config/config.h"
@@ -1164,14 +1165,13 @@ compileModuleImpl(CompilerInstance &Impo
 
   // Execute the action to actually build the module in-place. Use a separate
   // thread so that we get a stack large enough.
-  const unsigned ThreadStackSize = 8 << 20;
   llvm::CrashRecoveryContext CRC;
   CRC.RunSafelyOnThread(
       [&]() {
         GenerateModuleFromModuleMapAction Action;
         Instance.ExecuteAction(Action);
       },
-      ThreadStackSize);
+      DesiredStackSize);
 
   PostBuildStep(Instance);
 

Modified: cfe/trunk/lib/StaticAnalyzer/Frontend/ModelInjector.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Frontend/ModelInjector.cpp?rev=336231&r1=336230&r2=336231&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Frontend/ModelInjector.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Frontend/ModelInjector.cpp Tue Jul  3 14:34:13 2018
@@ -10,6 +10,7 @@
 #include "ModelInjector.h"
 #include "clang/AST/Decl.h"
 #include "clang/Basic/IdentifierTable.h"
+#include "clang/Basic/Stack.h"
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendAction.h"
@@ -95,11 +96,10 @@ void ModelInjector::onBodySynthesis(cons
 
   ParseModelFileAction parseModelFile(Bodies);
 
-  const unsigned ThreadStackSize = 8 << 20;
   llvm::CrashRecoveryContext CRC;
 
   CRC.RunSafelyOnThread([&]() { Instance.ExecuteAction(parseModelFile); },
-                        ThreadStackSize);
+                        DesiredStackSize);
 
   Instance.getPreprocessor().FinalizeForModelFile();
 

Modified: cfe/trunk/tools/driver/cc1_main.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/driver/cc1_main.cpp?rev=336231&r1=336230&r2=336231&view=diff
==============================================================================
--- cfe/trunk/tools/driver/cc1_main.cpp (original)
+++ cfe/trunk/tools/driver/cc1_main.cpp Tue Jul  3 14:34:13 2018
@@ -16,6 +16,7 @@
 #include "llvm/Option/Arg.h"
 #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
 #include "clang/Config/config.h"
+#include "clang/Basic/Stack.h"
 #include "clang/Driver/DriverDiagnostic.h"
 #include "clang/Driver/Options.h"
 #include "clang/Frontend/CompilerInstance.h"
@@ -73,13 +74,6 @@ void initializePollyPasses(llvm::PassReg
 #endif
 
 #ifdef CLANG_HAVE_RLIMITS
-// The amount of stack we think is "sufficient". If less than this much is
-// available, we may be unable to reach our template instantiation depth
-// limit and other similar limits.
-// FIXME: Unify this with the stack we request when spawning a thread to build
-// a module.
-static const int kSufficientStack = 8 << 20;
-
 #if defined(__linux__) && defined(__PIE__)
 static size_t getCurrentStackAllocation() {
   // If we can't compute the current stack usage, allow for 512K of command
@@ -117,7 +111,7 @@ static size_t getCurrentStackAllocation(
 #include <alloca.h>
 
 LLVM_ATTRIBUTE_NOINLINE
-static void ensureStackAddressSpace(int ExtraChunks = 0) {
+static void ensureStackAddressSpace() {
   // Linux kernels prior to 4.1 will sometimes locate the heap of a PIE binary
   // relatively close to the stack (they are only guaranteed to be 128MiB
   // apart). This results in crashes if we happen to heap-allocate more than
@@ -126,7 +120,7 @@ static void ensureStackAddressSpace(int
   // To avoid these crashes, ensure that we have sufficient virtual memory
   // pages allocated before we start running.
   size_t Curr = getCurrentStackAllocation();
-  const int kTargetStack = kSufficientStack - 256 * 1024;
+  const int kTargetStack = DesiredStackSize - 256 * 1024;
   if (Curr < kTargetStack) {
     volatile char *volatile Alloc =
         static_cast<volatile char *>(alloca(kTargetStack - Curr));
@@ -146,21 +140,21 @@ static void ensureSufficientStack() {
 
   // Increase the soft stack limit to our desired level, if necessary and
   // possible.
-  if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < kSufficientStack) {
+  if (rlim.rlim_cur != RLIM_INFINITY && rlim.rlim_cur < DesiredStackSize) {
     // Try to allocate sufficient stack.
-    if (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_max >= kSufficientStack)
-      rlim.rlim_cur = kSufficientStack;
+    if (rlim.rlim_max == RLIM_INFINITY || rlim.rlim_max >= DesiredStackSize)
+      rlim.rlim_cur = DesiredStackSize;
     else if (rlim.rlim_cur == rlim.rlim_max)
       return;
     else
       rlim.rlim_cur = rlim.rlim_max;
 
     if (setrlimit(RLIMIT_STACK, &rlim) != 0 ||
-        rlim.rlim_cur != kSufficientStack)
+        rlim.rlim_cur != DesiredStackSize)
       return;
   }
 
-  // We should now have a stack of size at least kSufficientStack. Ensure
+  // We should now have a stack of size at least DesiredStackSize. Ensure
   // that we can actually use that much, if necessary.
   ensureStackAddressSpace();
 }

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=336231&r1=336230&r2=336231&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Tue Jul  3 14:34:13 2018
@@ -26,6 +26,7 @@
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticCategories.h"
 #include "clang/Basic/DiagnosticIDs.h"
+#include "clang/Basic/Stack.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Basic/Version.h"
 #include "clang/Frontend/ASTUnit.h"
@@ -8474,8 +8475,8 @@ void clang::PrintLibclangResourceUsage(C
 // Misc. utility functions.
 //===----------------------------------------------------------------------===//
 
-/// Default to using an 8 MB stack size on "safety" threads.
-static unsigned SafetyStackThreadSize = 8 << 20;
+/// Default to using our desired 8 MB stack size on "safety" threads.
+static unsigned SafetyStackThreadSize = DesiredStackSize;
 
 namespace clang {
 




More information about the cfe-commits mailing list