This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Don't getDebugLoc for the end node of MBB iterator
ClosedPublic

Authored by StephenFan on Apr 13 2022, 6:53 AM.

Details

Summary

Because of shrink wrapping, the block to insert epilog may don't have
instructions (Only debug instructions). And the position to insert may
point to MBB.end() that don't have a DebugLoc. This patch fix this
problem.

The test program was copied from the issue:https://github.com/llvm/llvm-project/issues/53662

Diff Detail

Event Timeline

StephenFan created this revision.Apr 13 2022, 6:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2022, 6:53 AM
StephenFan requested review of this revision.Apr 13 2022, 6:53 AM
jmorse added a subscriber: jmorse.Apr 19 2022, 5:59 AM

Drive by review from a debug-info perspective -- I'm not a RISC-V person so don't feel confident enough to approve this, but I can see how the removed code will crash in the presence of a block only containing debug instructions, and that the new code will not crash. The fix looks correct in that sense, but someone else should confirm that it's not going to change codegen.

On the test front, IMO this is much better written as a MIR test that only runs prologepilog, where it will be resistant to optimisations, and the input to prologepilog will be very clear. It will also allow you to test that the correct (possibly empty) DebugLoc is assigned to instructions by the relevant function.

llvm/test/CodeGen/RISCV/xform_ah-min.ll
2 ↗(On Diff #422503)

It's also best practice to check at least one line of output to ensure that what you want to happen, actually happened. That ensures the test doesn't spuriously pass in the future. An example would be if the IR Verifier was tightened to reject the debug-info in this file, it would load and compile this IR and pass the test, but the scenario you're testing wouldn't be covered.

Adding a CHECK line, for example stopping after prologepilog and checking the relevant block has the right contents, would prevent that.

41–42 ↗(On Diff #422503)

Could you remove any unnecessary attributes, potentially all of them -- they're a future maintenance burden we can avoid now.

ChangeLog:

  1. Rewrite .ll test as MIR test
  2. Add '-run-pass=prologepilog' argument to test RUN line
  3. Add a CHECK line.

The fix looks fine.

Can you please clean up the Clang cruft from the MIR test file? And give it a meaningful name?
Shouldn't we also check the result of the pass?

If it helps, here's an automatically reduced test file, for reference:

|
  %struct.a = type { i32 }
  define i32 @b(%struct.a* %c) {
    %ab2 = getelementptr inbounds %struct.a, %struct.a* %c, i64 0, i32 0
    %1 = load i32, i32* %ab2, !tbaa !1
    br label %e.epilog
  e.epilog:
    %a.0.g = phi i32 [ 1, %e.epilog]
    br label %cleanup
  cleanup:
    %f.0 = phi i32 [ %a.0.g, %e.epilog ]
    ret i32 %f.0}
  !1 = !{}

ChangeLog:

  1. rename test file to pr53662.mir
  2. reduce pr53662.mir test file
This revision is now accepted and ready to land.Apr 22 2022, 2:19 AM

The fix looks fine.

Can you please clean up the Clang cruft from the MIR test file? And give it a meaningful name?
Shouldn't we also check the result of the pass?

If it helps, here's an automatically reduced test file, for reference:

|
  %struct.a = type { i32 }
  define i32 @b(%struct.a* %c) {
    %ab2 = getelementptr inbounds %struct.a, %struct.a* %c, i64 0, i32 0
    %1 = load i32, i32* %ab2, !tbaa !1
    br label %e.epilog
  e.epilog:
    %a.0.g = phi i32 [ 1, %e.epilog]
    br label %cleanup
  cleanup:
    %f.0 = phi i32 [ %a.0.g, %e.epilog ]
    ret i32 %f.0}
  !1 = !{}

Oh, I forget to thank @luismarques for giving me the reduced test file, and the updated test file is inspired by it. Thanks, Luis!
But I want to remind you may be your automatically reduced tool is out-of-date, since the reduced test file you give me seems invalid.

Use update_mir_test_checks.py, don’t hand-write specific check lines

ChangeLoc:

Use update_mir_test_checks.py to autogenerate check lines

luismarques accepted this revision.Apr 24 2022, 2:33 AM
This revision was landed with ongoing or failed builds.Apr 30 2022, 10:17 AM
This revision was automatically updated to reflect the committed changes.