Skip to content

Conversation

zhaoqi5
Copy link
Contributor

@zhaoqi5 zhaoqi5 commented Sep 10, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Sep 10, 2025

@llvm/pr-subscribers-backend-loongarch

Author: ZhaoQi (zhaoqi5)

Changes

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

2 Files Affected:

  • (modified) llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp (+17)
  • (modified) llvm/lib/Target/LoongArch/LoongArchISelLowering.h (+4)
diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
index 634914d3b3fd0..f3733f04d13d3 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
@@ -9094,3 +9094,20 @@ bool LoongArchTargetLowering::SimplifyDemandedBitsForTargetNode(
   return TargetLowering::SimplifyDemandedBitsForTargetNode(
       Op, OriginalDemandedBits, OriginalDemandedElts, Known, TLO, Depth);
 }
+
+bool LoongArchTargetLowering::isExtractSubvectorCheap(EVT ResVT, EVT SrcVT,
+                                                      unsigned Index) const {
+  if (!isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, ResVT))
+    return false;
+
+  // Extract a 128-bit subvector from index 0 of a 256-bit vector is free.
+  return Index == 0;
+}
+
+bool LoongArchTargetLowering::isExtractVecEltCheap(EVT VT,
+                                                   unsigned Index) const {
+  EVT EltVT = VT.getScalarType();
+
+  // Extract a scalar FP value from index 0 of a vector is free.
+  return (EltVT == MVT::f32 || EltVT == MVT::f64) && Index == 0;
+}
diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.h b/llvm/lib/Target/LoongArch/LoongArchISelLowering.h
index 9d14934a9d363..1495e98829e74 100644
--- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.h
+++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.h
@@ -332,6 +332,10 @@ class LoongArchTargetLowering : public TargetLowering {
                                          TargetLoweringOpt &TLO,
                                          unsigned Depth) const override;
 
+  bool isExtractSubvectorCheap(EVT ResVT, EVT SrcVT,
+                               unsigned Index) const override;
+  bool isExtractVecEltCheap(EVT VT, unsigned Index) const override;
+
 private:
   /// Target-specific function used to lower LoongArch calling conventions.
   typedef bool LoongArchCCAssignFn(const DataLayout &DL, LoongArchABI::ABI ABI,

Copy link
Contributor

@SixWeining SixWeining left a comment

Choose a reason for hiding this comment

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

Are there any test cases that have benefited from this?

@zhaoqi5
Copy link
Contributor Author

zhaoqi5 commented Sep 11, 2025

Are there any test cases that have benefited from this?

Mark extract floating point element from index 0 as cheap will lead to scalarize. Currently, we use float loads to get the scalar floating point constant, but use code sequence to generate the constants in vectors. So scalarization will get worse code in some cases.

A new pr (#158050) was just pushed to change the way of getting fp constant. After it is approved, I will find some tests for this commit.

@zhaoqi5 zhaoqi5 force-pushed the users/zhaoqi5/override-isxxxcheap-hooks branch from c08f34e to d516d07 Compare September 24, 2025 09:48
@zhaoqi5 zhaoqi5 changed the base branch from main to users/zhaoqi5/test-isxxxcheap September 24, 2025 09:49
@zhaoqi5 zhaoqi5 requested a review from heiher September 26, 2025 11:27
Copy link
Member

@heiher heiher left a comment

Choose a reason for hiding this comment

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

LGTM.

@zhaoqi5 zhaoqi5 force-pushed the users/zhaoqi5/test-isxxxcheap branch from 1362a74 to 6b0057d Compare September 29, 2025 06:52
Base automatically changed from users/zhaoqi5/test-isxxxcheap to main September 29, 2025 07:27
zhaoqi5 added a commit that referenced this pull request Sep 29, 2025
@zhaoqi5 zhaoqi5 force-pushed the users/zhaoqi5/override-isxxxcheap-hooks branch from 4321d4f to dca42a8 Compare September 29, 2025 07:32
@zhaoqi5 zhaoqi5 enabled auto-merge (squash) September 29, 2025 07:34
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Sep 29, 2025
@zhaoqi5 zhaoqi5 merged commit 99774ec into main Sep 29, 2025
9 checks passed
@zhaoqi5 zhaoqi5 deleted the users/zhaoqi5/override-isxxxcheap-hooks branch September 29, 2025 08:02
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants