-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
[LLVM-C] Support operand bundles #73914
Conversation
@llvm/pr-subscribers-llvm-ir Author: Quinton Miller (HertzDevil) ChangesResolves #71873. Full diff: https://github.com/llvm/llvm-project/pull/73914.diff 5 Files Affected:
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index b16f67ef02f3362..1e63a7d63af04b0 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 4e9967372d79f5d..d5474d986309fa0 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 e07664f8a17c6d9..93de5c120949ace 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<GlobalIFunc>(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<CallBase>(Instr)->getFunctionType());
}
+unsigned LLVMGetNumOperandBundles(LLVMValueRef C) {
+ return unwrap<CallBase>(C)->getNumOperandBundles();
+}
+
+void LLVMGetOperandBundles(LLVMValueRef C, LLVMOperandBundleRef *Dest) {
+ auto *Call = unwrap<CallBase>(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<CallBase>(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<OperandBundleDef, 8> OBs;
+ for (auto *Bundle : ArrayRef(Bundles, NumBundles)) {
+ OperandBundleDef *OB = unwrap(Bundle);
+ OBs.push_back(*OB);
+ }
+ return wrap(unwrap(B)->CreateInvoke(
+ unwrap<FunctionType>(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<FunctionType>(Ty);
+ SmallVector<OperandBundleDef, 8> 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 72d5b455badcbec..c3230109d5b3db1 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 3b07ccb29f3e061..56d05ded83545fa 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<LLVMValueRef, 8> Args;
- int ArgCount = LLVMGetNumArgOperands(Src);
- for (int i = 0; i < ArgCount; i++)
+ SmallVector<LLVMOperandBundleRef, 8> 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<LLVMValueRef, 8> Args;
- int ArgCount = LLVMGetNumArgOperands(Src);
- for (int i = 0; i < ArgCount; i++)
+ SmallVector<LLVMOperandBundleRef, 8> 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<LLVMValueRef, 8> 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.
{
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only remaining question I have here is whether LLVMBuildCall3 might be better called LLVMBuildCallWithOperandBundles or so. LLVMBuildCall3 doesn't really deprecate LLVMBuildCall2, as most uses will probably continue using it.
@nikic Changed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Resolves #71873.