Skip to content

Commit 301e321

Browse files
committed
[llvm-tblgen] Increase Coverage Index Size
The tablegen backend for DAGISel can, using the `-instrument-coverage` option, save information about which patterns are used. This saves an index into two tables, one that contains a string dump of the pattern, and another that contains the path the pattern came from. Before this change, these indexes were an unsigned 16-bit value. The RISC-V backend, however, goes beyond this limit. This change increases the indexes to be represented by a 32-bit unsigned value. `-instrument-coverage` is not enabled by default, so this should have minimal effect on the default configuration's code size. This is partly why I also just chose to double the size (from 16-bit to 32-bit) rather than going to 24-bit. Hopefully this size will never need to be changed again. Fixes #118259
1 parent bbea1de commit 301e321

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

llvm/include/llvm/CodeGen/SelectionDAGISel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ class SelectionDAGISel {
315315
OPC_MorphNodeTo1GlueOutput,
316316
OPC_MorphNodeTo2GlueOutput,
317317
OPC_CompleteMatch,
318-
// Contains offset in table for pattern being selected
318+
// Contains 32-bit offset in table for pattern being selected
319319
OPC_Coverage
320320
};
321321

llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4045,6 +4045,8 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
40454045
// So it should be safe to assume that this node has been selected
40464046
unsigned index = MatcherTable[MatcherIndex++];
40474047
index |= (MatcherTable[MatcherIndex++] << 8);
4048+
index |= (MatcherTable[MatcherIndex++] << 16);
4049+
index |= (MatcherTable[MatcherIndex++] << 24);
40484050
dbgs() << "COVERED: " << getPatternForIndex(index) << "\n";
40494051
dbgs() << "INCLUDED: " << getIncludePathForIndex(index) << "\n";
40504052
continue;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: llvm-tblgen -gen-dag-isel -instrument-coverage -I %p/../../include %s | FileCheck %s
2+
3+
include "llvm/Target/Target.td"
4+
5+
def TestTargetInstrInfo : InstrInfo;
6+
7+
def TestTarget : Target {
8+
let InstructionSet = TestTargetInstrInfo;
9+
}
10+
11+
def REG : Register<"REG">;
12+
def GPR : RegisterClass<"TestTarget", [i32], 32, (add REG)>;
13+
14+
// CHECK-LABEL: OPC_CheckOpcode, TARGET_VAL(ISD::UDIVREM)
15+
// CHECK: OPC_EmitNode2None, TARGET_VAL(::INSTR)
16+
// CHECK-NEXT: Results = #2 #3
17+
// CHECK-NEXT: OPC_Coverage, COVERAGE_IDX_VAL(0),
18+
// CHECK-NEXT: OPC_CompleteMatch, 2, 3, 2
19+
def INSTR : Instruction {
20+
let OutOperandList = (outs GPR:$r1, GPR:$r0);
21+
let InOperandList = (ins GPR:$t0, GPR:$t1);
22+
let Pattern = [(set i32:$r0, i32:$r1, (udivrem i32:$t0, i32:$t1))];
23+
}
24+
25+
26+
// CHECK: getPatternForIndex(unsigned Index)
27+
// CHECK: static const char *PATTERN_MATCH_TABLE[]
28+
// CHECK: return StringRef(PATTERN_MATCH_TABLE[Index]);
29+
30+
// CHECK: getIncludePathForIndex(unsigned Index)
31+
// CHECK: static const char *INCLUDE_PATH_TABLE[]
32+
// CHECK: return StringRef(INCLUDE_PATH_TABLE[Index]);

llvm/utils/TableGen/DAGISelMatcherEmitter.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ static void EndEmitFunction(raw_ostream &OS) {
381381

382382
void MatcherTableEmitter::EmitPatternMatchTable(raw_ostream &OS) {
383383

384-
assert(isUInt<16>(VecPatterns.size()) &&
385-
"Using only 16 bits to encode offset into Pattern Table");
384+
assert(isUInt<32>(VecPatterns.size()) &&
385+
"Using only 32 bits to encode offset into Pattern Table");
386386
assert(VecPatterns.size() == VecIncludeStrings.size() &&
387387
"The sizes of Pattern and include vectors should be the same");
388388

@@ -947,7 +947,7 @@ unsigned MatcherTableEmitter::EmitMatcher(const Matcher *N,
947947
std::string include_src = getIncludePath(PatRecord);
948948
unsigned Offset =
949949
getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
950-
OS << "TARGET_VAL(" << Offset << "),\n";
950+
OS << "COVERAGE_IDX_VAL(" << Offset << "),\n";
951951
OS.indent(FullIndexWidth + Indent);
952952
}
953953
}
@@ -1060,7 +1060,7 @@ unsigned MatcherTableEmitter::EmitMatcher(const Matcher *N,
10601060
std::string include_src = getIncludePath(PatRecord);
10611061
unsigned Offset =
10621062
getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
1063-
OS << "TARGET_VAL(" << Offset << "),\n";
1063+
OS << "COVERAGE_IDX_VAL(" << Offset << "),\n";
10641064
OS.indent(FullIndexWidth + Indent);
10651065
}
10661066
OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
@@ -1393,15 +1393,19 @@ void llvm::EmitMatcherTable(Matcher *TheMatcher, const CodeGenDAGPatterns &CGP,
13931393
// final stream.
13941394
OS << "{\n";
13951395
OS << " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
1396-
OS << " // this.\n";
1396+
OS << " // this. Coverage indexes are emitted as 4 bytes,\n";
1397+
OS << " // COVERAGE_IDX_VAL handles this.\n";
13971398
OS << " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
1399+
OS << " #define COVERAGE_IDX_VAL(X) X & 255, (unsigned(X) >> 8) & 255, ";
1400+
OS << "(unsigned(X) >> 16) & 255, (unsigned(X) >> 24) & 255\n";
13981401
OS << " static const unsigned char MatcherTable[] = {\n";
13991402
TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 1, 0, OS);
14001403
OS << " 0\n }; // Total Array size is " << (TotalSize + 1)
14011404
<< " bytes\n\n";
14021405

14031406
MatcherEmitter.EmitHistogram(TheMatcher, OS);
14041407

1408+
OS << " #undef COVERAGE_IDX_VAL\n";
14051409
OS << " #undef TARGET_VAL\n";
14061410
OS << " SelectCodeCommon(N, MatcherTable, sizeof(MatcherTable));\n";
14071411
OS << "}\n";

0 commit comments

Comments
 (0)