- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[InstCombine] Propagate invariant.load metadata across unpacked loads #152186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
a3d0e64
              af85146
              71d9921
              9899e69
              9955d70
              2d4e276
              efe732a
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 
          
            
          
           | 
    @@ -718,6 +718,14 @@ static Instruction *combineLoadToOperationType(InstCombinerImpl &IC, | |||||||||||||||||||||||||||||
| return nullptr; | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||||||
| // Check if the aggregate load has a invariant.load metadata | ||||||||||||||||||||||||||||||
| // If aggregate load has invariant.load metadata, add it to the | ||||||||||||||||||||||||||||||
| // unpacked loads as well. | ||||||||||||||||||||||||||||||
| static void copyInvariantLoadMetadata(LoadInst &LI, LoadInst *NewLoad) { | ||||||||||||||||||||||||||||||
| if (MDNode *MD = LI.getMetadata(LLVMContext::MD_invariant_load)) | ||||||||||||||||||||||||||||||
| NewLoad->setMetadata(LLVMContext::MD_invariant_load, MD); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| 
     | 
||||||||||||||||||||||||||||||
| static Instruction *unpackLoadToAggregate(InstCombinerImpl &IC, LoadInst &LI) { | ||||||||||||||||||||||||||||||
| // FIXME: We could probably with some care handle both volatile and atomic | ||||||||||||||||||||||||||||||
| // stores here but it isn't clear that this is important. | ||||||||||||||||||||||||||||||
| 
        
          
        
         | 
    @@ -737,6 +745,7 @@ static Instruction *unpackLoadToAggregate(InstCombinerImpl &IC, LoadInst &LI) { | |||||||||||||||||||||||||||||
| LoadInst *NewLoad = IC.combineLoadToNewType(LI, ST->getTypeAtIndex(0U), | ||||||||||||||||||||||||||||||
| ".unpack"); | ||||||||||||||||||||||||||||||
| NewLoad->setAAMetadata(LI.getAAMetadata()); | ||||||||||||||||||||||||||||||
| copyInvariantLoadMetadata(LI, NewLoad); | ||||||||||||||||||||||||||||||
| return IC.replaceInstUsesWith(LI, IC.Builder.CreateInsertValue( | ||||||||||||||||||||||||||||||
| PoisonValue::get(T), NewLoad, 0, Name)); | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| 
        
          
        
         | 
    @@ -762,8 +771,13 @@ static Instruction *unpackLoadToAggregate(InstCombinerImpl &IC, LoadInst &LI) { | |||||||||||||||||||||||||||||
| ST->getElementType(i), Ptr, | ||||||||||||||||||||||||||||||
| commonAlignment(Align, SL->getElementOffset(i).getKnownMinValue()), | ||||||||||||||||||||||||||||||
| Name + ".unpack"); | ||||||||||||||||||||||||||||||
| // Adjust AA metadata to new offset and size. | ||||||||||||||||||||||||||||||
| AAMDNodes adjustedAANodes = LI.getAAMetadata().adjustForAccess( | ||||||||||||||||||||||||||||||
| SL->getElementOffset(i), | ||||||||||||||||||||||||||||||
| SL->getElementOffset(i).getKnownMinValue()); | ||||||||||||||||||||||||||||||
                
       | 
||||||||||||||||||||||||||||||
| AAMDNodes adjustedAANodes = LI.getAAMetadata().adjustForAccess( | |
| SL->getElementOffset(i), | |
| SL->getElementOffset(i).getKnownMinValue()); | |
| TypeSize offset = SL->getElementOffset(i); | |
| AAMDNodes adjustedAANodes = LI.getAAMetadata(); | |
| if (offset.isFixed()) { | |
| adjustedAANodes = adjustedAANodes.adjustForAccess( | |
| offset.getFixedValue(), | |
| ST->getElementType(i), DL); | |
| } | |
| else { | |
| adjustedAANodes.TBAA = nullptr; | |
| adjustedAANodes.TBAAStruct = nullptr; | |
| } | 
For structs with scalable vector elements, the TBAA information is not applicable. You can either zero out or assert here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please file a separate patch for this change. I don't want to block this patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, this is a separate issue unrelated to this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I will upload this as a separate patch.
        
          
              
                Outdated
          
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| copyInvariantLoadMetadata(LI, L); | |
| NewLI->copyMetadata(LI, LLVMContext::MD_invariant_load); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dtcxzyw I have fixed these issues, could you please give a lgtm? I will create another PR for adjusting AA metadata.
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 | ||
| ; RUN: opt -S < %s -passes=instcombine | FileCheck %s | ||
| 
     | 
||
| %struct.double2 = type { double, double } | ||
| %struct.double1 = type { double } | ||
| 
     | 
||
| define %struct.double2 @func1(ptr addrspace(1) %a) { | ||
| ; CHECK-LABEL: define %struct.double2 @func1( | ||
| ; CHECK-SAME: ptr addrspace(1) [[A:%.*]]) { | ||
| ; CHECK-NEXT: [[DOTUNPACK:%.*]] = load double, ptr addrspace(1) [[A]], align 16, !invariant.load [[META0:![0-9]+]] | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue [[STRUCT_DOUBLE2:%.*]] poison, double [[DOTUNPACK]], 0 | ||
| ; CHECK-NEXT: [[DOTELT1:%.*]] = getelementptr inbounds nuw i8, ptr addrspace(1) [[A]], i64 8 | ||
| ; CHECK-NEXT: [[DOTUNPACK2:%.*]] = load double, ptr addrspace(1) [[DOTELT1]], align 8, !invariant.load [[META0]] | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = insertvalue [[STRUCT_DOUBLE2]] [[TMP1]], double [[DOTUNPACK2]], 1 | ||
| ; CHECK-NEXT: ret [[STRUCT_DOUBLE2]] [[TMP2]] | ||
| ; | ||
| %1 = load %struct.double2, ptr addrspace(1) %a, align 16, !invariant.load !1 | ||
| ret %struct.double2 %1 | ||
| } | ||
| 
     | 
||
| define %struct.double2 @func2(ptr %a) { | ||
| ; CHECK-LABEL: define %struct.double2 @func2( | ||
| ; CHECK-SAME: ptr [[A:%.*]]) { | ||
| ; CHECK-NEXT: [[DOTUNPACK:%.*]] = load double, ptr [[A]], align 16, !invariant.load [[META0]] | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue [[STRUCT_DOUBLE2:%.*]] poison, double [[DOTUNPACK]], 0 | ||
| ; CHECK-NEXT: [[DOTELT1:%.*]] = getelementptr inbounds nuw i8, ptr [[A]], i64 8 | ||
| ; CHECK-NEXT: [[DOTUNPACK2:%.*]] = load double, ptr [[DOTELT1]], align 8, !invariant.load [[META0]] | ||
| ; CHECK-NEXT: [[TMP2:%.*]] = insertvalue [[STRUCT_DOUBLE2]] [[TMP1]], double [[DOTUNPACK2]], 1 | ||
| ; CHECK-NEXT: ret [[STRUCT_DOUBLE2]] [[TMP2]] | ||
| ; | ||
| %1 = load %struct.double2, ptr %a, align 16, !invariant.load !1 | ||
| ret %struct.double2 %1 | ||
| } | ||
| 
     | 
||
| define %struct.double1 @func3(ptr %a) { | ||
| ; CHECK-LABEL: define %struct.double1 @func3( | ||
| ; CHECK-SAME: ptr [[A:%.*]]) { | ||
| ; CHECK-NEXT: [[DOTUNPACK:%.*]] = load double, ptr [[A]], align 16, !invariant.load [[META0]] | ||
| ; CHECK-NEXT: [[TMP1:%.*]] = insertvalue [[STRUCT_DOUBLE1:%.*]] poison, double [[DOTUNPACK]], 0 | ||
| ; CHECK-NEXT: ret [[STRUCT_DOUBLE1]] [[TMP1]] | ||
| ; | ||
| %1 = load %struct.double1, ptr %a, align 16, !invariant.load !1 | ||
| ret %struct.double1 %1 | ||
| } | ||
| 
     | 
||
| !1 = !{} | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.