From fdf7e437bfc9df20454361a3453e2811be9da56c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolai=20H=C3=A4hnle?= Date: Mon, 4 Jul 2022 14:41:03 +0200 Subject: [PATCH] llvm-c: Add LLVMDeleteInstruction to fix a test issue Not deleting the loose instruction with metadata associated to it causes an assertion when the LLVMContext is destroyed. This was previously hidden by the fact that llvm-c-test does not call LLVMShutdown. The planned removal of ManagedStatic exposed this issue. Differential Revision: https://reviews.llvm.org/D129114 --- llvm/docs/ReleaseNotes.rst | 3 +++ llvm/include/llvm-c/Core.h | 12 +++++++++++- llvm/lib/IR/Core.cpp | 4 ++++ llvm/tools/llvm-c-test/metadata.c | 7 +++---- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst index 58493357b4530..23810a9227ebe 100644 --- a/llvm/docs/ReleaseNotes.rst +++ b/llvm/docs/ReleaseNotes.rst @@ -199,6 +199,9 @@ Changes to the C API * ``LLVMConstURem`` * ``LLVMConstSRem`` +* Add ``LLVMDeleteInstruction`` function which allows deleting instructions that + are not inserted into a basic block. + Changes to the Go bindings -------------------------- diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h index f031554e856e8..91c336ac21f5e 100644 --- a/llvm/include/llvm-c/Core.h +++ b/llvm/include/llvm-c/Core.h @@ -3228,7 +3228,7 @@ LLVMValueRef LLVMGetNextInstruction(LLVMValueRef Inst); LLVMValueRef LLVMGetPreviousInstruction(LLVMValueRef Inst); /** - * Remove and delete an instruction. + * Remove an instruction. * * The instruction specified is removed from its containing building * block but is kept alive. @@ -3247,6 +3247,16 @@ void LLVMInstructionRemoveFromParent(LLVMValueRef Inst); */ void LLVMInstructionEraseFromParent(LLVMValueRef Inst); +/** + * Delete an instruction. + * + * The instruction specified is deleted. It must have previously been + * removed from its containing building block. + * + * @see llvm::Value::deleteValue() + */ +void LLVMDeleteInstruction(LLVMValueRef Inst); + /** * Obtain the code opcode for an individual instruction. * diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp index cc1f7c1bfa6b7..73f8c44b808c1 100644 --- a/llvm/lib/IR/Core.cpp +++ b/llvm/lib/IR/Core.cpp @@ -2803,6 +2803,10 @@ void LLVMInstructionEraseFromParent(LLVMValueRef Inst) { unwrap(Inst)->eraseFromParent(); } +void LLVMDeleteInstruction(LLVMValueRef Inst) { + unwrap(Inst)->deleteValue(); +} + LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) { if (ICmpInst *I = dyn_cast(unwrap(Inst))) return (LLVMIntPredicate)I->getPredicate(); diff --git a/llvm/tools/llvm-c-test/metadata.c b/llvm/tools/llvm-c-test/metadata.c index 4e1aa9cd99d88..b1d76083cd727 100644 --- a/llvm/tools/llvm-c-test/metadata.c +++ b/llvm/tools/llvm-c-test/metadata.c @@ -31,12 +31,11 @@ int llvm_set_metadata(void) { LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) }; // This used to trigger an assertion - LLVMSetMetadata( - LLVMBuildRetVoid(b), - LLVMGetMDKindID("kind", 4), - LLVMMDNode(values, 1)); + LLVMValueRef ret = LLVMBuildRetVoid(b); + LLVMSetMetadata(ret, LLVMGetMDKindID("kind", 4), LLVMMDNode(values, 1)); LLVMDisposeBuilder(b); + LLVMDeleteInstruction(ret); return 0; }