tools/memory-model: Clarify syntactic and semantic dependencies
PATCH 1/1 tools/memory-model: Clarify syntactic and semantic dependencies
Dependencies which are purely syntactic, i.e. not semantic, might imply ordering at first glance. However, since they do not affect defined behavior, compilers are within their rights to remove such dependencies when optimizing code.
Since syntactic dependencies are not related to any kind of dependency in particular, explicitly distinguish syntactic and semantic dependencies as part of the 'A WARNING' section in explanation.txt, which gives examples of how compilers might affect the LKMM's dependency orderings in general.
--- .../Documentation/explanation.txt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+)
tools/memory-model/Documentation/explanation.txt +25 -0
@@ -411,6 +411,31 @@ Given this version of the code, the LKMM would predict that the load from x could be executed after the store to y. Thus, the memory model's original prediction could be invalidated by the compiler. +Caution is also advised when dependencies are purely syntactic, i.e. +not semantic. A dependency between two marked accesses is purely +syntactic iff the defined behavior of the second access is unaffected +by its dependency. + +Compilers are aware of syntactic dependencies and are within their +rights to remove them as part of optimizations, thereby breaking any +guarantees of ordering. + +Notable cases are dependencies eliminated through constant propagation +or those where only one value leads to defined behavior as in the +following example: + + int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +The formal LKMM is unaware of syntactic dependencies and therefore +predicts ordering. However, since any other value than 0 for r1 would +result in an out-of-bounds access, which is undefined behavior, r2 is +not affected by its dependency to r1, making the above a purely +syntactic dependency. + Another issue arises from the fact that in C, arguments to many operators and function calls can be evaluated in any order. For example:
Signature · 1 line
2.33.1
Alan Stern
maintainerQuoted 10 lines · Paul Heidekrüger
Dependencies which are purely syntactic, i.e. not semantic, might imply ordering at first glance. However, since they do not affect defined behavior, compilers are within their rights to remove such dependencies when optimizing code.
Since syntactic dependencies are not related to any kind of dependency in particular, explicitly distinguish syntactic and semantic dependencies as part of the 'A WARNING' section in explanation.txt, which gives examples of how compilers might affect the LKMM's dependency orderings in general.
The "A WARNING" section is a bad place to put this material, because it comes before dependencies have been introduced. It would be better to put this at the end of the "DEPENDENCY RELATIONS: data, addr, and ctrl" section.
Quoted 14 lines
Signed-off-by Paul Heidekrüger <[email protected]>Cc Marco Elver <[email protected]>Cc Charalampos Mainas <[email protected]>Cc Pramod Bhatotia <[email protected]>--- .../Documentation/explanation.txt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+)tools/memory-model/Documentation/explanation.txt +0
-0
tools/memory-model/Documentation/explanation.txt Given this version of the code, the LKMM would predict that the load · line 417
Quoted 7 lines
tools/memory-model/Documentation/explanation.txt +4
-0@@ -411,6 +411,31 @@ Given this version of the code, the LKMM would predict that the load from x could be executed after the store to y. Thus, the memory model's original prediction could be invalidated by the compiler. +Caution is also advised when dependencies are purely syntactic, i.e. +not semantic. A dependency between two marked accesses is purely +syntactic iff the defined behavior of the second access is unaffected +by its dependency.
That's a very abstract way of describing the situation; it doesn't do a good job of getting the real idea across. It also mixes up two separate
ideas: behaviors being unaffected by a syntactic dependency and behaviors being undefined. They should be described separately. I would prefer something along these lines...
----------------------------------------
Here's a trick question: When is a dependency not a dependency? Answer:
When it is purely syntactic rather than semantic. We say a dependency between two accesses is purely syntactic if the second access doesn't actually depend on the result of the first. Here is a trivial example:
r1 = READ_ONCE(x); WRITE_ONCE(y, r1 * 0);
There appears to be a data dependency from the load of x to the store of y, since the value to be stored is computed from the value that was loaded. But in fact, the value stored does not really depend on anything since it will always be 0. Thus the data dependency is only syntactic (it appears to exist in the code) but not semantic (the second access will always be the same, regardless of the value of the first access). Given code like this, a compiler could simply eliminate the load from x, which would certainly destroy any dependency.
(It's natural to object that no one in their right mind would write code like the above. However, macro expansions can easily give rise to this sort of thing, in ways that generally are not apparent to the programmer.)
Another mechanism that can give rise to purely syntactic dependencies is related to the notion of "undefined behavior". Certain program behaviors are called "undefined" in the C language specification, which means that when they occur there are no guarantees at all about the outcome. Consider the following example:
int a[1]; int i; r1 = READ_ONCE(i); r2 = READ_ONCE(a[r1]);
Access beyond the end or before the beginning of an array is one kind of undefined behavior. Therefore the compiler doesn't have to worry about what will happen if r1 is nonzero, and it can assume that r1 will always be zero without actually loading anything from i. (If the assumption turns out to be wrong, the resulting behavior will be undefined anyway so the compiler doesn't care!) Thus the load from i can be eliminated, breaking the address dependency.
The LKMM is unaware that purely syntactic dependencies are different from semantic dependencies and therefore mistakenly predicts that the accesses in the two examples above will be ordered. This is another example of how the compiler can undermine the memory model. Be warned.
----------------------------------------
Alan
Quoted 26 lines
tools/memory-model/Documentation/explanation.txt +20
-0+Compilers are aware of syntactic dependencies and are within their +rights to remove them as part of optimizations, thereby breaking any +guarantees of ordering. + +Notable cases are dependencies eliminated through constant propagation +or those where only one value leads to defined behavior as in the +following example: + + int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +The formal LKMM is unaware of syntactic dependencies and therefore +predicts ordering. However, since any other value than 0 for r1 would +result in an out-of-bounds access, which is undefined behavior, r2 is +not affected by its dependency to r1, making the above a purely +syntactic dependency. + Another issue arises from the fact that in C, arguments to many operators and function calls can be evaluated in any order. For example:Signature · 1 line
2.33.1
Quoted 34 lines · Alan Stern
Quoted 10 lines · Paul Heidekrüger
Dependencies which are purely syntactic, i.e. not semantic, might imply ordering at first glance. However, since they do not affect defined behavior, compilers are within their rights to remove such dependencies when optimizing code.
Since syntactic dependencies are not related to any kind of dependency in particular, explicitly distinguish syntactic and semantic dependencies as part of the 'A WARNING' section in explanation.txt, which gives examples of how compilers might affect the LKMM's dependency orderings in general.
The "A WARNING" section is a bad place to put this material, because it comes before dependencies have been introduced. It would be better to put this at the end of the "DEPENDENCY RELATIONS: data, addr, and ctrl" section.
Quoted 17 lines
Signed-off-by Paul Heidekrüger <[email protected]>Cc Marco Elver <[email protected]>Cc Charalampos Mainas <[email protected]>Cc Pramod Bhatotia <[email protected]>--- .../Documentation/explanation.txt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+)tools/memory-model/Documentation/explanation.txt +0
-0@@ -411,6 +411,31 @@ Given this version of the code, the LKMM would predict that the load from x could be executed after the store to y. Thus, the memory model's original prediction could be invalidated by the compiler.
Quoted 9 lines
tools/memory-model/Documentation/explanation.txt Given this version of the code, the LKMM would predict that the load · line 417
Quoted 4 lines
tools/memory-model/Documentation/explanation.txt +4
-0+Caution is also advised when dependencies are purely syntactic, i.e. +not semantic. A dependency between two marked accesses is purely +syntactic iff the defined behavior of the second access is unaffected +by its dependency.That's a very abstract way of describing the situation; it doesn't do a good job of getting the real idea across. It also mixes up two separate
ideas: behaviors being unaffected by a syntactic dependency and behaviors being undefined. They should be described separately.
Many thanks for the feedback! I agree, the explanation works a lot better once readers have been introduced to data, addr and ctrl relations.
Quoted 1 line
I would prefer something along these lines...
Shall I resubmit the patch with you as co-developer, or, given that it's arguably your work now, would you like to submit the patch yourself?
Many thanks,
Paul
Quoted 79 lines
----------------------------------------
Here's a trick question: When is a dependency not a dependency? Answer:
When it is purely syntactic rather than semantic. We say a dependency between two accesses is purely syntactic if the second access doesn't actually depend on the result of the first. Here is a trivial example:r1 = READ_ONCE(x); WRITE_ONCE(y, r1 * 0);There appears to be a data dependency from the load of x to the store of y, since the value to be stored is computed from the value that was loaded. But in fact, the value stored does not really depend on anything since it will always be 0. Thus the data dependency is only syntactic (it appears to exist in the code) but not semantic (the second access will always be the same, regardless of the value of the first access). Given code like this, a compiler could simply eliminate the load from x, which would certainly destroy any dependency.
(It's natural to object that no one in their right mind would write code like the above. However, macro expansions can easily give rise to this sort of thing, in ways that generally are not apparent to the programmer.)
Another mechanism that can give rise to purely syntactic dependencies is related to the notion of "undefined behavior". Certain program behaviors are called "undefined" in the C language specification, which means that when they occur there are no guarantees at all about the outcome. Consider the following example:
int a[1]; int i; r1 = READ_ONCE(i); r2 = READ_ONCE(a[r1]);Access beyond the end or before the beginning of an array is one kind of undefined behavior. Therefore the compiler doesn't have to worry about what will happen if r1 is nonzero, and it can assume that r1 will always be zero without actually loading anything from i. (If the assumption turns out to be wrong, the resulting behavior will be undefined anyway so the compiler doesn't care!) Thus the load from i can be eliminated, breaking the address dependency.
The LKMM is unaware that purely syntactic dependencies are different from semantic dependencies and therefore mistakenly predicts that the accesses in the two examples above will be ordered. This is another example of how the compiler can undermine the memory model. Be warned.
----------------------------------------
Alan
Quoted 26 lines
tools/memory-model/Documentation/explanation.txt +20
-0+Compilers are aware of syntactic dependencies and are within their +rights to remove them as part of optimizations, thereby breaking any +guarantees of ordering. + +Notable cases are dependencies eliminated through constant propagation +or those where only one value leads to defined behavior as in the +following example: + + int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +The formal LKMM is unaware of syntactic dependencies and therefore +predicts ordering. However, since any other value than 0 for r1 would +result in an out-of-bounds access, which is undefined behavior, r2 is +not affected by its dependency to r1, making the above a purely +syntactic dependency. + Another issue arises from the fact that in C, arguments to many operators and function calls can be evaluated in any order. For example:Signature · 1 line
2.33.1
Alan Stern
maintainerQuoted 6 lines · Paul Heidekrüger
Quoted 4 lines · Alan Stern
That's a very abstract way of describing the situation; it doesn't do a good job of getting the real idea across. It also mixes up two separate
ideas: behaviors being unaffected by a syntactic dependency and behaviors being undefined. They should be described separately.
Quoted 8 lines
Many thanks for the feedback! I agree, the explanation works a lot better once readers have been introduced to data, addr and ctrl relations.
Quoted 1 line
I would prefer something along these lines...
Shall I resubmit the patch with you as co-developer, or, given that it's arguably your work now, would you like to submit the patch yourself?
I'll submit it myself, with Suggested-by: you. How does that sound?
Alan
Quoted 11 lines · Alan Stern
Quoted 10 lines · Paul Heidekrüger
Quoted 4 lines · Alan Stern
That's a very abstract way of describing the situation; it doesn't do a good job of getting the real idea across. It also mixes up two separate
ideas: behaviors being unaffected by a syntactic dependency and behaviors being undefined. They should be described separately.Many thanks for the feedback! I agree, the explanation works a lot better once readers have been introduced to data, addr and ctrl relations.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
I would prefer something along these lines...
Shall I resubmit the patch with you as co-developer, or, given that it's arguably your work now, would you like to submit the patch yourself?
I'll submit it myself, with Suggested-by: you. How does that sound?
Sounds good!
Many thanks,
Paul
Alan Stern
maintainerPATCH 1/1 tools/memory-model: Explain syntactic and semantic dependencies
Paul Heidekrüger pointed out that the Linux Kernel Memory Model documentation doesn't mention the distinction between syntactic and semantic dependencies. This is an important difference, because the compiler can easily break dependencies that are only syntactic, not semantic.
This patch adds a few paragraphs to the LKMM documentation explaining these issues and illustrating how they can matter.
---
[as1970]
tools/memory-model/Documentation/explanation.txt | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+)
tools/memory-model/Documentation/explanation.txt +47 -0
@@ -485,6 +485,53 @@ have R ->po X. It wouldn't make sense f somehow on a value that doesn't get loaded from shared memory until later in the code! +Here's a trick question: When is a dependency not a dependency? Answer: +When it is purely syntactic rather than semantic. We say a dependency +between two accesses is purely syntactic if the second access doesn't +actually depend on the result of the first. Here is a trivial example: + + r1 = READ_ONCE(x); + WRITE_ONCE(y, r1 * 0); + +There appears to be a data dependency from the load of x to the store of +y, since the value to be stored is computed from the value that was +loaded. But in fact, the value stored does not really depend on +anything since it will always be 0. Thus the data dependency is only +syntactic (it appears to exist in the code) but not semantic (the second +access will always be the same, regardless of the value of the first +access). Given code like this, a compiler could simply eliminate the +load from x, which would certainly destroy any dependency. + +(It's natural to object that no one in their right mind would write code +like the above. However, macro expansions can easily give rise to this +sort of thing, in ways that generally are not apparent to the +programmer.) + +Another mechanism that can give rise to purely syntactic dependencies is +related to the notion of "undefined behavior". Certain program behaviors +are called "undefined" in the C language specification, which means that +when they occur there are no guarantees at all about the outcome. +Consider the following example: + + int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +Access beyond the end or before the beginning of an array is one kind of +undefined behavior. Therefore the compiler doesn't have to worry about +what will happen if r1 is nonzero, and it can assume that r1 will always +be zero without actually loading anything from i. (If the assumption +turns out to be wrong, the resulting behavior will be undefined anyway +so the compiler doesn't care!) Thus the load from i can be eliminated, +breaking the address dependency. + +The LKMM is unaware that purely syntactic dependencies are different +from semantic dependencies and therefore mistakenly predicts that the +accesses in the two examples above will be ordered. This is another +example of how the compiler can undermine the memory model. Be warned. + THE READS-FROM RELATION: rf, rfi, and rfe -----------------------------------------
Re: [PATCH] tools/memory-model: Explain syntactic and semantic dependencies
Hi,
Quoted 69 lines · Alan Stern
Paul Heidekrüger pointed out that the Linux Kernel Memory Model documentation doesn't mention the distinction between syntactic and semantic dependencies. This is an important difference, because the compiler can easily break dependencies that are only syntactic, not semantic.
This patch adds a few paragraphs to the LKMM documentation explaining these issues and illustrating how they can matter.
Suggested-by Paul Heidekrüger <[email protected]>Signed-off-by Alan Stern <[email protected]>---
[as1970]
tools/memory-model/Documentation/explanation.txt | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+)Index:
usb-devel/tools/memory-model/Documentation/explanation.txt
===================================================================tools/memory-model/Documentation/explanation.txt +40
-0@@ -485,6 +485,53 @@ have R ->po X. It wouldn't make sense f somehow on a value that doesn't get loaded from shared memory until later in the code! +Here's a trick question: When is a dependency not a dependency? Answer: +When it is purely syntactic rather than semantic. We say a dependency +between two accesses is purely syntactic if the second access doesn't +actually depend on the result of the first. Here is a trivial example: + + r1 = READ_ONCE(x); + WRITE_ONCE(y, r1 * 0); + +There appears to be a data dependency from the load of x to the store of +y, since the value to be stored is computed from the value that was +loaded. But in fact, the value stored does not really depend on +anything since it will always be 0. Thus the data dependency is only +syntactic (it appears to exist in the code) but not semantic (the second +access will always be the same, regardless of the value of the first +access). Given code like this, a compiler could simply eliminate the +load from x, which would certainly destroy any dependency. + +(It's natural to object that no one in their right mind would write code +like the above. However, macro expansions can easily give rise to this +sort of thing, in ways that generally are not apparent to the +programmer.) + +Another mechanism that can give rise to purely syntactic dependencies is +related to the notion of "undefined behavior". Certain program behaviors +are called "undefined" in the C language specification, which means that +when they occur there are no guarantees at all about the outcome. +Consider the following example: + + int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +Access beyond the end or before the beginning of an array is one kind of +undefined behavior. Therefore the compiler doesn't have to worry about +what will happen if r1 is nonzero, and it can assume that r1 will always +be zero without actually loading anything from i. (If the assumption +turns out to be wrong, the resulting behavior will be undefined anyway +so the compiler doesn't care!) Thus the load from i can be eliminated,
tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f · line 537
Quoted 10 lines
tools/memory-model/Documentation/explanation.txt +7
-0+breaking the address dependency. + +The LKMM is unaware that purely syntactic dependencies are different +from semantic dependencies and therefore mistakenly predicts that the +accesses in the two examples above will be ordered. This is another +example of how the compiler can undermine the memory model. Be warned. + THE READS-FROM RELATION: rf, rfi, and rfe -----------------------------------------
FWIW,
Thanks, Akira
Paul E. McKenney
maintainerRe: [PATCH] tools/memory-model: Explain syntactic and semantic dependencies
In reply to Alan SternQuoted 35 lines · Alan Stern
Paul Heidekrüger pointed out that the Linux Kernel Memory Model documentation doesn't mention the distinction between syntactic and semantic dependencies. This is an important difference, because the compiler can easily break dependencies that are only syntactic, not semantic.
This patch adds a few paragraphs to the LKMM documentation explaining these issues and illustrating how they can matter.
Suggested-by Paul Heidekrüger <[email protected]>Signed-off-by Alan Stern <[email protected]>---
[as1970]
tools/memory-model/Documentation/explanation.txt | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+)Index:
usb-devel/tools/memory-model/Documentation/explanation.txt
===================================================================tools/memory-model/Documentation/explanation.txt +6
-0@@ -485,6 +485,53 @@ have R ->po X. It wouldn't make sense f somehow on a value that doesn't get loaded from shared memory until later in the code! +Here's a trick question: When is a dependency not a dependency? Answer: +When it is purely syntactic rather than semantic. We say a dependency +between two accesses is purely syntactic if the second access doesn't +actually depend on the result of the first. Here is a trivial example: + + r1 = READ_ONCE(x);
tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f · line 503
Quoted 10 lines
tools/memory-model/Documentation/explanation.txt +10
-0+ WRITE_ONCE(y, r1 * 0); + +There appears to be a data dependency from the load of x to the store of +y, since the value to be stored is computed from the value that was +loaded. But in fact, the value stored does not really depend on +anything since it will always be 0. Thus the data dependency is only +syntactic (it appears to exist in the code) but not semantic (the second +access will always be the same, regardless of the value of the first +access). Given code like this, a compiler could simply eliminate the +load from x, which would certainly destroy any dependency.
Are you OK with that last sentence reading as follows?
Given code like this, a compiler could simply discard the value return by the load from x, which would certainly destroy any dependency.
My concern with the original is that it might mislead people into thinking that compilers can omit volatile loads.
Quoted 12 lines
tools/memory-model/Documentation/explanation.txt +12
-0+ +(It's natural to object that no one in their right mind would write code +like the above. However, macro expansions can easily give rise to this +sort of thing, in ways that generally are not apparent to the +programmer.) + +Another mechanism that can give rise to purely syntactic dependencies is +related to the notion of "undefined behavior". Certain program behaviors +are called "undefined" in the C language specification, which means that +when they occur there are no guarantees at all about the outcome. +Consider the following example: +
tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f
Quoted 10 lines
tools/memory-model/Documentation/explanation.txt +10
-0+ int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +Access beyond the end or before the beginning of an array is one kind of +undefined behavior. Therefore the compiler doesn't have to worry about +what will happen if r1 is nonzero, and it can assume that r1 will always +be zero without actually loading anything from i.
And similarly here:
... and it can assume that r1 will always be zero regardless of the value actually loaded from i.
tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f · line 537
Quoted 13 lines
tools/memory-model/Documentation/explanation.txt +10
-0+ (If the assumption +turns out to be wrong, the resulting behavior will be undefined anyway +so the compiler doesn't care!) Thus the load from i can be eliminated, +breaking the address dependency. + +The LKMM is unaware that purely syntactic dependencies are different +from semantic dependencies and therefore mistakenly predicts that the +accesses in the two examples above will be ordered. This is another +example of how the compiler can undermine the memory model. Be warned. + THE READS-FROM RELATION: rf, rfi, and rfe -----------------------------------------
Looks great otherwise, and thank you all for your work on this!
Alan, would you like me to pull this in making those two changes and applying Akira's Reviewed-by, or would you prefer to send another version?
For that matter, am I off base in my suggested changes.
Thanx, Paul
Alan Stern
maintainerRe: [PATCH] tools/memory-model: Explain syntactic and semantic dependencies
Quoted 49 lines · Paul E. McKenney
Quoted 35 lines · Alan Stern
Paul Heidekrüger pointed out that the Linux Kernel Memory Model documentation doesn't mention the distinction between syntactic and semantic dependencies. This is an important difference, because the compiler can easily break dependencies that are only syntactic, not semantic.
This patch adds a few paragraphs to the LKMM documentation explaining these issues and illustrating how they can matter.
Suggested-by Paul Heidekrüger <[email protected]>Signed-off-by Alan Stern <[email protected]>---
[as1970]
tools/memory-model/Documentation/explanation.txt | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+)Index:
usb-devel/tools/memory-model/Documentation/explanation.txt
===================================================================tools/memory-model/Documentation/explanation.txt +6
-0@@ -485,6 +485,53 @@ have R ->po X. It wouldn't make sense f somehow on a value that doesn't get loaded from shared memory until later in the code! +Here's a trick question: When is a dependency not a dependency? Answer: +When it is purely syntactic rather than semantic. We say a dependency +between two accesses is purely syntactic if the second access doesn't +actually depend on the result of the first. Here is a trivial example: + + r1 = READ_ONCE(x);tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f · line 503
Quoted 10 lines
tools/memory-model/Documentation/explanation.txt +10
-0+ WRITE_ONCE(y, r1 * 0); + +There appears to be a data dependency from the load of x to the store of +y, since the value to be stored is computed from the value that was +loaded. But in fact, the value stored does not really depend on +anything since it will always be 0. Thus the data dependency is only +syntactic (it appears to exist in the code) but not semantic (the second +access will always be the same, regardless of the value of the first +access). Given code like this, a compiler could simply eliminate the +load from x, which would certainly destroy any dependency.Are you OK with that last sentence reading as follows?
Quoted 2 lines
Given code like this, a compiler could simply discard the value return by the load from x, which would certainly destroy any
s/return/returned/
Quoted 4 lines
dependency.
My concern with the original is that it might mislead people into thinking that compilers can omit volatile loads.
Yes, good point. Should we also tack on something like this?
(The compiler is not permitted to eliminate entirely the load generated for a READ_ONCE() -- that's one of the nice properties of READ_ONCE() -- but it is allowed to ignore the load's value.)
Quoted 25 lines
Quoted 12 lines
tools/memory-model/Documentation/explanation.txt +12
-0+ +(It's natural to object that no one in their right mind would write code +like the above. However, macro expansions can easily give rise to this +sort of thing, in ways that generally are not apparent to the +programmer.) + +Another mechanism that can give rise to purely syntactic dependencies is +related to the notion of "undefined behavior". Certain program behaviors +are called "undefined" in the C language specification, which means that +when they occur there are no guarantees at all about the outcome. +Consider the following example: +tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f
Quoted 10 lines
tools/memory-model/Documentation/explanation.txt +10
-0+ int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +Access beyond the end or before the beginning of an array is one kind of +undefined behavior. Therefore the compiler doesn't have to worry about +what will happen if r1 is nonzero, and it can assume that r1 will always +be zero without actually loading anything from i.And similarly here:
Quoted 2 lines
... and it can assume that r1 will always be zero regardless of the value actually loaded from i.
Right.
Quoted 4 lines
tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f · line 528
Quoted 4 lines
tools/memory-model/Documentation/explanation.txt +4
-0+ (If the assumption +turns out to be wrong, the resulting behavior will be undefined anyway +so the compiler doesn't care!) Thus the load from i can be eliminated, +breaking the address dependency.
This also should be changed:
Thus the value from the load can be discarded, breaking the address dependency.
Quoted 7 lines
Quoted 7 lines
tools/memory-model/Documentation/explanation.txt +6
-0+ +The LKMM is unaware that purely syntactic dependencies are different +from semantic dependencies and therefore mistakenly predicts that the +accesses in the two examples above will be ordered. This is another +example of how the compiler can undermine the memory model. Be warned. +
Quoted 7 lines
tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f · line 537
Quoted 2 lines
tools/memory-model/Documentation/explanation.txt +0
-0THE READS-FROM RELATION: rf, rfi, and rfe -----------------------------------------Looks great otherwise, and thank you all for your work on this!
Alan, would you like me to pull this in making those two changes and applying Akira's Reviewed-by, or would you prefer to send another version?
I'll send a new version.
Quoted 1 line
For that matter, am I off base in my suggested changes.
Not at all. Thanks.
Alan
Paul E. McKenney
maintainerRe: [PATCH] tools/memory-model: Explain syntactic and semantic dependencies
Quoted 50 lines · Alan Stern
Quoted 49 lines · Paul E. McKenney
Quoted 35 lines · Alan Stern
Paul Heidekrüger pointed out that the Linux Kernel Memory Model documentation doesn't mention the distinction between syntactic and semantic dependencies. This is an important difference, because the compiler can easily break dependencies that are only syntactic, not semantic.
This patch adds a few paragraphs to the LKMM documentation explaining these issues and illustrating how they can matter.
Suggested-by Paul Heidekrüger <[email protected]>Signed-off-by Alan Stern <[email protected]>---
[as1970]
tools/memory-model/Documentation/explanation.txt | 47 +++++++++++++++++++++++ 1 file changed, 47 insertions(+)Index:
usb-devel/tools/memory-model/Documentation/explanation.txt
===================================================================tools/memory-model/Documentation/explanation.txt +6
-0@@ -485,6 +485,53 @@ have R ->po X. It wouldn't make sense f somehow on a value that doesn't get loaded from shared memory until later in the code! +Here's a trick question: When is a dependency not a dependency? Answer: +When it is purely syntactic rather than semantic. We say a dependency +between two accesses is purely syntactic if the second access doesn't +actually depend on the result of the first. Here is a trivial example: + + r1 = READ_ONCE(x);tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f · line 503
Quoted 10 lines
tools/memory-model/Documentation/explanation.txt +10
-0+ WRITE_ONCE(y, r1 * 0); + +There appears to be a data dependency from the load of x to the store of +y, since the value to be stored is computed from the value that was +loaded. But in fact, the value stored does not really depend on +anything since it will always be 0. Thus the data dependency is only +syntactic (it appears to exist in the code) but not semantic (the second +access will always be the same, regardless of the value of the first +access). Given code like this, a compiler could simply eliminate the +load from x, which would certainly destroy any dependency.Are you OK with that last sentence reading as follows?
Quoted 4 lines
Quoted 2 lines
Given code like this, a compiler could simply discard the value return by the load from x, which would certainly destroy any
s/return/returned/
Good eyes!
Quoted 10 lines
Quoted 4 lines
dependency.
My concern with the original is that it might mislead people into thinking that compilers can omit volatile loads.
Yes, good point. Should we also tack on something like this?
(The compiler is not permitted to eliminate entirely the load generated for a
READ_ONCE()-- that's one of the nice properties ofREAD_ONCE()-- but it is allowed to ignore the load's value.)
Please!
Quoted 37 lines
Quoted 25 lines
Quoted 12 lines
tools/memory-model/Documentation/explanation.txt +12
-0+ +(It's natural to object that no one in their right mind would write code +like the above. However, macro expansions can easily give rise to this +sort of thing, in ways that generally are not apparent to the +programmer.) + +Another mechanism that can give rise to purely syntactic dependencies is +related to the notion of "undefined behavior". Certain program behaviors +are called "undefined" in the C language specification, which means that +when they occur there are no guarantees at all about the outcome. +Consider the following example: +tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f
Quoted 10 lines
tools/memory-model/Documentation/explanation.txt +10
-0+ int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +Access beyond the end or before the beginning of an array is one kind of +undefined behavior. Therefore the compiler doesn't have to worry about +what will happen if r1 is nonzero, and it can assume that r1 will always +be zero without actually loading anything from i.And similarly here:
Quoted 2 lines
... and it can assume that r1 will always be zero regardless of the value actually loaded from i.
Right.
Quoted 4 lines
tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f · line 528
Quoted 4 lines
tools/memory-model/Documentation/explanation.txt +4
-0+ (If the assumption +turns out to be wrong, the resulting behavior will be undefined anyway +so the compiler doesn't care!) Thus the load from i can be eliminated, +breaking the address dependency.This also should be changed:
Quoted 2 lines
Thus the value from the load can be discarded, breaking the address dependency.
Again, good eyes!
Quoted 12 lines
Quoted 12 lines
tools/memory-model/Documentation/explanation.txt have R ->po X. It wouldn't make sense f · line 537
Quoted 9 lines
tools/memory-model/Documentation/explanation.txt +6
-0+ +The LKMM is unaware that purely syntactic dependencies are different +from semantic dependencies and therefore mistakenly predicts that the +accesses in the two examples above will be ordered. This is another +example of how the compiler can undermine the memory model. Be warned. + THE READS-FROM RELATION: rf, rfi, and rfe -----------------------------------------Looks great otherwise, and thank you all for your work on this!
Quoted 4 lines
Quoted 2 lines
Alan, would you like me to pull this in making those two changes and applying Akira's Reviewed-by, or would you prefer to send another version?
I'll send a new version.
Very good, looking forward to it!
Thanx, Paul
Quoted 5 lines
Quoted 1 line
For that matter, am I off base in my suggested changes.
Not at all. Thanks.
Alan
Alan Stern
maintainerPATCH v2 1/1 tools/memory-model: Explain syntactic and semantic dependencies
In reply to Alan SternPaul Heidekrüger pointed out that the Linux Kernel Memory Model documentation doesn't mention the distinction between syntactic and semantic dependencies. This is an important difference, because the compiler can easily break dependencies that are only syntactic, not semantic.
This patch adds a few paragraphs to the LKMM documentation explaining these issues and illustrating how they can matter.
---
v2: Incorporate changes suggested by Paul McKenney, along with a few
other minor edits.
[as1970b]
tools/memory-model/Documentation/explanation.txt | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+)
tools/memory-model/Documentation/explanation.txt +51 -0
@@ -485,6 +485,57 @@ have R ->po X. It wouldn't make sense f somehow on a value that doesn't get loaded from shared memory until later in the code! +Here's a trick question: When is a dependency not a dependency? Answer: +When it is purely syntactic rather than semantic. We say a dependency +between two accesses is purely syntactic if the second access doesn't +actually depend on the result of the first. Here is a trivial example: + + r1 = READ_ONCE(x); + WRITE_ONCE(y, r1 * 0); + +There appears to be a data dependency from the load of x to the store +of y, since the value to be stored is computed from the value that was +loaded. But in fact, the value stored does not really depend on +anything since it will always be 0. Thus the data dependency is only +syntactic (it appears to exist in the code) but not semantic (the +second access will always be the same, regardless of the value of the +first access). Given code like this, a compiler could simply discard +the value returned by the load from x, which would certainly destroy +any dependency. (The compiler is not permitted to eliminate entirely +the load generated for a READ_ONCE() -- that's one of the nice +properties of READ_ONCE() -- but it is allowed to ignore the load's +value.) + +It's natural to object that no one in their right mind would write +code like the above. However, macro expansions can easily give rise +to this sort of thing, in ways that often are not apparent to the +programmer. + +Another mechanism that can lead to purely syntactic dependencies is +related to the notion of "undefined behavior". Certain program +behaviors are called "undefined" in the C language specification, +which means that when they occur there are no guarantees at all about +the outcome. Consider the following example: + + int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +Access beyond the end or before the beginning of an array is one kind +of undefined behavior. Therefore the compiler doesn't have to worry +about what will happen if r1 is nonzero, and it can assume that r1 +will always be zero regardless of the value actually loaded from i. +(If the assumption turns out to be wrong the resulting behavior will +be undefined anyway, so the compiler doesn't care!) Thus the value +from the load can be discarded, breaking the address dependency. + +The LKMM is unaware that purely syntactic dependencies are different +from semantic dependencies and therefore mistakenly predicts that the +accesses in the two examples above will be ordered. This is another +example of how the compiler can undermine the memory model. Be warned. + THE READS-FROM RELATION: rf, rfi, and rfe -----------------------------------------
Paul E. McKenney
maintainerRe: [PATCH v2] tools/memory-model: Explain syntactic and semantic dependencies
Quoted 11 lines · Alan Stern
Paul Heidekrüger pointed out that the Linux Kernel Memory Model documentation doesn't mention the distinction between syntactic and semantic dependencies. This is an important difference, because the compiler can easily break dependencies that are only syntactic, not semantic.
This patch adds a few paragraphs to the LKMM documentation explaining these issues and illustrating how they can matter.
Suggested-by Paul Heidekrüger <[email protected]>Signed-off-by Alan Stern <[email protected]>
Very good, queued and pushed with Akira's Reviewed-by.
Thank you all!
Thanx, Paul
Quoted 74 lines
---
v2: Incorporate changes suggested by Paul McKenney, along with a few
other minor edits.[as1970b]
tools/memory-model/Documentation/explanation.txt | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+)Index:
usb-devel/tools/memory-model/Documentation/explanation.txt
===================================================================tools/memory-model/Documentation/explanation.txt +51
-0@@ -485,6 +485,57 @@ have R ->po X. It wouldn't make sense f somehow on a value that doesn't get loaded from shared memory until later in the code! +Here's a trick question: When is a dependency not a dependency? Answer: +When it is purely syntactic rather than semantic. We say a dependency +between two accesses is purely syntactic if the second access doesn't +actually depend on the result of the first. Here is a trivial example: + + r1 = READ_ONCE(x); + WRITE_ONCE(y, r1 * 0); + +There appears to be a data dependency from the load of x to the store +of y, since the value to be stored is computed from the value that was +loaded. But in fact, the value stored does not really depend on +anything since it will always be 0. Thus the data dependency is only +syntactic (it appears to exist in the code) but not semantic (the +second access will always be the same, regardless of the value of the +first access). Given code like this, a compiler could simply discard +the value returned by the load from x, which would certainly destroy +any dependency. (The compiler is not permitted to eliminate entirely +the load generated for a READ_ONCE() -- that's one of the nice +properties of READ_ONCE() -- but it is allowed to ignore the load's +value.) + +It's natural to object that no one in their right mind would write +code like the above. However, macro expansions can easily give rise +to this sort of thing, in ways that often are not apparent to the +programmer. + +Another mechanism that can lead to purely syntactic dependencies is +related to the notion of "undefined behavior". Certain program +behaviors are called "undefined" in the C language specification, +which means that when they occur there are no guarantees at all about +the outcome. Consider the following example: + + int a[1]; + int i; + + r1 = READ_ONCE(i); + r2 = READ_ONCE(a[r1]); + +Access beyond the end or before the beginning of an array is one kind +of undefined behavior. Therefore the compiler doesn't have to worry +about what will happen if r1 is nonzero, and it can assume that r1 +will always be zero regardless of the value actually loaded from i. +(If the assumption turns out to be wrong the resulting behavior will +be undefined anyway, so the compiler doesn't care!) Thus the value +from the load can be discarded, breaking the address dependency. + +The LKMM is unaware that purely syntactic dependencies are different +from semantic dependencies and therefore mistakenly predicts that the +accesses in the two examples above will be ordered. This is another +example of how the compiler can undermine the memory model. Be warned. + THE READS-FROM RELATION: rf, rfi, and rfe -----------------------------------------