Skip to content

[llvm-tblgen] Increase Coverage Index Size #118329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/include/llvm/CodeGen/SelectionDAGISel.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class SelectionDAGISel {
OPC_MorphNodeTo1GlueOutput,
OPC_MorphNodeTo2GlueOutput,
OPC_CompleteMatch,
// Contains offset in table for pattern being selected
// Contains 32-bit offset in table for pattern being selected
OPC_Coverage
};

Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4045,6 +4045,8 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
// So it should be safe to assume that this node has been selected
unsigned index = MatcherTable[MatcherIndex++];
index |= (MatcherTable[MatcherIndex++] << 8);
index |= (MatcherTable[MatcherIndex++] << 16);
index |= (MatcherTable[MatcherIndex++] << 24);
dbgs() << "COVERED: " << getPatternForIndex(index) << "\n";
dbgs() << "INCLUDED: " << getIncludePathForIndex(index) << "\n";
continue;
Expand Down
32 changes: 32 additions & 0 deletions llvm/test/TableGen/dag-isel-instrument.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: llvm-tblgen -gen-dag-isel -instrument-coverage -I %p/../../include %s | FileCheck %s

include "llvm/Target/Target.td"

def TestTargetInstrInfo : InstrInfo;

def TestTarget : Target {
let InstructionSet = TestTargetInstrInfo;
}

def REG : Register<"REG">;
def GPR : RegisterClass<"TestTarget", [i32], 32, (add REG)>;

// CHECK-LABEL: OPC_CheckOpcode, TARGET_VAL(ISD::UDIVREM)
// CHECK: OPC_EmitNode2None, TARGET_VAL(::INSTR)
// CHECK-NEXT: Results = #2 #3
// CHECK-NEXT: OPC_Coverage, COVERAGE_IDX_VAL(0),
// CHECK-NEXT: OPC_CompleteMatch, 2, 3, 2
def INSTR : Instruction {
let OutOperandList = (outs GPR:$r1, GPR:$r0);
let InOperandList = (ins GPR:$t0, GPR:$t1);
let Pattern = [(set i32:$r0, i32:$r1, (udivrem i32:$t0, i32:$t1))];
}


// CHECK: getPatternForIndex(unsigned Index)
// CHECK: static const char *PATTERN_MATCH_TABLE[]
// CHECK: return StringRef(PATTERN_MATCH_TABLE[Index]);

// CHECK: getIncludePathForIndex(unsigned Index)
// CHECK: static const char *INCLUDE_PATH_TABLE[]
// CHECK: return StringRef(INCLUDE_PATH_TABLE[Index]);
16 changes: 11 additions & 5 deletions llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,10 @@ static void EndEmitFunction(raw_ostream &OS) {

void MatcherTableEmitter::EmitPatternMatchTable(raw_ostream &OS) {

assert(isUInt<16>(VecPatterns.size()) &&
"Using only 16 bits to encode offset into Pattern Table");
if (!isUInt<32>(VecPatterns.size()))
report_fatal_error("More patterns defined that can fit into 32-bit Pattern "
"Table index encoding");

assert(VecPatterns.size() == VecIncludeStrings.size() &&
"The sizes of Pattern and include vectors should be the same");

Expand Down Expand Up @@ -947,7 +949,7 @@ unsigned MatcherTableEmitter::EmitMatcher(const Matcher *N,
std::string include_src = getIncludePath(PatRecord);
unsigned Offset =
getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
OS << "TARGET_VAL(" << Offset << "),\n";
OS << "COVERAGE_IDX_VAL(" << Offset << "),\n";
OS.indent(FullIndexWidth + Indent);
}
}
Expand Down Expand Up @@ -1060,7 +1062,7 @@ unsigned MatcherTableEmitter::EmitMatcher(const Matcher *N,
std::string include_src = getIncludePath(PatRecord);
unsigned Offset =
getPatternIdxFromTable(src + " -> " + dst, std::move(include_src));
OS << "TARGET_VAL(" << Offset << "),\n";
OS << "COVERAGE_IDX_VAL(" << Offset << "),\n";
OS.indent(FullIndexWidth + Indent);
}
OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
Expand Down Expand Up @@ -1393,15 +1395,19 @@ void llvm::EmitMatcherTable(Matcher *TheMatcher, const CodeGenDAGPatterns &CGP,
// final stream.
OS << "{\n";
OS << " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
OS << " // this.\n";
OS << " // this. Coverage indexes are emitted as 4 bytes,\n";
OS << " // COVERAGE_IDX_VAL handles this.\n";
OS << " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
OS << " #define COVERAGE_IDX_VAL(X) X & 255, (unsigned(X) >> 8) & 255, ";
OS << "(unsigned(X) >> 16) & 255, (unsigned(X) >> 24) & 255\n";
OS << " static const unsigned char MatcherTable[] = {\n";
TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 1, 0, OS);
OS << " 0\n }; // Total Array size is " << (TotalSize + 1)
<< " bytes\n\n";

MatcherEmitter.EmitHistogram(TheMatcher, OS);

OS << " #undef COVERAGE_IDX_VAL\n";
OS << " #undef TARGET_VAL\n";
OS << " SelectCodeCommon(N, MatcherTable, sizeof(MatcherTable));\n";
OS << "}\n";
Expand Down
Loading