[llvm-dev] Attribute for a invariant function ?

Martin J. O'Riordan via llvm-dev llvm-dev at lists.llvm.org
Sat Jan 9 07:00:33 PST 2016


I use '__attribute__((pure))' for fast math functions and some other
intrinsic function I have added such as getting the current core ID.  This
tells the compiler that calling the same function with the same arguments
will always return the same value.  A function declared with this attribute
is not supposed to have any side-effects such as altering global variables.

 

So:

 

extern void *lookup(int) __attribute__((pure));

 

might do what you need though.

 

            MartinO 

 

From: llvm-dev [mailto:llvm-dev-bounces at lists.llvm.org] On Behalf Of Nat!
via llvm-dev
Sent: 09 January 2016 14:41
To: llvm-dev <llvm-dev at lists.llvm.org>
Subject: [llvm-dev] Attribute for a invariant function ?

 

I have a read-only lookup table, which takes integer arguments. Optimally I
would like to indicate in the C source, that the function is invariant (?),
so that the superflous calls can be optimized away.

```
extern void  *lookup( int a);

void   foo( void)
{
   void   *a, *b;
   
   a = lookup( 0x12345678);
   bar( a);

   b = lookup( 0x12345678);  // b guaranteed to be the same as a, no call to
(slow) lookup needed
   baz( b);
}
```

But I don't really find anything in clang or llvm in terms of function
attributes. The best I could come up with is in the llvm code to use
attributes "argmemonly=true" readonly=true". But still two calls did happen.
(I am on 3.7)

Ciao
   Nat!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160109/09d04d01/attachment.html>


More information about the llvm-dev mailing list