Debian Codegraph @

external/llvm/releases/14.0.0/patches_external/0006-SCEV-Cache-ZExt-SCEV-expressions.patch

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2024 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 /*========================== begin_copyright_notice ============================
10
11 Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
12 See https://llvm.org/LICENSE.txt for license information.
13 SPDX-License-Identifier: Apache-2.0 with LLVM-exception
14
15 ============================= end_copyright_notice ===========================*/
16
17 From 65fb672dce4a7f2df0df7626ec3a18d6cbc73090 Mon Sep 17 00:00:00 2001
18 From: Florian Hahn <[email protected]>
19 Date: Fri, 9 Dec 2022 22:14:03 +0000
20 Subject: [PATCH 3/5] [SCEV] Cache ZExt SCEV expressions.
21
22 When creating SCEV expressions for ZExt, there's quite a bit of
23 reasoning done and in many places the reasoning in turn will try to
24 create new SCEVs for other ZExts.
25
26 This can have a huge compile-time impact. The attached test from #58402
27 takes an excessive amount of compile time; without the patch, the test
28 doesn't complete in 1500+ seconds, but with the patch it completes in 1
29 second.
30
31 To speed up this case, cache created ZExt expressions for given (SCEV, Ty) pairs.
32 Caching just ZExts is relatively straight-forward, but it might make
33 sense to extend it to other expressions in the future.
34
35 This has a slight positive impact on CTMark:
36 * O3: -0.03%
37 * ReleaseThinLTO: -0.03%
38 * ReleaseLTO-g: 0.00%
39
40 The patch also improves compile-time for some internal real-world workloads
41 where time spent in SCEV goes from ~300 seconds to ~3 seconds.
42
43 There are a few cases where computing & caching the result earlier may
44 return more pessimistic results, but the compile-time savings seem to
45 outweigh that.
46
47 Fixes #58402.
48
49 Reviewed By: mkazantsev
50
51 Differential Revision: https://reviews.llvm.org/D137505
52 ---
53 llvm/include/llvm/Analysis/ScalarEvolution.h | 68 ++++++++++++++++++++
54 llvm/lib/Analysis/ScalarEvolution.cpp | 66 +++++++++++++++++++
55 2 files changed, 134 insertions(+)
56
57 diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
58 index b16aa7017719..f0c3ba40a8d6 100644
59 --- a/llvm/include/llvm/Analysis/ScalarEvolution.h
60 +++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
61 @@ -567,6 +567,8 @@ public:
62 const SCEV *getPtrToIntExpr(const SCEV *Op, Type *Ty);
63 const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0);
64 const SCEV *getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0);
65 + const SCEV *getZeroExtendExprImpl(const SCEV *Op, Type *Ty,
66 + unsigned Depth = 0);
67 const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth = 0);
68 const SCEV *getCastExpr(SCEVTypes Kind, const SCEV *Op, Type *Ty);
69 const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty);
70 @@ -1209,6 +1211,45 @@ public:
71 /// to be infinite, it must also be undefined.
72 bool loopIsFiniteByAssumption(const Loop *L);
73
74 + class FoldID {
75 + SmallVector<unsigned, 4> Bits;
76 +
77 + public:
78 + void addInteger(unsigned long I) { Bits.push_back(I); }
79 + void addInteger(unsigned I) { Bits.push_back(I); }
80 + void addInteger(int I) { Bits.push_back(I); }
81 +
82 + void addInteger(unsigned long long I) {
83 + addInteger(unsigned(I));
84 + addInteger(unsigned(I >> 32));
85 + }
86 +
87 + void addPointer(const void *Ptr) {
88 + // Note: this adds pointers to the hash using sizes and endianness that
89 + // depend on the host. It doesn't matter, however, because hashing on
90 + // pointer values is inherently unstable. Nothing should depend on the
91 + // ordering of nodes in the folding set.
92 + static_assert(sizeof(uintptr_t) <= sizeof(unsigned long long),
93 + "unexpected pointer size");
94 + addInteger(reinterpret_cast<uintptr_t>(Ptr));
95 + }
96 +
97 + unsigned computeHash() const {
98 + unsigned Hash = Bits.size();
99 + for (unsigned I = 0; I != Bits.size(); ++I)
100 + Hash = detail::combineHashValue(Hash, Bits[I]);
101 + return Hash;
102 + }
103 + bool operator==(const FoldID &RHS) const {
104 + if (Bits.size() != RHS.Bits.size())
105 + return false;
106 + for (unsigned I = 0; I != Bits.size(); ++I)
107 + if (Bits[I] != RHS.Bits[I])
108 + return false;
109 + return true;
110 + }
111 + };
112 +
113 private:
114 /// A CallbackVH to arrange for ScalarEvolution to be notified whenever a
115 /// Value is deleted.
116 @@ -1289,6 +1330,11 @@ private:
117 /// This is a cache of the values we have analyzed so far.
118 ValueExprMapType ValueExprMap;
119
120 + /// This is a cache for expressions that got folded to a different existing
121 + /// SCEV.
122 + DenseMap<FoldID, const SCEV *> FoldCache;
123 + DenseMap<const SCEV *, SmallVector<FoldID, 2>> FoldCacheUser;
124 +
125 /// Mark predicate values currently being processed by isImpliedCond.
126 SmallPtrSet<const Value *, 6> PendingLoopPredicates;
127
128 @@ -2263,6 +2309,28 @@ private:
129 const SCEV *BackedgeCount = nullptr;
130 };
131
132 +template <> struct DenseMapInfo<ScalarEvolution::FoldID> {
133 + static inline ScalarEvolution::FoldID getEmptyKey() {
134 + ScalarEvolution::FoldID ID;
135 + ID.addInteger(~0ULL);
136 + return ID;
137 + }
138 + static inline ScalarEvolution::FoldID getTombstoneKey() {
139 + ScalarEvolution::FoldID ID;
140 + ID.addInteger(~0ULL - 1ULL);
141 + return ID;
142 + }
143 +
144 + static unsigned getHashValue(const ScalarEvolution::FoldID &Val) {
145 + return Val.computeHash();
146 + }
147 +
148 + static bool isEqual(const ScalarEvolution::FoldID &LHS,
149 + const ScalarEvolution::FoldID &RHS) {
150 + return LHS == RHS;
151 + }
152 +};
153 +
154 } // end namespace llvm
155
156 #endif // LLVM_ANALYSIS_SCALAREVOLUTION_H
157 diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
158 index 495cd73d8eef..7c745ff07cac 100644
159 --- a/llvm/lib/Analysis/ScalarEvolution.cpp
160 +++ b/llvm/lib/Analysis/ScalarEvolution.cpp
161 @@ -1587,6 +1587,30 @@ ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
162 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
163 Ty = getEffectiveSCEVType(Ty);
164
165 + FoldID ID;
166 + ID.addInteger(scZeroExtend);
167 + ID.addPointer(Op);
168 + ID.addPointer(Ty);
169 + auto Iter = FoldCache.find(ID);
170 + if (Iter != FoldCache.end())
171 + return Iter->second;
172 +
173 + const SCEV *S = getZeroExtendExprImpl(Op, Ty, Depth);
174 + if (!isa<SCEVZeroExtendExpr>(S)) {
175 + FoldCache.insert({ID, S});
176 + auto R = FoldCacheUser.insert({S, {}});
177 + R.first->second.push_back(ID);
178 + }
179 + return S;
180 +}
181 +
182 +const SCEV *ScalarEvolution::getZeroExtendExprImpl(const SCEV *Op, Type *Ty,
183 + unsigned Depth) {
184 + assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
185 + "This is not an extending conversion!");
186 + assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!");
187 + assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
188 +
189 // Fold if the operand is constant.
190 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
191 return getConstant(
192 @@ -7840,6 +7864,8 @@ void ScalarEvolution::forgetAllLoops() {
193 HasRecMap.clear();
194 MinTrailingZerosCache.clear();
195 PredicatedSCEVRewrites.clear();
196 + FoldCache.clear();
197 + FoldCacheUser.clear();
198 }
199
200 void ScalarEvolution::forgetLoop(const Loop *L) {
201 @@ -13236,6 +13262,12 @@ void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) {
202 forgetBackedgeTakenCounts(Pair.getPointer(), Pair.getInt());
203 BECountUsers.erase(BEUsersIt);
204 }
205 +
206 + auto FoldUser = FoldCacheUser.find(S);
207 + if (FoldUser != FoldCacheUser.end())
208 + for (auto &KV : FoldUser->second)
209 + FoldCache.erase(KV);
210 + FoldCacheUser.erase(S);
211 }
212
213 void
214 @@ -13483,6 +13515,40 @@ void ScalarEvolution::verify() const {
215 }
216 }
217 }
218 +
219 + // Verify FoldCache/FoldCacheUser caches.
220 + // NOTE: Original patch used structured bindings, only available with C++17.
221 + for (const auto &It : FoldCache) {
222 + auto &FoldID = It.first;
223 + const SCEV *Expr = It.second;
224 + auto I = FoldCacheUser.find(Expr);
225 + if (I == FoldCacheUser.end()) {
226 + dbgs() << "Missing entry in FoldCacheUser for cached expression " << *Expr
227 + << "!\n";
228 + std::abort();
229 + }
230 + if (!is_contained(I->second, FoldID)) {
231 + dbgs() << "Missing FoldID in cached users of " << *Expr << "!\n";
232 + std::abort();
233 + }
234 + }
235 + for (const auto &It : FoldCacheUser) {
236 + const SCEV *Expr = It.first;
237 + auto &IDs = It.second;
238 + for (auto &FoldID : IDs) {
239 + auto I = FoldCache.find(FoldID);
240 + if (I == FoldCache.end()) {
241 + dbgs() << "Missing entry in FoldCache for expression " << *Expr
242 + << "!\n";
243 + std::abort();
244 + }
245 + if (I->second != Expr) {
246 + dbgs() << "Entry in FoldCache doesn't match FoldCacheUser: "
247 + << *I->second << " != " << *Expr << "!\n";
248 + std::abort();
249 + }
250 + }
251 + }
252 }
253
254 bool ScalarEvolution::invalidate(
255 --
256 2.34.1
257