Skip to content

Commit

Permalink
EarlyCSE: fix CmpPredicate duplicate-hashing (llvm#119902)
Browse files Browse the repository at this point in the history
Strip hash_value() for CmpPredicate, as different callers have different
hashing use-cases. In this case, there is just one caller, namely
EarlyCSE, which calls hash_combine() on a CmpPredicate, which used to
call hash_combine() on a CmpInst::Predicate prior to 4a0d53a
(PatternMatch: migrate to CmpPredicate). This has uncovered a bug where
two icmp instructions differing in just the fact that one of them has
the samesign flag on it are hashed differently, leading to divergent
hashing, and a crash. Fix this crash by dropping samesign information on
icmp instructions before hashing them, preserving the former behavior.

Fixes llvm#119893.

Change-Id: Iaa84f654b5f0cbe9792a78a14ba92af2ebaf5c18
  • Loading branch information
artagnon authored and searlmc1 committed Dec 16, 2024
1 parent 629442a commit 24b7c71
Showing 3 changed files with 2 additions and 11 deletions.
6 changes: 0 additions & 6 deletions llvm/include/llvm/IR/CmpPredicate.h
Original file line number Diff line number Diff line change
@@ -71,13 +71,7 @@ class CmpPredicate {

/// Get the swapped predicate of a CmpInst.
static CmpPredicate getSwapped(const CmpInst *Cmp);

/// Provided to facilitate storing a CmpPredicate in data structures that
/// require hashing.
friend hash_code hash_value(const CmpPredicate &Arg); // NOLINT
};

[[nodiscard]] hash_code hash_value(const CmpPredicate &Arg);
} // namespace llvm

#endif
4 changes: 0 additions & 4 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
@@ -3946,10 +3946,6 @@ CmpPredicate CmpPredicate::getSwapped(const CmpInst *Cmp) {
return getSwapped(get(Cmp));
}

hash_code llvm::hash_value(const CmpPredicate &Arg) { // NOLINT
return hash_combine(Arg.Pred, Arg.HasSameSign);
}

//===----------------------------------------------------------------------===//
// SwitchInst Implementation
//===----------------------------------------------------------------------===//
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Scalar/EarlyCSE.cpp
Original file line number Diff line number Diff line change
@@ -290,7 +290,8 @@ static unsigned getHashValueImpl(SimpleValue Val) {
Pred = CmpInst::getInversePredicate(Pred);
std::swap(A, B);
}
return hash_combine(Inst->getOpcode(), Pred, X, Y, A, B);
return hash_combine(Inst->getOpcode(),
static_cast<CmpInst::Predicate>(Pred), X, Y, A, B);
}

if (CastInst *CI = dyn_cast<CastInst>(Inst))

0 comments on commit 24b7c71

Please sign in to comment.