public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Kalamatee <kalamatee@gmail.com>
To: gcc-help@gcc.gnu.org
Subject: Problem understand optimization with inline asm
Date: Tue, 28 Mar 2023 12:15:37 +0100	[thread overview]
Message-ID: <CAJWNc-62ebOfM6E=z8H9h3vE=+qfss2kidaHpWH3usWLSO9iuA@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1854 bytes --]

Good afternoon,

I have spent a while debugging an issue in an operating system I contribute
to, and it comes down to the behaviour of optimizing code.

We have code which is compiled for a native, or "hosted" (e.g. under linux)
environment, and have code which does the following -:

foo(struct mystruct1 *bar)
{
 int somevar = 0; // variable is initialized.
int anothervar = 0;
#if (some clause to check if it is not hosted)
asm volatile(
<some asm block which sets "somevar" if a feature is enabled, otherwise
leaves it as its initialized state>
: "=m" (somevar)
);
#endif
if (somevar)
{
//use the feature
 bar->param = 20;
anothervar = 1;
}
if (!anothervar)
{
 bar->param = 10;
}
)


Without optimization, the code works as intended for both the native and
hosted cases, but once optimization is enabled the following things appear
to happen.

(1) - gcc concludes the asm always overwrites somevar, and optimizes the
initialization away leaving it uninitialized.
(2) it doesn't now warn that the code is using an uninitialized variable
but silently compiles it as if all is well.
(3) at runtime if (somevar) has random contents, so runs the //use feature
case when it shouldnt.

Now, I can work around this by using "+" (even though I don't read the
variable), or initializing the value a second time in the asm block - but
should I have to? This sounds like the compiler is doing the wrong thing
based on the assertion/assumption "=" means I will 100% change some
variable, and worse after optimizing the code and creating a case where I
now have an uninitialized variable being used - does not warn me this is
happening?

Would it not be better to interpret "=" to mean "may" change the value,
which would prevent it incorrectly optimizing the initialization away, or
if it does warn that the value may be used uninitialized?

Yours,
Nick Andrews.

             reply	other threads:[~2023-03-28 11:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-28 11:15 Kalamatee [this message]
2023-03-28 11:41 ` Andrew Haley
2023-03-28 11:44   ` Andrew Haley
2023-03-28 22:45 ` Segher Boessenkool

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='CAJWNc-62ebOfM6E=z8H9h3vE=+qfss2kidaHpWH3usWLSO9iuA@mail.gmail.com' \
    --to=kalamatee@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /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).