[libc] [lldb] [clang] [compiler-rt] [mlir] [polly] [libcxx] [openmp] [llvm] [C API] Add support for setting/getting new nneg flag on zext instructions (PR #73592)
https://github.com/Benjins updated https://github.com/llvm/llvm-project/pull/73592 >From de348ecdbf9d3c299eb4fe302ed2c224df7cde6b Mon Sep 17 00:00:00 2001 From: Benji Smith <6193112+benj...@users.noreply.github.com> Date: Mon, 27 Nov 2023 18:15:22 -0500 Subject: [PATCH] [C API] Add support for setting/getting new nneg flag on zext instructions 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 --- llvm/docs/ReleaseNotes.rst| 3 +++ llvm/include/llvm-c/Core.h| 11 +++ llvm/lib/IR/Core.cpp | 10 ++ llvm/test/Bindings/llvm-c/echo.ll | 2 ++ llvm/tools/llvm-c-test/echo.cpp | 8 5 files changed, 34 insertions(+) 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(P)->setIsExact(IsExact); } +LLVMBool LLVMGetNNeg(LLVMValueRef NonNegInst) { + Value *P = unwrap(NonNegInst); + return cast(P)->hasNonNeg(); +} + +void LLVMSetNNeg(LLVMValueRef NonNegInst, LLVMBool IsNonNeg) { + Value *P = unwrap(NonNegInst); + cast(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; } ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[compiler-rt] [llvm] [mlir] [polly] [clang] [libc] [libcxx] [openmp] [lldb] [C API] Add support for setting/getting new nneg flag on zext instructions (PR #73592)
Benjins wrote: Bumping this: if it's good to land, can someone with write access merge it? https://github.com/llvm/llvm-project/pull/73592 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[llvm] [clang] [clang-tools-extra] [C API] Add getters for Target Extension Types to C API (PR #71291)
https://github.com/Benjins updated https://github.com/llvm/llvm-project/pull/71291 >From 12e5ec3c0727d58bf8d91f673c3facd974f98c54 Mon Sep 17 00:00:00 2001 From: Benji Smith <6193112+benj...@users.noreply.github.com> Date: Sat, 4 Nov 2023 11:57:20 -0400 Subject: [PATCH 1/4] [C API] Add getters for Target Extension Types to C API These types were added in LLVM-16, and the C API supports constructing them, but not getting information back from them This change adds getters for them to the C API, updates the echo test to be able to clone Target Extension Types, and adds some usage of them to the echo.ll test to confirm that they work --- llvm/include/llvm-c/Core.h| 33 +++ llvm/lib/IR/Core.cpp | 32 ++ llvm/test/Bindings/llvm-c/echo.ll | 17 ++ llvm/tools/llvm-c-test/echo.cpp | 38 +-- 4 files changed, 118 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 4fc88b2b64eacea..2d95f198f64958f 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -1654,6 +1654,39 @@ LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name, unsigned *IntParams, unsigned IntParamCount); +/** + * Obtain the name for this target extension type + */ +const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy); + +/** + * Obtain the number of type parameters for this target extension type + */ +unsigned LLVMCountTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy); + +/** + * Obtain the values of a target extension type's type parameters, output to + * the passed-in pointer. The pointer should have enough space for all type + * params for the given target extension type + * + * @see LLVMCountTargetExtTypeTypeParams + */ +void LLVMGetTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy, LLVMTypeRef *Dest); + +/** + * Obtain the number of int parameters for this target extension type + */ +unsigned LLVMCountTargetExtTypeIntParams(LLVMTypeRef TargetExtTy); + +/** + * Obtain the int values of a target extension type's int parameters, output to + * the passed-in pointer. The pointer should have enough space for all int + * params for the given target extension type + * + * @see LLVMCountTargetExtTypeIntParams + */ +void LLVMGetTargetExtTypeIntParams(LLVMTypeRef TargetExtTy, unsigned *Dest); + /** * @} */ diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 076d1089582fe7e..eb1cda55288e334 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -924,6 +924,38 @@ LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name, TargetExtType::get(*unwrap(C), Name, TypeParamArray, IntParamArray)); } +const char *LLVMGetTargetExtTypeName(LLVMTypeRef TargetExtTy) { + TargetExtType *Type = unwrap(TargetExtTy); + return Type->getName().data(); +} + +unsigned LLVMCountTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy) { + TargetExtType *Type = unwrap(TargetExtTy); + return Type->getNumTypeParameters(); +} + +void LLVMGetTargetExtTypeTypeParams(LLVMTypeRef TargetExtTy, +LLVMTypeRef *Dest) { + TargetExtType *Type = unwrap(TargetExtTy); + + for (unsigned int i = 0; i < Type->getNumTypeParameters(); i++) { +Dest[i] = wrap(Type->getTypeParameter(i)); + } +} + +unsigned LLVMCountTargetExtTypeIntParams(LLVMTypeRef TargetExtTy) { + TargetExtType *Type = unwrap(TargetExtTy); + return Type->getNumIntParameters(); +} + +void LLVMGetTargetExtTypeIntParams(LLVMTypeRef TargetExtTy, unsigned *Dest) { + TargetExtType *Type = unwrap(TargetExtTy); + + for (unsigned int i = 0; i < Type->getNumIntParameters(); i++) { +Dest[i] = Type->getIntParameter(i); + } +} + /*===-- Operations on values --===*/ /*--.. Operations on all values --*/ diff --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll index 5daa238bfb8e533..e62670424112d51 100644 --- a/llvm/test/Bindings/llvm-c/echo.ll +++ b/llvm/test/Bindings/llvm-c/echo.ll @@ -66,6 +66,23 @@ define void @types() { ret void } +; Target extension types: +define target("target.ext.1") @target_ext_01(target("target.ext.1") %0) { + ret target("target.ext.1") %0 +} + +define target("target.ext.2", i8, i1) @target_ext_02(target("target.ext.2", i8, i1) %0) { + ret target("target.ext.2", i8, i1) %0 +} + +define target("target.ext.3", 7) @target_ext_03(target("target.ext.3", 7) %0) { + ret target("target.ext.3", 7) %0 +} + +define target("target.ext.4", i1, i32, 7) @target_ext_04(target("target.ext.4", i1, i32, 7) %0) { + ret target("target.ext.4", i1, i32, 7) %0 +} + define i32 @iops(i32 %a, i32 %b) { %1 = add i32 %a, %b %2 = mul i32 %a, %1 diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-
[clang] [clang-tools-extra] [llvm] [C API] Add getters for Target Extension Types to C API (PR #71291)
Benjins wrote: Bumping this for review, after merging from main and resolving the conflicts https://github.com/llvm/llvm-project/pull/71291 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [llvm] [clang-tools-extra] [C API] Add blockaddress getters to C API (PR #77390)
https://github.com/Benjins updated https://github.com/llvm/llvm-project/pull/77390 >From 43db89c2bc2a42ff1e88deb99b7214e3e37e317c Mon Sep 17 00:00:00 2001 From: Benji Smith <6193112+benj...@users.noreply.github.com> Date: Sun, 7 Jan 2024 20:16:25 -0500 Subject: [PATCH 1/2] Refactor llvm-c-test to pre-declare all basic blocks for all functions This is necessary for being able to clone blockaddress values, since they can reference basic blocks in other functions. We declare all basic blocks up front similar to how all functions are declared, and find them when the function itself is cloned --- llvm/tools/llvm-c-test/echo.cpp | 46 +++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp index bc708e2d472edd..5d9e26f875e5da 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -238,6 +238,20 @@ static LLVMValueRef clone_constant(LLVMValueRef Cst, LLVMModuleRef M) { return Ret; } +static LLVMBasicBlockRef find_bb_in_func(LLVMValueRef Fn, const char *BBName) { + LLVMBasicBlockRef CurBB = LLVMGetFirstBasicBlock(Fn); + while (CurBB != nullptr) { + +const char *CurBBName = LLVMGetBasicBlockName(CurBB); +if (strcmp(CurBBName, BBName) == 0) + return CurBB; + +CurBB = LLVMGetNextBasicBlock(CurBB); + } + + return nullptr; +} + static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) { if (!LLVMIsAConstant(Cst)) report_fatal_error("Expected a constant"); @@ -432,6 +446,17 @@ static LLVMValueRef clone_inline_asm(LLVMValueRef Asm, LLVMModuleRef M) { CanUnwind); } +static LLVMBasicBlockRef declare_bb_in_func(LLVMValueRef DstFn, +LLVMBasicBlockRef Src) { + const char *Name = LLVMGetBasicBlockName(Src); + + if (find_bb_in_func(DstFn, Name) != nullptr) +report_fatal_error("Trying to re-declare existing basic block"); + + LLVMBasicBlockRef DstBB = LLVMAppendBasicBlock(DstFn, Name); + return DstBB; +} + struct FunCloner { LLVMValueRef Fun; LLVMModuleRef M; @@ -1042,8 +1067,15 @@ struct FunCloner { if (Name != VName) report_fatal_error("Basic block name mismatch"); -LLVMBasicBlockRef BB = LLVMAppendBasicBlock(Fun, Name); -return BBMap[Src] = BB; +// Scan for existing basic blocks that we forward-declared +// If a basic block is not cached in BBMap already, then it should exist +// in Fun, since we should have pre-declared all basic blocks earlier in +// declare_symbols +if (LLVMBasicBlockRef ExistingBB = find_bb_in_func(Fun, Name)) + return BBMap[Src] = ExistingBB; + +report_fatal_error("Trying to declare new basic block"); +return nullptr; } LLVMBasicBlockRef CloneBB(LLVMBasicBlockRef Src) { @@ -1188,6 +1220,16 @@ static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) { } } +// Declare any basic blocks in this function: +// We need to do this here, in case any blockaddress value's are used, +// in which case we may reference basic blocks in any function +// Therefore, declare them before actually cloning any function +LLVMBasicBlockRef CurSrcBB = LLVMGetFirstBasicBlock(Cur); +while (CurSrcBB != nullptr) { + declare_bb_in_func(F, CurSrcBB); + CurSrcBB = LLVMGetNextBasicBlock(CurSrcBB); +} + Next = LLVMGetNextFunction(Cur); if (Next == nullptr) { if (Cur != End) >From 68a433cdda2cc5e310f36a424b37feaf93052e96 Mon Sep 17 00:00:00 2001 From: Benji Smith <6193112+benj...@users.noreply.github.com> Date: Sun, 7 Jan 2024 20:43:25 -0500 Subject: [PATCH 2/2] Add LLVMGetBlockAddressFunction and LLVMGetBlockAddressBasicBlock getters This allows for accessing the function/basic block that a blockaddress constant refers to Tests are added for it in the llvm-c-test echo.ll file --- llvm/docs/ReleaseNotes.rst| 3 +++ llvm/include/llvm-c/Core.h| 10 ++ llvm/lib/IR/Core.cpp | 8 llvm/test/Bindings/llvm-c/echo.ll | 26 ++ llvm/tools/llvm-c-test/echo.cpp | 16 5 files changed, 63 insertions(+) diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst index 52610e7de18751..24dc9614d97dc5 100644 --- a/llvm/docs/ReleaseNotes.rst +++ b/llvm/docs/ReleaseNotes.rst @@ -246,6 +246,9 @@ Changes to the C API the fast-math flags of an instruction, as well as ``LLVMCanValueUseFastMathFlags`` for checking if an instruction can use such flags +* Added ``LLVMGetBlockAddressFunction`` and ``LLVMGetBlockAddressBasicBlock`` + functions for accessing the values in a blockaddress constant + Changes to the CodeGen infrastructure - diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 83530ae7b51324..1b9683a1d490ad 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/inclu