clang 22.0.0git
Stack.cpp
Go to the documentation of this file.
1//===--- Stack.cpp - Utilities for dealing with stack space ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// Defines utilities for dealing with stack allocation and stack space.
11///
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/Stack.h"
15#include "llvm/Support/CrashRecoveryContext.h"
16#include "llvm/Support/ProgramStack.h"
17
18static LLVM_THREAD_LOCAL uintptr_t BottomOfStack = 0;
19
20void clang::noteBottomOfStack(bool ForceSet) {
21 if (!BottomOfStack || ForceSet)
22 BottomOfStack = llvm::getStackPointer();
23}
24
26 // We consider 256 KiB to be sufficient for any code that runs between checks
27 // for stack size.
28 constexpr size_t SufficientStack = 256 << 10;
29
30 // If we don't know where the bottom of the stack is, hope for the best.
31 if (!BottomOfStack)
32 return false;
33
34 intptr_t StackDiff =
35 (intptr_t)llvm::getStackPointer() - (intptr_t)BottomOfStack;
36 size_t StackUsage = (size_t)std::abs(StackDiff);
37
38 // If the stack pointer has a surprising value, we do not understand this
39 // stack usage scheme. (Perhaps the target allocates new stack regions on
40 // demand for us.) Don't try to guess what's going on.
41 if (StackUsage > DesiredStackSize)
42 return false;
43
44 return StackUsage >= DesiredStackSize - SufficientStack;
45}
46
47void clang::runWithSufficientStackSpaceSlow(llvm::function_ref<void()> Diag,
48 llvm::function_ref<void()> Fn) {
49 llvm::CrashRecoveryContext CRC;
50 // Preserve the BottomOfStack in case RunSafelyOnNewStack uses split stacks.
51 uintptr_t PrevBottom = BottomOfStack;
52 CRC.RunSafelyOnNewStack([&] {
54 Diag();
55 Fn();
57 BottomOfStack = PrevBottom;
58}
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
static LLVM_THREAD_LOCAL uintptr_t BottomOfStack
Definition: Stack.cpp:18
Defines utilities for dealing with stack allocation and stack space.
__SIZE_TYPE__ size_t
void runWithSufficientStackSpaceSlow(llvm::function_ref< void()> Diag, llvm::function_ref< void()> Fn)
Definition: Stack.cpp:47
bool isStackNearlyExhausted()
Determine whether the stack is nearly exhausted.
Definition: Stack.cpp:25
constexpr size_t DesiredStackSize
The amount of stack space that Clang would like to be provided with.
Definition: Stack.h:26
void noteBottomOfStack(bool ForceSet=false)
Call this once on each thread, as soon after starting the thread as feasible, to note the approximate...
Definition: Stack.cpp:20
__DEVICE__ _Tp abs(const std::complex< _Tp > &__c)
Definition: complex_cmath.h:34
__INTPTR_TYPE__ intptr_t
A signed integer type with the property that any valid pointer to void can be converted to this type,...
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...