This is an archive of the discontinued LLVM Phabricator instance.

Preserve aliasing info during memory intrinsics lowering
ClosedPublic

Authored by ebrevnov on Jan 28 2022, 1:15 AM.

Details

Summary

By specification, source and destination of llvm.memcpy.* must either be equal or non-overlapping. This semantics is hard or impossible to figure out once lowered. This patch explicitly marks loads from source and stores to destination as not aliasing if source and destination is known to be not equal.

Diff Detail

Event Timeline

ebrevnov created this revision.Jan 28 2022, 1:15 AM
ebrevnov requested review of this revision.Jan 28 2022, 1:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 28 2022, 1:15 AM
xbolva00 added inline comments.
llvm/unittests/Transforms/Utils/MemTransferLowering.cpp
134–135

Please remove code after //

xbolva00 added inline comments.Jan 28 2022, 1:52 AM
llvm/unittests/Transforms/Utils/MemTransferLowering.cpp
115–116

Fragile? Why not simple test to check presence of additional metadata?

ebrevnov edited the summary of this revision. (Show Details)Jan 28 2022, 1:57 AM
ebrevnov added reviewers: sfertile, arsenm.

Needs some lit tests that show the metadata after expansion

llvm/lib/Transforms/Utils/LowerMemIntrinsics.cpp
79

Braces

88

Braces

132

Braces

145

Braces

217

Braces

223

Braces

279

Braces

287

Braces

Needs some lit tests that show the metadata after expansion

The problem is there is now pass which runs this utility. I'm not aware how to exercise it from lit test. For that reason I had to create a unit test. Is there a way to build a lit test in such case?

llvm/lib/Transforms/Utils/LowerMemIntrinsics.cpp
79

According to code style single statement if doesn't need braces.

llvm/unittests/Transforms/Utils/MemTransferLowering.cpp
115–116

I think you are right it should be checking for metadata. On the other hand, my final goal is to make sure the loop is vectorizable. Do you think it's ok to test both cases?

134–135

Sure.

arsenm added a comment.Feb 1 2022, 5:37 PM

Needs some lit tests that show the metadata after expansion

The problem is there is now pass which runs this utility. I'm not aware how to exercise it from lit test. For that reason I had to create a unit test. Is there a way to build a lit test in such case?

AMDGPU uses this, see llvm/test/CodeGen/AMDGPU/lower-mem-intrinsics.ll

llvm/lib/Transforms/Utils/LowerMemIntrinsics.cpp
79

It's spread over multiple lines with the comment, it should have braces

Needs some lit tests that show the metadata after expansion

The problem is there is now pass which runs this utility. I'm not aware how to exercise it from lit test. For that reason I had to create a unit test. Is there a way to build a lit test in such case?

AMDGPU uses this, see llvm/test/CodeGen/AMDGPU/lower-mem-intrinsics.ll

In order for this to work I would need to modify AMDGPULowerIntrinsics pass to provide additional argument(result of ScalarEvolutionAnalysis) to expandMemCpyAsLoop. Currently, this pass doesn't depend on SCEV analysis.

ebrevnov updated this revision to Diff 405872.Feb 3 2022, 10:30 PM

Fixed according to comments

arsenm added a comment.Feb 4 2022, 7:49 AM

Needs some lit tests that show the metadata after expansion

The problem is there is now pass which runs this utility. I'm not aware how to exercise it from lit test. For that reason I had to create a unit test. Is there a way to build a lit test in such case?

AMDGPU uses this, see llvm/test/CodeGen/AMDGPU/lower-mem-intrinsics.ll

In order for this to work I would need to modify AMDGPULowerIntrinsics pass to provide additional argument(result of ScalarEvolutionAnalysis) to expandMemCpyAsLoop. Currently, this pass doesn't depend on SCEV analysis.

That's fine

Needs some lit tests that show the metadata after expansion

The problem is there is now pass which runs this utility. I'm not aware how to exercise it from lit test. For that reason I had to create a unit test. Is there a way to build a lit test in such case?

AMDGPU uses this, see llvm/test/CodeGen/AMDGPU/lower-mem-intrinsics.ll

In order for this to work I would need to modify AMDGPULowerIntrinsics pass to provide additional argument(result of ScalarEvolutionAnalysis) to expandMemCpyAsLoop. Currently, this pass doesn't depend on SCEV analysis.

That's fine

Do you mean it's fine to modify AMDGPULowerIntrinsics pass? If that's what you mean I don't see there is enough rational to do a non NFC change just to be able to create a test.

So otherwise upload a new patch which somehow uses this new functionality. What is motivation behind this change? If cannot be tested using current pipeline..

And if there is a one - wait until that one is accepted and you can push that one + this one together.

I'm extending functionality of memory lowering utilities which meant to be used in different context. At the moment there is no any upstream pass which will take advantage of added functionality immediately. Rather, we have downstream functionality which will make use of it. In future, upstream users can decide to utilize the functionality on case by case bases... I don't have enough expertise in those areas to drive changes

Ok, I think we dont think we really need to require lit test and the provided unit test is good enough.

Do you mean it's fine to modify AMDGPULowerIntrinsics pass? If that's what you mean I don't see there is enough rational to do a non NFC change just to be able to create a test.

Yes. It's a good improvement either way, and lit tests are much more stable and maintainable than ones directly touching the APIs

arsenm accepted this revision.Mar 28 2022, 4:08 PM

LGTM but a lit test would be much more maintainable

llvm/unittests/Transforms/Utils/MemTransferLowering.cpp
170

These operands should be swapped to get correct error messages

This revision is now accepted and ready to land.Mar 28 2022, 4:08 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2022, 4:08 PM
ebrevnov added inline comments.Mar 28 2022, 9:25 PM
llvm/unittests/Transforms/Utils/MemTransferLowering.cpp
170

Didn't know. Will fix. Thanks

arsenm accepted this revision.Apr 5 2022, 5:45 PM
This revision was landed with ongoing or failed builds.Apr 5 2022, 9:34 PM
This revision was automatically updated to reflect the committed changes.
arsenm added inline comments.Sep 22 2022, 3:03 PM
llvm/lib/Transforms/Utils/LowerMemIntrinsics.cpp
481

Isn't this implied by the usual constraints of memcpy? The source and destination aren't allowed to overlap. Why does this need the SCEV check?

nikic added a subscriber: nikic.Sep 22 2022, 3:08 PM
nikic added inline comments.
llvm/lib/Transforms/Utils/LowerMemIntrinsics.cpp
481

llvm.memcpy (unlike memcpy) is allowed to have src == dst (but no other overlap).