[llvm] [GISel] Add G_FDIV to CSE (PR #123624)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 20 19:58:44 PST 2025
https://github.com/lialan updated https://github.com/llvm/llvm-project/pull/123624
>From ec075b92842003a351c6012b35858a7ed697783d Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Mon, 20 Jan 2025 22:05:08 +0800
Subject: [PATCH 1/4] First commit
---
llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp | 1 +
llvm/unittests/CodeGen/GlobalISel/CSETest.cpp | 32 +++++++++++++++++++
2 files changed, 33 insertions(+)
diff --git a/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp b/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp
index 0ac4a8a0aa910b..4e225cccdec65f 100644
--- a/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp
@@ -65,6 +65,7 @@ bool CSEConfigFull::shouldCSEOpc(unsigned Opc) {
case TargetOpcode::G_BUILD_VECTOR:
case TargetOpcode::G_BUILD_VECTOR_TRUNC:
case TargetOpcode::G_SEXT_INREG:
+ case TargetOpcode::G_FDIV:
return true;
}
return false;
diff --git a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
index 822707a1f4ed32..4d3ff1ffcc1583 100644
--- a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
@@ -9,10 +9,36 @@
#include "GISelMITest.h"
#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
#include "llvm/CodeGen/GlobalISel/CSEMIRBuilder.h"
+#include "llvm/CodeGenTypes/LowLevelType.h"
#include "gtest/gtest.h"
namespace {
+TEST_F(AArch64GISelMITest, TestFPCSE) {
+ setUp();
+ if (!TM)
+ GTEST_SKIP();
+ LLT s16{LLT::scalar(16)};
+ LLT s32{LLT::scalar(32)};
+
+ GISelCSEInfo CSEInfo;
+ CSEInfo.setCSEConfig(std::make_unique<CSEConfigFull>());
+ CSEInfo.analyze(*MF);
+ B.setCSEInfo(&CSEInfo);
+ CSEMIRBuilder CSEB(B.getState());
+
+ CSEB.setInsertPt(B.getMBB(), B.getInsertPt());
+
+ // Build some fp constants.
+ auto MIBFP0 = CSEB.buildFConstant(s32, 1.0);
+ auto MIBFP0_1 = CSEB.buildFConstant(s32, 1.0);
+ EXPECT_TRUE(&*MIBFP0 == &*MIBFP0_1);
+ CSEInfo.print();
+
+ // Build some
+
+}
+
TEST_F(AArch64GISelMITest, TestCSE) {
setUp();
if (!TM)
@@ -75,6 +101,11 @@ TEST_F(AArch64GISelMITest, TestCSE) {
auto MIBUnmerge2 = CSEB.buildUnmerge({s32, s32}, Copies[0]);
EXPECT_TRUE(&*MIBUnmerge == &*MIBUnmerge2);
+ // Check G_FDIV
+ auto MIBFDiv = CSEB.buildFDiv(s32, Copies[0], Copies[1]);
+ auto MIBFDiv2 = CSEB.buildFDiv(s32, Copies[0], Copies[1]);
+ EXPECT_TRUE(&*MIBFDiv == &*MIBFDiv2);
+
// Check G_BUILD_VECTOR
Register Reg1 = MRI->createGenericVirtualRegister(s32);
Register Reg2 = MRI->createGenericVirtualRegister(s32);
@@ -100,6 +131,7 @@ TEST_F(AArch64GISelMITest, TestCSE) {
auto Undef1 = CSEB.buildUndef(s32);
EXPECT_EQ(&*Undef0, &*Undef1);
+
// If the observer is installed to the MF, CSE can also
// track new instructions built without the CSEBuilder and
// the newly built instructions are available for CSEing next
>From 36db9290adf5f49603b2ffe8e2631bb4659496fa Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Tue, 21 Jan 2025 00:19:42 +0800
Subject: [PATCH 2/4] remote useless code
---
llvm/unittests/CodeGen/GlobalISel/CSETest.cpp | 25 -------------------
1 file changed, 25 deletions(-)
diff --git a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
index 4d3ff1ffcc1583..4e2b5a1eb37136 100644
--- a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
@@ -14,31 +14,6 @@
namespace {
-TEST_F(AArch64GISelMITest, TestFPCSE) {
- setUp();
- if (!TM)
- GTEST_SKIP();
- LLT s16{LLT::scalar(16)};
- LLT s32{LLT::scalar(32)};
-
- GISelCSEInfo CSEInfo;
- CSEInfo.setCSEConfig(std::make_unique<CSEConfigFull>());
- CSEInfo.analyze(*MF);
- B.setCSEInfo(&CSEInfo);
- CSEMIRBuilder CSEB(B.getState());
-
- CSEB.setInsertPt(B.getMBB(), B.getInsertPt());
-
- // Build some fp constants.
- auto MIBFP0 = CSEB.buildFConstant(s32, 1.0);
- auto MIBFP0_1 = CSEB.buildFConstant(s32, 1.0);
- EXPECT_TRUE(&*MIBFP0 == &*MIBFP0_1);
- CSEInfo.print();
-
- // Build some
-
-}
-
TEST_F(AArch64GISelMITest, TestCSE) {
setUp();
if (!TM)
>From fca34c5355bb03a39c712e5459f359610d57cf5b Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Tue, 21 Jan 2025 09:08:33 +0800
Subject: [PATCH 3/4] linting
---
llvm/unittests/CodeGen/GlobalISel/CSETest.cpp | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
index 4e2b5a1eb37136..8503be09ed5539 100644
--- a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
@@ -106,7 +106,6 @@ TEST_F(AArch64GISelMITest, TestCSE) {
auto Undef1 = CSEB.buildUndef(s32);
EXPECT_EQ(&*Undef0, &*Undef1);
-
// If the observer is installed to the MF, CSE can also
// track new instructions built without the CSEBuilder and
// the newly built instructions are available for CSEing next
>From a20558791c2469abc40ffe852034802270a16a27 Mon Sep 17 00:00:00 2001
From: Alan Li <me at alanli.org>
Date: Tue, 21 Jan 2025 11:57:49 +0800
Subject: [PATCH 4/4] Experiment: Adding more fp opcodes to fold
---
llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp | 5 ++++
llvm/unittests/CodeGen/GlobalISel/CSETest.cpp | 25 +++++++++++++++++++
2 files changed, 30 insertions(+)
diff --git a/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp b/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp
index 4e225cccdec65f..7c21a6874db36d 100644
--- a/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CSEInfo.cpp
@@ -65,7 +65,12 @@ bool CSEConfigFull::shouldCSEOpc(unsigned Opc) {
case TargetOpcode::G_BUILD_VECTOR:
case TargetOpcode::G_BUILD_VECTOR_TRUNC:
case TargetOpcode::G_SEXT_INREG:
+ case TargetOpcode::G_FADD:
+ case TargetOpcode::G_FSUB:
+ case TargetOpcode::G_FMUL:
case TargetOpcode::G_FDIV:
+ case TargetOpcode::G_FNEG:
+ case TargetOpcode::G_FABS:
return true;
}
return false;
diff --git a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
index 8503be09ed5539..db964d1b29b4ea 100644
--- a/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/CSETest.cpp
@@ -76,11 +76,36 @@ TEST_F(AArch64GISelMITest, TestCSE) {
auto MIBUnmerge2 = CSEB.buildUnmerge({s32, s32}, Copies[0]);
EXPECT_TRUE(&*MIBUnmerge == &*MIBUnmerge2);
+ // Check G_FADD
+ auto MIBFAdd = CSEB.buildFAdd(s32, Copies[0], Copies[1]);
+ auto MIBFAdd2 = CSEB.buildFAdd(s32, Copies[0], Copies[1]);
+ EXPECT_TRUE(&*MIBFAdd == &*MIBFAdd2);
+
+ // Check G_FSUB
+ auto MIBFSub = CSEB.buildFSub(s32, Copies[0], Copies[1]);
+ auto MIBFSub2 = CSEB.buildFSub(s32, Copies[0], Copies[1]);
+ EXPECT_TRUE(&*MIBFSub == &*MIBFSub2);
+
+ // Check G_FMUL
+ auto MIBFMul = CSEB.buildFMul(s32, Copies[0], Copies[1]);
+ auto MIBFMul2 = CSEB.buildFMul(s32, Copies[0], Copies[1]);
+ EXPECT_TRUE(&*MIBFMul == &*MIBFMul2);
+
// Check G_FDIV
auto MIBFDiv = CSEB.buildFDiv(s32, Copies[0], Copies[1]);
auto MIBFDiv2 = CSEB.buildFDiv(s32, Copies[0], Copies[1]);
EXPECT_TRUE(&*MIBFDiv == &*MIBFDiv2);
+ // Check G_FNEG
+ auto MIBFNeg = CSEB.buildFNeg(s32, Copies[0]);
+ auto MIBFNeg2 = CSEB.buildFNeg(s32, Copies[0]);
+ EXPECT_TRUE(&*MIBFNeg == &*MIBFNeg2);
+
+ // Check G_FABS
+ auto MIBFAbs = CSEB.buildFAbs(s32, Copies[0]);
+ auto MIBFAbs2 = CSEB.buildFAbs(s32, Copies[0]);
+ EXPECT_TRUE(&*MIBFAbs == &*MIBFAbs2);
+
// Check G_BUILD_VECTOR
Register Reg1 = MRI->createGenericVirtualRegister(s32);
Register Reg2 = MRI->createGenericVirtualRegister(s32);
More information about the llvm-commits
mailing list