[llvm] [SPIR-V] Add support for OpFMod intrinsic (PR #193172)
Arseniy Obolenskiy via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 21 02:57:27 PDT 2026
https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/193172
>From 7ec8b3bd3eb374c2528a134f55465006b66f2857 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Tue, 21 Apr 2026 11:54:26 +0200
Subject: [PATCH] [SPIR-V] Add support for OpFMod intrinsic
Add the spv.fmod intrinsic and lower it directly to SPIRV::OpFMod covering scalar and vector cases
---
llvm/include/llvm/IR/IntrinsicsSPIRV.td | 1 +
.../Target/SPIRV/SPIRVInstructionSelector.cpp | 5 +++++
.../scalar-floating-point-arithmetic.ll | 16 +++++++++++++++-
.../vector-floating-point-arithmetic.ll | 17 ++++++++++++++++-
4 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/IR/IntrinsicsSPIRV.td b/llvm/include/llvm/IR/IntrinsicsSPIRV.td
index d2a5fa1f08724..67f890d1d6dac 100644
--- a/llvm/include/llvm/IR/IntrinsicsSPIRV.td
+++ b/llvm/include/llvm/IR/IntrinsicsSPIRV.td
@@ -98,6 +98,7 @@ let TargetPrefix = "spv" in {
def int_spv_degrees : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty], [IntrNoMem]>;
def int_spv_distance : DefaultAttrsIntrinsic<[LLVMVectorElementType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>], [IntrNoMem]>;
def int_spv_faceforward : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>, LLVMMatchType<0>], [IntrNoMem]>;
+ def int_spv_fmod : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty, LLVMMatchType<0>], [IntrNoMem]>;
def int_spv_frac : DefaultAttrsIntrinsic<[LLVMMatchType<0>], [llvm_anyfloat_ty], [IntrNoMem]>;
def int_spv_isinf : DefaultAttrsIntrinsic<[LLVMScalarOrSameVectorWidth<0, llvm_i1_ty>],
[llvm_anyfloat_ty], [IntrNoMem]>;
diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
index f1e0450bb20f9..8a4efd631e851 100644
--- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
@@ -677,6 +677,7 @@ static bool intrinsicHasSideEffects(Intrinsic::ID ID) {
case Intrinsic::spv_firstbitlow:
case Intrinsic::spv_firstbitshigh:
case Intrinsic::spv_firstbituhigh:
+ case Intrinsic::spv_fmod:
case Intrinsic::spv_frac:
case Intrinsic::spv_gep:
case Intrinsic::spv_global_offset:
@@ -4763,6 +4764,10 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
return selectExtInst(ResVReg, ResType, I, CL::degrees, GL::Degrees);
case Intrinsic::spv_faceforward:
return selectExtInst(ResVReg, ResType, I, GL::FaceForward);
+ case Intrinsic::spv_fmod:
+ return selectOpWithSrcs(
+ ResVReg, ResType, I,
+ {I.getOperand(2).getReg(), I.getOperand(3).getReg()}, SPIRV::OpFMod);
case Intrinsic::spv_frac:
return selectExtInst(ResVReg, ResType, I, CL::fract, GL::Fract);
case Intrinsic::spv_isinf:
diff --git a/llvm/test/CodeGen/SPIRV/instructions/scalar-floating-point-arithmetic.ll b/llvm/test/CodeGen/SPIRV/instructions/scalar-floating-point-arithmetic.ll
index b04db946f36d0..0856a33ac3bb8 100644
--- a/llvm/test/CodeGen/SPIRV/instructions/scalar-floating-point-arithmetic.ll
+++ b/llvm/test/CodeGen/SPIRV/instructions/scalar-floating-point-arithmetic.ll
@@ -8,7 +8,7 @@
; CHECK-DAG: OpName [[SCALAR_FDIV:%.+]] "scalar_fdiv"
; CHECK-DAG: OpName [[SCALAR_FREM:%.+]] "scalar_frem"
; CHECK-DAG: OpName [[SCALAR_FMA:%.+]] "scalar_fma"
-;; FIXME: add test for OpFMod
+; CHECK-DAG: OpName [[SCALAR_FMOD:%.+]] "scalar_fmod"
; CHECK-NOT: DAG-FENCE
@@ -109,6 +109,20 @@ define float @scalar_frem(float %a, float %b) {
; CHECK: OpReturnValue [[C]]
; CHECK-NEXT: OpFunctionEnd
+;; Test fmod on scalar:
+define float @scalar_fmod(float %a, float %b) {
+ %c = call float @llvm.spv.fmod.f32(float %a, float %b)
+ ret float %c
+}
+
+; CHECK: [[SCALAR_FMOD]] = OpFunction [[SCALAR]] None [[SCALAR_FN]]
+; CHECK-NEXT: [[A:%.+]] = OpFunctionParameter [[SCALAR]]
+; CHECK-NEXT: [[B:%.+]] = OpFunctionParameter [[SCALAR]]
+; CHECK: OpLabel
+; CHECK: [[C:%.+]] = OpFMod [[SCALAR]] [[A]] [[B]]
+; CHECK: OpReturnValue [[C]]
+; CHECK-NEXT: OpFunctionEnd
+
declare float @llvm.fma.f32(float, float, float)
;; Test fma on scalar:
diff --git a/llvm/test/CodeGen/SPIRV/instructions/vector-floating-point-arithmetic.ll b/llvm/test/CodeGen/SPIRV/instructions/vector-floating-point-arithmetic.ll
index 0b0e505829ed1..67f427c52aeae 100644
--- a/llvm/test/CodeGen/SPIRV/instructions/vector-floating-point-arithmetic.ll
+++ b/llvm/test/CodeGen/SPIRV/instructions/vector-floating-point-arithmetic.ll
@@ -7,7 +7,7 @@
; CHECK-DAG: OpName [[VECTOR_FMUL:%.+]] "vector_fmul"
; CHECK-DAG: OpName [[VECTOR_FDIV:%.+]] "vector_fdiv"
; CHECK-DAG: OpName [[VECTOR_FREM:%.+]] "vector_frem"
-;; TODO: add test for OpFMod
+; CHECK-DAG: OpName [[VECTOR_FMOD:%.+]] "vector_fmod"
; CHECK-NOT: DAG-FENCE
@@ -106,3 +106,18 @@ define <2 x half> @vector_frem(<2 x half> %a, <2 x half> %b) {
; CHECK: [[C:%.+]] = OpFRem [[VECTOR]] [[A]] [[B]]
; CHECK: OpReturnValue [[C]]
; CHECK-NEXT: OpFunctionEnd
+
+
+;; Test fmod on vector:
+define <2 x half> @vector_fmod(<2 x half> %a, <2 x half> %b) {
+ %c = call <2 x half> @llvm.spv.fmod.v2f16(<2 x half> %a, <2 x half> %b)
+ ret <2 x half> %c
+}
+
+; CHECK: [[VECTOR_FMOD]] = OpFunction [[VECTOR]] None [[VECTOR_FN]]
+; CHECK-NEXT: [[A:%.+]] = OpFunctionParameter [[VECTOR]]
+; CHECK-NEXT: [[B:%.+]] = OpFunctionParameter [[VECTOR]]
+; CHECK: OpLabel
+; CHECK: [[C:%.+]] = OpFMod [[VECTOR]] [[A]] [[B]]
+; CHECK: OpReturnValue [[C]]
+; CHECK-NEXT: OpFunctionEnd
More information about the llvm-commits
mailing list