Writing simple intrinsic in clang

I want to write a simple backend-specific instrinsic that will just call an instruction. How should I do that?

Is this really not documented anywhere...?

Anyway, the basic steps:

1. Add the builtin to the list of intrinsics for your target: include/llvm/IR/IntrinsicsYourTarget.td. Use a GCCBuiltin for the corresponding C intrinisc name.
2. Teach your target to lower the intrinsic to an instruction; you can usually just use the name of the intrinsic in a pattern in lib/Target/YourTarget/YourTargetInstrInfo.td.
3. Add the builtin to include/clang/Basic/BuiltinsYourTarget.def

An example of an intrinsic implemented this way is int_arm_qadd8 in the ARM backend (corresponding to the C builtin __builtin_arm_qadd8); it should be straightforward to follow that example.

This approach assumes your intrinsic is simple: it returns at most one value, and all the parameter and return types are legal. If that isn't true, you might need to write some C++ code to handle it in clang or in your backend.

Maybe we should add this as a section to http://llvm.org/docs/WritingAnLLVMBackend.html .

-Eli

Actually, I’ve done it, but I have problem with Intrinsic namespace, while adding my intrinsic to CodeGenFunction::EmitMyTargetBuiltinExpr I do something like CGF.getIntrinsic(Intrinsic::my_builtin) and it throws error
/tools/clang/lib/CodeGen/CGBuiltin.cpp:10327: error: no member named ‘arc_sub4’ in namespace ‘llvm::Intrinsic’
llvm::Function *F = CGM.getIntrinsic(Intrinsic::arc_sub4);

I don't know how to fix this.
I'm new to LLVM and need some help in it...

вт, 25 сент. 2018 г. в 21:12, Friedman, Eli <[efriedma@codeaurora.org](mailto:efriedma@codeaurora.org)>:

Are you sure you actually defined the intrinsic? (If you're adding a new .td file to include/llvm/IR/, you need to make sure there's a corresponding line in include/llvm/IR/Intrinsics.td.)

-Eli

You were right, i forgot the include, thanks for your answers)

ср, 26 сент. 2018 г. в 21:07, Friedman, Eli <efriedma@codeaurora.org>: