This is an archive of the discontinued LLVM Phabricator instance.

[AMDGPU] Correctly merge alias.scope and noalias metadata for memops
ClosedPublic

Authored by bcahoon on Sep 19 2021, 6:27 PM.

Details

Summary

When adding alias.scope and noalias metadata to a memcpy function,
the alias.scope and noalias metadata from the operands are merged.
The rule for merging alias.scope is to take the intersection of
the domains and the union of the scopes within those domains.
The rule for merging noalias is to take the intersection.

The bug is that AMDGPULowerModuleLDS was using concatenation for
both alias.scope and noalias. For example, when f1 and f2 are added
to the LDS structure and there is a memcpy(f2, f1, sizeof(f1)).
Then, concatenation creates noalias metadata for the memcpy that
includes both {f1, f2}. That means that the memcpy is assumed
not to alias a prior load of f2, which enables the optimizer to
remove a load of f2 that occurs after mempcy.

The function MDNode::getmostGenericAliasScope defines the semantics
for alias.scope. There is a function, combineMetadata in Local.cpp,
that uses intersect for noalias.

Diff Detail

Event Timeline

bcahoon created this revision.Sep 19 2021, 6:27 PM
bcahoon requested review of this revision.Sep 19 2021, 6:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 19 2021, 6:27 PM
hliao added a subscriber: hliao.Sep 20 2021, 8:36 AM
rampitec added inline comments.Sep 20 2021, 4:19 PM
llvm/test/CodeGen/AMDGPU/lower-lds-struct-aa-merge.ll
8–11

It essentially always comes empty, which is as good as having no AA at all. Maybe it is better to ignore new AA and keep the old one instead?

18

Can you remove these whitespace changes?

rampitec added inline comments.Sep 20 2021, 4:38 PM
llvm/test/CodeGen/AMDGPU/lower-lds-struct-aa-memcpy.ll
21

It is unclear from the test what are these !0, !3 etc values. Could you include it in the checks?

30

The original memcpy did not have any AA, we are merging AA produced by the same pass.

As I understand the problem is there is a single AA for the whole instruction, not for an operand. Clearly concat is not correct if we pass 2 different LDS pointers into a same call, but passing AA for only the first operand is not correct either, please disregard previous comment with respect to keeping the old AA value.

Using the intersect shall be fine, although on practice it will produce an empty list if pointers are to different fields. With that in mind the patch itself is correct, but please add metadata checks.

arsenm added inline comments.Sep 20 2021, 4:41 PM
llvm/test/CodeGen/AMDGPU/lower-lds-struct-aa-memcpy.ll
21

I think update_test_checks support checking metadata nodes these days

bcahoon updated this revision to Diff 373770.Sep 20 2021, 7:50 PM

Add CHECKs for the alias scope metadata. Removed unintentional ws.

bcahoon marked an inline comment as done.Sep 20 2021, 8:02 PM
bcahoon added inline comments.
llvm/test/CodeGen/AMDGPU/lower-lds-struct-aa-memcpy.ll
21

I tried using update_test_checks, but no checks were added for the metadata. I'm not sure, but perhaps UpdateTestChecks doesn't support alias.scope and noalias metadata. It looks like there is support for other metadata.

30

If there are more than two fields, then intersect wouldn't be empty. For example, the noalias value for a load of f1 would include the scopes for f2 and f3. The noalias value for a load of f2 would includes the scopes for f1 and f3. Then, if there is a memcpy(f2, f1), then the noalias for the memcpy would be the scope for f3 (the intersection of {f1, f3} and {f2, f3}).

The main issue with existing alias.scope information is that the domain will always be different than the domain created for the LDS alias.scope metadata, which is an anonymous domain. So, the intersection of the LDS alias.scope domain with any previous domain will always be empty.

llvm/test/CodeGen/AMDGPU/lower-lds-struct-aa-merge.ll
18

Thanks! That was unintentional.

This revision is now accepted and ready to land.Sep 21 2021, 9:35 AM
This revision was landed with ongoing or failed builds.Sep 21 2021, 11:06 AM
This revision was automatically updated to reflect the committed changes.
bcahoon marked an inline comment as done.