Skip to content

Commit

Permalink
JIT: Assume allocations succeed (#82961)
Browse files Browse the repository at this point in the history
We call a NOMEM function when allocations fail that is already marked as
no-return, but at least MSVC is not able to pick up on this fact. The
result is that all operator new calls end up with an unnecessary null
check. This adds a COMPILER_ASSUME to allow compilers to recognize
that allocations never fail. 

There was also a workaround for an old VSW bug that I've removed.
  • Loading branch information
jakobbotsch committed Mar 6, 2023
1 parent c7537bd commit c0765d3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 1 addition & 8 deletions src/coreclr/inc/unreachable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
#define __UNREACHABLE_H__

#if defined(_MSC_VER) || defined(_PREFIX_)
#if defined(TARGET_AMD64)
// Empty methods that consist of UNREACHABLE() result in a zero-sized declspec(noreturn) method
// which causes the pdb file to make the next method declspec(noreturn) as well, thus breaking BBT
// Remove when we get a VC compiler that fixes VSW 449170
# define __UNREACHABLE() do { DebugBreak(); __assume(0); } while (0)
#else
# define __UNREACHABLE() __assume(0)
#endif
#define __UNREACHABLE() __assume(0)
#else
#define __UNREACHABLE() __builtin_unreachable()
#endif
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/jit/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ class CompAllocator

void* p = m_arena->allocateMemory(count * sizeof(T));

#ifndef DEBUG
// Could be in DEBUG too, but COMPILER_ASSUME in debug builds has extra
// cruft in it that we'd like to avoid in checked builds.
COMPILER_ASSUME(p != nullptr);
#endif

// Ensure that the allocator returned sizeof(size_t) aligned memory.
assert((size_t(p) & (sizeof(size_t) - 1)) == 0);

Expand Down

0 comments on commit c0765d3

Please sign in to comment.