[llvm] [SelectionDAG] Add space-optimized forms of OPC_CheckComplexPat (PR #73310)
Wang Pengcheng via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 12 02:22:44 PST 2023
https://github.com/wangpc-pp updated https://github.com/llvm/llvm-project/pull/73310
>From a9db2c82a90fae112913c0d2b842ccdce0b64e17 Mon Sep 17 00:00:00 2001
From: wangpc <wangpengcheng.pp at bytedance.com>
Date: Fri, 24 Nov 2023 13:24:12 +0800
Subject: [PATCH] [SelectionDAG] Add space-optimized forms of
OPC_CheckComplexPat
We record the usage of each `ComplexPat` and sort the `ComplexPat`s
by usage.
For the top 8 `ComplexPat`s, we will emit a `OPC_CheckComplexPatN`
to save one byte.
Overall this reduces the llc binary size with all in-tree targets by
about 89K.
---
llvm/include/llvm/CodeGen/SelectionDAGISel.h | 8 +++++
.../CodeGen/SelectionDAG/SelectionDAGISel.cpp | 14 +++++++--
llvm/test/TableGen/dag-isel-complexpattern.td | 2 +-
llvm/utils/TableGen/CodeGenDAGPatterns.h | 12 ++++++++
llvm/utils/TableGen/DAGISelMatcher.h | 3 +-
llvm/utils/TableGen/DAGISelMatcherEmitter.cpp | 30 ++++++++++++-------
llvm/utils/TableGen/DAGISelMatcherGen.cpp | 10 +++----
7 files changed, 58 insertions(+), 21 deletions(-)
diff --git a/llvm/include/llvm/CodeGen/SelectionDAGISel.h b/llvm/include/llvm/CodeGen/SelectionDAGISel.h
index c604e7eaa0887e..de8fc9f3ea2815 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAGISel.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAGISel.h
@@ -207,6 +207,14 @@ class SelectionDAGISel : public MachineFunctionPass {
OPC_CheckChild2CondCode,
OPC_CheckValueType,
OPC_CheckComplexPat,
+ OPC_CheckComplexPat0,
+ OPC_CheckComplexPat1,
+ OPC_CheckComplexPat2,
+ OPC_CheckComplexPat3,
+ OPC_CheckComplexPat4,
+ OPC_CheckComplexPat5,
+ OPC_CheckComplexPat6,
+ OPC_CheckComplexPat7,
OPC_CheckAndImm,
OPC_CheckOrImm,
OPC_CheckImmAllOnesV,
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
index dd28ec09d0e2b9..46202f72d41b4e 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
@@ -3349,8 +3349,18 @@ void SelectionDAGISel::SelectCodeCommon(SDNode *NodeToMatch,
break;
continue;
}
- case OPC_CheckComplexPat: {
- unsigned CPNum = MatcherTable[MatcherIndex++];
+ case OPC_CheckComplexPat:
+ case OPC_CheckComplexPat0:
+ case OPC_CheckComplexPat1:
+ case OPC_CheckComplexPat2:
+ case OPC_CheckComplexPat3:
+ case OPC_CheckComplexPat4:
+ case OPC_CheckComplexPat5:
+ case OPC_CheckComplexPat6:
+ case OPC_CheckComplexPat7: {
+ unsigned CPNum = Opcode == OPC_CheckComplexPat
+ ? MatcherTable[MatcherIndex++]
+ : Opcode - OPC_CheckComplexPat0;
unsigned RecNo = MatcherTable[MatcherIndex++];
assert(RecNo < RecordedNodes.size() && "Invalid CheckComplexPat");
diff --git a/llvm/test/TableGen/dag-isel-complexpattern.td b/llvm/test/TableGen/dag-isel-complexpattern.td
index 3d74e4e46dc41c..b8f517a1fc2890 100644
--- a/llvm/test/TableGen/dag-isel-complexpattern.td
+++ b/llvm/test/TableGen/dag-isel-complexpattern.td
@@ -22,7 +22,7 @@ def CP32 : ComplexPattern<i32, 0, "SelectCP32">;
def INSTR : Instruction {
// CHECK-LABEL: OPC_CheckOpcode, TARGET_VAL(ISD::STORE)
// CHECK: OPC_CheckTypeI32
-// CHECK: OPC_CheckComplexPat, /*CP*/0, /*#*/1, // SelectCP32:$
+// CHECK: OPC_CheckComplexPat0, /*#*/1, // SelectCP32:$
// CHECK: Src: (st (add:{ *:[i32] } (CP32:{ *:[i32] }), (CP32:{ *:[i32] })), i64:{ *:[i64] }:$addr)
let OutOperandList = (outs);
let InOperandList = (ins GPR64:$addr);
diff --git a/llvm/utils/TableGen/CodeGenDAGPatterns.h b/llvm/utils/TableGen/CodeGenDAGPatterns.h
index 2611fe06f55ca5..8fc748119c3c2f 100644
--- a/llvm/utils/TableGen/CodeGenDAGPatterns.h
+++ b/llvm/utils/TableGen/CodeGenDAGPatterns.h
@@ -1117,6 +1117,9 @@ class CodeGenDAGPatterns {
std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
+ /// ComplexPatternUsage - Record the usage of ComplexPattern.
+ std::map<const ComplexPattern *, unsigned> ComplexPatternUsage;
+
// Specific SDNode definitions:
Record *intrinsic_void_sdnode;
Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
@@ -1163,6 +1166,15 @@ class CodeGenDAGPatterns {
return F->second;
}
+ const std::map<const ComplexPattern *, unsigned> &
+ getComplexPatternUsage() const {
+ return ComplexPatternUsage;
+ }
+
+ void increaseComplexPatternUsage(const ComplexPattern *CP) {
+ ++ComplexPatternUsage[CP];
+ }
+
const CodeGenIntrinsic &getIntrinsic(Record *R) const {
for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
if (Intrinsics[i].TheDef == R) return Intrinsics[i];
diff --git a/llvm/utils/TableGen/DAGISelMatcher.h b/llvm/utils/TableGen/DAGISelMatcher.h
index 0e8a948ec8a956..a8f683bcee53e3 100644
--- a/llvm/utils/TableGen/DAGISelMatcher.h
+++ b/llvm/utils/TableGen/DAGISelMatcher.h
@@ -34,8 +34,7 @@ namespace llvm {
class TreePattern;
Matcher *ConvertPatternToMatcher(const PatternToMatch &Pattern,
- unsigned Variant,
- const CodeGenDAGPatterns &CGP);
+ unsigned Variant, CodeGenDAGPatterns &CGP);
void OptimizeMatcher(std::unique_ptr<Matcher> &Matcher,
const CodeGenDAGPatterns &CGP);
void EmitMatcherTable(Matcher *Matcher, const CodeGenDAGPatterns &CGP,
diff --git a/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp b/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
index 5f96f11279f24d..7cc257da7d067a 100644
--- a/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/DAGISelMatcherEmitter.cpp
@@ -63,7 +63,6 @@ class MatcherTableEmitter {
StringMap<unsigned> PatternPredicateMap;
std::vector<std::string> PatternPredicates;
- DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
std::vector<const ComplexPattern*> ComplexPatterns;
@@ -85,7 +84,16 @@ class MatcherTableEmitter {
public:
MatcherTableEmitter(const CodeGenDAGPatterns &cgp)
- : CGP(cgp), OpcodeCounts(Matcher::HighestKind + 1, 0) {}
+ : CGP(cgp), OpcodeCounts(Matcher::HighestKind + 1, 0) {
+ // Sort ComplexPatterns by usage.
+ auto &ComplexPatternUsage = cgp.getComplexPatternUsage();
+ std::vector<std::pair<const ComplexPattern *, unsigned>> ComplexPatternList(
+ ComplexPatternUsage.begin(), ComplexPatternUsage.end());
+ sort(ComplexPatternList,
+ [](auto &A, auto &B) { return A.second > B.second; });
+ for (auto &ComplexPattern : ComplexPatternList)
+ ComplexPatterns.push_back(ComplexPattern.first);
+ }
unsigned EmitMatcherList(const Matcher *N, const unsigned Indent,
unsigned StartIdx, raw_ostream &OS);
@@ -146,12 +154,7 @@ class MatcherTableEmitter {
return Entry-1;
}
unsigned getComplexPat(const ComplexPattern &P) {
- unsigned &Entry = ComplexPatternMap[&P];
- if (Entry == 0) {
- ComplexPatterns.push_back(&P);
- Entry = ComplexPatterns.size();
- }
- return Entry-1;
+ return llvm::find(ComplexPatterns, &P) - ComplexPatterns.begin();
}
unsigned getNodeXFormID(Record *Rec) {
@@ -652,8 +655,13 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
case Matcher::CheckComplexPat: {
const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
const ComplexPattern &Pattern = CCPM->getPattern();
- OS << "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern) << ", /*#*/"
- << CCPM->getMatchNumber() << ',';
+ unsigned PatternNo = getComplexPat(Pattern);
+ if (PatternNo < 8)
+ OS << "OPC_CheckComplexPat" << PatternNo << ", /*#*/"
+ << CCPM->getMatchNumber() << ',';
+ else
+ OS << "OPC_CheckComplexPat, /*CP*/" << PatternNo << ", /*#*/"
+ << CCPM->getMatchNumber() << ',';
if (!OmitComments) {
OS << " // " << Pattern.getSelectFunc();
@@ -665,7 +673,7 @@ EmitMatcher(const Matcher *N, const unsigned Indent, unsigned CurrentIdx,
OS << " + chain result";
}
OS << '\n';
- return 3;
+ return PatternNo < 8 ? 2 : 3;
}
case Matcher::CheckAndImm: {
diff --git a/llvm/utils/TableGen/DAGISelMatcherGen.cpp b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
index 3526e97c8e08e8..1a3a742f10b7e6 100644
--- a/llvm/utils/TableGen/DAGISelMatcherGen.cpp
+++ b/llvm/utils/TableGen/DAGISelMatcherGen.cpp
@@ -56,7 +56,7 @@ static MVT::SimpleValueType getRegisterValueType(Record *R,
namespace {
class MatcherGen {
const PatternToMatch &Pattern;
- const CodeGenDAGPatterns &CGP;
+ CodeGenDAGPatterns &CGP;
/// PatWithNoTypes - This is a clone of Pattern.getSrcPattern() that starts
/// out with all of the types removed. This allows us to insert type checks
@@ -102,7 +102,7 @@ namespace {
/// which should have future checks stuck into its Next position.
Matcher *CurPredicate;
public:
- MatcherGen(const PatternToMatch &pattern, const CodeGenDAGPatterns &cgp);
+ MatcherGen(const PatternToMatch &pattern, CodeGenDAGPatterns &cgp);
bool EmitMatcherCode(unsigned Variant);
void EmitResultCode();
@@ -145,8 +145,7 @@ namespace {
} // end anonymous namespace
-MatcherGen::MatcherGen(const PatternToMatch &pattern,
- const CodeGenDAGPatterns &cgp)
+MatcherGen::MatcherGen(const PatternToMatch &pattern, CodeGenDAGPatterns &cgp)
: Pattern(pattern), CGP(cgp), NextRecordedOperandNo(0), TheMatcher(nullptr),
CurPredicate(nullptr) {
// We need to produce the matcher tree for the patterns source pattern. To
@@ -602,6 +601,7 @@ bool MatcherGen::EmitMatcherCode(unsigned Variant) {
// Emit a CheckComplexPat operation, which does the match (aborting if it
// fails) and pushes the matched operands onto the recorded nodes list.
+ CGP.increaseComplexPatternUsage(CP);
AddMatcher(new CheckComplexPatMatcher(*CP, RecNodeEntry, N->getName(),
NextRecordedOperandNo));
@@ -1082,7 +1082,7 @@ void MatcherGen::EmitResultCode() {
/// the specified variant. If the variant number is invalid, this returns null.
Matcher *llvm::ConvertPatternToMatcher(const PatternToMatch &Pattern,
unsigned Variant,
- const CodeGenDAGPatterns &CGP) {
+ CodeGenDAGPatterns &CGP) {
MatcherGen Gen(Pattern, CGP);
// Generate the code for the matcher.
More information about the llvm-commits
mailing list