Skip to content

[Hexagon] Add saturating add instructions #148132

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
Jul 11, 2025

Conversation

aankit-ca
Copy link
Contributor

@aankit-ca aankit-ca commented Jul 11, 2025

Generate the saturating add instructions for sadd.sat for scalar and vector instructions

Co-authored-by: Jyotsna Verma [email protected]

@llvmbot
Copy link
Member

llvmbot commented Jul 11, 2025

@llvm/pr-subscribers-backend-hexagon

Author: None (aankit-ca)

Changes

Generate the saturating add instructions for sadd.sat

sadd.sat.i32 -> A2_addsat
sadd.sat.i64 -> A2_addpsat


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

3 Files Affected:

  • (modified) llvm/lib/Target/Hexagon/HexagonISelLowering.cpp (+3)
  • (modified) llvm/lib/Target/Hexagon/HexagonPatterns.td (+8)
  • (added) llvm/test/CodeGen/Hexagon/addsat.ll (+22)
diff --git a/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp b/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp
index 00925ed42fcd4..d123a06cc5d9e 100644
--- a/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp
+++ b/llvm/lib/Target/Hexagon/HexagonISelLowering.cpp
@@ -1687,6 +1687,9 @@ HexagonTargetLowering::HexagonTargetLowering(const TargetMachine &TM,
     setOperationAction(ISD::SRL, VT, Custom);
   }
 
+  setOperationAction(ISD::SADDSAT, MVT::i32, Legal);
+  setOperationAction(ISD::SADDSAT, MVT::i64, Legal);
+
   // Extending loads from (native) vectors of i8 into (native) vectors of i16
   // are legal.
   setLoadExtAction(ISD::EXTLOAD,  MVT::v2i16, MVT::v2i8, Legal);
diff --git a/llvm/lib/Target/Hexagon/HexagonPatterns.td b/llvm/lib/Target/Hexagon/HexagonPatterns.td
index 2a991bafbf148..2337f185c7b36 100644
--- a/llvm/lib/Target/Hexagon/HexagonPatterns.td
+++ b/llvm/lib/Target/Hexagon/HexagonPatterns.td
@@ -1517,6 +1517,14 @@ def: Pat<(or  I32:$Rs, anyimm:$s10),   (A2_orir   I32:$Rs,  imm:$s10)>;
 def: Pat<(and I32:$Rs, anyimm:$s10),   (A2_andir  I32:$Rs,  imm:$s10)>;
 def: Pat<(sub anyimm:$s10, I32:$Rs),   (A2_subri  imm:$s10, I32:$Rs)>;
 
+class OpR_RR_pat_sat<InstHexagon MI, SDNode Op, ValueType ResType,
+                     PatFrag RxPred>
+  : Pat<(ResType (Op RxPred:$Rs, RxPred:$Rt)),
+        (MI RxPred:$Rs, RxPred:$Rt)>;
+
+def: OpR_RR_pat_sat<A2_addsat,  saddsat, i32, I32>;
+def: OpR_RR_pat_sat<A2_addpsat, saddsat, i64, I64>;
+
 def: OpR_RR_pat<A2_add,       Add,        i32,   I32>;
 def: OpR_RR_pat<A2_sub,       Sub,        i32,   I32>;
 def: OpR_RR_pat<A2_and,       And,        i32,   I32>;
diff --git a/llvm/test/CodeGen/Hexagon/addsat.ll b/llvm/test/CodeGen/Hexagon/addsat.ll
new file mode 100644
index 0000000000000..f958171ee3ae2
--- /dev/null
+++ b/llvm/test/CodeGen/Hexagon/addsat.ll
@@ -0,0 +1,22 @@
+; RUN: llc -march=hexagon < %s | FileCheck %s
+
+; Test for saturating add instructions.
+
+; CHECK-LABEL: test13
+; CHECK: r{{[0-9]+}} = add(r{{[0-9]+}},r{{[0-9]+}}):sat
+define i32 @test13(i32 %a0, i32 %a1) {
+entry:
+  %add = call i32 @llvm.sadd.sat.i32(i32 %a0, i32 %a1)
+  ret i32 %add
+}
+
+; CHECK-LABEL: test14
+; CHECK: r{{[0-9]+}}:{{[0-9]+}} = add(r{{[0-9]+}}:{{[0-9]+}},r{{[0-9]+}}:{{[0-9]+}}):sat
+define i64 @test14(i64 %a0, i64 %a1) {
+entry:
+  %add = call i64 @llvm.sadd.sat.i64(i64 %a0, i64 %a1)
+  ret i64 %add
+}
+
+declare i32 @llvm.sadd.sat.i32(i32, i32)
+declare i64 @llvm.sadd.sat.i64(i64, i64)

@aankit-ca aankit-ca changed the title [Hexagon] Add saturating scalar add for i32/i64 [Hexagon] Add saturating add instructions Jul 11, 2025
@aankit-ca aankit-ca requested a review from jverma-quic July 11, 2025 08:13
@svs-quic
Copy link
Contributor

svs-quic commented Jul 11, 2025

Hi Ankit, I dont see any changes for the vector instructions.

@androm3da
Copy link
Member

Hi Ankit, I dont see any changes for the vector instructions.

yeah - either the commit message is wrong or the vector instructions were omitted.

@jverma-quic
Copy link
Contributor

Hi Ankit, I dont see any changes for the vector instructions.

yeah - either the commit message is wrong or the vector instructions were omitted.

Agreed. The commit message needs to be updated. The changes in the PR are only for the scalar.

Generate the saturating add instructions for sadd.sat for scalar and
vector types

Co-authored-by: Jyotsna Verma <[email protected]>
Change-Id: Ie768876b3c6c33aafcf725a0b688dfcb9278e62c
@aankit-ca
Copy link
Contributor Author

aankit-ca commented Jul 11, 2025

My mistake, I forgot to git commit some of the changes. Fixed it now

Copy link

github-actions bot commented Jul 11, 2025

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

Change-Id: I52264b23b07eb4aba5bed1dcab9bb78e3ac1985b
@aankit-ca aankit-ca merged commit f9d3278 into llvm:main Jul 11, 2025
9 checks passed
@aankit-ca aankit-ca deleted the aankit/sat_add_sub branch July 11, 2025 22:00
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.

6 participants