[PATCH] Added llvm.is.constant intrinsic

Sobczak, Janusz janusz.sobczak at mobica.com
Tue Jul 1 04:59:13 PDT 2014


On 30 June 2014 16:01, Joerg Sonnenberger <joerg at britannica.bec.de> wrote:

> On Mon, Jun 30, 2014 at 03:04:21PM +0200, Sobczak, Janusz wrote:
> > On 30 June 2014 13:13, Joerg Sonnenberger <joerg at britannica.bec.de>
> wrote:
> >
> > > On Mon, Jun 30, 2014 at 10:45:24AM +0200, Sobczak, Janusz wrote:
> > > > I'm thinking about modyfing __builtin_constant_p() implementation in
> > > clang
> > > > to do this (for integer arguments):
> > > > if (argument is known to be constant)
> > > >     replace  __builtin_constant_p() with 1 (True)
> > > > else
> > > >    replace __builtin_constant_p() with @llvm.is_constant() IR
> > >
> > > What is the intended use case here? All cases that *I* consider to be
> > > sane applications of __builtin_constant_p are workaround for target
> > > specific features that are better exposed as intrinsics themselve, e.g.
> > > IO asm instructions on x86. It is one of the most abused features of
> > > GCC, especially since it can introduce magic variance due to optimizer
> > > changes.
> > >
> >
> > Please read the discussion here:
> http://llvm.org/bugs/show_bug.cgi?id=4898
>
> I consider the original example from the Linux kernel bogus and I read
> the discussion as agreeing with that. I haven't yet seen an example of
> the performance improvements you mentioned. The whole conditions in the
> original example will be folded away. If they are not, that's a bug.
>
> What uses of _builtin_constant_p() are sensible and what uses are, in
fact, abuses is an interesting topic for a discussion. Nevertheless,
current status is that one can use __builtin_constant_p() in inline
functions and it will work as expected when compiled with gcc but
__builtin_constant_p() would always return false if compiled with clang.
This means that currently if someone wants to use __builtin_constant_p(),
he needs to write macros instead of inline functions.

For example, there are function-like macros in the linux kernel like:

105 <http://lxr.free-electrons.com/source/include/uapi/linux/swab.h#L105>
#define __swab16 <http://lxr.free-electrons.com/ident?i=__swab16>(x
<http://lxr.free-electrons.com/ident?i=x>)
\106 <http://lxr.free-electrons.com/source/include/uapi/linux/swab.h#L106>
        (__builtin_constant_p((__u16
<http://lxr.free-electrons.com/ident?i=__u16>)(x
<http://lxr.free-electrons.com/ident?i=x>)) ?     \107
<http://lxr.free-electrons.com/source/include/uapi/linux/swab.h#L107>
       ___constant_swab16
<http://lxr.free-electrons.com/ident?i=___constant_swab16>(x
<http://lxr.free-electrons.com/ident?i=x>) :                 \108
<http://lxr.free-electrons.com/source/include/uapi/linux/swab.h#L108>
       __fswab16 <http://lxr.free-electrons.com/ident?i=__fswab16>(x
<http://lxr.free-electrons.com/ident?i=x>))109
<http://lxr.free-electrons.com/source/include/uapi/linux/swab.h#L109>

This macro swaps the bytes at compile time if it is possible or calls arch
specific, fast swab implementations if 'x' is not known to be constant
(i.e. calls __builtin_swab() if compiler supports it).

The fewer false negatives __builtin_constant_p() returns, the more often
the compile-time computation path can be taken.

Joerg
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>



-- 
Janusz Sobczak
Mobica Ltd
www.mobica.com <http://www.mobica.com>


*Mobica is a provider of software engineering, testing and consultancy
services based in the UK, Poland and the USA, with a worldwide customer
base. We have a proven track record in delivering innovative solutions to
some of the world’s best-known companies in a range of sectors including
Automotive, Mobile, Semiconductor, Finance, TV, Marine and Aviation.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140701/2f5fa65e/attachment.html>


More information about the llvm-commits mailing list