Skip to content

Conversation

SubashBoopathi
Copy link
Contributor

Added SPIR-V support for constrained floating-point comparison intrinsics (fcmp, fcmps) with lowering and tests.

Copy link

github-actions bot commented Sep 8, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 8, 2025

@llvm/pr-subscribers-backend-spir-v

Author: Subash B (SubashBoopathi)

Changes

Added SPIR-V support for constrained floating-point comparison intrinsics (fcmp, fcmps) with lowering and tests.


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

2 Files Affected:

  • (modified) llvm/lib/Target/SPIRV/SPIRVPrepareFunctions.cpp (+24)
  • (added) llvm/test/CodeGen/SPIRV/llvm-intrinsics/constrained-comparison.ll (+56)
diff --git a/llvm/lib/Target/SPIRV/SPIRVPrepareFunctions.cpp b/llvm/lib/Target/SPIRV/SPIRVPrepareFunctions.cpp
index 2bffbf73b574a..c10bfc66fdcb8 100644
--- a/llvm/lib/Target/SPIRV/SPIRVPrepareFunctions.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVPrepareFunctions.cpp
@@ -335,6 +335,21 @@ static void lowerFunnelShifts(IntrinsicInst *FSHIntrinsic) {
   FSHIntrinsic->setCalledFunction(FSHFunc);
 }
 
+static void lowerConstrainedFPCmpIntrinsic(
+    ConstrainedFPCmpIntrinsic *ConstrainedCmpIntrinsic,
+    SmallVector<Instruction *> &EraseFromParent) {
+  if (!ConstrainedCmpIntrinsic)
+    return;
+  // Extract the floating-point values being compared
+  Value *LHS = ConstrainedCmpIntrinsic->getArgOperand(0);
+  Value *RHS = ConstrainedCmpIntrinsic->getArgOperand(1);
+  FCmpInst::Predicate Pred = ConstrainedCmpIntrinsic->getPredicate();
+  IRBuilder<> Builder(ConstrainedCmpIntrinsic);
+  Value *FCmp = Builder.CreateFCmp(Pred, LHS, RHS);
+  ConstrainedCmpIntrinsic->replaceAllUsesWith(FCmp);
+  EraseFromParent.push_back(dyn_cast<Instruction>(ConstrainedCmpIntrinsic));
+}
+
 static void lowerExpectAssume(IntrinsicInst *II) {
   // If we cannot use the SPV_KHR_expect_assume extension, then we need to
   // ignore the intrinsic and move on. It should be removed later on by LLVM.
@@ -379,6 +394,7 @@ static bool toSpvOverloadedIntrinsic(IntrinsicInst *II, Intrinsic::ID NewID,
 bool SPIRVPrepareFunctions::substituteIntrinsicCalls(Function *F) {
   bool Changed = false;
   const SPIRVSubtarget &STI = TM.getSubtarget<SPIRVSubtarget>(*F);
+  SmallVector<Instruction *> EraseFromParent;
   for (BasicBlock &BB : *F) {
     for (Instruction &I : BB) {
       auto Call = dyn_cast<CallInst>(&I);
@@ -420,9 +436,17 @@ bool SPIRVPrepareFunctions::substituteIntrinsicCalls(Function *F) {
         lowerPtrAnnotation(II);
         Changed = true;
         break;
+      case Intrinsic::experimental_constrained_fcmp:
+      case Intrinsic::experimental_constrained_fcmps:
+        lowerConstrainedFPCmpIntrinsic(dyn_cast<ConstrainedFPCmpIntrinsic>(II),
+                                       EraseFromParent);
+        Changed = true;
+        break;
       }
     }
   }
+  for (auto *I : EraseFromParent)
+    I->eraseFromParent();
   return Changed;
 }
 
diff --git a/llvm/test/CodeGen/SPIRV/llvm-intrinsics/constrained-comparison.ll b/llvm/test/CodeGen/SPIRV/llvm-intrinsics/constrained-comparison.ll
new file mode 100644
index 0000000000000..49bb8eac10be8
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/llvm-intrinsics/constrained-comparison.ll
@@ -0,0 +1,56 @@
+; RUN: llc -verify-machineinstrs -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-DAG: OpFOrdEqual
+; CHECK-DAG: OpFOrdGreaterThan
+; CHECK-DAG: OpFOrdGreaterThanEqual
+; CHECK-DAG: OpFOrdLessThan
+; CHECK-DAG: OpFOrdLessThanEqual
+; CHECK-DAG: OpFOrdNotEqual
+; CHECK-DAG: OpOrdered
+; CHECK-DAG: OpFUnordEqual
+; CHECK-DAG: OpFUnordGreaterThan
+; CHECK-DAG: OpFUnordGreaterThanEqual
+; CHECK-DAG: OpFUnordLessThan
+; CHECK-DAG: OpFUnordLessThanEqual
+; CHECK-DAG: OpFUnordNotEqual
+; CHECK-DAG: OpUnordered
+
+define dso_local spir_kernel void @test(float %a){
+entry:
+  %cmp = tail call i1 @llvm.experimental.constrained.fcmps.f32(float %a, float %a, metadata !"oeq", metadata !"fpexcept.strict") 
+  %cmp1 = tail call i1 @llvm.experimental.constrained.fcmps.f32(float %a, float %a, metadata !"ogt", metadata !"fpexcept.strict") 
+  %cmp2 = tail call i1 @llvm.experimental.constrained.fcmps.f32(float %a, float %a, metadata !"oge", metadata !"fpexcept.strict") 
+  %cmp3 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"olt", metadata !"fpexcept.strict") 
+  %cmp4 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"ole", metadata !"fpexcept.strict") 
+  %cmp5 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"one", metadata !"fpexcept.strict") 
+  %cmp6 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"ord", metadata !"fpexcept.strict") 
+  %cmp7 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"ueq", metadata !"fpexcept.strict") 
+  %cmp8 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"ugt", metadata !"fpexcept.strict") 
+  %cmp9 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"uge", metadata !"fpexcept.strict") 
+  %cmp10 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"ult", metadata !"fpexcept.strict") 
+  %cmp11 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"ule", metadata !"fpexcept.strict") 
+  %cmp12 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"une", metadata !"fpexcept.strict") 
+  %cmp13 = tail call i1 @llvm.experimental.constrained.fcmp.f32(float %a, float %a, metadata !"uno", metadata !"fpexcept.strict") 
+
+  %or1 = or i1 %cmp, %cmp1
+  %or2 = or i1 %or1, %cmp2
+  %or3 = or i1 %or2, %cmp3
+  %or4 = or i1 %or3, %cmp4
+  %or5 = or i1 %or4, %cmp5
+  %or6 = or i1 %or5, %cmp6
+  %or7 = or i1 %or6, %cmp7
+  %or8 = or i1 %or7, %cmp8
+  %or9 = or i1 %or8, %cmp9
+  %or10 = or i1 %or9, %cmp10
+  %or11 = or i1 %or10, %cmp11
+  %or12 = or i1 %or11, %cmp12
+  %or13 = or i1 %or12, %cmp13
+  br i1 %or13, label %true_block, label %false_block
+true_block:
+  ret void
+false_block:
+  ret void
+}
+declare i1 @llvm.experimental.constrained.fcmps.f32(float, float, metadata, metadata) 
+declare i1 @llvm.experimental.constrained.fcmp.f32(float, float, metadata, metadata) 

@michalpaszkowski michalpaszkowski merged commit 047ddbf into llvm:main Sep 28, 2025
11 of 12 checks passed
Copy link

@SubashBoopathi Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 28, 2025

LLVM Buildbot has detected a new failure on builder llvm-clang-aarch64-darwin running on doug-worker-5 while building llvm at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/190/builds/28311

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
******************** TEST 'lld :: COFF/pdb-global-hashes.test' FAILED ********************
Exit Code: -6

Command Output (stdout):
--
# RUN: at line 1
/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/yaml2obj /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/lld/test/COFF/Inputs/pdb-hashes-1.yaml -o /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj
# executed command: /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/yaml2obj /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/lld/test/COFF/Inputs/pdb-hashes-1.yaml -o /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj
# note: command had no output on stdout or stderr
# RUN: at line 2
/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/yaml2obj /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/lld/test/COFF/Inputs/pdb-hashes-2.yaml -o /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.obj
# executed command: /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/yaml2obj /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/lld/test/COFF/Inputs/pdb-hashes-2.yaml -o /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.obj
# note: command had no output on stdout or stderr
# RUN: at line 3
/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/yaml2obj /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/lld/test/COFF/Inputs/pdb-hashes-2-missing.yaml -o /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.missing.obj
# executed command: /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/yaml2obj /Users/buildbot/buildbot-root2/aarch64-darwin/llvm-project/lld/test/COFF/Inputs/pdb-hashes-2-missing.yaml -o /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.missing.obj
# note: command had no output on stdout or stderr
# RUN: at line 4
/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/lld-link /debug /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.obj /entry:main /nodefaultlib /PDB:/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.nohash.pdb
# executed command: /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/lld-link /debug /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.obj /entry:main /nodefaultlib /PDB:/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.nohash.pdb
# note: command had no output on stdout or stderr
# RUN: at line 5
/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/lld-link /debug:ghash -verbose /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.obj /entry:main /nodefaultlib /PDB:/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.hash.pdb
# executed command: /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/lld-link /debug:ghash -verbose /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.obj /entry:main /nodefaultlib /PDB:/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.hash.pdb
# .---command stderr------------
# | lld-link: Reading /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj
# | lld-link: Directives: /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj:  /DEFAULTLIB:libcmt.lib /DEFAULTLIB:oldnames.lib
# | lld-link: Reading /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.obj
# | lld-link: Directives: /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.obj:  /DEFAULTLIB:libcmt.lib /DEFAULTLIB:oldnames.lib
# | lld-link: ghash table load factor: 70.37% (size 19 / capacity 27)
# | 
# | 
# | lld-link: Tpi record count: 13
# | lld-link: Ipi record count: 6
# `-----------------------------
# RUN: at line 6
/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/lld-link /debug:ghash /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.missing.obj /entry:main /nodefaultlib /PDB:/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.mixed.pdb
# executed command: /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/bin/lld-link /debug:ghash /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.1.obj /Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.2.missing.obj /entry:main /nodefaultlib /PDB:/Volumes/ExternalSSD/buildbot-root/aarch64-darwin/build/tools/lld/test/COFF/Output/pdb-global-hashes.test.tmp.mixed.pdb
# note: command had no output on stdout or stderr
# error: command failed with exit status: -6

--

********************


RiverDave pushed a commit that referenced this pull request Oct 1, 2025
)

Added SPIR-V support for constrained floating-point comparison
intrinsics (fcmp, fcmps) with lowering and tests.
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
…#157439)

Added SPIR-V support for constrained floating-point comparison
intrinsics (fcmp, fcmps) with lowering and tests.
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.

4 participants