Skip to content

[RISCV] Fix xcvbi bugs #64

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

Merged
merged 2 commits into from
Sep 27, 2023
Merged

Conversation

PhilippvK
Copy link
Contributor

I ran into these issues using +xcvbi in TVM's LLVM backend. The patches are minimal but I am not sure how to add a unit test for this bugfix.

@lewis-revill
Copy link
Contributor

Fix looks good! Since we are probably going to squash this into the xcvbi commits as a fixup when upstreaming, it wouldn't make sense to have a specific regression test for this in that case. But if you can provide some way of reproducing this issue as a separate issue in the issue tracker and link this PR as the fix - just some IR output from the frontend that causes the crash. Otherwise this is good to merge, if the person who merges this is able to write a descriptive commit message when merging.

@PhilippvK
Copy link
Contributor Author

I have to convert this into a Draft PR because I ran into a few more bugs related to the immediate Branching instructions.

  • It seems like using clang the cv.beqimm and cv.bneimm will only be inserted by LLVM with optimizations disabled (-O0) while for some reason clang foo.c -O3 -S -emit-llvm ... -> llc foo.ll -O3 ... -> clang foo.s -O3 ... seems to work.
  • If immediate branching instructions are present, it can happen that their mnemonic will be replaced with beq or bne by the control flow optimization pass. This does then result in invalid assembly code since the register-register branching instructions can not deal with the additional immediate operand. The main problem is that when replacing the Branch Maching Instructions (in RISCVInstrInfo::insertBranch) a reverse lookup solely based on the condition code (CC) is performed without checking the types of the Cond. c6a9e81 contains a workaround for that but I have the feeling, that there might be further patches required.

Here are some preliminary benchmark results with the workarounds implemented. Only up to 1% speedup (in dynamic instruction count) can be obtained here.

xcvbi_before_after

@PhilippvK
Copy link
Contributor Author

Proposal for unit test can be found here: #67 (comment)

@PhilippvK
Copy link
Contributor Author

Since the upstreaming of xcvbi is already in progress (https://reviews.llvm.org/D154412) and this fixes major bugs (segfaults) which almost everyone using -O2 or higher will run into. Wouldn't this patch need to be applied on the upstream as well before a merge?

@melonedo
Copy link
Contributor

melonedo commented Jul 14, 2023

Since the upstreaming of xcvbi is already in progress (https://reviews.llvm.org/D154412) and this fixes major bugs (segfaults) which almost everyone using -O2 or higher will run into. Wouldn't this patch need to be applied on the upstream as well before a merge?

No. Only encoding (Machine Code) is included in D154412, while this PR affects CodeGen, which is planned be submitted to LLVM after the encoding of all patches of encoding (in #65) are finished. So this PR will not immediately affect D154412 AFAICT.

@PhilippvK
Copy link
Contributor Author

@melonedo Oh, I missed that. Then a merge of this is not so urgent. Sorry for the confusion.

@jeremybennett
Copy link
Collaborator

@PhilippvK Thanks for this. Do you wish to resolve the remaining questions, so we can get this merged. Thanks

@PhilippvK
Copy link
Contributor Author

Sorry for the delay. I will add the proposed unit tests today or over the weekend. After that it should be ready to merge.

@PaoloS02
Copy link
Contributor

I've tested this pull request on top of the latest development branch and the macOS tests pass. So it's good to merge. It would be good to have the commits squashed into fewer or one commit if possible as some of the commits are just about fixing the indentation.

@PhilippvK
Copy link
Contributor Author

Rebased on top of upstream/development and squashed the changes into two commits. After CI passes, you can merge this.

@PaoloS02 PaoloS02 merged commit aea0c3a into openhwgroup:development Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants