Skip to content

Commit

Permalink
[LLVM-C] Use Length-Providing Value Name Getters and Setters
Browse files Browse the repository at this point in the history
Summary:
- Provide LLVMGetValueName2 and LLVMSetValueName2 that return and take the length of the provided C string respectively
- Deprecate LLVMGetValueName and LLVMSetValueName

Reviewers: whitequark, deadalnix

Reviewed By: whitequark

Subscribers: llvm-commits, harlanhaskins

Differential Revision: https://reviews.llvm.org/D46890

llvm-svn: 332810
  • Loading branch information
CodaFi committed May 19, 2018
1 parent c0b268f commit 025c78f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
9 changes: 7 additions & 2 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -1496,14 +1496,14 @@ LLVMValueKind LLVMGetValueKind(LLVMValueRef Val);
*
* @see llvm::Value::getName()
*/
const char *LLVMGetValueName(LLVMValueRef Val);
const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length);

/**
* Set the string name of a value.
*
* @see llvm::Value::setName()
*/
void LLVMSetValueName(LLVMValueRef Val, const char *Name);
void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen);

/**
* Dump a representation of a value to stderr.
Expand Down Expand Up @@ -1555,6 +1555,11 @@ LLVM_FOR_EACH_VALUE_SUBCLASS(LLVM_DECLARE_VALUE_CAST)
LLVMValueRef LLVMIsAMDNode(LLVMValueRef Val);
LLVMValueRef LLVMIsAMDString(LLVMValueRef Val);

/** Deprecated: Use LLVMGetValueName2 instead. */
const char *LLVMGetValueName(LLVMValueRef Val);
/** Deprecated: Use LLVMSetValueName2 instead. */
void LLVMSetValueName(LLVMValueRef Val, const char *Name);

/**
* @}
*/
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,16 @@ LLVMValueKind LLVMGetValueKind(LLVMValueRef Val) {
}
}

const char *LLVMGetValueName2(LLVMValueRef Val, size_t *Length) {
auto *V = unwrap(Val);
*Length = V->getName().size();
return V->getName().data();
}

void LLVMSetValueName2(LLVMValueRef Val, const char *Name, size_t NameLen) {
unwrap(Val)->setName(StringRef(Name, NameLen));
}

const char *LLVMGetValueName(LLVMValueRef Val) {
return unwrap(Val)->getName().data();
}
Expand Down
30 changes: 20 additions & 10 deletions llvm/tools/llvm-c-test/echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ static ValueMap clone_params(LLVMValueRef Src, LLVMValueRef Dst) {
LLVMValueRef SrcNext = nullptr;
LLVMValueRef DstNext = nullptr;
while (true) {
const char *Name = LLVMGetValueName(SrcCur);
LLVMSetValueName(DstCur, Name);
size_t NameLen;
const char *Name = LLVMGetValueName2(SrcCur, &NameLen);
LLVMSetValueName2(DstCur, Name, NameLen);

VMap[SrcCur] = DstCur;

Expand Down Expand Up @@ -232,7 +233,8 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {

// Maybe it is a symbol
if (LLVMIsAGlobalValue(Cst)) {
const char *Name = LLVMGetValueName(Cst);
size_t NameLen;
const char *Name = LLVMGetValueName2(Cst, &NameLen);

// Try function
if (LLVMIsAFunction(Cst)) {
Expand Down Expand Up @@ -402,7 +404,8 @@ struct FunCloner {
if (!LLVMIsAInstruction(Src))
report_fatal_error("Expected an instruction");

const char *Name = LLVMGetValueName(Src);
size_t NameLen;
const char *Name = LLVMGetValueName2(Src, &NameLen);

// Check if this is something we already computed.
{
Expand Down Expand Up @@ -734,7 +737,8 @@ struct FunCloner {
report_fatal_error("Basic block is not a basic block");

const char *Name = LLVMGetBasicBlockName(Src);
const char *VName = LLVMGetValueName(V);
size_t NameLen;
const char *VName = LLVMGetValueName2(V, &NameLen);
if (Name != VName)
report_fatal_error("Basic block name mismatch");

Expand Down Expand Up @@ -830,7 +834,8 @@ static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
}

while (true) {
const char *Name = LLVMGetValueName(Cur);
size_t NameLen;
const char *Name = LLVMGetValueName2(Cur, &NameLen);
if (LLVMGetNamedGlobal(M, Name))
report_fatal_error("GlobalVariable already cloned");
LLVMAddGlobal(M, LLVMGetElementType(TypeCloner(M).Clone(Cur)), Name);
Expand Down Expand Up @@ -863,7 +868,8 @@ static void declare_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Cur = Begin;
Next = nullptr;
while (true) {
const char *Name = LLVMGetValueName(Cur);
size_t NameLen;
const char *Name = LLVMGetValueName2(Cur, &NameLen);
if (LLVMGetNamedFunction(M, Name))
report_fatal_error("Function already cloned");
auto Ty = LLVMGetElementType(TypeCloner(M).Clone(Cur));
Expand Down Expand Up @@ -909,7 +915,8 @@ static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
}

while (true) {
const char *Name = LLVMGetValueName(Cur);
size_t NameLen;
const char *Name = LLVMGetValueName2(Cur, &NameLen);
LLVMValueRef G = LLVMGetNamedGlobal(M, Name);
if (!G)
report_fatal_error("GlobalVariable must have been declared already");
Expand Down Expand Up @@ -952,13 +959,16 @@ static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
Cur = Begin;
Next = nullptr;
while (true) {
const char *Name = LLVMGetValueName(Cur);
size_t NameLen;
const char *Name = LLVMGetValueName2(Cur, &NameLen);
LLVMValueRef Fun = LLVMGetNamedFunction(M, Name);
if (!Fun)
report_fatal_error("Function must have been declared already");

if (LLVMHasPersonalityFn(Cur)) {
const char *FName = LLVMGetValueName(LLVMGetPersonalityFn(Cur));
size_t FNameLen;
const char *FName = LLVMGetValueName2(LLVMGetPersonalityFn(Cur),
&FNameLen);
LLVMValueRef P = LLVMGetNamedFunction(M, FName);
if (!P)
report_fatal_error("Could not find personality function");
Expand Down

0 comments on commit 025c78f

Please sign in to comment.