Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/docs/ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ Changes to the Python bindings
Changes to the C API
--------------------

* Add `LLVMGetOrInsertFunction` to get or insert a function, replacing the combination of `LLVMGetNamedFunction` and `LLVMAddFunction`.

Changes to the CodeGen infrastructure
-------------------------------------

Expand Down
16 changes: 16 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,22 @@ LLVM_C_ABI unsigned LLVMGetDebugLocColumn(LLVMValueRef Val);
LLVM_C_ABI LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
LLVMTypeRef FunctionTy);

/**
* Obtain or insert a function into a module.
*
* If a function with the specified name already exists in the module, it
* is returned. Otherwise, a new function is created in the module with the
* specified name and type and is returned.
*
* The returned value corresponds to a llvm::Function instance.
*
* @see llvm::Module::getOrInsertFunction()
*/
LLVM_C_ABI LLVMValueRef LLVMGetOrInsertFunction(LLVMModuleRef M,
const char *Name,
size_t NameLen,
LLVMTypeRef FunctionTy);

/**
* Obtain a Function value from a Module by its name.
*
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,14 @@ LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
GlobalValue::ExternalLinkage, Name, unwrap(M)));
}

LLVMValueRef LLVMGetOrInsertFunction(LLVMModuleRef M, const char *Name,
size_t NameLen, LLVMTypeRef FunctionTy) {
return wrap(unwrap(M)
->getOrInsertFunction(StringRef(Name, NameLen),
unwrap<FunctionType>(FunctionTy))
.getCallee());
}

LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
return wrap(unwrap(M)->getFunction(Name));
}
Expand Down
19 changes: 19 additions & 0 deletions llvm/unittests/IR/FunctionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,23 @@ TEST(FunctionTest, Personality) {
EXPECT_FALSE(LLVMHasPersonalityFn(wrap(F)));
}

TEST(FunctionTest, LLVMGetOrInsertFunction) {
LLVMContext Ctx;
Module M("test", Ctx);
Type *Int8Ty = Type::getInt8Ty(Ctx);
FunctionType *FTy = FunctionType::get(Int8Ty, false);

// Create the function using the C API
LLVMValueRef FuncRef = LLVMGetOrInsertFunction(wrap(&M), "F", 1, wrap(FTy));

// Verify that the returned value is a function and has the correct type
Function *Func = unwrap<Function>(FuncRef);
EXPECT_EQ(Func->getName(), "F");
EXPECT_EQ(Func->getFunctionType(), FTy);

// Call LLVMGetOrInsertFunction again to ensure it returns the same function
LLVMValueRef FuncRef2 = LLVMGetOrInsertFunction(wrap(&M), "F", 1, wrap(FTy));
EXPECT_EQ(FuncRef, FuncRef2);
}

} // end namespace
Loading