473,749 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extent of the "as-if" rule

Hi all,
In a discussion with Tak-Shing Chan the question came up whether the
as-if rule can cover I/O functions. Basically, he maintains it can, and
I think it doesn't.

Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile ","rb");
return 0;
}

/*** b.c ***/
in main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?

I'd welcome a reference from the standard.

Best regards,

Sidney

Nov 14 '05 #1
145 6320
Sidney Cadot wrote:
Hi all,
In a discussion with Tak-Shing Chan the question came up whether the
as-if rule can cover I/O functions. Basically, he maintains it can, and
I think it doesn't.

Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile ","rb");
return 0;
}

/*** b.c ***/
in main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?

I'd welcome a reference from the standard.

Best regards,

Sidney


I'll take a stab at this...

Since fopen() is a function (15.2), it may therefore have side effects.
Optimizing it away would therefore be an error.

Perhaps the OS it's running on starts up a pot of coffee if someone
opens the 'somefile' file for reading, and that's the intended effect.

/J
Nov 14 '05 #2
Greetings.

In article <bu*********@ne ws.tudelft.nl>, Sidney Cadot wrote:
Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile ","rb");
return 0;
}

/*** b.c ***/
in main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?

I'd welcome a reference from the standard.


I don't know what the standard says, but from an implementation point of
view it might make sense not to optimize the fopen() away. Many file
systems maintain a "last accessed" timestamp for files; therefore, the
seemingly useless fopen() does indeed modify the environment.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 14 '05 #3
In <40********@new s.wineasy.se> Johan Lindh <jo***@linkdata .getridofthis.s e> writes:
Sidney Cadot wrote:
Hi all,
In a discussion with Tak-Shing Chan the question came up whether the
as-if rule can cover I/O functions. Basically, he maintains it can, and
I think it doesn't.
The as-if rule basically says that any optimisation/pessimisation is
allowed, as long as the *specified* output of the program is not affected.
Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile ","rb");
return 0;
}

/*** b.c ***/
int main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?
Yes, because both programs generate the same output, according to the
description of the abstract machine. Opening a file in read mode and
closing it (if the opening succeeded) does not generate any output.
I'd welcome a reference from the standard.


I'll take a stab at this...

Since fopen() is a function (15.2), it may therefore have side effects.
Optimizing it away would therefore be an error.

Perhaps the OS it's running on starts up a pot of coffee if someone
opens the 'somefile' file for reading, and that's the intended effect.


The only *relevant* side effects of fopen are those specified in the C
standard.

Here is the standard reference:

5.1.2.3 Program execution

1 The semantic descriptions in this International Standard describe
the behavior of an abstract machine in which issues of optimization
are irrelevant.

2 Accessing a volatile object, modifying an object, modifying a
file, or calling a function that does any of those operations
are all side effects,11) which are changes in the state of the
execution environment. Evaluation of an expression may produce
side effects. At certain specified points in the execution
sequence called sequence points, all side effects of previous
evaluations shall be complete and no side effects of subsequent
evaluations shall have taken place. (A summary of the sequence
points is given in annex C.)

3 In the abstract machine, all expressions are evaluated as
specified by the semantics. An actual implementation need not
evaluate part of an expression if it can deduce that its value is
not used and that no needed side effects are produced (including
any caused by calling a function or accessing a volatile object).

Opening a file in read mode doesn't modify the file, therefore it doesn't
count as a side effect, in the context of the C standard.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4

Cross-posted to comp.std.c from a discussion in comp.lang.c; feedback
welcomed.
Dan Pop wrote:
In <40********@new s.wineasy.se> Johan Lindh <jo***@linkdata .getridofthis.s e> writes:

Sidney Cadot wrote:

Hi all,
In a discussion with Tak-Shing Chan the question came up whether the
as-if rule can cover I/O functions. Basically, he maintains it can, and
I think it doesn't.

The as-if rule basically says that any optimisation/pessimisation is
allowed, as long as the *specified* output of the program is not affected.

Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile ","rb");
return 0;
}

/*** b.c ***/
int main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?

Yes, because both programs generate the same output, according to the
description of the abstract machine. Opening a file in read mode and
closing it (if the opening succeeded) does not generate any output.

I'd welcome a reference from the standard.


I'll take a stab at this...

Since fopen() is a function (15.2), it may therefore have side effects.
Optimizing it away would therefore be an error.

Perhaps the OS it's running on starts up a pot of coffee if someone
opens the 'somefile' file for reading, and that's the intended effect.

The only *relevant* side effects of fopen are those specified in the C
standard.

Here is the standard reference:

5.1.2.3 Program execution

1 The semantic descriptions in this International Standard describe
the behavior of an abstract machine in which issues of optimization
are irrelevant.

2 Accessing a volatile object, modifying an object, modifying a
file, or calling a function that does any of those operations
are all side effects,11) which are changes in the state of the
execution environment. Evaluation of an expression may produce
side effects. At certain specified points in the execution
sequence called sequence points, all side effects of previous
evaluations shall be complete and no side effects of subsequent
evaluations shall have taken place. (A summary of the sequence
points is given in annex C.)

3 In the abstract machine, all expressions are evaluated as
specified by the semantics. An actual implementation need not
evaluate part of an expression if it can deduce that its value is
not used and that no needed side effects are produced (including
any caused by calling a function or accessing a volatile object).

Opening a file in read mode doesn't modify the file, therefore it doesn't
count as a side effect, in the context of the C standard.


.... But that just dismisses one of the possible 'side effects' admitted
by the standard.

Does a fopen(name, "rb") count as 'calling a function that does any of
those operations' ? I think it does; it /has/, at some point, to
interact with the abstract machine's environment, which can only be done
via volatile objects or modifying an object, eventually, somewhere
down the line.

Anyway, we can pick on words (and that's valuable) but if anything, this
shows that the enumeration of 'side effects' as given in the standard
is not exhaustive. Clearly, opening a file (even for reading) interacts
with the outside world, e.g. on unix systems it updates the 'last
accessed' date as pointed out. This is not unambiguously covered by the
standard; if upon literal reading we were to conclude that it isn't
properly covered - well, then the standard needs to be mended on the
next occasion.

Best regards,

Sidney
Nov 14 '05 #5
On 19 Jan 2004 16:13:33 GMT, Da*****@cern.ch (Dan Pop) wrote in
comp.lang.c:
In <40********@new s.wineasy.se> Johan Lindh <jo***@linkdata .getridofthis.s e> writes:
Sidney Cadot wrote:
Hi all,
In a discussion with Tak-Shing Chan the question came up whether the
as-if rule can cover I/O functions. Basically, he maintains it can, and
I think it doesn't.
The as-if rule basically says that any optimisation/pessimisation is
allowed, as long as the *specified* output of the program is not affected.
Consider two programs:

/*** a.c ***/
#include <stdio.h>
int main(void)
{
fopen("somefile ","rb");
return 0;
}

/*** b.c ***/
int main(void)
{
return 0;
}

Would it be legal for a compiler (through optimization), to emit the
same code for program a.c and b.c ?
Yes, because both programs generate the same output, according to the
description of the abstract machine. Opening a file in read mode and
closing it (if the opening succeeded) does not generate any output.
I'd welcome a reference from the standard.


I'll take a stab at this...

Since fopen() is a function (15.2), it may therefore have side effects.
Optimizing it away would therefore be an error.

Perhaps the OS it's running on starts up a pot of coffee if someone
opens the 'somefile' file for reading, and that's the intended effect.


The only *relevant* side effects of fopen are those specified in the C
standard.

Here is the standard reference:

5.1.2.3 Program execution

1 The semantic descriptions in this International Standard describe
the behavior of an abstract machine in which issues of optimization
are irrelevant.

2 Accessing a volatile object, modifying an object, modifying a
file, or calling a function that does any of those operations
are all side effects,11) which are changes in the state of the
execution environment. Evaluation of an expression may produce
side effects. At certain specified points in the execution
sequence called sequence points, all side effects of previous
evaluations shall be complete and no side effects of subsequent
evaluations shall have taken place. (A summary of the sequence
points is given in annex C.)

3 In the abstract machine, all expressions are evaluated as
specified by the semantics. An actual implementation need not
evaluate part of an expression if it can deduce that its value is
not used and that no needed side effects are produced (including
any caused by calling a function or accessing a volatile object).

Opening a file in read mode doesn't modify the file, therefore it doesn't
count as a side effect, in the context of the C standard.


Given your reasoning, and I see nothing to argue with, optimizing away
the fopen() is a perfectly acceptable application of the as-if rule on
the typical *NIX system, where opening a file leaves no trace in the
system.

It would not be acceptable on a Win32 system, and quite possible other
systems. Win32 keeps multiple time values for files in the file
system, one of which is the last accessed time. An open of an
existing file followed by a close without reading or writing will
modify the file system's last access time, which does modify the file
even though it does not change the contents.

[also cross-posted to comp.std.c]

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #6
Jack Klein <ja*******@spam cop.net> writes:
Given your reasoning, and I see nothing to argue with, optimizing away
the fopen() is a perfectly acceptable application of the as-if rule on
the typical *NIX system, where opening a file leaves no trace in the
system.


Most Unix-like kernel, including Linux, also maintain "last
accessed" times. I don't know why you think they don't.
--
int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 14 '05 #7
On Mon, 19 Jan 2004 20:20:42 -0800, Ben Pfaff <bl*@cs.stanfor d.edu>
wrote in comp.std.c:
Jack Klein <ja*******@spam cop.net> writes:
Given your reasoning, and I see nothing to argue with, optimizing away
the fopen() is a perfectly acceptable application of the as-if rule on
the typical *NIX system, where opening a file leaves no trace in the
system.


Most Unix-like kernel, including Linux, also maintain "last
accessed" times. I don't know why you think they don't.


Doh! I probably knew that, although I haven't had anything running
Linux in quite a while. It's time of creation that they don't keep
separately, right?

In any case the real question is:

Given:

-Compilers that generate executables for platforms A and B

-Platform A does not change the externally visible state of its file
system in any way when an existing file is opened and closed with no
actual access in between.

-Platform B does change the externally visible state of its file
system in some way (e.g., updating a last accessed time stamp field
associated with the file) under the same circumstances.

-The following program:

#include <stdio.h>
int main(void)
{
fopen("somefile ","rb");
return 0;
}

....where the parameters passed to fopen() are such that the call
succeeds.

Then does it follow:

-A compiler for platform A may omit the fopen() call under the as-if
rule

-A compiler for platform B may not omit the fopen() call

....even though the output of the program itself is the same in either
case.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #8
Jack Klein wrote:
Given your reasoning, and I see nothing to argue with, optimizing away
the fopen() is a perfectly acceptable application of the as-if rule on
the typical *NIX system, where opening a file leaves no trace in the
system.


But that is *not* the way that Unix (POSIX) works!
Opening a file *does* have side effects in the environment.

Nov 14 '05 #9
Jack Klein wrote:
Then does it follow:
-A compiler for platform A may omit the fopen() call under the as-if
rule
-A compiler for platform B may not omit the fopen() call
...even though the output of the program itself is the same in either
case.


The question is whether there is any way, other than timing,
code size, and other aspects deemed "inessentia l", to detect
whether the code actually performs the call. In a way, this
is all a waste of time, because no compiler that I know of
would optimize away a call to fopen(). Some *would* optimize
away pointless calls to strcmp() etc., in contexts where they
know that a standard library function is involved (so that
its complete semantics are known) and known not to have any
observable effect in the particular case.

Is there some *real* issue lying behind this topic, or is it
just a matter of pedanticism?

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
2864
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville Vainio http://www.students.tut.fi/~vainio24
68
4358
by: Marco Bubke | last post by:
Hi I have read some mail on the dev mailing list about PEP 318 and find the new Syntax really ugly. def foo(x, y): pass I call this foo(1, 2), this isn't really intuitive to me! Also I don't like the brackets.
3
1426
by: Jase | last post by:
For some reason all of a sudden "£" signs are displaying as "?" when the field is displayed. The display code is: <%=rs.Fields("WagesText")%> This was working fine before, so I have no idea why it has suddenly changed. The code hasn't altered as far as I'm aware but it is a possibility. The database is SQL based.
27
5087
by: Curious Angel | last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to force the file to be saved, and in any event _not_ opened. Since the PDF will be in his cache in any event, I would just as soon place the employer in control of what directory he wishes to save it in, and there are two salient reasons for this: 1. I want him to OWN the document 2. I want him to FIND the document, quickly, on his hard drive In any...
0
364
by: Daniel | last post by:
how to make sure a xsl document has valid xsl syntax? i tried loading it into an xml document but that doesnt show syntax errors inside attributes such as "foo/bar" vs "bar\foo"
2
12172
by: Petr Jakes | last post by:
Hi, I am trying to set-up communication to the coin change-giver from my Linux box using the Python code. The change giver uses MDB (Multi Drop Bus) serial protocol to communicate with the master. MDB protocol is the 9bit serial protocol: (TX, RX lines only) 9600bps, 9bits, No Parity, 1 Start, 1 Stop. I would like to control the UART "parity bit" to try to simulate 9bit communication.
72
4218
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and c?
5
1799
by: ZSP747 | last post by:
Now,I prepare to write a English-Chinese dictionary. So I want find a function or library can convert the words from one form to another,such as "boys"->"boy","became->become","went"->"go". Because there is so many unregular convertions in English then I think it will be a hard work if do it by myself. Thanks.
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.