Skip to content

[DirectX] legalize usub.sat #135288

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 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
60 changes: 60 additions & 0 deletions llvm/test/CodeGen/DirectX/usub_sat.ll
Original file line number Diff line number Diff line change
@@ -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:.*:]]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to make the spacing consistent in these Check lines?

Copy link
Member Author

Choose a reason for hiding this comment

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

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

; 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
}
Loading