Skip to content

[DAG] Add SDPatternMatch::m_BitCast matcher #123327

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 2 commits into from
Jan 17, 2025
Merged

Conversation

RKSimon
Copy link
Collaborator

@RKSimon RKSimon commented Jan 17, 2025

Simplifies a future patch

@RKSimon RKSimon requested a review from mshockwave January 17, 2025 11:38
@llvmbot llvmbot added the llvm:SelectionDAG SelectionDAGISel as well label Jan 17, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 17, 2025

@llvm/pr-subscribers-llvm-selectiondag

Author: Simon Pilgrim (RKSimon)

Changes

Simplifies a future patch


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

3 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/SDPatternMatch.h (+5)
  • (modified) llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp (+1-1)
  • (modified) llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp (+5)
diff --git a/llvm/include/llvm/CodeGen/SDPatternMatch.h b/llvm/include/llvm/CodeGen/SDPatternMatch.h
index 4faa090901a6a6..5c9e829cc60e67 100644
--- a/llvm/include/llvm/CodeGen/SDPatternMatch.h
+++ b/llvm/include/llvm/CodeGen/SDPatternMatch.h
@@ -896,6 +896,11 @@ inline UnaryOpc_match<Opnd, true> m_ChainedUnaryOp(unsigned Opc,
   return UnaryOpc_match<Opnd, true>(Opc, Op);
 }
 
+template <typename Opnd>
+inline UnaryOpc_match<Opnd> m_BitCast(const Opnd &Op) {
+  return UnaryOpc_match<Opnd>(ISD::BITCAST, Op);
+}
+
 template <typename Opnd>
 inline UnaryOpc_match<Opnd> m_BSwap(const Opnd &Op) {
   return UnaryOpc_match<Opnd>(ISD::BSWAP, Op);
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index de7fb21f5903e3..49e5b7d9ef0141 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -15770,7 +15770,7 @@ SDValue DAGCombiner::foldBitcastedFPLogic(SDNode *N, SelectionDAG &DAG,
   // FIXME: I don't think looking for bitcast intrinsically makes sense, but
   // removing this would require more changes.
   auto IsBitCastOrFree = [&TLI, FPOpcode](SDValue Op, EVT VT) {
-    if (Op.getOpcode() == ISD::BITCAST && Op.getOperand(0).getValueType() == VT)
+    if (sd_match(Op, m_BitCast(m_SpecificVT(VT))))
       return true;
 
     return FPOpcode == ISD::FABS ? TLI.isFAbsFree(VT) : TLI.isFNegFree(VT);
diff --git a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
index bf9c597d8ac5e1..736a36da97f577 100644
--- a/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
+++ b/llvm/unittests/CodeGen/SelectionDAGPatternMatchTest.cpp
@@ -392,6 +392,7 @@ TEST_F(SelectionDAGPatternMatchTest, matchUnaryOp) {
   SDValue FPToSI = DAG->getNode(ISD::FP_TO_SINT, DL, FloatVT, Op2);
   SDValue FPToUI = DAG->getNode(ISD::FP_TO_UINT, DL, FloatVT, Op2);
 
+  SDValue Bcast = DAG->getNode(ISD::BITCAST, DL, FloatVT, Op0);
   SDValue Brev = DAG->getNode(ISD::BITREVERSE, DL, Int32VT, Op0);
   SDValue Bswap = DAG->getNode(ISD::BSWAP, DL, Int32VT, Op0);
 
@@ -423,8 +424,12 @@ TEST_F(SelectionDAGPatternMatchTest, matchUnaryOp) {
   EXPECT_FALSE(sd_match(FPToUI, m_FPToSI(m_Value())));
   EXPECT_FALSE(sd_match(FPToSI, m_FPToUI(m_Value())));
 
+  EXPECT_TRUE(sd_match(Bcast, m_BitCast(m_Value())));
+  EXPECT_TRUE(sd_match(Bcast, m_BitCast(m_SpecificVT(MVT::i32))));
   EXPECT_TRUE(sd_match(Brev, m_BitReverse(m_Value())));
   EXPECT_TRUE(sd_match(Bswap, m_BSwap(m_Value())));
+  EXPECT_FALSE(sd_match(Bcast, m_BitReverse(m_Value())));
+  EXPECT_FALSE(sd_match(Bcast, m_BitCast(m_SpecificVT(MVT::f32))));
   EXPECT_FALSE(sd_match(Brev, m_BSwap(m_Value())));
   EXPECT_FALSE(sd_match(Bswap, m_BitReverse(m_Value())));
 

Copy link

github-actions bot commented Jan 17, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@RKSimon RKSimon merged commit bacfdcd into llvm:main Jan 17, 2025
5 of 7 checks passed
@RKSimon RKSimon deleted the dag-match-bitcast branch January 17, 2025 12:22
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jan 17, 2025

LLVM Buildbot has detected a new failure on builder premerge-monolithic-linux running on premerge-linux-1 while building llvm at step 7 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/153/builds/20049

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'Flang :: Lower/module_use.f90' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: rm -fr /build/buildbot/premerge-monolithic-linux/build/tools/flang/test/Lower/Output/module_use.f90.tmp && mkdir -p /build/buildbot/premerge-monolithic-linux/build/tools/flang/test/Lower/Output/module_use.f90.tmp
+ rm -fr /build/buildbot/premerge-monolithic-linux/build/tools/flang/test/Lower/Output/module_use.f90.tmp
+ mkdir -p /build/buildbot/premerge-monolithic-linux/build/tools/flang/test/Lower/Output/module_use.f90.tmp
RUN: at line 2: bbc -emit-fir -module /build/buildbot/premerge-monolithic-linux/build/tools/flang/test/Lower/Output/module_use.f90.tmp /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Lower/module_definition.f90
+ bbc -emit-fir -module /build/buildbot/premerge-monolithic-linux/build/tools/flang/test/Lower/Output/module_use.f90.tmp /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Lower/module_definition.f90
RUN: at line 3: bbc -emit-fir -J /build/buildbot/premerge-monolithic-linux/build/tools/flang/test/Lower/Output/module_use.f90.tmp /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Lower/module_use.f90 -o - | /build/buildbot/premerge-monolithic-linux/build/bin/FileCheck /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Lower/module_use.f90
+ bbc -emit-fir -J /build/buildbot/premerge-monolithic-linux/build/tools/flang/test/Lower/Output/module_use.f90.tmp /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Lower/module_use.f90 -o -
+ /build/buildbot/premerge-monolithic-linux/build/bin/FileCheck /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Lower/module_use.f90
/build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Lower/module_use.f90:17:15: error: CHECK-DAG: expected string not found in input
 ! CHECK-DAG: fir.address_of(@_QMm1Ey) : !fir.ref<!fir.array<100xi32>>
              ^
<stdin>:5:23: note: scanning from here
 func.func @_QPm1use() -> f32 {
                      ^
<stdin>:10:7: note: possible intended match here
 %3 = fir.address_of(@_QMm1Ex) : !fir.ref<f32>
      ^
/build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Lower/module_use.f90:44:14: error: CHECK-DAG: expected string not found in input
! CHECK-DAG: fir.global @_QMm1Ey : !fir.array<100xi32>
             ^
<stdin>:20:31: note: scanning from here
 func.func @_QPmodcommon1use() -> f32 {
                              ^
<stdin>:49:2: note: possible intended match here
 fir.global @_QMm1Ex : f32
 ^

Input file: <stdin>
Check file: /build/buildbot/premerge-monolithic-linux/llvm-project/flang/test/Lower/module_use.f90

-dump-input=help explains the following input dump.

Input was:
<<<<<<
          1: module attributes {dlti.dl_spec = #dlti.dl_spec<i32 = dense<32> : vector<2xi64>, f64 = dense<64> : vector<2xi64>, !llvm.ptr<270> = dense<32> : vector<4xi64>, f16 = dense<16> : vector<2xi64>, !llvm.ptr = dense<64> : vector<4xi64>, i1 = dense<8> : vector<2xi64>, i8 = dense<8> : vector<2xi64>, i16 = dense<16> : vector<2xi64>, i128 = dense<128> : vector<2xi64>, f80 = dense<128> : vector<2xi64>, f128 = dense<128> : vector<2xi64>, !llvm.ptr<271> = dense<32> : vector<4xi64>, !llvm.ptr<272> = dense<64> : vector<4xi64>, i64 = dense<64> : vector<2xi64>, "dlti.endianness" = "little", "dlti.stack_alignment" = 128 : i64>, fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", llvm.ident = "flang version 20.0.0 (https://github.com/llvm/llvm-project.git bacfdcd7e0989117a3c76b040fe9efe093fa8708)", llvm.target_triple = "x86_64-unknown-linux-gnu"} { 
          2:  fir.global common @__BLNK__(dense<0> : vector<4xi8>) {alignment = 4 : i64} : !fir.array<4xi8> 
          3:  fir.global common @named1_(dense<0> : vector<4xi8>) {alignment = 4 : i64} : !fir.array<4xi8> 
          4:  fir.global common @named2_(dense<0> : vector<4xi8>) {alignment = 4 : i64} : !fir.array<4xi8> 
          5:  func.func @_QPm1use() -> f32 { 
dag:17'0                           X~~~~~~~~~ error: no match found
          6:  %c1_i32 = arith.constant 1 : i32 
dag:17'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          7:  %0 = fir.alloca i32 {adapt.valuebyref} 
dag:17'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          8:  %1 = fir.alloca f32 {bindc_name = "m1use", uniq_name = "_QFm1useEm1use"} 
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:SelectionDAG SelectionDAGISel as well
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants