[llvm] [C API] Add getter/setter for samesign flag on icmp (PR #145247)
Benji Smith via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 22 14:01:12 PDT 2025
https://github.com/Benjins created https://github.com/llvm/llvm-project/pull/145247
This was added to the C++ API in https://github.com/llvm/llvm-project/pull/111419 so this change adds accessors in the C API, along with a couple tests.
>From f852fe72bdb6e61d5b4cc69bfce3ff34cc0a5ce3 Mon Sep 17 00:00:00 2001
From: Benji Smith <benjsith at gmail.com>
Date: Sun, 22 Jun 2025 15:53:39 -0400
Subject: [PATCH] [C API] Add getter/setter for samesign flag on icmp
This was added to the C++ API in https://github.com/llvm/llvm-project/pull/111419
so this change adds accessors in the C API, along with a couple tests.
---
llvm/docs/ReleaseNotes.md | 3 +++
llvm/include/llvm-c/Core.h | 18 ++++++++++++++++++
llvm/lib/IR/Core.cpp | 8 ++++++++
llvm/test/Bindings/llvm-c/echo.ll | 8 ++++++++
llvm/tools/llvm-c-test/echo.cpp | 2 ++
5 files changed, 39 insertions(+)
diff --git a/llvm/docs/ReleaseNotes.md b/llvm/docs/ReleaseNotes.md
index 0395f43c61953..95bcc1a1f3f55 100644
--- a/llvm/docs/ReleaseNotes.md
+++ b/llvm/docs/ReleaseNotes.md
@@ -251,6 +251,9 @@ Changes to the C API
* Added ``LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision`` for creating
debugging metadata of enumerators larger than 64 bits.
+* Added ``LLVMGetICmpSameSign`` and ``LLVMSetICmpSameSign`` for the `samesign`
+ flag on `icmp` instructions.
+
Changes to the CodeGen infrastructure
-------------------------------------
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 6857944e6875f..3f30ed92997b4 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -3675,6 +3675,24 @@ LLVMOpcode LLVMGetInstructionOpcode(LLVMValueRef Inst);
*/
LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst);
+/**
+ * Get whether or not an icmp instruction has the samesign flag.
+ *
+ * This is only valid for instructions that correspond to llvm::ICmpInst.
+ *
+ * @see llvm::ICmpInst::hasSameSign()
+ */
+LLVMBool LLVMGetICmpSameSign(LLVMValueRef Inst);
+
+/**
+ * Set the samesign flag on an icmp instruction.
+ *
+ * This is only valid for instructions that correspond to llvm::ICmpInst.
+ *
+ * @see llvm::ICmpInst::setSameSign()
+ */
+void LLVMSetICmpSameSign(LLVMValueRef Inst, LLVMBool SameSign);
+
/**
* Obtain the float predicate of an instruction.
*
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 9810f04cc503c..f7ef4aa473ef5 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -2951,6 +2951,14 @@ LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
return (LLVMIntPredicate)0;
}
+LLVMBool LLVMGetICmpSameSign(LLVMValueRef Inst) {
+ return unwrap<ICmpInst>(Inst)->hasSameSign();
+}
+
+void LLVMSetICmpSameSign(LLVMValueRef Inst, LLVMBool SameSign) {
+ unwrap<ICmpInst>(Inst)->setSameSign(SameSign);
+}
+
LLVMRealPredicate LLVMGetFCmpPredicate(LLVMValueRef Inst) {
if (FCmpInst *I = dyn_cast<FCmpInst>(unwrap(Inst)))
return (LLVMRealPredicate)I->getPredicate();
diff --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll
index 0a688afab6125..ab1771d1f879f 100644
--- a/llvm/test/Bindings/llvm-c/echo.ll
+++ b/llvm/test/Bindings/llvm-c/echo.ll
@@ -417,6 +417,14 @@ define ptr @test_gep_no_wrap_flags(ptr %0) {
ret ptr %gep.nusw
}
+define void @test_icmp_same_sign(i32 %a, i32 %b) {
+ %icmp.1 = icmp eq i32 %a, %b
+ %icmp.2 = icmp slt i32 %a, %b
+ %icmp.3 = icmp samesign eq i32 %a, %b
+ %icmp.4 = icmp samesign slt i32 %a, %b
+ ret void
+}
+
!llvm.dbg.cu = !{!0, !2}
!llvm.module.flags = !{!3}
diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp
index 3ec40fdba0bad..026d815b43da7 100644
--- a/llvm/tools/llvm-c-test/echo.cpp
+++ b/llvm/tools/llvm-c-test/echo.cpp
@@ -823,9 +823,11 @@ struct FunCloner {
}
case LLVMICmp: {
LLVMIntPredicate Pred = LLVMGetICmpPredicate(Src);
+ LLVMBool IsSameSign = LLVMGetICmpSameSign(Src);
LLVMValueRef LHS = CloneValue(LLVMGetOperand(Src, 0));
LLVMValueRef RHS = CloneValue(LLVMGetOperand(Src, 1));
Dst = LLVMBuildICmp(Builder, Pred, LHS, RHS, Name);
+ LLVMSetICmpSameSign(Dst, IsSameSign);
break;
}
case LLVMPHI: {
More information about the llvm-commits
mailing list