This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Fix assertion failure when zcmp extension is enabled.
ClosedPublic

Authored by garvitgupta08 on Aug 18 2023, 12:58 AM.

Details

Summary

Before accessing "getOpcode" thorugh machine instruction, check if the iterator
has reached the end of Machine basic block otherwise we will crash at the
assertion !NodePtr->isKnownSentinel().

The above assertion is hit in "Prologue/Epilogue Insertion & Frame Finalization
pass".

Diff Detail

Event Timeline

garvitgupta08 created this revision.Aug 18 2023, 12:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 18 2023, 12:58 AM
garvitgupta08 requested review of this revision.Aug 18 2023, 12:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 18 2023, 12:58 AM
jrtc27 requested changes to this revision.Aug 18 2023, 1:04 AM
jrtc27 added inline comments.
llvm/test/CodeGen/RISCV/prolog-epilog-crash.mir
2 ↗(On Diff #551414)

REQUIRES

3 ↗(On Diff #551414)
4 ↗(On Diff #551414)

Don't need a custom CHECK prefix when there's just one in the whole file (and if you only have one for that FileCheck invocation you should use --check-prefix). Besides, what's IPE?..

7 ↗(On Diff #551414)

Is the IR actually needed?

35 ↗(On Diff #551414)

If you're going to call it f then the test needs a comment for what's being tested here

This revision now requires changes to proceed.Aug 18 2023, 1:04 AM
garvitgupta08 marked 4 inline comments as done.Aug 18 2023, 3:00 AM
garvitgupta08 added inline comments.
llvm/test/CodeGen/RISCV/prolog-epilog-crash.mir
7 ↗(On Diff #551414)

Yes, I think that is how it has been done what other mir unit test cases. Besides it would help to see the .ll file from which the mir testcase is generated.

35 ↗(On Diff #551414)

It it the reduced testcase through bugpoint which tests the prologepilog pass.

garvitgupta08 marked 2 inline comments as done.Aug 18 2023, 3:03 AM
garvitgupta08 added inline comments.
llvm/test/CodeGen/RISCV/prolog-epilog-crash.mir
7 ↗(On Diff #551414)

There is a typo - for other mir unit testcases.

jrtc27 added inline comments.Aug 18 2023, 8:43 AM
llvm/test/CodeGen/RISCV/prolog-epilog-crash.mir
7 ↗(On Diff #551414)

Not all MIR tests need it. So I'm asking whether this one specifically does.

35 ↗(On Diff #551414)

So? bugpoint reduces it, but you still need to turn it into a meaningful and readable test.

craig.topper added inline comments.Aug 18 2023, 3:12 PM
llvm/test/CodeGen/RISCV/prolog-epilog-crash.mir
31 ↗(On Diff #551450)

This attribute shouldn't be needed since it is on the command line.

When enabling zcmp extension to build compiler-rt, fixunstfsi.c was crashing at the above mentioned pass. The testcase used is reduced through bugpoint from the generated llvm IR from fixunstfsi.c source.

garvitgupta08 marked an inline comment as done.Aug 22 2023, 1:33 AM
anmolparalkar-nxp added a comment.EditedAug 22 2023, 5:17 PM

@garvitgupta08 I have closed D158379 after starting with your patch from this (D158256) and merging in llvm/test/CodeGen/RISCV/frame-lowering-crash.ll
Kindly download the patch from D158379 as the starting point prior to your next upload to this (D158256). Thank you!
@wangpc thanks for the advice.

Thanks @anmolparalkar-nxp for informing. Merged the change from D158379.

anmolparalkar-nxp added a comment.EditedAug 23 2023, 9:28 AM

@garvitgupta08 Once committed to main, please do file an issue to add the fix to the 17.0.x milestone, for it to make it into LLVM 17.0.0-RC4 Thank you!

Hi @craig.topper @jrtc27, do let me know if there are any further changes needed. If the current patch is fine, please commit it on my behalf; below are my credentials
Name - Garvit Gupta
Username - garvitgupta08
email id - quic_garvgupt@quicinc.com

craig.topper added inline comments.Aug 23 2023, 10:47 PM
llvm/test/CodeGen/RISCV/frame-lowering-crash.ll
1 ↗(On Diff #552770)

Can we decide a single name for the test files? Either frame-lowering-crash.ll/mir or prolog-epilog-crash.mir?

Maybe worth including zcmp in the test file name too?

13 ↗(On Diff #552770)

Do we need the hidden and local_unnamed_addr attributes?

Well for starters you've not even addressed all the feedback from prior reviews, so pinging is a bit premature and rude

llvm/test/CodeGen/RISCV/frame-lowering-crash.ll
1 ↗(On Diff #552770)

Use update_llc_test_checks.py

1 ↗(On Diff #552770)

Though why is there an IR test and a MIR test? The MIR test seems preferable, surely?

1–6 ↗(On Diff #552770)

Though it looks like the same code is generated for both so can share one prefix?

14 ↗(On Diff #552770)

nounwind

14 ↗(On Diff #552770)

Space between declaration and definition

60 ↗(On Diff #552770)

Clean up the IR of these comments?

64 ↗(On Diff #552770)

Dangling attribute reference

75 ↗(On Diff #552770)

This seems overly-reduced

85 ↗(On Diff #552770)

Fix

llvm/test/CodeGen/RISCV/prolog-epilog-crash.mir
3–4 ↗(On Diff #552770)
65 ↗(On Diff #552770)

Get rid of these with nounwind, however that's done in MIR

7 ↗(On Diff #551414)

You've still not answered this

35 ↗(On Diff #551414)

Still pending

garvitgupta08 added a reviewer: anmolparalkar-nxp.

@anmolparalkar-nxp can you please take a look at the comments on zcmp-prolog-epilog-crash.ll test?

When enabling zcmp extension to build compiler-rt, fixunstfsi.c was crashing at the above mentioned pass. The testcase used is reduced through bugpoint from the generated llvm IR from fixunstfsi.c source.

@jrtc27 I commented about the source of the IR used in MIR testcase. I didn't hear back on it so I thought the test currently is fine. Let me know if I should put the original IR in the testcase without reducing it through bugpoint so that it can more meaningful and readable.

llvm/test/CodeGen/RISCV/prolog-epilog-crash.mir
7 ↗(On Diff #551414)

The only reason for me to include IR in the testcase is to know the source from which the Machine instructions are generated. If it does not help much in this case, I can remove it.

@anmolparalkar-nxp can you please take a look at the comments on zcmp-prolog-epilog-crash.ll test?

@garvitgupta08 I will start with your current diff from here, address the comments, and upload the new patch to D158379 and ping you; following which, please start by downloading and continuing from there ...
Also, please feel free to drop it (IR test) in favour of the more preferred MIR test ...

llvm/test/CodeGen/RISCV/frame-lowering-crash.ll
75 ↗(On Diff #552770)

@jrtc27 Kindly clarify (overly-reduced) I'll include matter as necessary, thanks.

craig.topper added inline comments.Aug 24 2023, 6:11 PM
llvm/test/CodeGen/RISCV/frame-lowering-crash.ll
75 ↗(On Diff #552770)

icmp slt i32 poison, poison doesn't make sense. The compiler would be allowed to delete it. So a future optimization could break this test.

store i32 0, ptr null, align 4 is a store to null which is undefined behavior. So the compiler is allowed to delete it. Again a future optimization could delete it and break this test.

Jim added inline comments.Aug 24 2023, 6:30 PM
llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.ll
15 ↗(On Diff #553000)

Does it have any difference of codegen between RV32 and RV64? Can we use the same check-prefixes?

llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.ll
15 ↗(On Diff #553000)

No difference in codegen between RV32 and RV64; yes, agreed on the same check-prefixes. Thanks.

llvm/test/CodeGen/RISCV/frame-lowering-crash.ll
75 ↗(On Diff #552770)

icmp slt i32 poison, poison doesn't make sense. The compiler would be allowed to delete it. So a future optimization could break this test.

Agreed, I can rewrite without the use of poison (here and elsewhere) and preserve reproducibility.

store i32 0, ptr null, align 4 is a store to null which is undefined behavior. So the compiler is allowed to delete it. Again a future optimization could delete it and break this test.

  • however, I am unable to reproduce the crash if I replace the 'ptr null' with a regular address. Any suggestions? Thanks.
llvm/test/CodeGen/RISCV/frame-lowering-crash.ll
75 ↗(On Diff #552770)

store i32 0, ptr null, align 4 is a store to null which is undefined behavior. So the compiler is allowed to delete it. Again a future optimization could delete it and break this test.

  • however, I am unable to reproduce the crash if I replace the 'ptr null' with a regular address. Any suggestions? Thanks.

ah, never mind; I am able to reproduce the crash by replacing the 'ptr null' with:

store i32 0, ptr inttoptr (i32 -559038737 to ptr), align 4

@garvitgupta08 Please download updated patch from D158379 with review comments to llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.ll addressed
I have included a checklist of the comments from here, along with their resolution for you to be able to verify and check each one off. Thanks so much!

Addressed review comments to llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.ll from D158256:

Do we need the hidden and local_unnamed_addr attributes?
DONE

icmp slt i32 poison, poison doesn't make sense. The compiler would be allowed to delete it. So a future optimization could break this test.
DONE

store i32 0, ptr null, align 4 is a store to null which is undefined behavior. So the compiler is allowed to delete it. Again a future optimization could delete it and break this test.
DONE

Use update_llc_test_checks.py
DONE

Though why is there an IR test and a MIR test? The MIR test seems preferable, surely?
Please feel free to drop: llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.ll

Though it looks like the same code is generated for both so can share one prefix?
Actually, there is a difference ...

nounwind
DONE

Space between declaration and definition
DONE

Clean up the IR of these comments?
DONE

Dangling attribute reference
DONE

This seems overly-reduced
Asked for clarification ...

Fix // No newline at end of file
DONE?

llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.ll
15 ↗(On Diff #553000)

After incorporating @craig.topper's comments on the use of poison value and the write to 'ptr null' being code fragments that could potentially be optimized away in the future, I respun the code and now the code generated is different for RV32 and RV64; the checks themselves being auto-generated by utils/update_llc_test_checks.py

llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.ll
15 ↗(On Diff #553000)

No longer applicable; please see note right below for the clarification. Thanks.

llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.ll
15 ↗(On Diff #553000)

@garvitgupta08 Please mark both as Done (actually, this is no longer applicable, as there _is_ a difference between the RV32 code and RV64 code - and the check-prefixes are automatically generated by utils/update_llc_test_checks.py). Please change the state from Not Done to Done on both @Jim's and @anmolparalkar-nxp's comments, thank you.

garvitgupta08 marked 3 inline comments as done.Aug 28 2023, 10:29 AM

Once again, since I can’t obviously see an answer to it: why are there both MIR and IR tests?

anmolparalkar-nxp added a comment.EditedAug 28 2023, 12:31 PM

@jrtc27 - to answer your question, the .ll test came in per @wangpc's advice in D158379 to merge the test from there, in here; I incorporated all the comments on the .ll test, made here, to make myself more educated in the methodology to be followed, in general, and of-course, to respect the review feedback that was given for the .ll, here. That said, I have already clarified on August 24th: "Please feel free to drop: llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.ll" Thank you.

@garvitgupta08, It is most important that this fix gets included in the 17.0.x milestone, hopefully, it makes it in time for LLVM 17.0.0-RC4 Thanks!

I would like to merge this patch as soon as possible so that it can be merged into community release 17.0. Do let me know if there is really a need to have only MIR test, otherwise I think that all comments have been addressed.

I would like to merge this patch as soon as possible so that it can be merged into community release 17.0. Do let me know if there is really a need to have only MIR test, otherwise I think that all comments have been addressed.

Drop the IR test. We don't need two tests for the same issue.

Addressed the comments

wangpc accepted this revision.Aug 29 2023, 7:31 PM

LGTM.

Please commit it on my behalf; below are my credentials
Name - Garvit Gupta
Username - garvitgupta08
email id - quic_garvgupt@quicinc.com

craig.topper added inline comments.Aug 29 2023, 10:11 PM
llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.mir
3

Why does the test require asserts?

This revision was not accepted when it landed; it landed in state Needs Review.Aug 29 2023, 10:12 PM
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
garvitgupta08 added inline comments.Aug 29 2023, 10:15 PM
llvm/test/CodeGen/RISCV/zcmp-prolog-epilog-crash.mir
3

The assertion is hit only when asserts are enabled. With release build with no assert, it is able to generate some machine instructions

jrtc27 added inline comments.Aug 29 2023, 10:17 PM
llvm/test/CodeGen/RISCV/prolog-epilog-crash.mir
3–4 ↗(On Diff #552770)

This wasn't addressed