<div><span style="word-spacing:1px;border-color:rgb(49,49,49);color:rgb(49,49,49)">The C to IR conversion happens in CGBuiltin.cpp where it calls </span><div dir="auto" style="font-size:1rem;word-spacing:1px;border-color:rgb(49,49,49);color:rgb(49,49,49)">Intrinsic::getIntrinsicForGCCBuiltin.</div><div dir="auto" style="word-spacing:1px;border-color:rgb(49,49,49);color:rgb(49,49,49)"><br></div><div dir="auto" style="font-size:1rem;word-spacing:1px;border-color:rgb(49,49,49);color:rgb(49,49,49)">The error your getting is coming from SelectionDAG. The error seems to indicate that i32 is not a legal type for the AVR and LegalizeIntegerTypes.cpp does know how to make it legal. It wants to split into 2 i16s but it doesn’t know how. Intrinsics with illegal types are usually handled by adding setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i32, Custom) to the AVRTargetLowering constructor. And then adding a case for INTRINSIC_W_CHAIN to AVRTargetLowering::LowerOperation to detect the specific intrinsic and handle it.</div><div dir="auto" style="word-spacing:1px;border-color:rgb(49,49,49);color:rgb(49,49,49)"><br></div><div dir="auto" style="font-size:1rem;word-spacing:1px;border-color:rgb(49,49,49);color:rgb(49,49,49)">Alternatively you chang the intrinsic to use i16 instead of i32 if that meets your needs.</div></div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Oct 24, 2021 at 9:30 AM Mara Sophie Grosch via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-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)">that's good to know, thank you. Where is the code generation for those<br>
intrinsics then?<br>
<br>
(forwarded to list because I don't use mailings lists very often and<br>
answered Craig only..)<br>
<br>
Am Sun, Oct 24, 2021 at 09:23:34AM -0700 schrieb Craig Topper:<br>
> If you add GCCBuiltin to the intrinsic definition, clang will automatically<br>
> do the conversion from C to IR. EmitTargetArchBuiltin will only be called<br>
> if no GCCBuiltin can be found.<br>
> <br>
> On Sun, Oct 24, 2021 at 6:48 AM Mara Sophie Grosch via cfe-dev <<br>
> <a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>> wrote:<br>
> <br>
> > 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,<br>
> > 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,<br>
> > 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>
> > <br>
> -- <br>
> ~Craig<br>
<br>
----- Ende weitergeleitete Nachricht -----<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div>