Skip to content

Commit

Permalink
llvm-c: Add LLVMDeleteInstruction to fix a test issue
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nhaehnle committed Jul 7, 2022
1 parent 0f4339a commit fdf7e43
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
3 changes: 3 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Expand Up @@ -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
--------------------------

Expand Down
12 changes: 11 additions & 1 deletion llvm/include/llvm-c/Core.h
Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions llvm/lib/IR/Core.cpp
Expand Up @@ -2803,6 +2803,10 @@ void LLVMInstructionEraseFromParent(LLVMValueRef Inst) {
unwrap<Instruction>(Inst)->eraseFromParent();
}

void LLVMDeleteInstruction(LLVMValueRef Inst) {
unwrap<Instruction>(Inst)->deleteValue();
}

LLVMIntPredicate LLVMGetICmpPredicate(LLVMValueRef Inst) {
if (ICmpInst *I = dyn_cast<ICmpInst>(unwrap(Inst)))
return (LLVMIntPredicate)I->getPredicate();
Expand Down
7 changes: 3 additions & 4 deletions llvm/tools/llvm-c-test/metadata.c
Expand Up @@ -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;
}

0 comments on commit fdf7e43

Please sign in to comment.