public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: Rasmus Villemoes <rasmus.villemoes@prevas.dk>,
	"gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: side-effect-free function
Date: Tue, 9 Mar 2021 10:41:12 -0700	[thread overview]
Message-ID: <f2afe3ea-a44f-5148-1db8-122f48841ce2@gmail.com> (raw)
In-Reply-To: <9d23c6b3-e20a-1775-4f9c-24ea4349bd87@prevas.dk>

On 3/9/21 8:05 AM, Rasmus Villemoes via Gcc wrote:
> Hi,
> 
> Consider some function now() which returns some kind of "current
> timestamp" as a simple scalar. It could be a wrapper for
> clock_gettime(CLOCK_MONOTONIC) which converts the timespec value to
> nanoseconds, or in the linux kernel one of the ktime_get* family.
> 
> Then consider code like
> 
> start = now();
> do_something();
> end = now();
> debug("something took %lu\n", end - start);
> 
> If debug() is a macro that expands to nothing (or an if(0) statement),
> the now() calls are actually redundant. But AFAIU one can't mark now()
> as pure, since gcc must not assume it returns the same value when
> do_something() provably doesn't touch global memory.
> 
> Is there some way to specify that a function doesn't have any side
> effects, but may return a different value each time it is called? I.e.,
> if its return value is not used, it can be elided completely, but
> consecutive calls can not be assumed to return the same value.

I don't believe there is one.  I'm also not sure the concept
of having "no side effect" would fit a function that can return
a different value on each call with the same argument values.
The only way to do that is to read and/or write global or, more
likely in the case of a function like now(), volatile memory.
Those define the concept of a side effect (in C and C++).

I think we'd need a different concept (in addition to a better
name).  It could apply not just to functions like now() above
but also to functions like malloc() calls to which GCC already
knows to eliminate if their result is unused.

Martin

      reply	other threads:[~2021-03-09 17:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-09 15:05 Rasmus Villemoes
2021-03-09 17:41 ` Martin Sebor [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f2afe3ea-a44f-5148-1db8-122f48841ce2@gmail.com \
    --to=msebor@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=rasmus.villemoes@prevas.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).