[llvm-dev] INTRINSIC SELECTION

Sayantan Majumdar via llvm-dev
Wed, 28 Jun 2017 11:33:39 -0000

Being new to LLVM, I am finding some difficulties in properly defining a
custom intrinsic (*foo_sqrt_test*). So far I have done the following steps:

*Back-end: *
1. Added IntrinsicsFoo.td <https://pastebin.com/psrUCNGH> in
*include/llvm/IR/*.
2. Added FooInstrInfo.td <https://pastebin.com/9LsXYGMR> in
*llvm/Target/Foo/.*
3. Added case for *Intrinsic::foo_sqrt_test *in
*llvm/lib/Analysis/ConstantFolding.cpp#canConstantFoldCallTo(ImmutableCallSite,
const Function *).*


*Front-end:*
1. Added BuiltinsFoo.def <https://pastebin.com/kTXE3GRA> in
*include/clang/Basic/.*
2. Added namespace Foo in *include/clang/Basic/TargetBuiltins.h *like this
<https://pastebin.com/J18Vi2kV>.
3. Added declaration for *EmitFooBuiltinExpr(unsigned BuiltinID, const
CallExpr *E)* in *clang/lib/CodeGen/CodeGenFunction.h*.
4. Defined above function in *clang/lib/CodeGen/*CGBuiltin.cpp like this
<https://pastebin.com/hAD2tdjY>.

*Custom Pass: *I added a custom pass InsertInst.cpp
<https://pastebin.com/J7pSPEtR>  in *llvm/lib/Transforms/Insert.*

After making the above changes, I built the entire *llvm* project (along
with *clang*) using *ninja* and the build was successful.


*Testing:*
So basically what I'm trying to do here is, whenever there is a *fadd
*instruction
in the *IR *file, a floating point constant 5 will be added and the square
root of this new value will be taken to be as the final value to be
displayed. (This is working fine with the predefined Intrinsic::sqrt that
LLVM provides)
1. C file: test.c
<https://pastebin.com/XgKBwzcL>
2. IR file (test1.s <https://pastebin.com/yN3aBtFQ>) generated using:

*           $clang -c -emit-llvm test.c -o test.ll*

*           $llvm-dis < test.ll > test1.s *
3. IR file (test2.s <https://pastebin.com/tLNnqedf>) generated using:


*$opt -load ../build/lib/LLVMInsertInstruction.so -insertinst -o
test_opt.ll < test.ll           $llvm-dis < test_opt.ll > test2.s$lli
test.ll *is working fine.
*$lli test_opt.ll *is showing:

*LLVM ERROR: Cannot select: intrinsic %llvm.foo.foo_sqrt_test*
I cannot find any clue as to what other changes to be made. It would be
great if I am able to get some help regarding this issue. Thanks!

--

Sayantan Majumdar
Research Intern at RISE Lab,
Indian Institute of Technology Madras, Chennai
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20170628/b59b22da/attachment.html>