[llvm-bugs] [Bug 34600] New: Add function __attribute__((idempotent))

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Sep 14 02:55:15 PDT 2017


            Bug ID: 34600
           Summary: Add function __attribute__((idempotent))
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: bugzilla at tecnocode.co.uk
                CC: llvm-bugs at lists.llvm.org

(Copied from a similar gcc bug here:

tl;dr: It would be useful for GObject-based C code if Clang supported a new
__attribute__((idempotent)) to mark functions as doing something (and probably
changing global state) the first time they are called, but being effectively
__attribute__((const)) for every subsequent call.

In GNOME's gobject object-oriented code, each type has a "get_type" function. 
Something like, eg:

  static GType our_type = 0;

  if (our_type == 0)
    our_type = g_boxed_type_register_static (I_("PangoLayoutLine"),
                           (GBoxedCopyFunc) pango_layout_line_ref,
                           (GBoxedFreeFunc) pango_layout_line_unref);
  return our_type;

Since these functions are called quite frequently (as part of casts among other
things), people have started marking them with __attribute__((const)) so the
compiler can optimize away multiple calls to them.  However, there is a down
side to it.  Sometimes one needs to make sure the type is registered, so they
call the _get_type() function to ensure that, but the compiler, seeing the
((const)) attribute, optimizes away the call.

This is of course a bug to mark functions with side-effects as ((const)). 
That's why a new function attribute like "idempotent" is useful.  What it means
is that subsequent calls to this function return the same value as the first
call, have no side-effect, and can be optimized away, but not the first call...

More information about GObject requirements here:


This would presumably also be useful for various implementations of singletons
in other idiomatic C code.

You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20170914/f27a9095/attachment.html>

More information about the llvm-bugs mailing list