This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add CFI directives for RISCV prologue/epilog.
ClosedPublic

Authored by HsiangKai on May 9 2019, 6:35 PM.

Details

Summary

In order to generate correct debug frame information, it needs to generate CFI information in prologue and epilog.

Diff Detail

Repository
rL LLVM

Event Timeline

HsiangKai created this revision.May 9 2019, 6:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 9 2019, 6:35 PM

Hi Kai,

Other codegen test cases will generate .cfi directive with the patch. So other codegen test cases may need to update.

HsiangKai updated this revision to Diff 199764.May 16 2019, 1:42 AM

Update test cases for RISCV.

asb added a comment.May 16 2019, 6:43 AM

Better to modify the tests to add nounwind. I have a patch that does that sitting around (I added CFI directives downstream but didn't clean it up for submission). Let me apply the test suite patch upstream, then can you rebase on that.

asb added a comment.May 16 2019, 6:53 AM
In D61773#1504714, @asb wrote:

Better to modify the tests to add nounwind. I have a patch that does that sitting around (I added CFI directives downstream but didn't clean it up for submission). Let me apply the test suite patch upstream, then can you rebase on that.

Committed in rL360897

In D61773#1504748, @asb wrote:
In D61773#1504714, @asb wrote:

Better to modify the tests to add nounwind. I have a patch that does that sitting around (I added CFI directives downstream but didn't clean it up for submission). Let me apply the test suite patch upstream, then can you rebase on that.

Committed in rL360897

Fine. I will rebase on it. Thanks.

HsiangKai updated this revision to Diff 199958.May 16 2019, 7:10 PM

Update test cases.

asb requested changes to this revision.May 23 2019, 5:44 AM

Hi @HsiangKai - it seems I applied an old/wip version of my nounwind patch upstream. I've now fixed that in rL361493. With that, there should be no test changes when implementing this feature.

Please take a look at the two minor suggestions I had on updating frame-info.ll.

test/CodeGen/RISCV/frame-info.ll
1 ↗(On Diff #199958)

For completeness, please add a riscv64 RUN line too.

2 ↗(On Diff #199958)

I think it would be worth generating this with update_llc_test_checks.py. Then you can also see the generated instructions in the CHECK lines which makes it easy to check that the cfi directives match up to the generated stack layout.

This revision now requires changes to proceed.May 23 2019, 5:44 AM
asb added inline comments.May 23 2019, 7:11 AM
lib/Target/RISCV/RISCVFrameLowering.cpp
125 ↗(On Diff #199958)

Should say -StackSize?

142 ↗(On Diff #199958)

I think you can do this more concisely with:

for (const auto &Entry : CSI) {
  unsigned Reg = Entry.getReg();
  int FI = Entry.getFrameIdx();
  ....
HsiangKai marked an inline comment as done.May 23 2019, 7:02 PM
HsiangKai added inline comments.
lib/Target/RISCV/RISCVFrameLowering.cpp
125 ↗(On Diff #199958)

It should be ".cfi_def_cfa_offset StackSize". This directive is put after "addi sp, sp, -StackSize". CFA should be a fixed point. So, after updating stack pointer to lower address, we should update CFA as current stack pointer plus StackSize.

HsiangKai updated this revision to Diff 201116.May 23 2019, 7:03 PM
HsiangKai updated this revision to Diff 201365.May 24 2019, 5:02 PM

Update the test case.

Hi Kai,
We could also generate .cfi_restore for epilogue by:

unsigned CFIIndex = MF->addFrameInst(MCCFIInstruction::createRestore(
    nullptr, MRI->getDwarfRegNum(Reg, 1)));
BuildMI(MBB, MBBI, DL, TII.get(TargetOpcode::CFI_INSTRUCTION))
    .addCFIIndex(CFIIndex);

Add .cfi_restore for callee-saved registers in epilogue.

HsiangKai updated this revision to Diff 202083.May 29 2019, 5:37 PM

Update test cases.

asb accepted this revision.Jun 5 2019, 10:49 PM
asb added a subscriber: probinson.

This looks good from my perspective - @probinson, how does it look to you?

This revision is now accepted and ready to land.Jun 5 2019, 10:49 PM
asb added a comment.Jun 11 2019, 3:09 AM

I'm happy this is a big improvement over what we have (which is no CFI directive emission :)), so please go ahead and commit this. Thanks!

This revision was automatically updated to reflect the committed changes.