[llvm-dev] The doubt to LLVM.org-How to add the new type?

mats petersson via llvm-dev llvm-dev at lists.llvm.org
Thu Jul 6 05:16:19 PDT 2017

The detailed answer will depend on how you want this type to behave and
what the exact semantics both in C and LLVM-IR should be. It is a fair
amount of text to even begin to describe this, so I'm not about to write a
"complete guide to changing clang to support another type" - that would
probably take several days of intense writing [at least for me!]

A trivial model would be to use existing bitwise structs, e.g.

   struct X {
       int a:1;
       int b:3;

   X x;

   x.a = 1;
   x.b = 5;

and simply make the codegen part of Clang and LLVM understand how to
generate whatever instructions the target processor has to support this (I
have not looked at what clang generates for this, but I'd expect this to be
"translatable" to 'bit operatons' in some fashion either way) - one way to
solve this may be to introduce llvm intrinsic functions that can then be
handled in the machine instruction generaton - and have Clang codegen
simply translate x.a = 1; into llvm.something.set_bit(x, 0, 1, 1) [where
arguments represent address, bit number, number of bits and value to set].
That is just ONE possible solution, and I'm sure there are a dozen other
choices - not saying this is the right or best method, but it seems to me
as a plausible scenario.

On the other hand, if you really need a new type, you'll be adding code in
many places. Here's a patch I found adding the "PipeType" (I work on
OpenCL, so I looked through for types added for OpenCL as that's something
I know has happened...)

It is probably not a perfect example, but it will give you some rough idea
of what sort of work is involved in adding a new type.
Conceptually, these things aren't very hard to do, but there's typically
several places that need to be modified for even a relatively simple type
addition. If the type then "behaves" different to regular existing types,
you'll also
need to implement code to reflect that in the compiler (e.g. "you can't use
operator + on a 'bit' would need suitable changes in Sema)

You may then have to add types to LLVM too - I'm not familiar with much of
that, so can't really advice much there.


On 6 July 2017 at 11:35, To be a better me via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> Dear Mr.::
>    So far,I still have two questions.
>      For the first question,I am adding the recognition to a new type , so
> I want to know about LLVM / CLANG running mechanism of identifying the type
> . For example, when recogning the type , CLANG is own analytical type or
> call LLVM to resolve the type.So now can you tell me about LLVM and CLANG’operation
> mechanism for the identification of the type Or give me some relevant
> information?
>       For the second question, the new type I am adding is the base type
> Bit in the microcontroller. I now follow the step to add a new type in the
> LLVM official website[1] to modify the LLVM source code ,what’s more,It
> is compiled successfully. So I wrote a test program in VS2015 to identify
> the new type I added, but still did not recognize it. I debug the test
> program, followed the program to run inside, did not go to the LLVM code I
> added. So I do not know where the problem is in.I also not clear that the
> code in CLANG is or not needs to be modified. Can you explain it to me?
>       Thanke you for your guidance.
>  sincerely  zhangya
> [1] http://llvm.org/docs/ExtendingLLVM.html#adding-a-fundamental-type
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170706/66923e44/attachment-0001.html>

More information about the llvm-dev mailing list