Dipanjan Das via llvm-dev
2017-Apr-28 23:32 UTC
[llvm-dev] How to pass a StringRef to a function call inserted as instrumentation?
I am wriitng an LLVM pass to insert instrumentation at certain points of the program. I want to pass the `StringRef` obtained from `getName()` as a parameter to a function `func(char* s)`. I can allocate some space on stack using `AllocaInst` to generate an `alloca` instruction. But, how can I copy the `StringRef` to the stack space? -- Thanks & Regards, Dipanjan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170428/e211fe2d/attachment.html>
陳韋任 via llvm-dev
2017-Apr-28 23:37 UTC
[llvm-dev] How to pass a StringRef to a function call inserted as instrumentation?
StringRef::str().c_str() or StringRef::data()? 2017-04-29 7:32 GMT+08:00 Dipanjan Das via llvm-dev <llvm-dev at lists.llvm.org>:> > I am wriitng an LLVM pass to insert instrumentation at certain points of > the program. I want to pass the `StringRef` obtained from `getName()` as a > parameter to a function `func(char* s)`. I can allocate some space on stack > using `AllocaInst` to generate an `alloca` instruction. But, how can I copy > the `StringRef` to the stack space? > > -- > > Thanks & Regards, > Dipanjan > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-- Wei-Ren Chen (陳韋任) Homepage: https://people.cs.nctu.edu.tw/~chenwj -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170429/9cccca0c/attachment.html>
Dipanjan Das via llvm-dev
2017-Apr-28 23:44 UTC
[llvm-dev] How to pass a StringRef to a function call inserted as instrumentation?
StringRef::data, basically char* On 28 April 2017 at 16:37, 陳韋任 <chenwj.cs97g at g2.nctu.edu.tw> wrote:> StringRef::str().c_str() or StringRef::data()? > > 2017-04-29 7:32 GMT+08:00 Dipanjan Das via llvm-dev < > llvm-dev at lists.llvm.org>: > >> >> I am wriitng an LLVM pass to insert instrumentation at certain points of >> the program. I want to pass the `StringRef` obtained from `getName()` as a >> parameter to a function `func(char* s)`. I can allocate some space on stack >> using `AllocaInst` to generate an `alloca` instruction. But, how can I copy >> the `StringRef` to the stack space? >> >> -- >> >> Thanks & Regards, >> Dipanjan >> >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >> >> > > > -- > Wei-Ren Chen (陳韋任) > Homepage: https://people.cs.nctu.edu.tw/~chenwj >-- Thanks & Regards, Dipanjan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170428/c449fd7f/attachment.html>
Tim Northover via llvm-dev
2017-Apr-29 00:27 UTC
[llvm-dev] How to pass a StringRef to a function call inserted as instrumentation?
Hi, Dipanjan, On 28 April 2017 at 16:32, Dipanjan Das via llvm-dev <llvm-dev at lists.llvm.org> wrote:> I am wriitng an LLVM pass to insert instrumentation at certain points of the > program. I want to pass the `StringRef` obtained from `getName()` as a > parameter to a function `func(char* s)`. I can allocate some space on stack > using `AllocaInst` to generate an `alloca` instruction. But, how can I copy > the `StringRef` to the stack space?I think this is usually done via anonymous globals. For example if you use Clang to compile this: void note_store(char *LocName, void *Addr); void foo(int *Ptr) { note_store("store in foo", (void *)Ptr); *Ptr = 42; } you get (essentially): @.str = private unnamed_addr constant [13 x i8] c"store in foo\00", align 1 define void @foo(i32* %Ptr) { %Ptr8 = bitcast i32* %Ptr to i8* call void @note_store(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %Ptr8) store i32 42, i32* %Ptr, align 4 ret void } So you'll create a new global i8 array that gets initialized to your StringRef's data and then pass a pointer to its first element into your instrumentation function. Cheers. Tim.
Dipanjan Das via llvm-dev
2017-May-03 20:44 UTC
[llvm-dev] How to pass a StringRef to a function call inserted as instrumentation?
Hi Tim, I think this is usually done via anonymous globals. For example if you> use Clang to compile this: > . > . > . > So you'll create a new global i8 array that gets initialized to your > StringRef's data and then pass a pointer to its first element into > your instrumentation function. >Sorry to reply late. It worked perfectly fine. For the sake of completeness, this is what I did: Value* var_name = builder.CreateGlobalStringPtr(po->getName()); // builder is an IRBuilder instance Above created the following IR: @0 = private unnamed_addr constant [2 x i8] c"a\00" -- Thanks & Regards, Dipanjan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170503/b80fcc9e/attachment.html>