Skip to content

Commit

Permalink
[APInt] improve initialization performance (#106945)
Browse files Browse the repository at this point in the history
The purpose is to save an extra memset in both cases:

1. When `int64_t(val) < 0`, zeroing out is redundant as the subsequent
for-loop will initialize to `val .. 0xFFFFF ....`. Instead we should
only create an uninitialized buffer, and transform the slow for-loop
into a memset to initialize the higher words to `0xFF`.
2. In the other case, first we create an uninitialized array (`new
int64_t[]`) and _then_ we zero it out with `memset`. But this can be
combined in one operation with `new int64_t[]()`, which
default-initializes the array.

On one example where use of APInt was heavy, this improved compile time
by 1%.
  • Loading branch information
Prince781 authored Sep 4, 2024
1 parent 0628683 commit 427e202
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions llvm/lib/Support/APInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ using namespace llvm;
/// A utility function for allocating memory, checking for allocation failures,
/// and ensuring the contents are zeroed.
inline static uint64_t* getClearedMemory(unsigned numWords) {
uint64_t *result = new uint64_t[numWords];
memset(result, 0, numWords * sizeof(uint64_t));
return result;
return new uint64_t[numWords]();
}

/// A utility function for allocating memory and checking for allocation
Expand Down Expand Up @@ -74,12 +72,15 @@ inline static unsigned getDigit(char cdigit, uint8_t radix) {


void APInt::initSlowCase(uint64_t val, bool isSigned) {
U.pVal = getClearedMemory(getNumWords());
U.pVal[0] = val;
if (isSigned && int64_t(val) < 0)
for (unsigned i = 1; i < getNumWords(); ++i)
U.pVal[i] = WORDTYPE_MAX;
clearUnusedBits();
if (isSigned && int64_t(val) < 0) {
U.pVal = getMemory(getNumWords());
U.pVal[0] = val;
memset(&U.pVal[1], 0xFF, APINT_WORD_SIZE * (getNumWords() - 1));
clearUnusedBits();
} else {
U.pVal = getClearedMemory(getNumWords());
U.pVal[0] = val;
}
}

void APInt::initSlowCase(const APInt& that) {
Expand Down

0 comments on commit 427e202

Please sign in to comment.