Skip to content

Commit dcf0160

Browse files
authored
[TableGen] Optimize intrinsic info type signature encoding (#106809)
Change the "fixed encoding" table used for encoding intrinsic type signature to use 16-bit encoding as opposed to 32-bit. This results in both space and time improvements. For space, the total static storage size (in bytes) of this info reduces by 50%: - Current = 14193*4 (Fixed table) + 16058 + 3 (Long Table) = 72833 - New size = 14193*2 (Fixed table) + 19879 + 3 (Long Table) = 48268. - Reduction = 50.9% For time, with the added benchmark, we see a 7.3% speedup in `GetIntrinsicInfoTableEntries` benchmark. Actual output of the benchmark in included in the GitHub MR.
1 parent dd754cd commit dcf0160

File tree

4 files changed

+87
-38
lines changed

4 files changed

+87
-38
lines changed

llvm/benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ add_benchmark(DummyYAML DummyYAML.cpp PARTIAL_SOURCES_INTENDED)
66
add_benchmark(xxhash xxhash.cpp PARTIAL_SOURCES_INTENDED)
77
add_benchmark(GetIntrinsicForClangBuiltin GetIntrinsicForClangBuiltin.cpp PARTIAL_SOURCES_INTENDED)
88
add_benchmark(FormatVariadicBM FormatVariadicBM.cpp PARTIAL_SOURCES_INTENDED)
9+
add_benchmark(GetIntrinsicInfoTableEntriesBM GetIntrinsicInfoTableEntriesBM.cpp PARTIAL_SOURCES_INTENDED)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===- GetIntrinsicInfoTableEntries.cpp - IIT signature benchmark ---------===//
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+
#include "benchmark/benchmark.h"
10+
#include "llvm/ADT/SmallVector.h"
11+
#include "llvm/IR/Intrinsics.h"
12+
13+
using namespace llvm;
14+
using namespace Intrinsic;
15+
16+
static void BM_GetIntrinsicInfoTableEntries(benchmark::State &state) {
17+
SmallVector<IITDescriptor> Table;
18+
for (auto _ : state) {
19+
for (ID ID = 1; ID < num_intrinsics; ++ID) {
20+
// This makes sure the vector does not keep growing, as well as after the
21+
// first iteration does not result in additional allocations.
22+
Table.clear();
23+
getIntrinsicInfoTableEntries(ID, Table);
24+
}
25+
}
26+
}
27+
28+
BENCHMARK(BM_GetIntrinsicInfoTableEntries);
29+
30+
BENCHMARK_MAIN();

llvm/lib/IR/Function.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,22 +1381,24 @@ static void DecodeIITType(unsigned &NextElt, ArrayRef<unsigned char> Infos,
13811381

13821382
void Intrinsic::getIntrinsicInfoTableEntries(ID id,
13831383
SmallVectorImpl<IITDescriptor> &T){
1384+
static_assert(sizeof(IIT_Table[0]) == 2,
1385+
"Expect 16-bit entries in IIT_Table");
13841386
// Check to see if the intrinsic's type was expressible by the table.
1385-
unsigned TableVal = IIT_Table[id-1];
1387+
uint16_t TableVal = IIT_Table[id - 1];
13861388

13871389
// Decode the TableVal into an array of IITValues.
1388-
SmallVector<unsigned char, 8> IITValues;
1390+
SmallVector<unsigned char> IITValues;
13891391
ArrayRef<unsigned char> IITEntries;
13901392
unsigned NextElt = 0;
1391-
if ((TableVal >> 31) != 0) {
1393+
if (TableVal >> 15) {
13921394
// This is an offset into the IIT_LongEncodingTable.
13931395
IITEntries = IIT_LongEncodingTable;
13941396

13951397
// Strip sentinel bit.
1396-
NextElt = (TableVal << 1) >> 1;
1398+
NextElt = TableVal & 0x7fff;
13971399
} else {
1398-
// Decode the TableVal into an array of IITValues. If the entry was encoded
1399-
// into a single word in the table itself, decode it now.
1400+
// If the entry was encoded into a single word in the table itself, decode
1401+
// it from an array of nibbles to an array of bytes.
14001402
do {
14011403
IITValues.push_back(TableVal & 0xF);
14021404
TableVal >>= 4;

llvm/utils/TableGen/IntrinsicEmitter.cpp

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,37 @@ static TypeSigTy ComputeTypeSignature(const CodeGenIntrinsic &Int) {
282282
return TypeSig;
283283
}
284284

285+
// Pack the type signature into 32-bit fixed encoding word.
286+
static std::optional<uint32_t> encodePacked(const TypeSigTy &TypeSig) {
287+
if (TypeSig.size() > 8)
288+
return std::nullopt;
289+
290+
uint32_t Result = 0;
291+
for (unsigned char C : reverse(TypeSig)) {
292+
if (C > 15)
293+
return std::nullopt;
294+
Result = (Result << 4) | C;
295+
}
296+
return Result;
297+
}
298+
285299
void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
286300
raw_ostream &OS) {
287-
// If we can compute a 32-bit fixed encoding for this intrinsic, do so and
301+
// Note: the code below can be switched to use 32-bit fixed encoding by
302+
// flipping the flag below.
303+
constexpr bool Use16BitFixedEncoding = true;
304+
using FixedEncodingTy =
305+
std::conditional_t<Use16BitFixedEncoding, uint16_t, uint32_t>;
306+
constexpr unsigned FixedEncodingBits = sizeof(FixedEncodingTy) * CHAR_BIT;
307+
// Mask with all bits 1 except the most significant bit.
308+
const unsigned Mask = (1U << (FixedEncodingBits - 1)) - 1;
309+
const unsigned MSBPostion = FixedEncodingBits - 1;
310+
StringRef FixedEncodingTypeName =
311+
Use16BitFixedEncoding ? "uint16_t" : "uint32_t";
312+
313+
// If we can compute a 16/32-bit fixed encoding for this intrinsic, do so and
288314
// capture it in this vector, otherwise store a ~0U.
289-
std::vector<unsigned> FixedEncodings;
315+
std::vector<FixedEncodingTy> FixedEncodings;
290316
SequenceToOffsetTable<TypeSigTy> LongEncodingTable;
291317

292318
FixedEncodings.reserve(Ints.size());
@@ -296,69 +322,59 @@ void IntrinsicEmitter::EmitGenerator(const CodeGenIntrinsicTable &Ints,
296322
// Get the signature for the intrinsic.
297323
TypeSigTy TypeSig = ComputeTypeSignature(Int);
298324

299-
// Check to see if we can encode it into a 32-bit word. We can only encode
300-
// 8 nibbles into a 32-bit word.
301-
if (TypeSig.size() <= 8) {
302-
// Attempt to pack elements of TypeSig into a 32-bit word, starting from
303-
// the most significant nibble.
304-
unsigned Result = 0;
305-
bool Failed = false;
306-
for (unsigned char C : reverse(TypeSig)) {
307-
if (C > 15) {
308-
Failed = true;
309-
break;
310-
}
311-
Result = (Result << 4) | C;
312-
}
313-
314-
// If this could be encoded into a 31-bit word, return it.
315-
if (!Failed && (Result >> 31) == 0) {
316-
FixedEncodings.push_back(Result);
317-
continue;
318-
}
325+
// Check to see if we can encode it into a 16/32 bit word.
326+
std::optional<uint32_t> Result = encodePacked(TypeSig);
327+
if (Result && (*Result & Mask) == Result) {
328+
FixedEncodings.push_back(static_cast<FixedEncodingTy>(*Result));
329+
continue;
319330
}
320331

321-
// Otherwise, we're going to unique the sequence into the
322-
// LongEncodingTable, and use its offset in the 32-bit table instead.
323332
LongEncodingTable.add(TypeSig);
324333

325334
// This is a placehold that we'll replace after the table is laid out.
326-
FixedEncodings.push_back(~0U);
335+
FixedEncodings.push_back(static_cast<FixedEncodingTy>(~0U));
327336
}
328337

329338
LongEncodingTable.layout();
330339

331-
OS << R"(// Global intrinsic function declaration type table.
340+
OS << formatv(R"(// Global intrinsic function declaration type table.
332341
#ifdef GET_INTRINSIC_GENERATOR_GLOBAL
333-
static constexpr unsigned IIT_Table[] = {
334-
)";
342+
static constexpr {0} IIT_Table[] = {{
343+
)",
344+
FixedEncodingTypeName);
335345

346+
unsigned MaxOffset = 0;
336347
for (auto [Idx, FixedEncoding, Int] : enumerate(FixedEncodings, Ints)) {
337348
if ((Idx & 7) == 7)
338349
OS << "\n ";
339350

340351
// If the entry fit in the table, just emit it.
341-
if (FixedEncoding != ~0U) {
352+
if ((FixedEncoding & Mask) == FixedEncoding) {
342353
OS << "0x" << Twine::utohexstr(FixedEncoding) << ", ";
343354
continue;
344355
}
345356

346357
TypeSigTy TypeSig = ComputeTypeSignature(Int);
358+
unsigned Offset = LongEncodingTable.get(TypeSig);
359+
MaxOffset = std::max(MaxOffset, Offset);
347360

348361
// Otherwise, emit the offset into the long encoding table. We emit it this
349362
// way so that it is easier to read the offset in the .def file.
350-
OS << "(1U<<31) | " << LongEncodingTable.get(TypeSig) << ", ";
363+
OS << formatv("(1U<<{0}) | {1}, ", MSBPostion, Offset);
351364
}
352365

353366
OS << "0\n};\n\n";
354367

368+
// verify that all offsets will fit in 16/32 bits.
369+
if ((MaxOffset & Mask) != MaxOffset)
370+
PrintFatalError("Offset of long encoding table exceeds encoding bits");
371+
355372
// Emit the shared table of register lists.
356373
OS << "static constexpr unsigned char IIT_LongEncodingTable[] = {\n";
357374
if (!LongEncodingTable.empty())
358375
LongEncodingTable.emit(
359376
OS, [](raw_ostream &OS, unsigned char C) { OS << (unsigned)C; });
360-
OS << " 255\n};\n\n";
361-
377+
OS << " 255\n};\n";
362378
OS << "#endif\n\n"; // End of GET_INTRINSIC_GENERATOR_GLOBAL
363379
}
364380

0 commit comments

Comments
 (0)