Skip to content

Revert "[llvm][clang] Allocate a new stack instead of spawning a new … #135865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ Non-comprehensive list of changes in this release
- Added `__builtin_elementwise_exp10`.
- For AMDPGU targets, added `__builtin_v_cvt_off_f32_i4` that maps to the `v_cvt_off_f32_i4` instruction.
- Added `__builtin_elementwise_minnum` and `__builtin_elementwise_maxnum`.
- Clang itself now uses split stacks instead of threads for allocating more
stack space when running on Apple AArch64 based platforms. This means that
stack traces of Clang from debuggers, crashes, and profilers may look
different than before.

New Compiler Flags
------------------
Expand Down
5 changes: 1 addition & 4 deletions clang/include/clang/Basic/Stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ namespace clang {

/// Call this once on each thread, as soon after starting the thread as
/// feasible, to note the approximate address of the bottom of the stack.
///
/// \param ForceSet set to true if you know the call is near the bottom of a
/// new stack. Used for split stacks.
void noteBottomOfStack(bool ForceSet = false);
void noteBottomOfStack();

/// Determine whether the stack is nearly exhausted.
bool isStackNearlyExhausted();
Expand Down
40 changes: 28 additions & 12 deletions clang/lib/Basic/Stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,33 @@

#include "clang/Basic/Stack.h"
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/ProgramStack.h"

static LLVM_THREAD_LOCAL uintptr_t BottomOfStack = 0;
#ifdef _MSC_VER
#include <intrin.h> // for _AddressOfReturnAddress
#endif

void clang::noteBottomOfStack(bool ForceSet) {
if (!BottomOfStack || ForceSet)
BottomOfStack = llvm::getStackPointer();
static LLVM_THREAD_LOCAL void *BottomOfStack = nullptr;

static void *getStackPointer() {
#if __GNUC__ || __has_builtin(__builtin_frame_address)
return __builtin_frame_address(0);
#elif defined(_MSC_VER)
return _AddressOfReturnAddress();
#else
char CharOnStack = 0;
// The volatile store here is intended to escape the local variable, to
// prevent the compiler from optimizing CharOnStack into anything other
// than a char on the stack.
//
// Tested on: MSVC 2015 - 2019, GCC 4.9 - 9, Clang 3.2 - 9, ICC 13 - 19.
char *volatile Ptr = &CharOnStack;
return Ptr;
#endif
}

void clang::noteBottomOfStack() {
if (!BottomOfStack)
BottomOfStack = getStackPointer();
}

bool clang::isStackNearlyExhausted() {
Expand All @@ -31,8 +51,7 @@ bool clang::isStackNearlyExhausted() {
if (!BottomOfStack)
return false;

intptr_t StackDiff =
(intptr_t)llvm::getStackPointer() - (intptr_t)BottomOfStack;
intptr_t StackDiff = (intptr_t)getStackPointer() - (intptr_t)BottomOfStack;
size_t StackUsage = (size_t)std::abs(StackDiff);

// If the stack pointer has a surprising value, we do not understand this
Expand All @@ -47,12 +66,9 @@ bool clang::isStackNearlyExhausted() {
void clang::runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
llvm::function_ref<void()> Fn) {
llvm::CrashRecoveryContext CRC;
// Preserve the BottomOfStack in case RunSafelyOnNewStack uses split stacks.
uintptr_t PrevBottom = BottomOfStack;
CRC.RunSafelyOnNewStack([&] {
noteBottomOfStack(true);
CRC.RunSafelyOnThread([&] {
noteBottomOfStack();
Diag();
Fn();
}, DesiredStackSize);
BottomOfStack = PrevBottom;
}
2 changes: 1 addition & 1 deletion clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ bool CompilerInstance::compileModule(SourceLocation ImportLoc,

// Execute the action to actually build the module in-place. Use a separate
// thread so that we get a stack large enough.
bool Crashed = !llvm::CrashRecoveryContext().RunSafelyOnNewStack(
bool Crashed = !llvm::CrashRecoveryContext().RunSafelyOnThread(
[&]() {
GenerateModuleFromModuleMapAction Action;
Instance.ExecuteAction(Action);
Expand Down
3 changes: 0 additions & 3 deletions llvm/include/llvm/Support/CrashRecoveryContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ class CrashRecoveryContext {
return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize);
}

bool RunSafelyOnNewStack(function_ref<void()>,
unsigned RequestedStackSize = 0);

/// Explicitly trigger a crash recovery in the current process, and
/// return failure from RunSafely(). This function does not return.
[[noreturn]] void HandleExit(int RetCode);
Expand Down
63 changes: 0 additions & 63 deletions llvm/include/llvm/Support/ProgramStack.h

This file was deleted.

1 change: 0 additions & 1 deletion llvm/include/llvm/Support/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ inline thread::id get_id() { return std::this_thread::get_id(); }

#else // !LLVM_ENABLE_THREADS

#include "llvm/Support/ErrorHandling.h"
#include <utility>

namespace llvm {
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,6 @@ add_llvm_component_library(LLVMSupport
Path.cpp
Process.cpp
Program.cpp
ProgramStack.cpp
RWMutex.cpp
Signals.cpp
Threading.cpp
Expand Down
11 changes: 0 additions & 11 deletions llvm/lib/Support/CrashRecoveryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ExitCodes.h"
#include "llvm/Support/ProgramStack.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/thread.h"
#include <cassert>
Expand Down Expand Up @@ -524,13 +523,3 @@ bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn,
CRC->setSwitchedThread();
return Info.Result;
}

bool CrashRecoveryContext::RunSafelyOnNewStack(function_ref<void()> Fn,
unsigned RequestedStackSize) {
#ifdef LLVM_HAS_SPLIT_STACKS
return runOnNewStack(RequestedStackSize,
function_ref<bool()>([&]() { return RunSafely(Fn); }));
#else
return RunSafelyOnThread(Fn, RequestedStackSize);
#endif
}
114 changes: 0 additions & 114 deletions llvm/lib/Support/ProgramStack.cpp

This file was deleted.

1 change: 0 additions & 1 deletion llvm/unittests/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ add_llvm_unittest(SupportTests
PerThreadBumpPtrAllocatorTest.cpp
ProcessTest.cpp
ProgramTest.cpp
ProgramStackTest.cpp
RecyclerTest.cpp
RegexTest.cpp
ReverseIterationTest.cpp
Expand Down
35 changes: 0 additions & 35 deletions llvm/unittests/Support/ProgramStackTest.cpp

This file was deleted.

Loading