This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen] Fix restore blocks' BasicBlock information in branch relaxation
ClosedPublic

Authored by piggynl on Aug 14 2022, 10:29 AM.

Details

Summary

In branch relaxation pass, restore blocks are created and placed before the jump destination if indirect branches are required. For example:

        foo
        sd      s11, 0(sp)
        jump    .restore, s11
        bar
        bar
        bar
        j       .dest
.restore:
        ld      s11, 0(sp)
.dest:
        baz

The BasicBlock information of the restore MachineBasicBlock should be identical to the dest MachineBasicBlock.

Depends on D131862, which pre-commits the modified test.

Diff Detail

Event Timeline

piggynl created this revision.Aug 14 2022, 10:29 AM
piggynl requested review of this revision.Aug 14 2022, 10:29 AM
piggynl updated this revision to Diff 456353.Aug 29 2022, 8:22 AM

Rebase and ping for review :)

Ping for review :)

arsenm added inline comments.Sep 9 2022, 11:06 AM
llvm/lib/CodeGen/BranchRelaxation.cpp
21

Don’t see why you need this include

What’s the point of copying the IR block?

What problem are you trying to solve with this? I was wondering if blocks only indirectly derived from source IR blocks should perhaps not have the IR backlinks (but we do currently copy around these IR references in other block splitting contexts)

arsenm requested changes to this revision.Nov 16 2022, 3:27 PM

I guess there's nothing wrong with this, although I'm not sure split blocks should propagate the underlying IR block, we do that in other places today. I think you can still remove the include

This revision now requires changes to proceed.Nov 16 2022, 3:27 PM
piggynl updated this revision to Diff 477377.Nov 22 2022, 8:55 PM

Removes unnecessary #include.

arsenm accepted this revision.Nov 28 2022, 1:26 PM

LGTM but I would appreciate a note on why you care about this for future reference

This revision is now accepted and ready to land.Nov 28 2022, 1:26 PM
piggynl added a comment.EditedDec 1 2022, 4:45 AM

Because if the generated restore blocks have the incorrect BasicBlock information, like the # %dest_3 for .LBB8_6 and .LBB6_10 before this change, they would be confusing. Although I believe it is also acceptable if they contain no corresponding BasicBlock information, it will be easier to understand if they do, such as the '# %dest 1' with '.LBB8_6' and '# %dest 2' with '.LBB6_10' after this change.

arsenm added a comment.Dec 1 2022, 5:19 AM

Because if the generated restore blocks have the incorrect BasicBlock information, like the # %dest_3 for .LBB8_6 and .LBB6_10 before this change, they would be confusing. Although I believe it is also acceptable if they contain no corresponding BasicBlock information, it will be easier to understand if they do, such as the '# %dest 1' with '.LBB8_6' and '# %dest 2' with '.LBB6_10' after this change.

OK, so it's just a comment change - nonfunctional

Yes, exactly. Thank you for your time on this patch!

Because if the generated restore blocks have the incorrect BasicBlock information, like the # %dest_3 for .LBB8_6 and .LBB6_10 before this change, they would be confusing. Although I believe it is also acceptable if they contain no corresponding BasicBlock information, it will be easier to understand if they do, such as the '# %dest 1' with '.LBB8_6' and '# %dest 2' with '.LBB6_10' after this change.

OK, so it's just a comment change - nonfunctional

Yes, exactly. Thank you for your time on this patch!

This revision was landed with ongoing or failed builds.Dec 1 2022, 10:44 AM
This revision was automatically updated to reflect the committed changes.
hctim added a subscriber: hctim.Dec 1 2022, 11:02 AM

Looks like this patch might've caused a breakage for check-llvm, although I've done no work to actually validate this (just check-llvm from about 10-behind-head (i.e. without this patch) was okay, and at head right now is broken).

/usr/local/google/home/mitchp/llvm/llvm/test/CodeGen/RISCV/branch-relaxation.ll:2944:20: error: CHECK-RV64-NEXT:
 expected string not found in input                                                                             
; CHECK-RV64-NEXT: sd ra, 16(sp) # 8-byte Folded Spill                                                          
                   ^                                                                                            
<stdin>:1055:9: note: scanning from here                                                                        
 #NO_APP                                                                                                        
        ^                                                                                                       
<stdin>:1139:2: note: possible intended match here                                                              
 sd t5, 16(sp) # 8-byte Folded Spill                                                                            
 ^                                                                                                              

Input file: <stdin>
Check file: /usr/local/google/home/mitchp/llvm/llvm/test/CodeGen/RISCV/branch-relaxation.ll

-dump-input=help explains the following input dump.

Input was:
<<<<<<
             .
             .
             .
          1050:  .cfi_offset s9, -88 
          1051:  .cfi_offset s10, -96 
          1052:  .cfi_offset s11, -104 
          1053:  #APP 
          1054:  li ra, 1 
          1055:  #NO_APP 
next:2944'0             X error: no match found
          1056:  #APP 
next:2944'0     ~~~~~~
          1060:  li t1, 6                                                                                       
next:2944'0     ~~~~~~~~~~                                                                                      
             .                                                                                                  
             .                                                                                                  
             .
          1134:  #APP 
next:2944'0     ~~~~~~
          1135:  li t6, 31 
next:2944'0     ~~~~~~~~~~~
          1136:  #NO_APP 
next:2944'0     ~~~~~~~~~
          1137:  sd t6, 8(sp) # 8-byte Folded Spill 
next:2944'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          1138:  sext.w t6, t6 
next:2944'0     ~~~~~~~~~~~~~~~
          1139:  sd t5, 16(sp) # 8-byte Folded Spill 
next:2944'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
next:2944'1      ?                                    possible intended match
          1140:  sext.w t5, t5 
next:2944'0     ~~~~~~~~~~~~~~~
          1141:  bne t5, t6, .LBB6_1 
next:2944'0     ~~~~~~~~~~~~~~~~~~~~~
          1142: # %bb.7: # %entry 
next:2944'0     ~~~~~~~~~~~~~~~~~~
          1143:  jump .LBB6_4, t5 
next:2944'0     ~~~~~~~~~~~~~~~~~~
          1144: .LBB6_1: # %cond_2 
next:2944'0     ~~~~~~~~~~~~~~~~~~~
             .
             .
             .
>>>>>>
hctim added a comment.Dec 1 2022, 1:32 PM

Hmm, looks like it might've just been a problem on my end (or it's been fixed in-tree and didn't update this phab review). Sorry for the noise!