Skip to content

[PatternMatch] Refactor m_SpecificInt to avoid constructing APInt. NFC. #86259

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 1 commit into from
Mar 26, 2024

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented Mar 22, 2024

This patch passes APInt by const reference in m_SpecificInt instead of by value. Specifically, it refactors m_SpecificInt(uint64_t V) to avoid APInt construction and dangling reference.

I believe it is safe to pass the APInt by const reference into m_SpecificInt even if it is a temporary.
See also https://en.cppreference.com/w/cpp/language/lifetime

All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created

Compile-time impact: https://llvm-compile-time-tracker.com/compare.php?from=d1f182c895728d89c5c3d198b133e212a5d9d4a3&to=7edf459b95ab2be33b70ec67faf87b3b8cc84f09&stat=instructions:u

@dtcxzyw dtcxzyw requested review from arsenm, nikic and RKSimon March 22, 2024 09:10
@llvmbot
Copy link
Member

llvmbot commented Mar 22, 2024

@llvm/pr-subscribers-llvm-ir

Author: Yingwei Zheng (dtcxzyw)

Changes

This patch passes APInt by const reference in m_SpecificInt instead of by value. Specifically, it refactors m_SpecificInt(uint64_t V) to avoid APInt construction and dangling reference.

I believe it is safe to pass the APInt by const reference into m_SpecificInt even if it is a temporary.
See also https://en.cppreference.com/w/cpp/language/lifetime
> All temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created

Compile-time impact: https://llvm-compile-time-tracker.com/compare.php?from=d1f182c895728d89c5c3d198b133e212a5d9d4a3&to=7edf459b95ab2be33b70ec67faf87b3b8cc84f09&stat=instructions:u


Full diff: https://github.com/llvm/llvm-project/pull/86259.diff

1 Files Affected:

  • (modified) llvm/include/llvm/IR/PatternMatch.h (+25-10)
diff --git a/llvm/include/llvm/IR/PatternMatch.h b/llvm/include/llvm/IR/PatternMatch.h
index 382009d9df785d..d857030014cd3b 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -884,9 +884,9 @@ struct bind_const_intval_ty {
 /// Match a specified integer value or vector of all elements of that
 /// value.
 template <bool AllowUndefs> struct specific_intval {
-  APInt Val;
+  const APInt &Val;
 
-  specific_intval(APInt V) : Val(std::move(V)) {}
+  specific_intval(const APInt &V) : Val(V) {}
 
   template <typename ITy> bool match(ITy *V) {
     const auto *CI = dyn_cast<ConstantInt>(V);
@@ -898,22 +898,37 @@ template <bool AllowUndefs> struct specific_intval {
   }
 };
 
+template <bool AllowUndefs> struct specific_intval64 {
+  uint64_t Val;
+
+  specific_intval64(uint64_t V) : Val(V) {}
+
+  template <typename ITy> bool match(ITy *V) {
+    const auto *CI = dyn_cast<ConstantInt>(V);
+    if (!CI && V->getType()->isVectorTy())
+      if (const auto *C = dyn_cast<Constant>(V))
+        CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndefs));
+
+    return CI && CI->getValue() == Val;
+  }
+};
+
 /// Match a specific integer value or vector with all elements equal to
 /// the value.
-inline specific_intval<false> m_SpecificInt(APInt V) {
-  return specific_intval<false>(std::move(V));
+inline specific_intval<false> m_SpecificInt(const APInt &V) {
+  return specific_intval<false>(V);
 }
 
-inline specific_intval<false> m_SpecificInt(uint64_t V) {
-  return m_SpecificInt(APInt(64, V));
+inline specific_intval64<false> m_SpecificInt(uint64_t V) {
+  return specific_intval64<false>(V);
 }
 
-inline specific_intval<true> m_SpecificIntAllowUndef(APInt V) {
-  return specific_intval<true>(std::move(V));
+inline specific_intval<true> m_SpecificIntAllowUndef(const APInt &V) {
+  return specific_intval<true>(V);
 }
 
-inline specific_intval<true> m_SpecificIntAllowUndef(uint64_t V) {
-  return m_SpecificIntAllowUndef(APInt(64, V));
+inline specific_intval64<true> m_SpecificIntAllowUndef(uint64_t V) {
+  return specific_intval64<true>(V);
 }
 
 /// Match a ConstantInt and bind to its value.  This does not match

@RKSimon
Copy link
Collaborator

RKSimon commented Mar 24, 2024

Should this be done for SDPatternMatch as well? I don't think MIPatternMatch has an equivalent though

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Mar 24, 2024

Should this be done for SDPatternMatch as well? I don't think MIPatternMatch has an equivalent though

SDPatternMatch always copies APInt :( We should refactor ISD::isConstantSplatVector first.

// === Constants ===
struct ConstantInt_match {
APInt *BindVal;
explicit ConstantInt_match(APInt *V) : BindVal(V) {}
template <typename MatchContext> bool match(const MatchContext &, SDValue N) {
// The logics here are similar to that in
// SelectionDAG::isConstantIntBuildVectorOrConstantInt, but the latter also
// treats GlobalAddressSDNode as a constant, which is difficult to turn into
// APInt.
if (auto *C = dyn_cast_or_null<ConstantSDNode>(N.getNode())) {
if (BindVal)
*BindVal = C->getAPIntValue();
return true;
}
APInt Discard;
return ISD::isConstantSplatVector(N.getNode(),
BindVal ? *BindVal : Discard);
}
};

struct SpecificInt_match {
APInt IntVal;
explicit SpecificInt_match(APInt APV) : IntVal(std::move(APV)) {}
template <typename MatchContext>
bool match(const MatchContext &Ctx, SDValue N) {
APInt ConstInt;
if (sd_context_match(N, Ctx, m_ConstInt(ConstInt)))
return APInt::isSameValue(IntVal, ConstInt);
return false;
}
};

Copy link
Contributor

@arsenm arsenm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a problem sanitizers should catch it

@dtcxzyw dtcxzyw merged commit 93d33a1 into llvm:main Mar 26, 2024
@dtcxzyw dtcxzyw deleted the perf/apint-pattern-match branch March 26, 2024 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants