[cfe-dev] need help adding arch-specific builtin

Mara Sophie Grosch via cfe-dev cfe-dev at lists.llvm.org
Sun Oct 24 06:48:27 PDT 2021


Hi lists,

I'm trying to add a new compiler builtin for AVR architecture,
`void __builtin_avr_delay_cycles(int32)`.

I already made clang recognize this as a builtin with the correct type.
Now there are some assumptions on what I have to do:

* I think have to map this to an arch-specific intrinsic in LLVM
* in clang/lib/CodeGen/CGBuiltin I'd have to add a function for AVR
   builtins and call it from EmitTargetArchBuiltinExpr
* in this new function, I would generate the actual instructions -
   building the "generate code from builtin call" part

I added an intrinsic for this[1] and added the function in CGBuiltin and
call it for Arch == llvm::Triple::avr - but cannot get my new code to be
called. I even added an `assert(false)` in EmitTargetArchBuiltinExpr(),
which should cause the compiler to crash on that assert whenever it is
supposed to handle a builtin for AVR targets - but that assert never
fires.

Instead, I get the error given below [2], which I cannot make a lot out
of it.

With my not-yet-very-good knowledge how everything is combined together
in clang and llvm, I assume this might be because it tries to pass a i32
to the builtin (which is expected), but cannot do that because the arch
cannot pass i32s around in arguments? But that shouldn't be needed for a
builtin - that call to it shouldn't be in the resulting machine code
anyway.

Any clues where to find more information on how to achieve what I'm
trying to do would be appreciated. I poked around at a lot of different
places in the code base by now, trying to find more info by myself, but
am stuck.

The builtin in question is supposed to generate a loop taking the given
amount of clock cycles to complete - something commonly used on
microcontrollers to have timing. Frequency of the CPU is known at
compile time via preprocessor definition for that and the builtin
already exists on gcc, I just want to also have it in clang.

Thanks for any help and comments
Mara
(I subscribed to cfe-dev in digest mode only - answering me directly too
would be appreciated)


[1] I added this into llvm/include/llvm/IR/IntrinsicsAVR.td, which I
include from llvm/include/llvm/IR/Intrinsics.td:

let TargetPrefix = "avr" in {  // All intrinsics start with "llvm.avr."
   def int_avr_delay_cycles : GCCBuiltin<"__builtin_avr_delay_cycles">,
               Intrinsic<[], [llvm_i32_ty], [IntrInaccessibleMemOnly, IntrCold, IntrNoDuplicate, IntrNoMerge, IntrHasSideEffects]>;
}


[2] Trying to compile code using this builtin results in this error:
ExpandIntegerOperand Op #2: t8: ch = llvm.avr.delay.cycles t5, TargetConstant:i16<2445>, Constant:i32<8000000>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20211024/8e050516/attachment.sig>


More information about the cfe-dev mailing list