[llvm-dev] How to convert function insertion module pass to intrinsic to inline

K Jelesnianski via llvm-dev llvm-dev at lists.llvm.org
Tue Oct 29 12:16:02 PDT 2019


Dear mailing list,

I currently have a traditional module instrumentation pass that
inserts new function calls into a given IR according to some logic
(inserted functions are external from a small lib that is later linked
to given program). Running experiments, a lot of my overhead is from
the cost of executing a function call to the library function.

I therefore would like to inline these function bodies into the IR of
the given program to get rid of this bottleneck. I assume an intrinsic
would be a clean way of doing this, since an intrinsic function would
be expanded to its function body when being lowered to ASM (please
correct me if my understanding is incorrect here, this is my first
time working with intrinsics/LTO).

My original library call definition:
void register_my_mem(void *user_vaddr){
  ... C code ...
}

So far:
- I have created a def in:   llvm-project/llvm/include/llvm/IR/IntrinsicsX86.td
let TargetPrefix = "x86" in {
def int_x86_register_mem : GCCBuiltin<"__builtin_register_my_mem">,
Intrinsic<[], [llvm_anyint_ty], []>;
}

- Added another def in:
otwm/llvm-project/clang/include/clang/Basic/BuiltinsX86.def
TARGET_BUILTIN(__builtin_register_my_mem, "vv*", "", "")

- Added my library source (*.c, *.h) to the compiler-rt/lib/test_lib
and added to CMakeLists.txt

- Replaced the function insertion with trying to insert the intrinsic
instead in: llvm/lib/Transforms/Instrumentation/myModulePass.cpp
WAS:
FunctionCallee sm_func =
curr_inst->getModule()->getOrInsertFunction("register_my_mem",
func_type);
ArrayRef<Value*> args = {
      builder.CreatePointerCast(sm_arg_val, currType->getPointerTo())
};
builder.CreateCall(sm_func, args);

NEW:
Intrinsic::ID aREGISTER(Intrinsic::x86_register_my_mem);
Function *sm_func = Intrinsic::getDeclaration(currFunc->getParent(),
aREGISTER, func_type);
ArrayRef<Value*> args = {
      builder.CreatePointerCast(sm_arg_val, currType->getPointerTo())
};
builder.CreateCall(sm_func, args);
----------------------
Questions:
- If my logic for inserting the intrinsic functions shouldnt be a
module pass, where do i put it?
- Am I confusing LTO with intrinsics?
-Do I put my library function definitions into the following files as
mentioned in http://lists.llvm.org/pipermail/llvm-dev/2017-June/114322.html
??
clang/lib/CodeGen/CodeGenFunction.cpp - define llvm::Instrinsic::ID
clang/lib/CodeGen/CodeGenFunction.h - declare llvm::Intrinsic::ID
as for example EmitRegisterMyMem()?

My LLVM compiles, so it is semantically correct, but currently when
trying to insert this function call, LLVM segfaults saying the type
parameter type is invalid.

Sincerely,

Christopher Jelesnianski
Graduate Research Assistant
Virginia Tech


More information about the llvm-dev mailing list