[IR] Memory locations for floating-point operations #226826
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
@llvm/pr-subscribers-llvm-ir
@llvm/pr-subscribers-llvm-analysis
@llvm/pr-subscribers-compiler-rt-sanitizer
Author: Serge Pavlov (spavloff)
<details>
<summary>Changes</summary>
Floating-point operations in a strictfp function have side effects, which are modeled using memory effects in the form of a read-write access to "inaccessible memory". This helps maintain strictfp semantics but may hinder optimizations. Floating-point operations may depend on rounding mode or not - this fact may be used to reorder them in a more optimal way. Similarly, functions that control floating-point environment (like set_rounding, set_fpmode etc.) also have more specific access than generic read-write. Also, "inaccessible memory" is used in cases other than FP operation, this results in unnecessary restrictions.
This change implements two new memory location to use instead of the access to "inaccessible memory". The "fpcontrol" location is used to represent access to floating-point control modes, of which only rounding mode is currently supported. The other location, "fpstatus", represents access to floating-point exceptions. Together they replace the use of "inaccessible memory".
The new memory locations are not supported in all cases. The Attributor does not support them now, as these locations do not exactly represent memory. Writing to a floating-point control register inside a function does not mean that the function itself changes some floating-point control mode. Supporting these locations in the Attributor requires additional considerations and, probably, new attributes.
The meaning of MemoryEffects::onlyAccessesInaccessibleMem is changed in this commit. Now this method checks not only the "inaccessible memory" but also floating-point environment. This change agrees with the current usage of the method in the compiler sources and will hopefully reduce the risk on misuse.
This change only introduces the new memory locations. Their use in the functions that access the floating-point environment will be implemented in another commit.
This MR was obtained from
https://github.com/llvm/llvm-project/pull/221754 by keeping only the changes required to introduce the new memory locations only.
---
Full diff: https://github.com/llvm/llvm-project/pull/226826.diff
17 Files Affected:
````````````The server is unavailable at this time. Please wait a few minutes before you try again.
</details>
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
````````git-clang-format --diff origin/main HEAD --extensions cpp,h -- llvm/include/llvm/AsmParser/LLToken.h llvm/include/llvm/Support/ModRef.h llvm/include/llvm/Transforms/IPO/Attributor.h llvm/lib/Analysis/AliasAnalysis.cpp llvm/lib/Analysis/BasicAliasAnalysis.cpp llvm/lib/Analysis/GlobalsModRef.cpp llvm/lib/AsmParser/LLLexer.cpp llvm/lib/AsmParser/LLParser.cpp llvm/lib/IR/Attributes.cpp llvm/lib/Support/ModRef.cpp llvm/lib/Transforms/IPO/Attributor.cpp llvm/lib/Transforms/IPO/AttributorAttributes.cpp llvm/lib/Transforms/IPO/FunctionAttrs.cpp llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp llvm/unittests/Support/ModRefTest.cpp --diff_from_common_commit
``
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````diff --git a/llvm/unittests/Support/ModRefTest.cpp b/llvm/unittests/Support/ModRefTest.cpp
index bbb104872..d74dad12f 100644
--- a/llvm/unittests/Support/ModRefTest.cpp
+++ b/llvm/unittests/Support/ModRefTest.cpp
@@ -19,10 +19,10 @@ namespace {
TEST(ModRefTest, PrintMemoryEffects) {
std::string S;
raw_string_ostream OS(S);
- OS << MemoryEffects::none();
- EXPECT_EQ(S, "ArgMem: NoModRef, InaccessibleMem: NoModRef, ErrnoMem: "
- "NoModRef, Other: NoModRef, TargetMem0: NoModRef, "
- "TargetMem1: NoModRef, FPControl: NoModRef, FPStatus: NoModRef");
+ OS << MemoryEffects::none();
+ EXPECT_EQ(S, "ArgMem: NoModRef, InaccessibleMem: NoModRef, ErrnoMem: "
+ "NoModRef, Other: NoModRef, TargetMem0: NoModRef, "
+ "TargetMem1: NoModRef, FPControl: NoModRef, FPStatus: NoModRef");
}
} // namespace
</details>
I dont understand why we only set part of the location here.
AFAIU this is only initializing ME and then going through all the memory attributes and setting it.
The name of the function is
getModRefInfoInaccessibleAndTargetMemLoc and we are also now checking fp modes.
There is nothing wrong, but are we saying that fp locations is the same as inaccessible memory?
Why are you adding this here:
fpcontrol, fpstatus
I think we can remove, mainly because in line 2868 you are not setting fp memory locations
This set Inaccessible memory, but does not set fp memory locations.
It is a bit strange that sometime FP memory location is equal to inaccessible memory(onlyAccessesInaccessibleMem or in getModRefInfoInaccessibleAndTargetMemLoc ) and sometimes is not.
Like now, FP locations is not being treated as inaccessible memory, because this sets all locations including inaccessible memory.
Maybe it would be better to differentiate them, like we did for target_memory.
For all the Transform changes if you keep this checking MemoryEffects::unknown() and the memory set(ME = MemoryEffects(ME.getModRef());) you still set all locations, what would be a problem?
I dont understand what is the problem of setting all bits for now and checking all bits for the memory location.
Can you combine this with target memory as well, so we can check the changes you did in the parser .
The new variant of this PR treats the floating-point locations same as other location. It is more consistent with the current design of memory effects, than a special treatment of the FP locations. It however requires changes in tests, where additional fpenv: none may be required. To reduce number of such tests, the changes not necessary to support the new locations are removed.
You are right. Now unknown_mem is removed.
The function is now renamed to getModRefInfoNonaddressableMemLoc.
User may use this attributes in their asm files.
Now FP locations behave exactly as other memories. Originally it seemed that treating these locations specifically, (like using natural default settings) can provide more convenient design. However it is hard to combine defferent logic in a single solution. Treating all memory locations uniformly seems more clear implementation.
Now unknown_mem is removed.
FP access has more complicated restrictions. For example read access to FP status bits require setting strictfp attribute. The initial intent was to uses NoModRef for FP locations as this is the common case. Now it requires more careful treatment in attribute deduction.
The new test cases were added.
Does the new version resolve the comments?
Ping.
Don't think we need this?
Should this be an llvm::any_of - and similarly above?
To make sure I'm reading this right, this is a parser convenience for when the control and status words have the same effects?
Do we need this? It seems redundant with the lit tests.
This might be better expressed as a lit test?
I think this change is pretty reasonable, I've just got a few minor notes, and am willing to stamp after they're addressed
static auto fpEnvLocations() {
And similar for the other ones. I think this is more consistent with capitalization of other members in this class.
/// registers.
(or add "the")
Missing fpenv?
Missing a test with both fpenv and fpcontrol/fpstatus, I think?
Yes, please.
This generally looks reasonable to me. Something I'm unhappy about is the inaccessiblemem vs nonaddressablemem -- those words mean the same thing.
I think the way I'd model this is to have inaccessiblemem refer to all kinds of inaccessible memory and replace the old inaccessiblemem by other_inaccessiblemem, as the category for inaccessible memory that has not been otherwise categorized. I expect we have a bunch of inaccessiblemem queries in the code base that really ought to be covering the new fpenv memory as well.
Removed.
Exactly. This is made similar to the TargetMem.
The lit test is in llvm/test/Analysis/BasicAA/fpenv.ll
Also updated fpEnvOnly, fpControlOnly and fpStatusOnly.
Fixed (used registers).
Such test is in llvm/test/Assembler/memory-attribute-errors.ll.
To address the naming problem, either IRMemLocation::InaccessibleMem should be renamed, or functions like onlyAccessesNonaddressableMem() should be given a different stem.
If IRMemLocation::InaccessibleMem is renamed, many of the existing calls to functions like onlyAccessesInaccessibleMemory will be preserved, but not all. This is an advantage - it requires fewer changes in the code. However, tests in this case also should be updated. There are about 900 occurences of inaccessiblemem in tests, which make this approach invasive. Additionally, It is not easy to invent a descriptive name for inaccessiblemem, as it is used as a generic way to implement memory-like ordering.
If the stem inaccessible is changed to something new, most of the functions like onlyAccessesInaccessibleMemory will have to be changed. However, the number of such calls is not large, and they must be reviewed for update anyway, no matter which approach is chosen.
In this revision, a low-invasive solution is proposed. IRMemLocation::InaccessibleMem remains intact, and only function names are changed. The memory locations InaccessibleMem, TargetMem*, FPControl and FPStatus are groupped into "PseudoMem". None of them are true memory location, and none allow pointers. In this patch only a minimal set of calls are updated to keep the patch compact. A full update of the functions will be implemented in follow-up patches.
I think the way I'd model this is to have inaccessiblemem refer to all kinds of inaccessible memory and replace the old inaccessiblemem by other_inaccessiblemem, as the category for inaccessible memory that has not been otherwise categorized. I expect we have a bunch of inaccessiblemem queries in the code base that really ought to be covering the new fpenv memory as well.
The first part of such solution is presented in https://github.com/llvm/llvm-project/pull/245318. It renames the existing inaccessiblemem.
CarolineConcatto
spavloff
Floating-point operations in a strictfp function have side effects, which are modeled using memory effects in the form of a read-write access to "inaccessible memory". This helps maintain strictfp semantics but may hinder optimizations. Floating-point operations may depend on rounding mode or not - this fact may be used to reorder them in a more optimal way. Similarly, functions that control floating-point environment (like
set_rounding,set_fpmodeetc.) also have more specific access than generic read-write. Also, "inaccessible memory" is used in cases other than FP operation, this results in unnecessary restrictions.This change implements two new memory location to use instead of the access to "inaccessible memory". The "fpcontrol" location is used to represent access to floating-point control modes, of which only rounding mode is currently supported. The other location, "fpstatus", represents access to floating-point exceptions. Together they replace the use of "inaccessible memory".
This change only introduces the new memory locations. Their use in the functions that access the floating-point environment will be implemented in another commit.
This MR was obtained from
https://github.com/llvm/llvm-project/pull/221754 by keeping only the changes required to introduce the new memory locations only.