<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Add function __attribute__((idempotent))"
   href="https://bugs.llvm.org/show_bug.cgi?id=34600">34600</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Add function __attribute__((idempotent))
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>bugzilla@tecnocode.co.uk
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>(Copied from a similar gcc bug here:
<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=32911">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=32911</a>)

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:

GType
pango_layout_line_get_type(void)
{
  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:

<a href="https://bugzilla.gnome.org/show_bug.cgi?id=64994">https://bugzilla.gnome.org/show_bug.cgi?id=64994</a>

This would presumably also be useful for various implementations of singletons
in other idiomatic C code.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>