[llvm] 534a2bf - [TableGen] Rewrite type set intersection in type inference
Krzysztof Parzyszek via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 7 12:49:44 PDT 2022
Author: Krzysztof Parzyszek
Date: 2022-07-07T12:49:28-07:00
New Revision: 534a2bf99e7cce4ea71dca6f91997794e3a36587
URL: https://github.com/llvm/llvm-project/commit/534a2bf99e7cce4ea71dca6f91997794e3a36587
DIFF: https://github.com/llvm/llvm-project/commit/534a2bf99e7cce4ea71dca6f91997794e3a36587.diff
LOG: [TableGen] Rewrite type set intersection in type inference
The previous code had a bug when dealing with matching iPTR against a
set of integer types. It was trying to handle it all in a compact way,
but that implementation couldn't be modified to correct the problem in
a simple way. The code wasn't long, and it was easier to rewrite it.
The actual issue was that non-scalar-integer types were considered when
matching against iPTR. For example {iPTR} intersected with {i32 f32}
was {iPTR} (due to multiple types in the other set), but should be just
{i32}, because i32 is the only integer scalar in the other set.
Added:
Modified:
llvm/utils/TableGen/CodeGenDAGPatterns.cpp
Removed:
################################################################################
diff --git a/llvm/utils/TableGen/CodeGenDAGPatterns.cpp b/llvm/utils/TableGen/CodeGenDAGPatterns.cpp
index e886962e988b..03566a91778f 100644
--- a/llvm/utils/TableGen/CodeGenDAGPatterns.cpp
+++ b/llvm/utils/TableGen/CodeGenDAGPatterns.cpp
@@ -46,6 +46,9 @@ static inline bool isVector(MVT VT) {
static inline bool isScalar(MVT VT) {
return !VT.isVector();
}
+static inline bool isScalarInteger(MVT VT) {
+ return VT.isScalarInteger();
+}
template <typename Predicate>
static bool berase_if(MachineValueTypeSet &S, Predicate P) {
@@ -270,10 +273,11 @@ void TypeSetByHwMode::dump() const {
bool TypeSetByHwMode::intersect(SetType &Out, const SetType &In) {
bool OutP = Out.count(MVT::iPTR), InP = In.count(MVT::iPTR);
- auto Int = [&In](MVT T) -> bool { return !In.count(T); };
+ // Complement of In.
+ auto CompIn = [&In](MVT T) -> bool { return !In.count(T); };
if (OutP == InP)
- return berase_if(Out, Int);
+ return berase_if(Out, CompIn);
// Compute the intersection of scalars separately to account for only
// one set containing iPTR.
@@ -289,42 +293,64 @@ bool TypeSetByHwMode::intersect(SetType &Out, const SetType &In) {
// { iPTR i32 } * { i32 i64 } -> { i32 i64 }
// { iPTR i32 } * { i32 i64 i128 } -> { iPTR i32 }
- // Compute the
diff erence between the two sets in such a way that the
- // iPTR is in the set that is being subtracted. This is to see if there
- // are any extra scalars in the set without iPTR that are not in the
- // set containing iPTR. Then the iPTR could be considered a "wildcard"
- // matching these scalars. If there is only one such scalar, it would
- // replace the iPTR, if there are more, the iPTR would be retained.
- SetType Diff;
+ // Let In' = elements only in In, Out' = elements only in Out, and
+ // IO = elements common to both. Normally IO would be returned as the result
+ // of the intersection, but we need to account for iPTR being a "wildcard" of
+ // sorts. Since elements in IO are those that match both sets exactly, they
+ // will all belong to the output. If any of the "leftovers" (i.e. In' or
+ // Out') contain iPTR, it means that the other set doesn't have it, but it
+ // could have (1) a more specific type, or (2) a set of types that is less
+ // specific. The "leftovers" from the other set is what we want to examine
+ // more closely.
+
+ auto subtract = [](const SetType &A, const SetType &B) {
+ SetType Diff = A;
+ berase_if(Diff, [&B](MVT T) { return B.count(T); });
+ return Diff;
+ };
+
if (InP) {
- Diff = Out;
- berase_if(Diff, [&In](MVT T) { return In.count(T); });
- // Pre-remove these elements and rely only on InP/OutP to determine
- // whether a change has been made.
- berase_if(Out, [&Diff](MVT T) { return Diff.count(T); });
- } else {
- Diff = In;
- berase_if(Diff, [&Out](MVT T) { return Out.count(T); });
- Out.erase(MVT::iPTR);
- }
-
- // The actual intersection.
- bool Changed = berase_if(Out, Int);
- unsigned NumD = Diff.size();
- if (NumD == 0)
- return Changed;
-
- if (NumD == 1) {
- Out.insert(*Diff.begin());
- // This is a change only if Out was the one with iPTR (which is now
- // being replaced).
- Changed |= OutP;
- } else {
- // Multiple elements from Out are now replaced with iPTR.
- Out.insert(MVT::iPTR);
- Changed |= !OutP;
+ SetType OutOnly = subtract(Out, In);
+ if (OutOnly.empty()) {
+ // This means that Out \subset In, so no change to Out.
+ return false;
+ }
+ unsigned NumI = llvm::count_if(OutOnly, isScalarInteger);
+ if (NumI == 1 && OutOnly.size() == 1) {
+ // There is only one element in Out', and it happens to be a scalar
+ // integer that should be kept as a match for iPTR in In.
+ return false;
+ }
+ berase_if(Out, CompIn);
+ if (NumI == 1) {
+ // Replace the iPTR with the leftover scalar integer.
+ Out.insert(*llvm::find_if(OutOnly, isScalarInteger));
+ } else if (NumI > 1) {
+ Out.insert(MVT::iPTR);
+ }
+ return true;
}
- return Changed;
+
+ // OutP == true
+ SetType InOnly = subtract(In, Out);
+ unsigned SizeOut = Out.size();
+ berase_if(Out, CompIn); // This will remove at least the iPTR.
+ unsigned NumI = llvm::count_if(InOnly, isScalarInteger);
+ if (NumI == 0) {
+ // iPTR deleted from Out.
+ return true;
+ }
+ if (NumI == 1) {
+ // Replace the iPTR with the leftover scalar integer.
+ Out.insert(*llvm::find_if(InOnly, isScalarInteger));
+ return true;
+ }
+
+ // NumI > 1: Keep the iPTR in Out.
+ Out.insert(MVT::iPTR);
+ // If iPTR was the only element initially removed from Out, then Out
+ // has not changed.
+ return SizeOut != Out.size();
}
bool TypeSetByHwMode::validate() const {
More information about the llvm-commits
mailing list