[llvm-dev] How to pass a StringRef to a function call inserted as instrumentation?

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Fri Apr 28 17:27:01 PDT 2017


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.


More information about the llvm-dev mailing list