Skip to content
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][CodeGen] Fix illegal register aliasing bug for mops instrs #88869

Merged
merged 3 commits into from
Apr 18, 2024

Conversation

nasherm
Copy link
Contributor

@nasherm nasherm commented Apr 16, 2024

A bug was found where mops instructions were being generated that aliased the source and size registers. This is unpredictable behaviour. This patch usess the earlyclobber constraint on the input source register so that it doesn't alias with the size register. Also a test is introduced which tests affected instructions can't violate this constraint.

@llvmbot llvmbot added backend:AArch64 mc Machine (object) code labels Apr 16, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 16, 2024

@llvm/pr-subscribers-backend-aarch64

@llvm/pr-subscribers-mc

Author: Nashe Mncube (nasherm)

Changes

A bug was found where mops instructions were being generated that aliased the source and size registers. This is unpredictable behaviour. This patch usess the earlyclobber constraint on the input source register so that it doesn't alias with the size register. Also a test is introduced which tests affected instructions can't violate this constraint.


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

2 Files Affected:

  • (modified) llvm/lib/Target/AArch64/AArch64InstrInfo.td (+1-1)
  • (added) llvm/test/MC/AArch64/armv9.3a-mops-register-aliasing.s (+15)
diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.td b/llvm/lib/Target/AArch64/AArch64InstrInfo.td
index e1624f70185e1e..3bf90778363c6c 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.td
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.td
@@ -9486,7 +9486,7 @@ let Predicates = [HasMOPS], Defs = [NZCV], Size = 12, mayStore = 1 in {
   let mayLoad = 0 in {
     def MOPSMemorySetPseudo  : Pseudo<(outs GPR64common:$Rd_wb, GPR64:$Rn_wb),
                                       (ins GPR64common:$Rd, GPR64:$Rn, GPR64:$Rm),
-                                      [], "$Rd = $Rd_wb,$Rn = $Rn_wb">, Sched<[]>;
+                                      [], "$Rd = $Rd_wb,$Rn = $Rn_wb,@earlyclobber $Rn_wb">, Sched<[]>;
   }
 }
 let Predicates = [HasMOPS, HasMTE], Defs = [NZCV], Size = 12, mayLoad = 0, mayStore = 1 in {
diff --git a/llvm/test/MC/AArch64/armv9.3a-mops-register-aliasing.s b/llvm/test/MC/AArch64/armv9.3a-mops-register-aliasing.s
new file mode 100644
index 00000000000000..109b33857a9d2d
--- /dev/null
+++ b/llvm/test/MC/AArch64/armv9.3a-mops-register-aliasing.s
@@ -0,0 +1,15 @@
+// RUN: not llvm-mc -triple aarch64 -mattr=+mops < %s 2>&1 | FileCheck %s
+
+  setp  [x0]!, x1!, x1
+  setm  [x0]!, x1!, x1
+  sete  [x0]!, x1!, x1
+
+// CHECK:      error: invalid SET instruction, source and size registers are the same
+// CHECK-NEXT:   setp  [x0]!, x1!, x1
+// CHECK-NEXT:         ^
+// CHECK-NEXT: error: invalid SET instruction, source and size registers are the same
+// CHECK-NEXT:   setm  [x0]!, x1!, x1
+// CHECK-NEXT:         ^
+// CHECK-NEXT: error: invalid SET instruction, source and size registers are the same
+// CHECK-NEXT:   sete  [x0]!, x1!, x1
+// CHECK-NEXT:         ^

A bug was found where mops instructions were being generated that
aliased the source and size registers. This is unpredictable behaviour.
This patch usess the earlyclobber constraint on the input source register
so that it doesn't alias with the size register. Also a test is introduced
which tests affected instructions can't violate this constraint.

Change-Id: I34debad21fe8a5f6c33e159b43a1e13d092764a0
Copy link
Contributor

@Stylie777 Stylie777 left a comment

Choose a reason for hiding this comment

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

Are there any LLVM IR tests that can be added for this? As this is a CodeGen issue we should add these.

@nasherm
Copy link
Contributor Author

nasherm commented Apr 16, 2024

Are there any LLVM IR tests that can be added for this? As this is a CodeGen issue we should add these.

Is the llvm-mc test not enough? My thinking is that an IR test obfuscates what the actual issue is which shows up much later in the compiler backend

@Stylie777
Copy link
Contributor

I think there should also be some LLVM-Lit tests that check the IR CodeGen. The llvm-mc test checks any assembly which the user inputs into the compiler, but if an ACLE Intrinsic is used this can be checked differently within the tests.

The tests can be written in a way that the output is check not to be a certain value/register. See #77770 for an example of how a test can ensure early-clobber was being respected within IR and CodeGen.

@nasherm nasherm force-pushed the master branch 2 times, most recently from 69bb2db to b60f15c Compare April 16, 2024 15:18
Change-Id: Ifc34f656cc4fb33ad8a46ea80b4896f8d0f2ac60
Change-Id: I68edcdf4414d8a21b2267e7593997f0276ad6e54
@nasherm
Copy link
Contributor Author

nasherm commented Apr 17, 2024

I think there should also be some LLVM-Lit tests that check the IR CodeGen. The llvm-mc test checks any assembly which the user inputs into the compiler, but if an ACLE Intrinsic is used this can be checked differently within the tests.

The tests can be written in a way that the output is check not to be a certain value/register. See #77770 for an example of how a test can ensure early-clobber was being respected within IR and CodeGen.

I've written an IR test and updated the PR

Copy link
Contributor

@Stylie777 Stylie777 left a comment

Choose a reason for hiding this comment

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

One more comment from me.

@Stylie777
Copy link
Contributor

Approved - But worth getting a 2nd set of eyes over it.

Copy link
Collaborator

@ostannard ostannard left a comment

Choose a reason for hiding this comment

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

LGTM

@nasherm nasherm merged commit 3e64f8a into llvm:main Apr 18, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:AArch64 mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants