Skip to content
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

[C API] Add support for setting/getting new nneg flag on zext instructions #73592

Merged
merged 2 commits into from
Nov 29, 2023

Conversation

Benjins
Copy link
Contributor

@Benjins Benjins commented Nov 28, 2023

This flag was added in #67982, but was not yet accessible via the C API. This commit adds a getter/setter for this flag, and a test for it

…tions

This flag was added in llvm#67982, but was not yet accessible via the C API. This
commit adds a getter/setter for this flag, and a test for it
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 28, 2023

@llvm/pr-subscribers-llvm-ir

Author: Benji Smith (Benjins)

Changes

This flag was added in #67982, but was not yet accessible via the C API. This commit adds a getter/setter for this flag, and a test for it


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

5 Files Affected:

  • (modified) llvm/docs/ReleaseNotes.rst (+3)
  • (modified) llvm/include/llvm-c/Core.h (+11)
  • (modified) llvm/lib/IR/Core.cpp (+10)
  • (modified) llvm/test/Bindings/llvm-c/echo.ll (+2)
  • (modified) llvm/tools/llvm-c-test/echo.cpp (+8)
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 2c663932f8f8c2f..2c160f1707cbb95 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -199,6 +199,9 @@ Changes to the C API
   The option structure exposes an additional setting (i.e., the target ABI) and
   provides default values for unspecified settings.
 
+* Added ``LLVMGetNNeg`` and ``LLVMSetNNeg`` for setting/getting the new nneg flag
+  on zext instructions
+
 Changes to the CodeGen infrastructure
 -------------------------------------
 
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index b752fd42a7a12cf..b16f67ef02f3362 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -3974,6 +3974,17 @@ void LLVMSetNSW(LLVMValueRef ArithInst, LLVMBool HasNSW);
 LLVMBool LLVMGetExact(LLVMValueRef DivOrShrInst);
 void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact);
 
+/**
+ * Gets if the instruction has the non-negative flag set
+ * Only valid for zext instructions
+ */
+LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst);
+/**
+ * Sets the non-negative flag for the instruction
+ * Only valid for zext instructions
+ */
+void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg);
+
 /* Memory */
 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name);
 LLVMValueRef LLVMBuildArrayMalloc(LLVMBuilderRef, LLVMTypeRef Ty,
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index b089dd48e55b4d4..e07664f8a17c6d9 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -3454,6 +3454,16 @@ void LLVMSetExact(LLVMValueRef DivOrShrInst, LLVMBool IsExact) {
   cast<Instruction>(P)->setIsExact(IsExact);
 }
 
+LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) {
+  Value *P = unwrap<Value>(NonNegInst);
+  return cast<Instruction>(P)->hasNonNeg();
+}
+
+void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) {
+  Value *P = unwrap<Value>(NonNegInst);
+  cast<Instruction>(P)->setNonNeg(IsNonNeg);
+}
+
 /*--.. Memory ..............................................................--*/
 
 LLVMValueRef LLVMBuildMalloc(LLVMBuilderRef B, LLVMTypeRef Ty,
diff --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll
index 5daa238bfb8e533..72d5b455badcbec 100644
--- a/llvm/test/Bindings/llvm-c/echo.ll
+++ b/llvm/test/Bindings/llvm-c/echo.ll
@@ -90,6 +90,8 @@ define i32 @iops(i32 %a, i32 %b) {
   %21 = sdiv exact i32 %20, %2
   %22 = lshr exact i32 %21, %4
   %23 = ashr exact i32 %22, %14
+  %24 = zext i32 %23 to i64
+  %25 = zext nneg i32 %23 to i64
   ret i32 %23
 }
 
diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp
index 06966ce528eae4d..3b07ccb29f3e061 100644
--- a/llvm/tools/llvm-c-test/echo.cpp
+++ b/llvm/tools/llvm-c-test/echo.cpp
@@ -899,6 +899,14 @@ struct FunCloner {
         Dst = LLVMBuildFence(Builder, Ordering, IsSingleThreaded, Name);
         break;
       }
+      case LLVMZExt: {
+        LLVMValueRef Val = CloneValue(LLVMGetOperand(Src, 0));
+        LLVMTypeRef DestTy = CloneType(LLVMTypeOf(Src));
+        LLVMBool NNeg = LLVMGetNNeg(Src);
+        Dst = LLVMBuildZExt(Builder, Val, DestTy, Name);
+        LLVMSetNNeg(Dst, NNeg);
+        break;
+      }
       default:
         break;
     }

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@Benjins
Copy link
Contributor Author

Benjins commented Nov 28, 2023

Not sure what the issue with the build is: the only test failure is in the Window's build in TEST 'MLIR :: python/dialects/scf.py', which shouldn't be affected by these changes. I can try merging from main if we want to make it green before landing, otherwise is this good to merge in?

@Benjins
Copy link
Contributor Author

Benjins commented Nov 29, 2023

Bumping this: if it's good to land, can someone with write access merge it?

@nikic nikic merged commit fad77dc into llvm:main Nov 29, 2023
4 checks passed
@Benjins Benjins deleted the bns-zext-nneg-c-api branch November 29, 2023 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants