About implementing new intrinsic

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

About implementing new intrinsic

Ferad Zyulkyarov-2
Hi,

I want to implement a new intrinsic in llvm that will denote a
parallel section within a function. I followed the documentation for
extending llvm (http://llvm.org/docs/ExtendingLLVM.html) but there is
something about the working mechanism that is not clear for me.

1. Why do we have to add support for the C backend? Is this only
necessary to transform the llvm assembly (bytecode) into C code by
using the library APIs?

2. Is the support that we add for the specific target the one that
takes part in generating the target specific code (native) from the
llvm bytecode?

3. Can I introduce an intrinsic that is actually a call to my function
that implements the logic? I suppose it is possible but unfortunately
I couldn't figure it out. For example, in GCC we can write an
intrinsic that translates to a C code.

Thanks,
Ferad
--
Ferad Zyulkyarov
Barcelona Supercomputing Center
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Reid Spencer-2
On Sat, 2007-03-31 at 15:34 +0200, Ferad Zyulkyarov wrote:
> Hi,
>
> I want to implement a new intrinsic in llvm that will denote a
> parallel section within a function. I followed the documentation for
> extending llvm (http://llvm.org/docs/ExtendingLLVM.html) but there is
> something about the working mechanism that is not clear for me.
>
> 1. Why do we have to add support for the C backend?

Because it is an important part of LLVM. For example, bugpoint uses the
CBE as a reference code generator. If you break CBE, you break bugpoint.
If you break bugpoint, we will have a much harder time finding bugs in
LLC, LLI, and JIT. The CBE is just as important a code generator as any
of the targets. For example, it would be a vital component of
bootstrapping LLVM onto a platform that had no C++ compiler.

> Is this only
> necessary to transform the llvm assembly (bytecode) into C code by
> using the library APIs?

I'm not sure what you mean by "by using the library APIs", but yes,
that's the basic idea. The CBE takes an LLVM Module (in C++ IR form) and
generates C code that will execute the represented program. It is
necessary for your intrinsic to be supported in the CBE.

>
> 2. Is the support that we add for the specific target the one that
> takes part in generating the target specific code (native) from the
> llvm bytecode?

There are two parts to this. First, you must lower the intrinsic from
its IR form (a Function*) into a SelectionDAG node. This is done in
SelectionDAGISel.cpp in the visitIntrinsicCall method. You might need to
change DAGCombiner.cpp and LegalizeDAG.cpp depending on the intrinsic
and its lowering.  The second part is to modify the targets or generic
code gen to handle the lowered intrinsic call. This part is target
specific.

> 3. Can I introduce an intrinsic that is actually a call to my function
> that implements the logic? I suppose it is possible but unfortunately
> I couldn't figure it out. For example, in GCC we can write an
> intrinsic that translates to a C code.

As part of PR1297 (http://llvm.org/PR1297) I am about to make this
happen. There are certain kinds of intrinsics that want to have a
function body generated for them if a target or code generator cannot
handle the intrinsic natively. For example, the company I work for has
targets that know how to do a "bit_concat". That is, take two integers
of any width and concatenate them into a longer integer.  Most other
targets, however, don't know how to do this natively. In such cases the
intrinsic for "bit_concat" is lowered to an internal function that does
the necessary shift/or implementation.

I expect this functionality to be incorporated into LLVM in the next
week or so. Watch for changes to lib/CodeGen/IntrinsicLowering.cpp

Reid.
>
> Thanks,
> Ferad

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Chris Lattner
>> 3. Can I introduce an intrinsic that is actually a call to my function
>> that implements the logic? I suppose it is possible but unfortunately
>> I couldn't figure it out. For example, in GCC we can write an
>> intrinsic that translates to a C code.
>
> As part of PR1297 (http://llvm.org/PR1297) I am about to make this
> happen. There are certain kinds of intrinsics that want to have a
> function body generated for them if a target or code generator cannot
> handle the intrinsic natively. For example, the company I work for has

IntrinsicLowering already does this.  It lets you lower intrinsics to
arbitrary LLVM calls, including calls to external functions.

-Chris

--
http://nondot.org/sabre/
http://llvm.org/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Ferad Zyulkyarov
Hi,

> IntrinsicLowering already does this.  It lets you lower intrinsics to
> arbitrary LLVM calls, including calls to external functions.

I will try to do that in IntrinscLowering class. May you point me an
intrinsic implementation that lowers to an llvm call.

Thanks for advices ans hints,
Ferad


--
Ferad Zyulkyarov
Barcelona Supercomputing Center
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Reid Spencer-2
In reply to this post by Chris Lattner
On Sat, 2007-03-31 at 12:38 -0800, Chris Lattner wrote:

> >> 3. Can I introduce an intrinsic that is actually a call to my function
> >> that implements the logic? I suppose it is possible but unfortunately
> >> I couldn't figure it out. For example, in GCC we can write an
> >> intrinsic that translates to a C code.
> >
> > As part of PR1297 (http://llvm.org/PR1297) I am about to make this
> > happen. There are certain kinds of intrinsics that want to have a
> > function body generated for them if a target or code generator cannot
> > handle the intrinsic natively. For example, the company I work for has
>
> IntrinsicLowering already does this.  It lets you lower intrinsics to
> arbitrary LLVM calls, including calls to external functions.

I think that when Ferad said "in GCC we can write an intrinsic that
translates to a C code" meant that the intrinsic would be expanded to
have a body much as I'm planning on doing in the this PR1297. To my
understanding, IntrinsicLowering doesn't support expansion to a function
with a body.  Or, am I just missing something on that?

Perhaps Ferad could explain in a little more detail what he meant?

>
> -Chris
>

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Chris Lattner
In reply to this post by Ferad Zyulkyarov
On Sat, 31 Mar 2007, Ferad Zyulkyarov wrote:
>> IntrinsicLowering already does this.  It lets you lower intrinsics to
>> arbitrary LLVM calls, including calls to external functions.
>
> I will try to do that in IntrinscLowering class. May you point me an
> intrinsic implementation that lowers to an llvm call.

bswap expands into a series of shifts and or's, for example.  It would be
straight-forward to expand it into a libcall if you desired.

-Chris

--
http://nondot.org/sabre/
http://llvm.org/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Ferad Zyulkyarov
In reply to this post by Reid Spencer-2
Hi,

I will try to explain by giving an example.

Let's say that I have an intrinsic: int llvm.myintrinsic(int)
I have a function: int myintrinsic_handler(int)

When
%var = call int %llvm.myintrinsic( int %arg )

is met in the code, I want the code generator put in its place: a call
to function
"myintrinsic_handler" (i.e. %var = call int %myintrinsic_handler( int %arg ) )

or probably in native assembly (i.e. x86) it will look like something this
push %arg
call myintrinsic_handler
pop res

On 3/31/07, Reid Spencer <[hidden email]> wrote:

> On Sat, 2007-03-31 at 12:38 -0800, Chris Lattner wrote:
> > >> 3. Can I introduce an intrinsic that is actually a call to my function
> > >> that implements the logic? I suppose it is possible but unfortunately
> > >> I couldn't figure it out. For example, in GCC we can write an
> > >> intrinsic that translates to a C code.
> > >
> > > As part of PR1297 (http://llvm.org/PR1297) I am about to make this
> > > happen. There are certain kinds of intrinsics that want to have a
> > > function body generated for them if a target or code generator cannot
> > > handle the intrinsic natively. For example, the company I work for has
> >
> > IntrinsicLowering already does this.  It lets you lower intrinsics to
> > arbitrary LLVM calls, including calls to external functions.
>
> I think that when Ferad said "in GCC we can write an intrinsic that
> translates to a C code" meant that the intrinsic would be expanded to
> have a body much as I'm planning on doing in the this PR1297. To my
> understanding, IntrinsicLowering doesn't support expansion to a function
> with a body.  Or, am I just missing something on that?
>
> Perhaps Ferad could explain in a little more detail what he meant?
>
> >
> > -Chris
> >
>
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>


--
Ferad Zyulkyarov
Barcelona Supercomputing Center
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Ferad Zyulkyarov
In reply to this post by Chris Lattner
> >> IntrinsicLowering already does this.  It lets you lower intrinsics to
> >> arbitrary LLVM calls, including calls to external functions.
> >
> > I will try to do that in IntrinscLowering class. May you point me an
> > intrinsic implementation that lowers to an llvm call.
>
> bswap expands into a series of shifts and or's, for example.  It would be
> straight-forward to expand it into a libcall if you desired.

I will throw a look how bswap is implemented. Currently I work on the
1.9 source.. are these features available there or I should checkout
from svn?

Thanks again for your helpful advices,
Ferad

--
Ferad Zyulkyarov
Barcelona Supercomputing Center
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Chris Lattner
In reply to this post by Ferad Zyulkyarov
On Sat, 31 Mar 2007, Ferad Zyulkyarov wrote:
> I will try to explain by giving an example.
> Let's say that I have an intrinsic: int llvm.myintrinsic(int)
> I have a function: int myintrinsic_handler(int)
> When
> %var = call int %llvm.myintrinsic( int %arg )
> is met in the code, I want the code generator put in its place: a call
> to function
> "myintrinsic_handler" (i.e. %var = call int %myintrinsic_handler( int %arg ) )

This is exactly what intrinsic lowering does.  To answer your other email,
LLVM 1.9 also has this related stuff, including bswap:
http://llvm.org/releases/1.9/docs/LangRef.html

-Chris

--
http://nondot.org/sabre/
http://llvm.org/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Ferad Zyulkyarov
Thanks Chris

--
Ferad Zyulkyarov
Barcelona Supercomputing Center
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Devang Patel
In reply to this post by Ferad Zyulkyarov-2

On Mar 31, 2007, at 6:34 AM, Ferad Zyulkyarov wrote:

> Hi,
>
> I want to implement a new intrinsic in llvm that will denote a
> parallel section within a function.

What parallelization model are you implementing ?

Thanks,
-
Devang

> I followed the documentation for
> extending llvm (http://llvm.org/docs/ExtendingLLVM.html) but there is
> something about the working mechanism that is not clear for me.

> 1. Why do we have to add support for the C backend? Is this only
> necessary to transform the llvm assembly (bytecode) into C code by
> using the library APIs?
>
> 2. Is the support that we add for the specific target the one that
> takes part in generating the target specific code (native) from the
> llvm bytecode?
>
> 3. Can I introduce an intrinsic that is actually a call to my function
> that implements the logic? I suppose it is possible but unfortunately
> I couldn't figure it out. For example, in GCC we can write an
> intrinsic that translates to a C code.
>
> Thanks,
> Ferad
> --
> Ferad Zyulkyarov
> Barcelona Supercomputing Center
> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Vikram S. Adve
In reply to this post by Ferad Zyulkyarov-2

On Mar 31, 2007, at 8:34 AM, Ferad Zyulkyarov wrote:

> I want to implement a new intrinsic in llvm that will denote a
> parallel section within a function.

Can you explain what you mean by a parallel section within a function?

--Vikram

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Duncan Sands
> > I want to implement a new intrinsic in llvm that will denote a
> > parallel section within a function.
>
> Can you explain what you mean by a parallel section within a function?

Maybe it's a reference to OMP, http://www.openmp.org/.  gcc has added
support for this (GOMP).

Ciao,

Duncan.
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Ferad Zyulkyarov
In reply to this post by Vikram S. Adve
Hi,

> > I want to implement a new intrinsic in llvm that will denote a
> > parallel section within a function.
>
> Can you explain what you mean by a parallel section within a function?

I want to see how OpenMP's parallel fit in the LLVM architecture, is
it easy to implement or not. GCC is too heavy platform to work on..

Ferad

--
Ferad Zyulkyarov
Barcelona Supercomputing Center
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Chris Lattner
On Mon, 2 Apr 2007, Ferad Zyulkyarov wrote:
>>> I want to implement a new intrinsic in llvm that will denote a
>>> parallel section within a function.
>> Can you explain what you mean by a parallel section within a function?
> I want to see how OpenMP's parallel fit in the LLVM architecture, is
> it easy to implement or not. GCC is too heavy platform to work on..

When implemented in LLVM, OpenMP will be supported in a very similiar way
to what GCC does: it will extract out the parallel region (the part that
runs on multiple threads) into a self-contained function.

-Chris

--
http://nondot.org/sabre/
http://llvm.org/
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: About implementing new intrinsic

Ferad Zyulkyarov-2
Hi,

> >>> I want to implement a new intrinsic in llvm that will denote a
> >>> parallel section within a function.
> >> Can you explain what you mean by a parallel section within a function?
> > I want to see how OpenMP's parallel fit in the LLVM architecture, is
> > it easy to implement or not. GCC is too heavy platform to work on..
>
> When implemented in LLVM, OpenMP will be supported in a very similiar way
> to what GCC does: it will extract out the parallel region (the part that
> runs on multiple threads) into a self-contained function.

I agree also. But I just noticed that the LLVM internals are much
easier to manipulate (ok at least it seems to me).

Thanks,
Ferad

--
Ferad Zyulkyarov
Barcelona Supercomputing Center
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev