Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #52554

Re: Average swaps per word?

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: Average swaps per word?
Date 2016-11-12 12:23 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2016Nov12.132348@mips.complang.tuwien.ac.at> (permalink)
References (1 earlier) <20161109033429.75b93326@_> <d811e3fa-6243-48e6-a5c0-b989daca5127@googlegroups.com> <nvvk2q$nch$3@dont-email.me> <58236c98.176487035@news.eternal-september.org> <81df7e62-3f3e-4571-821c-218f9abee4ed@googlegroups.com>

Show all headers | View raw


Harold <hzrabbie@gmail.com> writes:
>What kind of optimization is a compiler allowed to perform?  Must the compi=
>ler generate a load or store for every occurrence of @ or ! ?

If it changes the user-visible behaviour of the program, yes.

>For example, an embedded system might include the following word to pulse a=
> memory-mapped device register
>
>: pulse-register  ( reg-addr -- )
>  DUP 1 SWAP ! 0 SWAP !
>;
>
>Is an optimizing compiler allowed to replace this with
>
> 0 SWAP !
>
>since the end result is the same?

Obviously, the user-visble behaviour changes in this case, so if the
program runs in an environment where you can store to such devices,
the compiler must not optimize any of these stores away unless it can
prove that the stores are not to such a device.  On the practical
side, it is pretty difficult to optimize stores away in useful code
anyway (even if you know that you are not storing to such a device),
so I don't expect the compiler to do it, and I would not go for it as
a compiler writer.

A more problematic issue is fetches from I/O devices which may have a
side effect; e.g., a serial chip where you can get the next byte of
the data with each c@.  If you want to ignore a byte of input, you
might write

... input-port c@ drop ...

Optimizing the whole sequence away is a pretty natural thing for an
analytic compiler to do, but of course it would be wrong in this case.
AFAIK VFX optimizes such a @ DROP sequence (but, trying it out now,
not C@ DROP) away in the PC version, but leaves it alone in the
embedded version.

But even if the PC version left it alone, too, that would slow down
real-world code by exactly 0%, because you don't write @ DROP unless
you want the side effect.

A bigger problem is

  ... @ ... @ ...

You may want to reorder the fetches (if your compiler does instruction
scheduling) if the result of the second fetch is needed more urgently
than the result of the first.  Or

begin
  ... A @ ...
repeat

Here the compiler may want to pull the A @ out of the loop (and the
alternative of doing it manually is particularly unattractive because
it complicates the stack), but of course that's only allowed in
certain environments.

At the moment the mainstream Forth compilers are not sophisticated
enough to try such things, though.

>Other languages have the concept of a volatile variable type qualifier to a=
>llow the programmer to prevent this sort of optimization.  Are all non-stac=
>k memory accesses in Forth considered to be volatile?  I couldn't find any =
>mention of this topic in the 2012 standard.

It's not specified in the standard.  It depends on the environment in
which the program is run.  If all the program can do is access RAM
that is not shared with other processes or threads, the compiler can
perform more optimizations than otherwise.

One could add @/! variants for non-shared RAM, shared RAM,
write-combining, write-through, and non-cached I/O areas (you may not
need all of these variants) to allow some of these optimizations in
some cases, but that would lead to an explosion of word names, and
probably buys little, so I lean towards just leaving these
optimizations to the programmer.

E.g., for the loop case above the programmer can use a local to avoid
the stack complication:

a @ {: a@ :} begin
  ... a@ ...
repeat

Another alternative would be to turn A into a UVALUE (thread-local
VALUE), but that assumes the presence of UVALUEs in the system, and
that the compiler is sophisticated enough to move loop-invariant code
out of loops.

>A similar issue arises in multi-threaded environments with shared-memory lo=
>cks.

Well, that's not standardized yet, although it is on the agenda.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2016: http://www.euroforth.org/ef16/

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Average swaps per word? trans <transfire@gmail.com> - 2016-11-08 22:03 -0800
  Re: Average swaps per word? "Elizabeth D. Rather" <erather@forth.com> - 2016-11-08 20:20 -1000
  Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-09 08:16 +0000
  Re: Average swaps per word? Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2016-11-09 03:34 -0500
    Re: Average swaps per word? trans <transfire@gmail.com> - 2016-11-09 02:53 -0800
      Re: Average swaps per word? rickman <gnuarm@gmail.com> - 2016-11-09 11:50 -0500
        Re: Average swaps per word? stephen@mpeforth.com (Stephen Pelc) - 2016-11-09 18:37 +0000
          Re: Average swaps per word? Harold <hzrabbie@gmail.com> - 2016-11-10 16:28 -0800
            Re: Average swaps per word? stephen@mpeforth.com (Stephen Pelc) - 2016-11-11 17:51 +0000
            Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-12 12:23 +0000
              Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-12 16:09 +0100
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-13 14:55 +0000
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-13 20:34 +0100
                Re: Average swaps per word? Paul Rubin <no.email@nospam.invalid> - 2016-11-13 11:43 -0800
                Re: Average swaps per word? hughaguilar96@gmail.com - 2016-11-13 16:58 -0800
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-14 16:41 +0100
                Re: Average swaps per word? hughaguilar96@gmail.com - 2016-11-14 16:38 -0800
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-14 17:42 +0000
                Re: Average swaps per word? Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2016-11-14 16:39 -0500
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-17 12:40 +0000
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-17 14:10 +0100
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-18 15:14 +0000
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-20 18:16 +0100
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-22 10:29 +0000
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-22 15:54 +0100
                Re: Average swaps per word? James Kuyper <jameskuyper@verizon.net> - 2016-11-22 11:50 -0500
                Re: Average swaps per word? raltbos@xs4all.nl (Richard Bos) - 2016-11-24 17:27 +0000
                Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-15 03:55 -0600
                Re: Average swaps per word? Rod Pemberton <NeedNotReplyHere@xrsevnneqk.cem> - 2016-11-15 17:49 -0500
                Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-16 02:59 -0600
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-17 13:07 +0000
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-16 22:57 +0000
                Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-17 03:12 -0600
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-17 12:32 +0100
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-18 15:02 +0000
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-17 13:23 +0000
                Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-17 08:54 -0600
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-18 15:54 +0000
                Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-18 12:37 -0600
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-14 17:32 +0000
                Re: Average swaps per word? Mark Wills <markwills1970@gmail.com> - 2016-11-15 02:09 -0800
                Re: Average swaps per word? jim <anon@example.com> - 2016-11-15 10:44 +0000
                Re: Average swaps per word? Mark Wills <markwills1970@gmail.com> - 2016-11-15 03:12 -0800
                Re: Average swaps per word? Alex <alex@rivadpm.com> - 2016-11-15 13:01 +0000
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-17 12:51 +0000
                Re: Average swaps per word? Alex <alex@rivadpm.com> - 2016-11-17 14:02 +0000
                Re: Average swaps per word? Alex <alex@rivadpm.com> - 2016-11-17 14:06 +0000
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-18 15:26 +0000
                Re: Average swaps per word? Alex <alex@rivadpm.com> - 2016-11-18 18:33 +0000
                Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-19 03:32 -0600
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-17 14:29 +0100
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-15 13:51 +0100
                Re: Average swaps per word? jim <anon@example.com> - 2016-11-15 13:01 +0000
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-17 12:46 +0000
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-17 14:16 +0100
              Re: Average swaps per word? Harold <hzrabbie@gmail.com> - 2016-11-13 11:08 -0800
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-14 17:08 +0000
                Re: Average swaps per word? Mark Wills <markwills1970@gmail.com> - 2016-11-15 02:04 -0800
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-15 14:04 +0100
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-15 14:05 +0100
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-17 12:58 +0000
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-17 15:16 +0100
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-18 15:41 +0000
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-20 18:12 +0100
  Re: Average swaps per word? minforth@arcor.de - 2016-11-09 03:15 -0800
    Re: Average swaps per word? trans <transfire@gmail.com> - 2016-11-09 11:22 -0800
    Re: Average swaps per word? "HAA" <someone@microsoft.com> - 2016-11-10 12:20 +1100
      Re: Average swaps per word? "Elizabeth D. Rather" <erather@forth.com> - 2016-11-09 21:04 -1000
      Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-10 03:45 -0600
        Re: Average swaps per word? rickman <gnuarm@gmail.com> - 2016-11-10 11:52 -0500
          Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-10 11:42 -0600
            Re: Average swaps per word? rickman <gnuarm@gmail.com> - 2016-11-10 13:34 -0500
              Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-10 12:54 -0600
                Re: Average swaps per word? rickman <gnuarm@gmail.com> - 2016-11-10 16:33 -0500
                Re: Average swaps per word? "Elizabeth D. Rather" <erather@forth.com> - 2016-11-10 15:56 -1000
                Re: Average swaps per word? rickman <gnuarm@gmail.com> - 2016-11-10 21:30 -0500
                Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-11 03:04 -0600
                Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-12 13:25 +0000
                Re: Average swaps per word? Andrew Haley <andrew29@littlepinkcloud.invalid> - 2016-11-14 03:34 -0600
                Re: Average swaps per word? Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2016-11-11 10:41 +0000
                Re: Average swaps per word? hughaguilar96@gmail.com - 2016-11-11 16:45 -0800
      Re: Average swaps per word? jim <anon@example.com> - 2016-11-10 11:17 +0000
        Re: Average swaps per word? Raimond Dragomir <raimond.dragomir@gmail.com> - 2016-11-10 04:34 -0800
          Re: Average swaps per word? jim <anon@example.com> - 2016-11-10 12:44 +0000
            Re: Average swaps per word? Raimond Dragomir <raimond.dragomir@gmail.com> - 2016-11-10 07:27 -0800
              Re: Average swaps per word? Ron Aaron <rambamist@gmail.com> - 2016-11-10 18:26 +0200
              Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-10 16:05 +0000
        Re: Average swaps per word? stephen@mpeforth.com (Stephen Pelc) - 2016-11-10 14:55 +0000
          Re: Average swaps per word? "HAA" <someone@microsoft.com> - 2016-11-12 11:36 +1100
            Re: Average swaps per word? hughaguilar96@gmail.com - 2016-11-11 16:56 -0800
            Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-12 16:14 +0100
          Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-12 12:14 +0000
            Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-12 16:16 +0100
              Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-13 14:51 +0000
                Re: Average swaps per word? albert@cherry.spenarnc.xs4all.nl (Albert van der Horst) - 2016-11-13 20:41 +0100
      Re: Average swaps per word? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-11-10 14:42 +0000
  Re: Average swaps per word? hughaguilar96@gmail.com - 2016-11-11 15:54 -0800

csiph-web