From 08dd313544d3fc1aa8c499e44c7187cb90326c1a Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Thu, 30 Nov 2023 00:30:11 +0800 Subject: [PATCH 1/6] Support operand bundles in the C API --- llvm/include/llvm-c/Core.h | 118 ++++++++++++++++++++++++++++++ llvm/include/llvm-c/Types.h | 5 ++ llvm/lib/IR/Core.cpp | 81 ++++++++++++++++++++ llvm/test/Bindings/llvm-c/echo.ll | 11 +++ llvm/tools/llvm-c-test/echo.cpp | 44 +++++++++-- 5 files changed, 252 insertions(+), 7 deletions(-) diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index b16f67ef02f336..1e63a7d63af04b 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -3000,6 +3000,83 @@ LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, /** Deprecated: Use LLVMMDNodeInContext2 instead. */ LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count); +/** + * @} + */ + +/** + * @defgroup LLVMCCoreOperandBundle Operand Bundles + * + * Functions in this group operate on LLVMOperandBundleRef instances that + * correspond to llvm::OperandBundleDef instances. + * + * @see llvm::OperandBundleDef + * + * @{ + */ + +/** + * Create a new operand bundle. + * + * Every invocation should be paired with LLVMDisposeOperandBundle() or memory + * will be leaked. + * + * @param Tag Tag name of the operand bundle + * @param TagLen Length of Tag + * @param Args Memory address of an array of bundle operands + * @param NumArgs Length of Args + */ +LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, unsigned TagLen, + LLVMValueRef *Args, + unsigned NumArgs); + +/** + * Destroy an operand bundle. + * + * This must be called for every created operand bundle or memory will be + * leaked. + */ +void LLVMDisposeOperandBundle(LLVMOperandBundleRef Bundle); + +/** + * Obtain the tag of an operand bundle as a string. + * + * @param Bundle Operand bundle to obtain tag of. + * @param Len Out parameter which holds the length of the returned string. + * @return The tag name of Bundle. + * @see OperandBundleDef::getTag() + */ +const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len); + +/** + * Obtain the number of operands for an operand bundle. + * + * @param Bundle Operand bundle to obtain operand count of. + * @return The number of operands. + * @see OperandBundleDef::input_size() + */ +unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle); + +/** + * Obtain the operands for an operand bundle. + * + * @param Bundle Operand bundle to obtain operands of. + * @param Dest Memory address of an array to be filled with operands. + * @see OperandBundleDef::inputs() + */ +void LLVMGetOperandBundleArgs(LLVMOperandBundleRef Bundle, LLVMValueRef *Dest); + +/** + * Obtain the operand for an operand bundle at the given index. + * + * @param Bundle Operand bundle to obtain operand of. + * @param Index An operand index, must be less than + * LLVMGetNumOperandBundleArgs(). + * @return The operand. + */ +LLVMValueRef LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle, + unsigned Index); + /** * @} */ @@ -3451,6 +3528,38 @@ LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef C); */ LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr); +/** + * Obtain the number of operand bundles attached to this instruction. + * + * This only works on llvm::CallInst and llvm::InvokeInst instructions. + * + * @see llvm::CallInst::getNumOperandBundles() + * @see llvm::InvokeInst::getNumOperandBundles() + */ +unsigned LLVMGetNumOperandBundles(LLVMValueRef C); + +/** + * Obtain the operand bundles attached to this instruction. Use + * LLVMDisposeOperandBundle to free the operand bundles. + * + * The Dest parameter should point to a pre-allocated array of + * LLVMOperandBundleRef at least LLVMGetNumOperandBundles() large. On return, + * the first LLVMGetNumOperandBundles() entries in the array will be populated + * with LLVMOperandBundleRef instances. + * + * This only works on llvm::CallInst and llvm::InvokeInst instructions. + */ +void LLVMGetOperandBundles(LLVMValueRef C, LLVMOperandBundleRef *Dest); + +/** + * Obtain the operand bundle attached to this instruction at the given index. + * Use LLVMDisposeOperandBundle to free the operand bundle. + * + * This only works on llvm::CallInst and llvm::InvokeInst instructions. + */ +LLVMOperandBundleRef LLVMGetOperandBundleAtIndex(LLVMValueRef C, + unsigned Index); + /** * Obtain whether a call instruction is a tail call. * @@ -3815,6 +3924,11 @@ LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args, unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, const char *Name); +LLVMValueRef LLVMBuildInvoke3(LLVMBuilderRef, LLVMTypeRef Ty, LLVMValueRef Fn, + LLVMValueRef *Args, unsigned NumArgs, + LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, + LLVMOperandBundleRef *Bundles, + unsigned NumBundles, const char *Name); LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef); /* Exception Handling */ @@ -4110,6 +4224,10 @@ LLVMValueRef LLVMBuildPhi(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name); LLVMValueRef LLVMBuildCall2(LLVMBuilderRef, LLVMTypeRef, LLVMValueRef Fn, LLVMValueRef *Args, unsigned NumArgs, const char *Name); +LLVMValueRef LLVMBuildCall3(LLVMBuilderRef, LLVMTypeRef, LLVMValueRef Fn, + LLVMValueRef *Args, unsigned NumArgs, + LLVMOperandBundleRef *Bundles, unsigned NumBundles, + const char *Name); LLVMValueRef LLVMBuildSelect(LLVMBuilderRef, LLVMValueRef If, LLVMValueRef Then, LLVMValueRef Else, const char *Name); diff --git a/llvm/include/llvm-c/Types.h b/llvm/include/llvm-c/Types.h index 4e9967372d79f5..d5474d986309fa 100644 --- a/llvm/include/llvm-c/Types.h +++ b/llvm/include/llvm-c/Types.h @@ -132,6 +132,11 @@ typedef struct LLVMOpaquePassManager *LLVMPassManagerRef; * @see llvm::Use */ typedef struct LLVMOpaqueUse *LLVMUseRef; +/** + * @see llvm::OperandBundleDef + */ +typedef struct LLVMOpaqueOperandBundle *LLVMOperandBundleRef; + /** * Used to represent an attributes. * diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index e07664f8a17c6d..93de5c120949ac 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -43,6 +43,8 @@ using namespace llvm; +DEFINE_SIMPLE_CONVERSION_FUNCTIONS(OperandBundleDef, LLVMOperandBundleRef) + #define DEBUG_TYPE "ir" void llvm::initializeCore(PassRegistry &Registry) { @@ -2567,6 +2569,40 @@ void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) { unwrap(IFunc)->removeFromParent(); } +/*--.. Operations on operand bundles........................................--*/ + +LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, unsigned TagLen, + LLVMValueRef *Args, + unsigned NumArgs) { + return wrap(new OperandBundleDef(std::string(Tag, TagLen), + ArrayRef(unwrap(Args), NumArgs))); +} + +void LLVMDisposeOperandBundle(LLVMOperandBundleRef Bundle) { + delete unwrap(Bundle); +} + +const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len) { + StringRef Str = unwrap(Bundle)->getTag(); + *Len = Str.size(); + return Str.data(); +} + +unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle) { + return unwrap(Bundle)->inputs().size(); +} + +void LLVMGetOperandBundleArgs(LLVMOperandBundleRef Bundle, LLVMValueRef *Dest) { + OperandBundleDef *OB = unwrap(Bundle); + for (Value *V : OB->inputs()) + *Dest++ = wrap(V); +} + +LLVMValueRef LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle, + unsigned Index) { + return wrap(unwrap(Bundle)->inputs()[Index]); +} + /*--.. Operations on basic blocks ..........................................--*/ LLVMValueRef LLVMBasicBlockAsValue(LLVMBasicBlockRef BB) { @@ -2858,6 +2894,22 @@ LLVMTypeRef LLVMGetCalledFunctionType(LLVMValueRef Instr) { return wrap(unwrap(Instr)->getFunctionType()); } +unsigned LLVMGetNumOperandBundles(LLVMValueRef C) { + return unwrap(C)->getNumOperandBundles(); +} + +void LLVMGetOperandBundles(LLVMValueRef C, LLVMOperandBundleRef *Dest) { + auto *Call = unwrap(C); + for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) + *Dest++ = wrap(new OperandBundleDef(Call->getOperandBundleAt(i))); +} + +LLVMOperandBundleRef LLVMGetOperandBundleAtIndex(LLVMValueRef C, + unsigned Index) { + return wrap( + new OperandBundleDef(unwrap(C)->getOperandBundleAt(Index))); +} + /*--.. Operations on call instructions (only) ..............................--*/ LLVMBool LLVMIsTailCall(LLVMValueRef Call) { @@ -3140,6 +3192,21 @@ LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, ArrayRef(unwrap(Args), NumArgs), Name)); } +LLVMValueRef LLVMBuildInvoke3(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, + LLVMValueRef *Args, unsigned NumArgs, + LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, + LLVMOperandBundleRef *Bundles, + unsigned NumBundles, const char *Name) { + SmallVector OBs; + for (auto *Bundle : ArrayRef(Bundles, NumBundles)) { + OperandBundleDef *OB = unwrap(Bundle); + OBs.push_back(*OB); + } + return wrap(unwrap(B)->CreateInvoke( + unwrap(Ty), unwrap(Fn), unwrap(Then), unwrap(Catch), + ArrayRef(unwrap(Args), NumArgs), OBs, Name)); +} + LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef PersFn, unsigned NumClauses, const char *Name) { @@ -3868,6 +3935,20 @@ LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, ArrayRef(unwrap(Args), NumArgs), Name)); } +LLVMValueRef LLVMBuildCall3(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, + LLVMValueRef *Args, unsigned NumArgs, + LLVMOperandBundleRef *Bundles, unsigned NumBundles, + const char *Name) { + FunctionType *FTy = unwrap(Ty); + SmallVector OBs; + for (auto *Bundle : ArrayRef(Bundles, NumBundles)) { + OperandBundleDef *OB = unwrap(Bundle); + OBs.push_back(*OB); + } + return wrap(unwrap(B)->CreateCall( + FTy, unwrap(Fn), ArrayRef(unwrap(Args), NumArgs), OBs, Name)); +} + LLVMValueRef LLVMBuildSelect(LLVMBuilderRef B, LLVMValueRef If, LLVMValueRef Then, LLVMValueRef Else, const char *Name) { diff --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll index 72d5b455badcbe..c3230109d5b3db 100644 --- a/llvm/test/Bindings/llvm-c/echo.ll +++ b/llvm/test/Bindings/llvm-c/echo.ll @@ -268,6 +268,17 @@ exit: ret void } +define void @operandbundles() personality ptr @personalityFn { + call void @decl() [ "foo"(), "bar\00x"(i32 0, ptr null, token none) ] + invoke void @decl() [ "baz"(label %bar) ] to label %foo unwind label %bar +foo: + ret void +bar: + %1 = landingpad { ptr, i32 } + cleanup + ret void +} + define void @with_debuginfo() !dbg !4 { ret void, !dbg !7 } diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp index 3b07ccb29f3e06..56d05ded83545f 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -548,16 +548,25 @@ struct FunCloner { break; case LLVMInvoke: { SmallVector Args; - int ArgCount = LLVMGetNumArgOperands(Src); - for (int i = 0; i < ArgCount; i++) + SmallVector Bundles; + unsigned ArgCount = LLVMGetNumArgOperands(Src); + for (unsigned i = 0; i < ArgCount; ++i) Args.push_back(CloneValue(LLVMGetOperand(Src, i))); + unsigned BundleCount = LLVMGetNumOperandBundles(Src); + for (unsigned i = 0; i < BundleCount; ++i) { + auto Bundle = LLVMGetOperandBundleAtIndex(Src, i); + Bundles.push_back(CloneOB(Bundle)); + LLVMDisposeOperandBundle(Bundle); + } LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src)); LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src)); LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src)); - Dst = LLVMBuildInvoke2(Builder, FnTy, Fn, Args.data(), ArgCount, - Then, Unwind, Name); + Dst = LLVMBuildInvoke3(Builder, FnTy, Fn, Args.data(), ArgCount, Then, + Unwind, Bundles.data(), Bundles.size(), Name); CloneAttrs(Src, Dst); + for (auto Bundle : Bundles) + LLVMDisposeOperandBundle(Bundle); break; } case LLVMUnreachable: @@ -762,14 +771,24 @@ struct FunCloner { } case LLVMCall: { SmallVector Args; - int ArgCount = LLVMGetNumArgOperands(Src); - for (int i = 0; i < ArgCount; i++) + SmallVector Bundles; + unsigned ArgCount = LLVMGetNumArgOperands(Src); + for (unsigned i = 0; i < ArgCount; ++i) Args.push_back(CloneValue(LLVMGetOperand(Src, i))); + unsigned BundleCount = LLVMGetNumOperandBundles(Src); + for (unsigned i = 0; i < BundleCount; ++i) { + auto Bundle = LLVMGetOperandBundleAtIndex(Src, i); + Bundles.push_back(CloneOB(Bundle)); + LLVMDisposeOperandBundle(Bundle); + } LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src)); LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); - Dst = LLVMBuildCall2(Builder, FnTy, Fn, Args.data(), ArgCount, Name); + Dst = LLVMBuildCall3(Builder, FnTy, Fn, Args.data(), ArgCount, + Bundles.data(), Bundles.size(), Name); LLVMSetTailCallKind(Dst, LLVMGetTailCallKind(Src)); CloneAttrs(Src, Dst); + for (auto Bundle : Bundles) + LLVMDisposeOperandBundle(Bundle); break; } case LLVMResume: { @@ -933,6 +952,17 @@ struct FunCloner { return VMap[Src] = Dst; } + LLVMOperandBundleRef CloneOB(LLVMOperandBundleRef Src) { + size_t TagLen; + const char *Tag = LLVMGetOperandBundleTag(Src, &TagLen); + + SmallVector Args; + for (unsigned i = 0, n = LLVMGetNumOperandBundleArgs(Src); i != n; ++i) + Args.push_back(CloneValue(LLVMGetOperandBundleArgAtIndex(Src, i))); + + return LLVMCreateOperandBundle(Tag, TagLen, Args.data(), Args.size()); + } + LLVMBasicBlockRef DeclareBB(LLVMBasicBlockRef Src) { // Check if this is something we already computed. { From 8e5546c95dd14d7b89a47809a6707fa21cfba4a0 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 8 Dec 2023 02:10:05 +0800 Subject: [PATCH 2/6] remove index-less accessors --- llvm/include/llvm-c/Core.h | 22 ---------------------- llvm/lib/IR/Core.cpp | 12 ------------ 2 files changed, 34 deletions(-) diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 1e63a7d63af04b..3ae3e3d8cd1b16 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -3057,15 +3057,6 @@ const char *LLVMGetOperandBundleTag(LLVMOperandBundleRef Bundle, size_t *Len); */ unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle); -/** - * Obtain the operands for an operand bundle. - * - * @param Bundle Operand bundle to obtain operands of. - * @param Dest Memory address of an array to be filled with operands. - * @see OperandBundleDef::inputs() - */ -void LLVMGetOperandBundleArgs(LLVMOperandBundleRef Bundle, LLVMValueRef *Dest); - /** * Obtain the operand for an operand bundle at the given index. * @@ -3538,19 +3529,6 @@ LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr); */ unsigned LLVMGetNumOperandBundles(LLVMValueRef C); -/** - * Obtain the operand bundles attached to this instruction. Use - * LLVMDisposeOperandBundle to free the operand bundles. - * - * The Dest parameter should point to a pre-allocated array of - * LLVMOperandBundleRef at least LLVMGetNumOperandBundles() large. On return, - * the first LLVMGetNumOperandBundles() entries in the array will be populated - * with LLVMOperandBundleRef instances. - * - * This only works on llvm::CallInst and llvm::InvokeInst instructions. - */ -void LLVMGetOperandBundles(LLVMValueRef C, LLVMOperandBundleRef *Dest); - /** * Obtain the operand bundle attached to this instruction at the given index. * Use LLVMDisposeOperandBundle to free the operand bundle. diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 93de5c120949ac..812edef4e52410 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -2592,12 +2592,6 @@ unsigned LLVMGetNumOperandBundleArgs(LLVMOperandBundleRef Bundle) { return unwrap(Bundle)->inputs().size(); } -void LLVMGetOperandBundleArgs(LLVMOperandBundleRef Bundle, LLVMValueRef *Dest) { - OperandBundleDef *OB = unwrap(Bundle); - for (Value *V : OB->inputs()) - *Dest++ = wrap(V); -} - LLVMValueRef LLVMGetOperandBundleArgAtIndex(LLVMOperandBundleRef Bundle, unsigned Index) { return wrap(unwrap(Bundle)->inputs()[Index]); @@ -2898,12 +2892,6 @@ unsigned LLVMGetNumOperandBundles(LLVMValueRef C) { return unwrap(C)->getNumOperandBundles(); } -void LLVMGetOperandBundles(LLVMValueRef C, LLVMOperandBundleRef *Dest) { - auto *Call = unwrap(C); - for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) - *Dest++ = wrap(new OperandBundleDef(Call->getOperandBundleAt(i))); -} - LLVMOperandBundleRef LLVMGetOperandBundleAtIndex(LLVMValueRef C, unsigned Index) { return wrap( From 399138b51a3e01fc4a544433e7a6d74a0b01873b Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 8 Dec 2023 02:10:52 +0800 Subject: [PATCH 3/6] fix doc --- llvm/include/llvm-c/Core.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 3ae3e3d8cd1b16..799cb5fd517213 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -3524,8 +3524,7 @@ LLVMValueRef LLVMGetCalledValue(LLVMValueRef Instr); * * This only works on llvm::CallInst and llvm::InvokeInst instructions. * - * @see llvm::CallInst::getNumOperandBundles() - * @see llvm::InvokeInst::getNumOperandBundles() + * @see llvm::CallBase::getNumOperandBundles() */ unsigned LLVMGetNumOperandBundles(LLVMValueRef C); From 9d9950f1a1e4dc8be198e0385b66c0957ea8ba10 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 8 Dec 2023 02:11:41 +0800 Subject: [PATCH 4/6] use `size_t` for `TagLen` --- llvm/include/llvm-c/Core.h | 2 +- llvm/lib/IR/Core.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 799cb5fd517213..8547fad858a051 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -3026,7 +3026,7 @@ LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count); * @param Args Memory address of an array of bundle operands * @param NumArgs Length of Args */ -LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, unsigned TagLen, +LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, size_t TagLen, LLVMValueRef *Args, unsigned NumArgs); diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 812edef4e52410..1a3427a5e6b75e 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -2571,7 +2571,7 @@ void LLVMRemoveGlobalIFunc(LLVMValueRef IFunc) { /*--.. Operations on operand bundles........................................--*/ -LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, unsigned TagLen, +LLVMOperandBundleRef LLVMCreateOperandBundle(const char *Tag, size_t TagLen, LLVMValueRef *Args, unsigned NumArgs) { return wrap(new OperandBundleDef(std::string(Tag, TagLen), From 22fec8c4a406e7c8a4339a80efc8ef2d2b0ffdbb Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 8 Dec 2023 02:24:37 +0800 Subject: [PATCH 5/6] add release notes --- llvm/docs/ReleaseNotes.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst index 2c160f1707cbb9..d69bc650760da0 100644 --- a/llvm/docs/ReleaseNotes.rst +++ b/llvm/docs/ReleaseNotes.rst @@ -202,6 +202,19 @@ Changes to the C API * Added ``LLVMGetNNeg`` and ``LLVMSetNNeg`` for setting/getting the new nneg flag on zext instructions +* Added the following functions for manipulating operand bundles, as well as + building ``call`` and ``invoke`` instructions that use operand bundles: + + * ``LLVMBuildCall3`` + * ``LLVMBuildInvoke3`` + * ``LLVMCreateOperandBundle`` + * ``LLVMDisposeOperandBundle`` + * ``LLVMGetNumOperandBundles`` + * ``LLVMGetOperandBundleAtIndex`` + * ``LLVMGetNumOperandBundleArgs`` + * ``LLVMGetOperandBundleArgAtIndex`` + * ``LLVMGetOperandBundleTag`` + Changes to the CodeGen infrastructure ------------------------------------- From b8a5f1e308bc54de4fd00536940e11e6fcabc2b9 Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 8 Dec 2023 22:08:09 +0800 Subject: [PATCH 6/6] `3` -> `WithOperandBundles` --- llvm/docs/ReleaseNotes.rst | 4 ++-- llvm/include/llvm-c/Core.h | 18 +++++++++--------- llvm/lib/IR/Core.cpp | 18 +++++++++--------- llvm/tools/llvm-c-test/echo.cpp | 10 ++++++---- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst index d69bc650760da0..af8e175bb1d2ea 100644 --- a/llvm/docs/ReleaseNotes.rst +++ b/llvm/docs/ReleaseNotes.rst @@ -205,8 +205,8 @@ Changes to the C API * Added the following functions for manipulating operand bundles, as well as building ``call`` and ``invoke`` instructions that use operand bundles: - * ``LLVMBuildCall3`` - * ``LLVMBuildInvoke3`` + * ``LLVMBuildCallWithOperandBundles`` + * ``LLVMBuildInvokeWithOperandBundles`` * ``LLVMCreateOperandBundle`` * ``LLVMDisposeOperandBundle`` * ``LLVMGetNumOperandBundles`` diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index 8547fad858a051..862eaefad25cb3 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -3901,11 +3901,10 @@ LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args, unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, const char *Name); -LLVMValueRef LLVMBuildInvoke3(LLVMBuilderRef, LLVMTypeRef Ty, LLVMValueRef Fn, - LLVMValueRef *Args, unsigned NumArgs, - LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, - LLVMOperandBundleRef *Bundles, - unsigned NumBundles, const char *Name); +LLVMValueRef LLVMBuildInvokeWithOperandBundles( + LLVMBuilderRef, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args, + unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, + LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name); LLVMValueRef LLVMBuildUnreachable(LLVMBuilderRef); /* Exception Handling */ @@ -4201,10 +4200,11 @@ LLVMValueRef LLVMBuildPhi(LLVMBuilderRef, LLVMTypeRef Ty, const char *Name); LLVMValueRef LLVMBuildCall2(LLVMBuilderRef, LLVMTypeRef, LLVMValueRef Fn, LLVMValueRef *Args, unsigned NumArgs, const char *Name); -LLVMValueRef LLVMBuildCall3(LLVMBuilderRef, LLVMTypeRef, LLVMValueRef Fn, - LLVMValueRef *Args, unsigned NumArgs, - LLVMOperandBundleRef *Bundles, unsigned NumBundles, - const char *Name); +LLVMValueRef +LLVMBuildCallWithOperandBundles(LLVMBuilderRef, LLVMTypeRef, LLVMValueRef Fn, + LLVMValueRef *Args, unsigned NumArgs, + LLVMOperandBundleRef *Bundles, + unsigned NumBundles, const char *Name); LLVMValueRef LLVMBuildSelect(LLVMBuilderRef, LLVMValueRef If, LLVMValueRef Then, LLVMValueRef Else, const char *Name); diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index 1a3427a5e6b75e..a360bee15d4b08 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -3180,11 +3180,10 @@ LLVMValueRef LLVMBuildInvoke2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, ArrayRef(unwrap(Args), NumArgs), Name)); } -LLVMValueRef LLVMBuildInvoke3(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, - LLVMValueRef *Args, unsigned NumArgs, - LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, - LLVMOperandBundleRef *Bundles, - unsigned NumBundles, const char *Name) { +LLVMValueRef LLVMBuildInvokeWithOperandBundles( + LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, LLVMValueRef *Args, + unsigned NumArgs, LLVMBasicBlockRef Then, LLVMBasicBlockRef Catch, + LLVMOperandBundleRef *Bundles, unsigned NumBundles, const char *Name) { SmallVector OBs; for (auto *Bundle : ArrayRef(Bundles, NumBundles)) { OperandBundleDef *OB = unwrap(Bundle); @@ -3923,10 +3922,11 @@ LLVMValueRef LLVMBuildCall2(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, ArrayRef(unwrap(Args), NumArgs), Name)); } -LLVMValueRef LLVMBuildCall3(LLVMBuilderRef B, LLVMTypeRef Ty, LLVMValueRef Fn, - LLVMValueRef *Args, unsigned NumArgs, - LLVMOperandBundleRef *Bundles, unsigned NumBundles, - const char *Name) { +LLVMValueRef +LLVMBuildCallWithOperandBundles(LLVMBuilderRef B, LLVMTypeRef Ty, + LLVMValueRef Fn, LLVMValueRef *Args, + unsigned NumArgs, LLVMOperandBundleRef *Bundles, + unsigned NumBundles, const char *Name) { FunctionType *FTy = unwrap(Ty); SmallVector OBs; for (auto *Bundle : ArrayRef(Bundles, NumBundles)) { diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp index 56d05ded83545f..a00fe87dccb5e2 100644 --- a/llvm/tools/llvm-c-test/echo.cpp +++ b/llvm/tools/llvm-c-test/echo.cpp @@ -562,8 +562,9 @@ struct FunCloner { LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); LLVMBasicBlockRef Then = DeclareBB(LLVMGetNormalDest(Src)); LLVMBasicBlockRef Unwind = DeclareBB(LLVMGetUnwindDest(Src)); - Dst = LLVMBuildInvoke3(Builder, FnTy, Fn, Args.data(), ArgCount, Then, - Unwind, Bundles.data(), Bundles.size(), Name); + Dst = LLVMBuildInvokeWithOperandBundles( + Builder, FnTy, Fn, Args.data(), ArgCount, Then, Unwind, + Bundles.data(), Bundles.size(), Name); CloneAttrs(Src, Dst); for (auto Bundle : Bundles) LLVMDisposeOperandBundle(Bundle); @@ -783,8 +784,9 @@ struct FunCloner { } LLVMTypeRef FnTy = CloneType(LLVMGetCalledFunctionType(Src)); LLVMValueRef Fn = CloneValue(LLVMGetCalledValue(Src)); - Dst = LLVMBuildCall3(Builder, FnTy, Fn, Args.data(), ArgCount, - Bundles.data(), Bundles.size(), Name); + Dst = LLVMBuildCallWithOperandBundles(Builder, FnTy, Fn, Args.data(), + ArgCount, Bundles.data(), + Bundles.size(), Name); LLVMSetTailCallKind(Dst, LLVMGetTailCallKind(Src)); CloneAttrs(Src, Dst); for (auto Bundle : Bundles)