Skip to content

[SPIR-V] Support saturation arithmetic intrinsics in SPIR-V Backend #91722

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
May 14, 2024
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
9 changes: 9 additions & 0 deletions llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,15 @@ bool SPIRVInstructionSelector::spvSelect(Register ResVReg,
case TargetOpcode::G_UMULH:
return selectExtInst(ResVReg, ResType, I, CL::u_mul_hi);

case TargetOpcode::G_SADDSAT:
return selectExtInst(ResVReg, ResType, I, CL::s_add_sat);
case TargetOpcode::G_UADDSAT:
return selectExtInst(ResVReg, ResType, I, CL::u_add_sat);
case TargetOpcode::G_SSUBSAT:
return selectExtInst(ResVReg, ResType, I, CL::s_sub_sat);
case TargetOpcode::G_USUBSAT:
return selectExtInst(ResVReg, ResType, I, CL::u_sub_sat);

case TargetOpcode::G_SEXT:
return selectExt(ResVReg, ResType, I, true);
case TargetOpcode::G_ANYEXT:
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/Target/SPIRV/SPIRVLegalizerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ SPIRVLegalizerInfo::SPIRVLegalizerInfo(const SPIRVSubtarget &ST) {

// Struct return types become a single scalar, so cannot easily legalize.
getActionDefinitionsBuilder({G_SMULH, G_UMULH}).alwaysLegal();

// supported saturation arithmetic
getActionDefinitionsBuilder({G_SADDSAT, G_UADDSAT, G_SSUBSAT, G_USUBSAT})
.legalFor(allIntScalarsAndVectors);
}

getLegacyLegalizerInfo().computeTables();
Expand Down
37 changes: 37 additions & 0 deletions llvm/test/CodeGen/SPIRV/llvm-intrinsics/satur-arith.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv32-unknown-unknown %s -o - -filetype=obj | spirv-val %}

; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}

; CHECK: OpExtInstImport "OpenCL.std"
; CHECK-DAG: OpName %[[#Foo:]] "foo"
; CHECK-DAG: OpName %[[#Bar:]] "bar"
; CHECK: %[[#Foo]] = OpFunction
; CHECK: %[[#]] = OpExtInst %[[#]] %[[#]] u_add_sat
; CHECK-NEXT: %[[#]] = OpExtInst %[[#]] %[[#]] u_sub_sat
; CHECK-NEXT: %[[#]] = OpExtInst %[[#]] %[[#]] s_add_sat
; CHECK-NEXT: %[[#]] = OpExtInst %[[#]] %[[#]] s_sub_sat
; CHECK: %[[#Bar]] = OpFunction
; CHECK: %[[#]] = OpExtInst %[[#]] %[[#]] u_add_sat
; CHECK-NEXT: %[[#]] = OpExtInst %[[#]] %[[#]] u_sub_sat
; CHECK-NEXT: %[[#]] = OpExtInst %[[#]] %[[#]] s_add_sat
; CHECK-NEXT: %[[#]] = OpExtInst %[[#]] %[[#]] s_sub_sat

define spir_func void @foo(i16 %x, i16 %y) {
entry:
%r1 = tail call i16 @llvm.uadd.sat.i16(i16 %x, i16 %y)
%r2 = tail call i16 @llvm.usub.sat.i16(i16 %x, i16 %y)
%r3 = tail call i16 @llvm.sadd.sat.i16(i16 %x, i16 %y)
%r4 = tail call i16 @llvm.ssub.sat.i16(i16 %x, i16 %y)
ret void
}

define spir_func void @bar(<4 x i32> %x, <4 x i32> %y) {
entry:
%r1 = tail call <4 x i32> @llvm.uadd.sat.v4i32(<4 x i32> %x, <4 x i32> %y)
%r2 = tail call <4 x i32> @llvm.usub.sat.v4i32(<4 x i32> %x, <4 x i32> %y)
%r3 = tail call <4 x i32> @llvm.sadd.sat.v4i32(<4 x i32> %x, <4 x i32> %y)
%r4 = tail call <4 x i32> @llvm.ssub.sat.v4i32(<4 x i32> %x, <4 x i32> %y)
ret void
}
Loading