<div dir="auto">If you add GCCBuiltin to the intrinsic definition, clang will automatically do the conversion from C to IR. EmitTargetArchBuiltin will only be called if no GCCBuiltin can be found.</div><div dir="auto"><br></div><div><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Oct 24, 2021 at 6:48 AM Mara Sophie Grosch via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)">Hi lists,<br>
<br>
I'm trying to add a new compiler builtin for AVR architecture,<br>
`void __builtin_avr_delay_cycles(int32)`.<br>
<br>
I already made clang recognize this as a builtin with the correct type.<br>
Now there are some assumptions on what I have to do:<br>
<br>
* I think have to map this to an arch-specific intrinsic in LLVM<br>
* in clang/lib/CodeGen/CGBuiltin I'd have to add a function for AVR<br>
   builtins and call it from EmitTargetArchBuiltinExpr<br>
* in this new function, I would generate the actual instructions -<br>
   building the "generate code from builtin call" part<br>
<br>
I added an intrinsic for this[1] and added the function in CGBuiltin and<br>
call it for Arch == llvm::Triple::avr - but cannot get my new code to be<br>
called. I even added an `assert(false)` in EmitTargetArchBuiltinExpr(),<br>
which should cause the compiler to crash on that assert whenever it is<br>
supposed to handle a builtin for AVR targets - but that assert never<br>
fires.<br>
<br>
Instead, I get the error given below [2], which I cannot make a lot out<br>
of it.<br>
<br>
With my not-yet-very-good knowledge how everything is combined together<br>
in clang and llvm, I assume this might be because it tries to pass a i32<br>
to the builtin (which is expected), but cannot do that because the arch<br>
cannot pass i32s around in arguments? But that shouldn't be needed for a<br>
builtin - that call to it shouldn't be in the resulting machine code<br>
anyway.<br>
<br>
Any clues where to find more information on how to achieve what I'm<br>
trying to do would be appreciated. I poked around at a lot of different<br>
places in the code base by now, trying to find more info by myself, but<br>
am stuck.<br>
<br>
The builtin in question is supposed to generate a loop taking the given<br>
amount of clock cycles to complete - something commonly used on<br>
microcontrollers to have timing. Frequency of the CPU is known at<br>
compile time via preprocessor definition for that and the builtin<br>
already exists on gcc, I just want to also have it in clang.<br>
<br>
Thanks for any help and comments<br>
Mara<br>
(I subscribed to cfe-dev in digest mode only - answering me directly too<br>
would be appreciated)<br>
<br>
<br>
[1] I added this into llvm/include/llvm/IR/IntrinsicsAVR.td, which I<br>
include from llvm/include/llvm/IR/Intrinsics.td:<br>
<br>
let TargetPrefix = "avr" in {  // All intrinsics start with "llvm.avr."<br>
   def int_avr_delay_cycles : GCCBuiltin<"__builtin_avr_delay_cycles">,<br>
               Intrinsic<[], [llvm_i32_ty], [IntrInaccessibleMemOnly, IntrCold, IntrNoDuplicate, IntrNoMerge, IntrHasSideEffects]>;<br>
}<br>
<br>
<br>
[2] Trying to compile code using this builtin results in this error:<br>
ExpandIntegerOperand Op #2: t8: ch = llvm.avr.delay.cycles t5, TargetConstant:i16<2445>, Constant:i32<8000000><br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div>