-
Notifications
You must be signed in to change notification settings - Fork 10.9k
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
[AArch64] Drop poison-generating flags in genSubAdd2SubSub
combiner
#90028
[AArch64] Drop poison-generating flags in genSubAdd2SubSub
combiner
#90028
Conversation
@llvm/pr-subscribers-backend-aarch64 Author: Antonio Frighetto (antoniofrighetto) ChangesA miscompilation issue has been addressed with improved handling. Fixes: #88950. Full diff: https://github.com/llvm/llvm-project/pull/90028.diff 2 Files Affected:
diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index 7bf06e71a03059..8653f621ba545f 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -6924,14 +6924,22 @@ genSubAdd2SubSub(MachineFunction &MF, MachineRegisterInfo &MRI,
assert((Opcode == AArch64::SUBWrr || Opcode == AArch64::SUBXrr) &&
"Unexpected instruction opcode.");
+ uint32_t RootFlags = Root.getFlags();
+ if (RootFlags & MachineInstr::NoSWrap)
+ Root.clearFlag(MachineInstr::NoSWrap);
+ if (RootFlags & MachineInstr::NoUWrap)
+ Root.clearFlag(MachineInstr::NoUWrap);
+
MachineInstrBuilder MIB1 =
BuildMI(MF, MIMetadata(Root), TII->get(Opcode), NewVR)
.addReg(RegA, getKillRegState(RegAIsKill))
- .addReg(RegB, getKillRegState(RegBIsKill));
+ .addReg(RegB, getKillRegState(RegBIsKill))
+ .setMIFlags(Root.getFlags());
MachineInstrBuilder MIB2 =
BuildMI(MF, MIMetadata(Root), TII->get(Opcode), ResultReg)
.addReg(NewVR, getKillRegState(true))
- .addReg(RegC, getKillRegState(RegCIsKill));
+ .addReg(RegC, getKillRegState(RegCIsKill))
+ .setMIFlags(Root.getFlags());
InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
InsInstrs.push_back(MIB1);
diff --git a/llvm/test/CodeGen/AArch64/machine-combiner-subadd2.mir b/llvm/test/CodeGen/AArch64/machine-combiner-subadd2.mir
index d1770bb25fae49..0b09e8a4b5cd38 100644
--- a/llvm/test/CodeGen/AArch64/machine-combiner-subadd2.mir
+++ b/llvm/test/CodeGen/AArch64/machine-combiner-subadd2.mir
@@ -237,3 +237,30 @@ body: |
RET_ReallyLR implicit $w0
...
+---
+# Drop nowrap flags in SUB
+
+# CHECK-LABEL: name: test8
+# CHECK: %7:gpr64 = SUBXrr %1, %0
+# CHECK-NEXT: %4:gpr64common = SUBXrr killed %7, killed %2
+
+name: test8
+registers:
+ - { id: 0, class: gpr64 }
+ - { id: 1, class: gpr64 }
+ - { id: 2, class: gpr64common }
+ - { id: 3, class: gpr64 }
+ - { id: 4, class: gpr64common }
+ - { id: 5, class: gpr64 }
+body: |
+ bb.0:
+ %1:gpr64 = COPY $x1
+ %0:gpr64 = COPY $x0
+ %2:gpr64common = ORRXri %0:gpr64, 4096
+ %3:gpr64 = ADDXrr killed %2:gpr64common, %0:gpr64
+ %4:gpr64common = nsw SUBSXrr %1:gpr64, killed %3:gpr64, implicit-def dead $nzcv
+ %5:gpr64 = SUBSXri %4:gpr64common, 0, 0, implicit-def $nzcv
+ $x0 = COPY %5:gpr64
+ RET_ReallyLR implicit $x0
+
+...
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for putting the patch together.
I think the flags were being added by
uint32_t Flags = Root.getFlags(); |
It might be worth having the switch at
break; |
5743cbc
to
eb0e62a
Compare
Thanks for reviewing it!
Right, although clearing the flags makes explicit the semantic of dropping flags in the combiner. I'd personally leave the
|
@davemgreen Out of curiosity, you seem to prefer not to mutate Is |
eb0e62a
to
69e8cea
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi - yeah I was expecting Root to always be replaced, hence it should be correct either way. Not modifying Root feels a little cleaner though. You would not usually expect the old code to be modified as the new nodes are created, and it can be a bit confusing to follow the logic of setting the on the Root, to be used later on (in code that is easy to miss), to override the flags of the instructions that have just been created.
DelInstrs.push_back(&Root);
Yeah I was thinking that would need to be added, either to the switch or into the genSubAdd2SubSub method.
69e8cea
to
84c6c43
Compare
Should be on track now! |
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. LGTM
A miscompilation issue has been addressed with improved handling. Fixes: llvm#88950.
84c6c43
to
23b6709
Compare
It looks like there's a slight issue with the test. If you have enabled expensive checks, the combiner calls
|
@momchil-velikov Thank you for reporting, and sorry to get back only now, somehow I missed the notification for this. Fixed in ca25702. |
A miscompilation issue has been addressed with improved handling.
Fixes: #88950.