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

[ARM] Validate STREX instruction in the assembler #85074

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

adamszilagyi
Copy link

@adamszilagyi adamszilagyi commented Mar 13, 2024

Based on the ARM Architecture Reference Manual:

Use of R15 Specifying R15 for register <Rd>, <Rn>, or <Rm> has UNPREDICTABLE results.
Operand restrictions <Rd> must be distinct from both <Rm> and <Rn>, otherwise the results are UNPREDICTABLE.

Altough in codegen it is not possible to end up with a strex instruction with same register as destination and source, because the regallocator is restricted by the instructions definion where the destination register is marked as @earlyclobber, with inline asm it is possible to overcome this and write a code like the following

__attribute__((naked)) void foo(void) { __asm__ volatile("strex r1, r2, [r1]"); }

which previously went through the assembler whitout any diagnostic that the behaviour of the strex is unpredictable in this case.

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 llvmbot added backend:ARM mc Machine (object) code labels Mar 13, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 13, 2024

@llvm/pr-subscribers-backend-arm

@llvm/pr-subscribers-mc

Author: None (adamszilagyi)

Changes

Based on the ARM Architecture Reference Manual:

**Use of R15** Specifying R15 for register &lt;Rd&gt;, &lt;Rn&gt;, or &lt;Rm&gt; has UNPREDICTABLE results.
**Operand restrictions** &lt;Rd&gt; must be distinct from both &lt;Rm&gt; and &lt;Rn&gt;, otherwise the results are UNPREDICTABLE.

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

2 Files Affected:

  • (modified) llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp (+33)
  • (modified) llvm/test/MC/ARM/diagnostics.s (+52)
diff --git a/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp b/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
index c320bf723c88bb..8c96cce7a5cf55 100644
--- a/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
+++ b/llvm/lib/Target/ARM/AsmParser/ARMAsmParser.cpp
@@ -7629,6 +7629,39 @@ bool ARMAsmParser::validateInstruction(MCInst &Inst,
     }
     return false;
   }
+  case ARM::t2STREXB:
+  case ARM::t2STREXH:
+  case ARM::t2STREX:
+  case ARM::STREXB:
+  case ARM::STREXH:
+  case ARM::STREX:
+  case ARM::STREXD: {
+    unsigned Rd = MRI->getEncodingValue(Inst.getOperand(0).getReg());
+    unsigned Rm = MRI->getEncodingValue(Inst.getOperand(1).getReg());
+    unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());
+
+    if (Rd == 15 || Rm == 15 || Rn == 15)
+      return Error(Operands[3]->getStartLoc(), "operand can't be R15");
+
+    if (Rd == Rm || Rd == Rn || (Opcode == ARM::STREXD && Rd == Rm + 1))
+      return Error(Operands[3]->getStartLoc(),
+                   "destination operand can't be identical to source operand");
+    return false;
+  }
+  case ARM::t2STREXD: {
+    unsigned Rd = MRI->getEncodingValue(Inst.getOperand(0).getReg());
+    unsigned Rm1 = MRI->getEncodingValue(Inst.getOperand(1).getReg());
+    unsigned Rm2 = MRI->getEncodingValue(Inst.getOperand(2).getReg());
+    unsigned Rn = MRI->getEncodingValue(Inst.getOperand(3).getReg());
+
+    if (Rd == 15 || Rn == 15)
+      return Error(Operands[4]->getStartLoc(), "operand can't be R15");
+
+    if (Rd == Rm1 || Rd == Rm2 || Rd == Rn)
+      return Error(Operands[4]->getStartLoc(),
+                   "destination operand can't be identical to source operand");
+    return false;
+  }
   case ARM::t2IT: {
     // Encoding is unpredictable if it ever results in a notional 'NV'
     // predicate. Since we don't parse 'NV' directly this means an 'AL'
diff --git a/llvm/test/MC/ARM/diagnostics.s b/llvm/test/MC/ARM/diagnostics.s
index e6d80ea7a6280c..f29be4e3bafcc8 100644
--- a/llvm/test/MC/ARM/diagnostics.s
+++ b/llvm/test/MC/ARM/diagnostics.s
@@ -336,6 +336,58 @@
 @ CHECK-ERRORS:         strexd  r6, r5, r3, [r8]
 @ CHECK-ERRORS:                         ^
 
+        @ Invalid Rm/Rn operands for strex
+        strex r1, r2, [r1]
+        strex r1, r1, [r2]
+
+@ CHECK-ERRORS: error: destination operand can't be identical to source operand
+@ CHECK-ERRORS:         strex r1, r2, [r1]
+@ CHECK-ERRORS:                     ^
+@ CHECK-ERRORS: error: destination operand can't be identical to source operand
+@ CHECK-ERRORS:         strex r1, r1, [r2]
+@ CHECK-ERRORS:                     ^
+
+        @ Can not use R15 as operand for strex
+        strex r15, r2, [r1]
+        strex r1, r15, [r2]
+        strex r1, r2, [r15]
+
+@ CHECK-ERRORS: error: operand can't be R15
+@ CHECK-ERRORS:         strex r15, r2, [r1]
+@ CHECK-ERRORS:                    ^
+@ CHECK-ERRORS: error: operand can't be R15
+@ CHECK-ERRORS:         strex r1, r15, [r2]
+@ CHECK-ERRORS:                   ^
+@ CHECK-ERRORS: error: operand can't be R15
+@ CHECK-ERRORS:         strex r1, r2, [r15]
+@ CHECK-ERRORS:                   ^
+
+        @ Can not use R15 as operand for strexd
+        strexd r15, r0, r1, [r6]
+        strexd r2, r0, r1, [r15]
+
+@ CHECK-ERRORS: error: operand can't be R15
+@ CHECK-ERRORS:         strexd r15, r0, r1, [r6]
+@ CHECK-ERRORS:                     ^
+@ CHECK-ERRORS: error: operand can't be R15
+@ CHECK-ERRORS:         strexd r2, r0, r1, [r15]
+@ CHECK-ERRORS:                    ^
+
+        @ Invalid Rm/Rn operands for strexd
+        strexd r1, r0, r1, [r6]
+        strexd r2, r2, r3, [r6]
+        strexd r4, r0, r1, [r4]
+
+@ CHECK-ERRORS: error: destination operand can't be identical to source operand
+@ CHECK-ERRORS:         strexd r1, r0, r1, [r6]
+@ CHECK-ERRORS:                    ^
+@ CHECK-ERRORS: error: destination operand can't be identical to source operand
+@ CHECK-ERRORS:         strexd r2, r2, r3, [r6]
+@ CHECK-ERRORS:                    ^
+@ CHECK-ERRORS: error: destination operand can't be identical to source operand
+@ CHECK-ERRORS:         strexd r4, r0, r1, [r4]
+@ CHECK-ERRORS:                        ^
+
         @ Illegal rotate operators for extend instructions
         sxtb r8, r3, #8
         sxtb r8, r3, ror 24


@ CHECK-ERRORS: error: operand can't be R15
@ CHECK-ERRORS: strex r15, r2, [r1]
@ CHECK-ERRORS: ^
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better if this pointed to the problematic operand (same for the cases below too). Especially if someone writes pc rather than r15, the error message would be confusing.

unsigned Rn = MRI->getEncodingValue(Inst.getOperand(2).getReg());

if (Rd == 15 || Rm == 15 || Rn == 15)
return Error(Operands[3]->getStartLoc(), "operand can't be R15");
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we handle other cases of UNPREDICTABLE, but it seems like these should be warnings rather than errors, since it is possible to actually emit the encoding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:ARM mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants