int to StringRed conversion

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

int to StringRed conversion

Alexandru Ionut Diaconescu
Hello everyone,

I have an integer and I want to convert it to StringRef in order to set metadata.
setMetadata->(StringRef, MDNode*);

It is there a native LLVM way to do it?

1. In the llvm::APSInt Class is toString() method, which seems it is not for this purpose
2. itoa and string are not part of LLVM
3. stringstream is not part of LLVM
4. to_string is not part of LLVM
5. any casting method?

Also, I would like to get the metadata and convert it back to integer.

Thank you !

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: int to StringRed conversion

Logan Chien
Hi,

  I think you may try to use llvm::Twine(int).  For example, to convert 30 to string, you can use:

Twine(30).str()

To convert the string back to integer, you can try the StringRef::getAsInteger(unsigned, APInt &).  For example:

APInt i;
str.getAsInteger(/*radix=*/ 10, /*output=*/ i);

Sincerely,
Logan



On Thu, May 2, 2013 at 9:53 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Hello everyone,

I have an integer and I want to convert it to StringRef in order to set metadata.
setMetadata->(StringRef, MDNode*);

It is there a native LLVM way to do it?

1. In the llvm::APSInt Class is toString() method, which seems it is not for this purpose
2. itoa and string are not part of LLVM
3. stringstream is not part of LLVM
4. to_string is not part of LLVM
5. any casting method?

Also, I would like to get the metadata and convert it back to integer.

Thank you !

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev



_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: int to StringRed conversion

Alexandru Ionut Diaconescu
Thank you for your help!

Almost solved my problem. Now I don't have any compilation errors. I have only one segfault:

opt: LLVMContext.cpp:147: unsigned int llvm::LLVMContext::getMDKindID(llvm::StringRef) const: Assertion `isValidName(Name) && "Invalid MDNode name"' failed.

This is due because when I setMetadata() it failes.

StringRef tsts = llvm::Twine(srsr).str();
....
LLVMContext& C = is->getContext();
MDNode* N = MDNode::get(C, MDString::get(C, "path"));
is->setMetadata(tsts, N);


If I put
is->setMetadata("marked", N);, it goes ok and the .bc is modified. But if I directly put tsts it failes...

and the definition is :
void Instruction::setMetadata ( StringRef  Kind,


MDNode Node 

)

I don't know how I can use the tsts parameter..

Thank you again !


On Thu, May 2, 2013 at 4:42 PM, Logan Chien <[hidden email]> wrote:
Hi,

  I think you may try to use llvm::Twine(int).  For example, to convert 30 to string, you can use:

Twine(30).str()

To convert the string back to integer, you can try the StringRef::getAsInteger(unsigned, APInt &).  For example:

APInt i;
str.getAsInteger(/*radix=*/ 10, /*output=*/ i);

Sincerely,
Logan



On Thu, May 2, 2013 at 9:53 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Hello everyone,

I have an integer and I want to convert it to StringRef in order to set metadata.
setMetadata->(StringRef, MDNode*);

It is there a native LLVM way to do it?

1. In the llvm::APSInt Class is toString() method, which seems it is not for this purpose
2. itoa and string are not part of LLVM
3. stringstream is not part of LLVM
4. to_string is not part of LLVM
5. any casting method?

Also, I would like to get the metadata and convert it back to integer.

Thank you !

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev





--
Best regards,
Alexandru Ionut Diaconescu

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: int to StringRed conversion

Alexandru Ionut Diaconescu
The problem is that I want to pass only srsr which is an int. "marked" was just an example :)

Thanks you!


On Thu, May 2, 2013 at 5:06 PM, Logan Chien <[hidden email]> wrote:
I'm not familiar with this, but maybe you can try:

StringRef tst = ("marked" + Twine(srsr)).str();

It seems that you can't use integer as meta data kind name.

Logan


On Thu, May 2, 2013 at 10:59 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Thank you for your help!

Almost solved my problem. Now I don't have any compilation errors. I have only one segfault:

opt: LLVMContext.cpp:147: unsigned int llvm::LLVMContext::getMDKindID(llvm::StringRef) const: Assertion `isValidName(Name) && "Invalid MDNode name"' failed.

This is due because when I setMetadata() it failes.

StringRef tsts = llvm::Twine(srsr).str();
....
LLVMContext& C = is->getContext();
MDNode* N = MDNode::get(C, MDString::get(C, "path"));
is->setMetadata(tsts, N);


If I put
is->setMetadata("marked", N);, it goes ok and the .bc is modified. But if I directly put tsts it failes...

and the definition is :
void Instruction::setMetadata ( StringRef  Kind,


MDNode Node 

)

I don't know how I can use the tsts parameter..

Thank you again !


On Thu, May 2, 2013 at 4:42 PM, Logan Chien <[hidden email]> wrote:
Hi,

  I think you may try to use llvm::Twine(int).  For example, to convert 30 to string, you can use:

Twine(30).str()

To convert the string back to integer, you can try the StringRef::getAsInteger(unsigned, APInt &).  For example:

APInt i;
str.getAsInteger(/*radix=*/ 10, /*output=*/ i);

Sincerely,
Logan



On Thu, May 2, 2013 at 9:53 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Hello everyone,

I have an integer and I want to convert it to StringRef in order to set metadata.
setMetadata->(StringRef, MDNode*);

It is there a native LLVM way to do it?

1. In the llvm::APSInt Class is toString() method, which seems it is not for this purpose
2. itoa and string are not part of LLVM
3. stringstream is not part of LLVM
4. to_string is not part of LLVM
5. any casting method?

Also, I would like to get the metadata and convert it back to integer.

Thank you !

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev





--
Best regards,
Alexandru Ionut Diaconescu




--
Best regards,
Alexandru Ionut Diaconescu

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: int to StringRed conversion

Logan Chien
I think the better solution should be:

LLVMContext& C = is->getContext();
Value *values[] = {
  ConstantInt::getSigned(Type::getInt64Ty(C), scsr),
  MDString::get(C, "path")
};

lnstr.setMetadata("your_analysis_name", MDNode::get(C, values));

So that you can take advantage of the type system of LLVM bitcode, and don't have to cast the integers from/to strings by yourself.

Logan


On Thu, May 2, 2013 at 11:10 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
The problem is that I want to pass only srsr which is an int. "marked" was just an example :)

Thanks you!


On Thu, May 2, 2013 at 5:06 PM, Logan Chien <[hidden email]> wrote:
I'm not familiar with this, but maybe you can try:

StringRef tst = ("marked" + Twine(srsr)).str();

It seems that you can't use integer as meta data kind name.

Logan


On Thu, May 2, 2013 at 10:59 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Thank you for your help!

Almost solved my problem. Now I don't have any compilation errors. I have only one segfault:

opt: LLVMContext.cpp:147: unsigned int llvm::LLVMContext::getMDKindID(llvm::StringRef) const: Assertion `isValidName(Name) && "Invalid MDNode name"' failed.

This is due because when I setMetadata() it failes.

StringRef tsts = llvm::Twine(srsr).str();
....
LLVMContext& C = is->getContext();
MDNode* N = MDNode::get(C, MDString::get(C, "path"));
is->setMetadata(tsts, N);


If I put
is->setMetadata("marked", N);, it goes ok and the .bc is modified. But if I directly put tsts it failes...

and the definition is :
void Instruction::setMetadata ( StringRef  Kind,


MDNode Node 

)

I don't know how I can use the tsts parameter..

Thank you again !


On Thu, May 2, 2013 at 4:42 PM, Logan Chien <[hidden email]> wrote:
Hi,

  I think you may try to use llvm::Twine(int).  For example, to convert 30 to string, you can use:

Twine(30).str()

To convert the string back to integer, you can try the StringRef::getAsInteger(unsigned, APInt &).  For example:

APInt i;
str.getAsInteger(/*radix=*/ 10, /*output=*/ i);

Sincerely,
Logan



On Thu, May 2, 2013 at 9:53 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Hello everyone,

I have an integer and I want to convert it to StringRef in order to set metadata.
setMetadata->(StringRef, MDNode*);

It is there a native LLVM way to do it?

1. In the llvm::APSInt Class is toString() method, which seems it is not for this purpose
2. itoa and string are not part of LLVM
3. stringstream is not part of LLVM
4. to_string is not part of LLVM
5. any casting method?

Also, I would like to get the metadata and convert it back to integer.

Thank you !

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev





--
Best regards,
Alexandru Ionut Diaconescu




--
Best regards,
Alexandru Ionut Diaconescu


_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: int to StringRed conversion

Alexandru Ionut Diaconescu
Yes, it sounds good. I can try tomorrow.

Thank you for your advice !


On Thu, May 2, 2013 at 5:43 PM, Logan Chien <[hidden email]> wrote:
I think the better solution should be:


LLVMContext& C = is->getContext();
Value *values[] = {
  ConstantInt::getSigned(Type::getInt64Ty(C), scsr),
  MDString::get(C, "path")
};

lnstr.setMetadata("your_analysis_name", MDNode::get(C, values));

So that you can take advantage of the type system of LLVM bitcode, and don't have to cast the integers from/to strings by yourself.

Logan


On Thu, May 2, 2013 at 11:10 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
The problem is that I want to pass only srsr which is an int. "marked" was just an example :)

Thanks you!


On Thu, May 2, 2013 at 5:06 PM, Logan Chien <[hidden email]> wrote:
I'm not familiar with this, but maybe you can try:

StringRef tst = ("marked" + Twine(srsr)).str();

It seems that you can't use integer as meta data kind name.

Logan


On Thu, May 2, 2013 at 10:59 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Thank you for your help!

Almost solved my problem. Now I don't have any compilation errors. I have only one segfault:

opt: LLVMContext.cpp:147: unsigned int llvm::LLVMContext::getMDKindID(llvm::StringRef) const: Assertion `isValidName(Name) && "Invalid MDNode name"' failed.

This is due because when I setMetadata() it failes.

StringRef tsts = llvm::Twine(srsr).str();
....
LLVMContext& C = is->getContext();
MDNode* N = MDNode::get(C, MDString::get(C, "path"));
is->setMetadata(tsts, N);


If I put
is->setMetadata("marked", N);, it goes ok and the .bc is modified. But if I directly put tsts it failes...

and the definition is :
void Instruction::setMetadata ( StringRef  Kind,


MDNode Node 

)

I don't know how I can use the tsts parameter..

Thank you again !


On Thu, May 2, 2013 at 4:42 PM, Logan Chien <[hidden email]> wrote:
Hi,

  I think you may try to use llvm::Twine(int).  For example, to convert 30 to string, you can use:

Twine(30).str()

To convert the string back to integer, you can try the StringRef::getAsInteger(unsigned, APInt &).  For example:

APInt i;
str.getAsInteger(/*radix=*/ 10, /*output=*/ i);

Sincerely,
Logan



On Thu, May 2, 2013 at 9:53 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Hello everyone,

I have an integer and I want to convert it to StringRef in order to set metadata.
setMetadata->(StringRef, MDNode*);

It is there a native LLVM way to do it?

1. In the llvm::APSInt Class is toString() method, which seems it is not for this purpose
2. itoa and string are not part of LLVM
3. stringstream is not part of LLVM
4. to_string is not part of LLVM
5. any casting method?

Also, I would like to get the metadata and convert it back to integer.

Thank you !

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev





--
Best regards,
Alexandru Ionut Diaconescu




--
Best regards,
Alexandru Ionut Diaconescu




--
Best regards,
Alexandru Ionut Diaconescu

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: int to StringRed conversion

Alexandru Ionut Diaconescu
Thank you very much ! It works ! And also I can get metadata accordingly.


On Thu, May 2, 2013 at 6:39 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Yes, it sounds good. I can try tomorrow.

Thank you for your advice !



On Thu, May 2, 2013 at 5:43 PM, Logan Chien <[hidden email]> wrote:
I think the better solution should be:


LLVMContext& C = is->getContext();
Value *values[] = {
  ConstantInt::getSigned(Type::getInt64Ty(C), scsr),
  MDString::get(C, "path")
};

lnstr.setMetadata("your_analysis_name", MDNode::get(C, values));

So that you can take advantage of the type system of LLVM bitcode, and don't have to cast the integers from/to strings by yourself.

Logan


On Thu, May 2, 2013 at 11:10 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
The problem is that I want to pass only srsr which is an int. "marked" was just an example :)

Thanks you!


On Thu, May 2, 2013 at 5:06 PM, Logan Chien <[hidden email]> wrote:
I'm not familiar with this, but maybe you can try:

StringRef tst = ("marked" + Twine(srsr)).str();

It seems that you can't use integer as meta data kind name.

Logan


On Thu, May 2, 2013 at 10:59 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Thank you for your help!

Almost solved my problem. Now I don't have any compilation errors. I have only one segfault:

opt: LLVMContext.cpp:147: unsigned int llvm::LLVMContext::getMDKindID(llvm::StringRef) const: Assertion `isValidName(Name) && "Invalid MDNode name"' failed.

This is due because when I setMetadata() it failes.

StringRef tsts = llvm::Twine(srsr).str();
....
LLVMContext& C = is->getContext();
MDNode* N = MDNode::get(C, MDString::get(C, "path"));
is->setMetadata(tsts, N);


If I put
is->setMetadata("marked", N);, it goes ok and the .bc is modified. But if I directly put tsts it failes...

and the definition is :
void Instruction::setMetadata ( StringRef  Kind,


MDNode Node 

)

I don't know how I can use the tsts parameter..

Thank you again !


On Thu, May 2, 2013 at 4:42 PM, Logan Chien <[hidden email]> wrote:
Hi,

  I think you may try to use llvm::Twine(int).  For example, to convert 30 to string, you can use:

Twine(30).str()

To convert the string back to integer, you can try the StringRef::getAsInteger(unsigned, APInt &).  For example:

APInt i;
str.getAsInteger(/*radix=*/ 10, /*output=*/ i);

Sincerely,
Logan



On Thu, May 2, 2013 at 9:53 PM, Alexandru Ionut Diaconescu <[hidden email]> wrote:
Hello everyone,

I have an integer and I want to convert it to StringRef in order to set metadata.
setMetadata->(StringRef, MDNode*);

It is there a native LLVM way to do it?

1. In the llvm::APSInt Class is toString() method, which seems it is not for this purpose
2. itoa and string are not part of LLVM
3. stringstream is not part of LLVM
4. to_string is not part of LLVM
5. any casting method?

Also, I would like to get the metadata and convert it back to integer.

Thank you !

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev





--
Best regards,
Alexandru Ionut Diaconescu




--
Best regards,
Alexandru Ionut Diaconescu




--
Best regards,
Alexandru Ionut Diaconescu



--
Best regards,
Alexandru Ionut Diaconescu

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev