Skip to content

[RISCV] Fix typo in CV_SH_rr_inc pattern #120246

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 1 commit into from
Dec 17, 2024

Conversation

PhilippvK
Copy link
Contributor

This typo in https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td#L701:L701 caused a compiler crash in 'RISC-V Assembly Printer' because CV_SH_ri_inc was selected, leading to getImmOpValue being called for a register operand.

This bug did not affect the Assembler output and therefore does not trigger any existing unit tests, but is visible by examining the final MIR function.

I came up with a minimal test, but I am unsure how to integrate this into llvm/test/CodeGen/RISCV/xcvmem.ll while still using utils/update_llc_test_checks.py. I would be happy I you could provide a hint on how both CHECK_ASM as well as CHECK_MIR lines could be generated automatically in the same test.

; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -O3 -mtriple=riscv32 -mattr=+xcvmem -verify-machineinstrs -stop-after=instruction-select < %s \
; RUN:   | FileCheck %s --check-prefixes=CHECK_MIR

define i16* @sh_ri_inc(i16* %a, i16 %b) {
; CHECK_MIR: CV_SH_ri_inc
  store i16 %b, i16* %a
  %1 = getelementptr i16, i16* %a, i32 42
  ret i16* %1
}

define i16* @sh_rr_inc(i16* %a, i16 %b, i32 %c) {
; CHECK_MIR: CV_SH_rr_inc
  store i16 %b, i16* %a
  %1 = getelementptr i16, i16* %a, i32 %c
  ret i16* %1
}

This typo caused a compiler crash in 'RISC-V Assembly Printer' because
CV_SH_ri_inc was selected, leading to `getImmOpValue` being called for
a register operand.

This bug did not affect the Assembler output and therefore does not trigger
any existing unit tests, but is visible by examining the final MIR function.
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2024

@llvm/pr-subscribers-backend-risc-v

Author: Philipp van Kempen (PhilippvK)

Changes

This typo in https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td#L701:L701 caused a compiler crash in 'RISC-V Assembly Printer' because CV_SH_ri_inc was selected, leading to getImmOpValue being called for a register operand.

This bug did not affect the Assembler output and therefore does not trigger any existing unit tests, but is visible by examining the final MIR function.

I came up with a minimal test, but I am unsure how to integrate this into llvm/test/CodeGen/RISCV/xcvmem.ll while still using utils/update_llc_test_checks.py. I would be happy I you could provide a hint on how both CHECK_ASM as well as CHECK_MIR lines could be generated automatically in the same test.

; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc -O3 -mtriple=riscv32 -mattr=+xcvmem -verify-machineinstrs -stop-after=instruction-select &lt; %s \
; RUN:   | FileCheck %s --check-prefixes=CHECK_MIR

define i16* @<!-- -->sh_ri_inc(i16* %a, i16 %b) {
; CHECK_MIR: CV_SH_ri_inc
  store i16 %b, i16* %a
  %1 = getelementptr i16, i16* %a, i32 42
  ret i16* %1
}

define i16* @<!-- -->sh_rr_inc(i16* %a, i16 %b, i32 %c) {
; CHECK_MIR: CV_SH_rr_inc
  store i16 %b, i16* %a
  %1 = getelementptr i16, i16* %a, i32 %c
  ret i16* %1
}

Full diff: https://github.com/llvm/llvm-project/pull/120246.diff

1 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td (+1-1)
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td b/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
index 4478e246111080..b98934d8c63964 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
@@ -698,7 +698,7 @@ let Predicates = [HasVendorXCVmem, IsRV32], AddedComplexity = 1 in {
   def : CVStriPat<post_store, CV_SW_ri_inc>;
 
   def : CVStrriPat<post_truncsti8, CV_SB_rr_inc>;
-  def : CVStrriPat<post_truncsti16, CV_SH_ri_inc>;
+  def : CVStrriPat<post_truncsti16, CV_SH_rr_inc>;
   def : CVStrriPat<post_store, CV_SW_rr_inc>;
 
   def : CVStrrPat<truncstorei8, CV_SB_rr>;

@lenary
Copy link
Member

lenary commented Dec 17, 2024

You're almost right with your file header, but not quite.

You need to make sure that the -stop-after= is naming the right pass. I think instruction-select is the GlobalISel name, and you want riscv-isel for the default pipeline (SelectionDAG).

I'm not sure you can use update_llc_test_checks.py with MIR output - you want update_mir_test_checks.py. I think a test ending at selection only would be enough for this change, because I don't think it's possible to mix update_mir_test_checks.py (mir output) and update_llc_test_checks.py (asm output).

topperc added a commit to topperc/llvm-project that referenced this pull request Dec 17, 2024
…o::verifyInstruction.

The generic verifier will do this if the operand type is
OPERAND_IMMEDIATE, but we use our own custom operand types.
Immediate operands are still allowed to be globals, constant pools,
blockaddress, etc. so we can't check !isImm().

Fix the same typo as llvm#120246 which is now detected by this.
@topperc
Copy link
Collaborator

topperc commented Dec 17, 2024

I just posted #120286 which I think removes the need to test MIR for this. If we agree, we can land just the .td change in this patch without any test and I'll land the verifier fix after.

@PhilippvK
Copy link
Contributor Author

PhilippvK commented Dec 17, 2024

You're almost right with your file header, but not quite.

You need to make sure that the -stop-after= is naming the right pass. I think instruction-select is the GlobalISel name, and you want riscv-isel for the default pipeline (SelectionDAG).

I'm not sure you can use update_llc_test_checks.py with MIR output - you want update_mir_test_checks.py. I think a test ending at selection only would be enough for this change, because I don't think it's possible to mix update_mir_test_checks.py (mir output) and update_llc_test_checks.py (asm output).

@lenary Thank you for the hint. I wanted to avoid adding a completely new set of test files for testing the XCV* extensions on the MIR level.

I just posted #120286 which I think removes the need to test MIR for this. If we agree, we can land just the .td change in this patch without any test and I'll land the verifier fix after.

@topperc That actually sounds like the best solution. Your fix makes a lot of sense, thank you! Can I expect this fix to be backported to LLVM19.1.6?

@topperc
Copy link
Collaborator

topperc commented Dec 17, 2024

You're almost right with your file header, but not quite.
You need to make sure that the -stop-after= is naming the right pass. I think instruction-select is the GlobalISel name, and you want riscv-isel for the default pipeline (SelectionDAG).
I'm not sure you can use update_llc_test_checks.py with MIR output - you want update_mir_test_checks.py. I think a test ending at selection only would be enough for this change, because I don't think it's possible to mix update_mir_test_checks.py (mir output) and update_llc_test_checks.py (asm output).

@lenary Thank you for the hint. I wanted to avoid adding a completely new set of test files for testing the XCV* extensions on the MIR level.

I just posted #120286 which I think removes the need to test MIR for this. If we agree, we can land just the .td change in this patch without any test and I'll land the verifier fix after.

@topperc That actually sounds like the best solution. Your fix makes a lot of sense, thank you! Can I expect this fix to be backported to LLVM19.1.6?

I think we can backport the .td change to 19.1.6. I'll have to check if my verifier patch can be backported.

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@topperc
Copy link
Collaborator

topperc commented Dec 17, 2024

@PhilippvK I assume you need me to commit this?

@PhilippvK
Copy link
Contributor Author

@topperc Yes, that would be great!

@topperc topperc merged commit e8ce6c4 into llvm:main Dec 17, 2024
8 checks passed
Copy link

@PhilippvK Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@topperc topperc added this to the LLVM 19.X Release milestone Dec 17, 2024
@topperc
Copy link
Collaborator

topperc commented Dec 17, 2024

/cherry-pick e8ce6c4

@PhilippvK PhilippvK deleted the fix-xcvmem-pat-typo branch December 17, 2024 20:24
@llvmbot
Copy link
Member

llvmbot commented Dec 17, 2024

/pull-request #120296

tru pushed a commit to llvmbot/llvm-project that referenced this pull request Jan 13, 2025
This typo in
https://github.com/llvm/llvm-project/blob/main/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td#L701:L701
caused a compiler crash in 'RISC-V Assembly Printer' because
CV_SH_ri_inc was selected, leading to `getImmOpValue` being called for a
register operand.

This bug did not affect the Assembler output and therefore does not
trigger any existing unit tests, but is visible by examining the final
MIR function.

(cherry picked from commit e8ce6c4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

4 participants