Merged
Conversation
fixes llvm#135285 This change implements the usub.sat intrinsic to perform an unsigned saturating subtraction on the 2 arguments. The minimum value this operation is clamp to is 0.
Member
|
@llvm/pr-subscribers-backend-directx Author: Farzon Lotfi (farzonl) Changesfixes #135285 This change implements the Full diff: https://github.com/llvm/llvm-project/pull/135288.diff 2 Files Affected:
diff --git a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
index 84acf4d536d0c..70f284e08b250 100644
--- a/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
+++ b/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
@@ -65,12 +65,27 @@ static bool isIntrinsicExpansion(Function &F) {
case Intrinsic::dx_sign:
case Intrinsic::dx_step:
case Intrinsic::dx_radians:
+ case Intrinsic::usub_sat:
case Intrinsic::vector_reduce_add:
case Intrinsic::vector_reduce_fadd:
return true;
}
return false;
}
+
+static Value *expandUsubSat(CallInst *Orig) {
+ Value *A = Orig->getArgOperand(0);
+ Value *B = Orig->getArgOperand(1);
+ Type *Ty = A->getType();
+
+ IRBuilder<> Builder(Orig);
+
+ Value *Cmp = Builder.CreateICmpULT(A, B, "usub.cmp");
+ Value *Sub = Builder.CreateSub(A, B, "usub.sub");
+ Value *Zero = ConstantInt::get(Ty, 0);
+ return Builder.CreateSelect(Cmp, Zero, Sub, "usub.sat");
+}
+
static Value *expandVecReduceAdd(CallInst *Orig, Intrinsic::ID IntrinsicId) {
assert(IntrinsicId == Intrinsic::vector_reduce_add ||
IntrinsicId == Intrinsic::vector_reduce_fadd);
@@ -586,6 +601,9 @@ static bool expandIntrinsic(Function &F, CallInst *Orig) {
case Intrinsic::dx_radians:
Result = expandRadiansIntrinsic(Orig);
break;
+ case Intrinsic::usub_sat:
+ Result = expandUsubSat(Orig);
+ break;
case Intrinsic::vector_reduce_add:
case Intrinsic::vector_reduce_fadd:
Result = expandVecReduceAdd(Orig, IntrinsicId);
diff --git a/llvm/test/CodeGen/DirectX/usub_sat.ll b/llvm/test/CodeGen/DirectX/usub_sat.ll
new file mode 100644
index 0000000000000..8cfb1a1fe9bd1
--- /dev/null
+++ b/llvm/test/CodeGen/DirectX/usub_sat.ll
@@ -0,0 +1,60 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -dxil-intrinsic-expansion -mtriple=dxil-pc-shadermodel6.3-library %s | FileCheck %s
+
+; Make sure dxil operation function calls for pow are generated.
+
+define noundef i16 @usub_sat_i16(i16 noundef %a, i16 noundef %b) {
+; CHECK-LABEL: define noundef i16 @usub_sat_i16(
+; CHECK-SAME: i16 noundef [[A:%.*]], i16 noundef [[B:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[USUB_CMP:%.*]] = icmp ult i16 [[A]], [[B]]
+; CHECK-NEXT: [[USUB_SUB:%.*]] = sub i16 [[A]], [[B]]
+; CHECK-NEXT: [[ELT_USUB_SAT:%.*]] = select i1 [[USUB_CMP]], i16 0, i16 [[USUB_SUB]]
+; CHECK-NEXT: ret i16 [[ELT_USUB_SAT]]
+;
+entry:
+ %elt.usub_sat = call i16 @llvm.usub.sat.i16(i16 %a, i16 %b)
+ ret i16 %elt.usub_sat
+}
+
+define noundef i32 @usub_sat_i32(i32 noundef %a, i32 noundef %b) {
+; CHECK-LABEL: define noundef i32 @usub_sat_i32(
+; CHECK-SAME: i32 noundef [[A:%.*]], i32 noundef [[B:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[USUB_CMP:%.*]] = icmp ult i32 [[A]], [[B]]
+; CHECK-NEXT: [[USUB_SUB:%.*]] = sub i32 [[A]], [[B]]
+; CHECK-NEXT: [[ELT_USUB_SAT:%.*]] = select i1 [[USUB_CMP]], i32 0, i32 [[USUB_SUB]]
+; CHECK-NEXT: ret i32 [[ELT_USUB_SAT]]
+;
+entry:
+ %elt.usub_sat = call i32 @llvm.usub.sat.i32(i32 %a, i32 %b)
+ ret i32 %elt.usub_sat
+}
+
+define noundef i64 @usub_sat_i64(i64 noundef %a, i64 noundef %b) {
+; CHECK-LABEL: define noundef i64 @usub_sat_i64(
+; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[USUB_CMP:%.*]] = icmp ult i64 [[A]], [[B]]
+; CHECK-NEXT: [[USUB_SUB:%.*]] = sub i64 [[A]], [[B]]
+; CHECK-NEXT: [[ELT_USUB_SAT:%.*]] = select i1 [[USUB_CMP]], i64 0, i64 [[USUB_SUB]]
+; CHECK-NEXT: ret i64 [[ELT_USUB_SAT]]
+;
+entry:
+ %elt.usub_sat = call i64 @llvm.usub.sat.i64(i64 %a, i64 %b)
+ ret i64 %elt.usub_sat
+}
+
+define noundef <4 x i32> @usub_sat_vec(<4 x i32> noundef %a, <4 x i32> noundef %b) {
+; CHECK-LABEL: define noundef <4 x i32> @usub_sat_vec(
+; CHECK-SAME: <4 x i32> noundef [[A:%.*]], <4 x i32> noundef [[B:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[USUB_CMP:%.*]] = icmp ult <4 x i32> [[A]], [[B]]
+; CHECK-NEXT: [[USUB_SUB:%.*]] = sub <4 x i32> [[A]], [[B]]
+; CHECK-NEXT: [[ELT_USUB_SAT:%.*]] = select <4 x i1> [[USUB_CMP]], <4 x i32> zeroinitializer, <4 x i32> [[USUB_SUB]]
+; CHECK-NEXT: ret <4 x i32> [[ELT_USUB_SAT]]
+;
+entry:
+ %elt.usub_sat = call <4 x i32> @llvm.usub.sat.v4i32(<4 x i32> %a, <4 x i32> %b)
+ ret <4 x i32> %elt.usub_sat
+}
|
Icohedron
approved these changes
Apr 11, 2025
spall
approved these changes
Apr 11, 2025
| define noundef i16 @usub_sat_i16(i16 noundef %a, i16 noundef %b) { | ||
| ; CHECK-LABEL: define noundef i16 @usub_sat_i16( | ||
| ; CHECK-SAME: i16 noundef [[A:%.*]], i16 noundef [[B:%.*]]) { | ||
| ; CHECK-NEXT: [[ENTRY:.*:]] |
Contributor
There was a problem hiding this comment.
Is it possible to make the spacing consistent in these Check lines?
Member
Author
There was a problem hiding this comment.
its tool generated. I tried changing it and see if the tool would leave it alone, but the next run of update_test_checks.py changes the formatting back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #135285
This change implements the
usub.satintrinsic to perform an unsigned saturating subtraction on the 2 arguments.The minimum value this operation is clamp to is 0.