<div dir="ltr"><div><div><div><div><div>Being new to LLVM, I am finding some difficulties in properly defining a custom intrinsic (<i>foo_sqrt_test</i>). So far I have done the following steps: <br></div><i><b>Back-end: </b><br></i></div>1. Added <a href="https://pastebin.com/psrUCNGH">IntrinsicsFoo.td</a> in <i>include/llvm/IR/</i>. <br></div><div>2. Added <a href="https://pastebin.com/9LsXYGMR">FooInstrInfo.td</a> in <i>llvm/Target/Foo/.<br></i></div><div>3. Added case for <i>Intrinsic::foo_sqrt_test </i>in <i>llvm/lib/Analysis/ConstantFolding.cpp#canConstantFoldCallTo(ImmutableCallSite, const Function *).</i><br></div><div><i><br></i></div><div><i><b>Front-end:<br></b></i></div><div>1. Added <a href="https://pastebin.com/kTXE3GRA">BuiltinsFoo.def</a> in <i>include/clang/Basic/.<br></i></div><div>2. Added namespace Foo in <i>include/clang/Basic/TargetBuiltins.h </i>like <a href="https://pastebin.com/J18Vi2kV">this</a>. <br></div>3. Added declaration for <i>EmitFooBuiltinExpr(unsigned BuiltinID, const CallExpr *E)</i> in <i>clang/lib/CodeGen/CodeGenFunction.h</i>.<br></div>4. Defined above function in <i>clang/lib/CodeGen/</i>CGBuiltin.cpp like<a href="https://pastebin.com/hAD2tdjY"> this</a>.  <br><br></div><div><i><b>Custom Pass: </b></i>I added a custom pass <a href="https://pastebin.com/J7pSPEtR">InsertInst.cpp</a>  in <i>llvm/lib/Transforms/Insert.</i><b> </b><br></div><div><br></div><div>After making the above changes, I built the entire <i>llvm</i> project (along with <i>clang</i>) using <i>ninja</i> and the build was successful. <br><br></div><div><i><b>Testing:<br></b></i></div><div>So basically what I'm trying to do here is, whenever there is a <i>fadd </i>instruction in the <i>IR </i>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)<br></div><div>1. C file: <a href="https://pastebin.com/XgKBwzcL">test.c<br></a></div><div> 2. IR file (<a href="https://pastebin.com/yN3aBtFQ">test1.s</a>) generated using:  <i><br>           $clang -c -emit-llvm test.c -o test.ll<br></i></div><div><i>           $llvm-dis < test.ll > test1.s <br></i></div><div>3. IR file (<a href="https://pastebin.com/tLNnqedf">test2.s</a>) generated using: <br></div><div>           <i>$opt -load ../build/lib/LLVMInsertInstruction.so -insertinst -o test_opt.ll < test.ll<br>           $llvm-dis < test_opt.ll > test2.s<br>$lli test.ll </i>is working fine.<br></div><div><i>$lli test_opt.ll </i>is showing: <b>LLVM ERROR: Cannot select: intrinsic %llvm.foo.foo_sqrt_test<br><br></b></div><div>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! <br></div><div><div><div><div><div><div><br></div></div></div></div></div></div></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature"><div dir="ltr"><p dir="ltr">Sayantan Majumdar<br>
Research Intern at RISE Lab,<br>Indian Institute of Technology Madras, Chennai</p>
</div></div>