Skip to content

[LLVM][C API] Clearing initializer and personality by passing NULL. #105521

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

Merged
merged 1 commit into from
Aug 28, 2024
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
3 changes: 3 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ Changes to the C API
Because of backwards compatibility, ``LLVMIsAtomicSingleThread`` and
``LLVMSetAtomicSingleThread`` continue to work with any instruction type.

* The `LLVMSetPersonalityFn` and `LLVMSetInitializer` APIs now support clearing the
personality function and initializer respectively by passing a null pointer.


Changes to the CodeGen infrastructure
-------------------------------------
Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2261,8 +2261,8 @@ LLVMValueRef LLVMGetInitializer(LLVMValueRef GlobalVar) {
}

void LLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
unwrap<GlobalVariable>(GlobalVar)
->setInitializer(unwrap<Constant>(ConstantVal));
unwrap<GlobalVariable>(GlobalVar)->setInitializer(
ConstantVal ? unwrap<Constant>(ConstantVal) : nullptr);
}

LLVMBool LLVMIsThreadLocal(LLVMValueRef GlobalVar) {
Expand Down Expand Up @@ -2445,7 +2445,8 @@ LLVMValueRef LLVMGetPersonalityFn(LLVMValueRef Fn) {
}

void LLVMSetPersonalityFn(LLVMValueRef Fn, LLVMValueRef PersonalityFn) {
unwrap<Function>(Fn)->setPersonalityFn(unwrap<Constant>(PersonalityFn));
unwrap<Function>(Fn)->setPersonalityFn(
PersonalityFn ? unwrap<Constant>(PersonalityFn) : nullptr);
}

unsigned LLVMGetIntrinsicID(LLVMValueRef Fn) {
Expand Down
26 changes: 26 additions & 0 deletions llvm/unittests/IR/FunctionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/IR/Function.h"
#include "llvm-c/Core.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/SourceMgr.h"
Expand Down Expand Up @@ -601,4 +602,29 @@ TEST(FunctionTest, UWTable) {
EXPECT_FALSE(F.hasUWTable());
EXPECT_TRUE(F.getUWTableKind() == UWTableKind::None);
}

TEST(FunctionTest, Personality) {
LLVMContext Ctx;
Module M("test", Ctx);
Type *Int8Ty = Type::getInt8Ty(Ctx);
FunctionType *FTy = FunctionType::get(Int8Ty, false);
Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage, "F", &M);
Function *PersonalityFn =
Function::Create(FTy, GlobalValue::ExternalLinkage, "PersonalityFn", &M);

EXPECT_FALSE(F->hasPersonalityFn());
F->setPersonalityFn(PersonalityFn);
EXPECT_TRUE(F->hasPersonalityFn());
EXPECT_EQ(F->getPersonalityFn(), PersonalityFn);
F->setPersonalityFn(nullptr);
EXPECT_FALSE(F->hasPersonalityFn());

EXPECT_FALSE(LLVMHasPersonalityFn(wrap(F)));
LLVMSetPersonalityFn(wrap(F), wrap(PersonalityFn));
EXPECT_TRUE(LLVMHasPersonalityFn(wrap(F)));
EXPECT_EQ(LLVMGetPersonalityFn(wrap(F)), wrap(PersonalityFn));
LLVMSetPersonalityFn(wrap(F), nullptr);
EXPECT_FALSE(LLVMHasPersonalityFn(wrap(F)));
}

} // end namespace
24 changes: 24 additions & 0 deletions llvm/unittests/IR/ValueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/IR/Value.h"
#include "llvm-c/Core.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IntrinsicInst.h"
Expand Down Expand Up @@ -391,4 +392,27 @@ TEST(ValueTest, replaceUsesOutsideBlockDbgVariableRecord) {
EXPECT_TRUE(DVR2->getVariableLocationOp(0) == cast<Value>(B));
}

TEST(GlobalTest, Initializer) {
LLVMContext Ctx;
Module M("test", Ctx);
Type *Int8Ty = Type::getInt8Ty(Ctx);
Constant *Int8Null = Constant::getNullValue(Int8Ty);

GlobalVariable *GV = new GlobalVariable(
M, Int8Ty, false, GlobalValue::ExternalLinkage, nullptr, "GV");

EXPECT_FALSE(GV->hasInitializer());
GV->setInitializer(Int8Null);
EXPECT_TRUE(GV->hasInitializer());
EXPECT_EQ(GV->getInitializer(), Int8Null);
GV->setInitializer(nullptr);
EXPECT_FALSE(GV->hasInitializer());

EXPECT_EQ(LLVMGetInitializer(wrap(GV)), nullptr);
LLVMSetInitializer(wrap(GV), wrap(Int8Null));
EXPECT_EQ(LLVMGetInitializer(wrap(GV)), wrap(Int8Null));
LLVMSetInitializer(wrap(GV), nullptr);
EXPECT_EQ(LLVMGetInitializer(wrap(GV)), nullptr);
}

} // end anonymous namespace
Loading