[llvm] 95e49f5 - Make `prefetchit{0/1}` emit an assembler warning if the operand is not rip-rel

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 31 23:26:44 PST 2023


Author: Noah Goldstein
Date: 2023-02-01T01:26:06-06:00
New Revision: 95e49f5a74c9e79778a62cc58b15875613cf9e59

URL: https://github.com/llvm/llvm-project/commit/95e49f5a74c9e79778a62cc58b15875613cf9e59
DIFF: https://github.com/llvm/llvm-project/commit/95e49f5a74c9e79778a62cc58b15875613cf9e59.diff

LOG: Make `prefetchit{0/1}` emit an assembler warning if the operand is not rip-rel

Without a rip-rel operand, `prefetchit{0/1}` is a nop. This is a
reasonable mistake for someone to make and is almost certainly not
what they are after.

This matches the same warning in `gas`.

Reviewed By: pengfei

Differential Revision: https://reviews.llvm.org/D142797

Added: 
    llvm/test/MC/X86/prefetchit-non-rip-op.s

Modified: 
    llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
index f0e18cc5ef033..5409b112e7440 100644
--- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -3923,6 +3923,15 @@ bool X86AsmParser::validateInstruction(MCInst &Inst, const OperandVector &Ops) {
     }
   }
 
+  if ((Opcode == X86::PREFETCHIT0 || Opcode == X86::PREFETCHIT1)) {
+    const MCOperand &MO = Inst.getOperand(X86::AddrBaseReg);
+    if (!MO.isReg() || MO.getReg() != X86::RIP)
+      return Warning(
+          Ops[0]->getStartLoc(),
+          Twine((Inst.getOpcode() == X86::PREFETCHIT0 ? "'prefetchit0'"
+                                                      : "'prefetchit1'")) +
+              " only supports RIP-relative address");
+  }
   return false;
 }
 

diff  --git a/llvm/test/MC/X86/prefetchit-non-rip-op.s b/llvm/test/MC/X86/prefetchit-non-rip-op.s
new file mode 100644
index 0000000000000..35d7bcbc30a24
--- /dev/null
+++ b/llvm/test/MC/X86/prefetchit-non-rip-op.s
@@ -0,0 +1,67 @@
+// RUN: llvm-mc -triple x86_64-unknown-unknown %s > %t 2> %t.err
+// RUN: FileCheck --check-prefix=CHECK-STDERR < %t.err %s
+// RUN: FileCheck < %t %s
+
+// CHECK: prefetchit0 (%rdi)
+// CHECK-STDERR: warning: 'prefetchit0' only supports RIP-relative address
+          prefetchit0 (%rdi)
+
+// CHECK: prefetchit1 (%rcx)
+// CHECK-STDERR: warning: 'prefetchit1' only supports RIP-relative address
+          prefetchit1 (%rcx)
+
+// CHECK: prefetchit0 1(%rdx)
+// CHECK-STDERR: warning: 'prefetchit0' only supports RIP-relative address
+          prefetchit0 1(%rdx)
+
+// CHECK: prefetchit1 12(%rsi)
+// CHECK-STDERR: warning: 'prefetchit1' only supports RIP-relative address
+          prefetchit1 12(%rsi)
+
+// CHECK: prefetchit0 123(%r8,%rax)
+// CHECK-STDERR: warning: 'prefetchit0' only supports RIP-relative address
+          prefetchit0 123(%r8,%rax)
+
+// CHECK: prefetchit1 1234(%r9,%r10)
+// CHECK-STDERR: warning: 'prefetchit1' only supports RIP-relative address
+          prefetchit1 1234(%r9,%r10)
+
+// CHECK: prefetchit0 (%r11,%r12)
+// CHECK-STDERR: warning: 'prefetchit0' only supports RIP-relative address
+          prefetchit0 (%r11,%r12)
+
+// CHECK: prefetchit1 (%r13,%r14)
+// CHECK-STDERR: warning: 'prefetchit1' only supports RIP-relative address
+          prefetchit1 (%r13,%r14)
+
+// CHECK: prefetchit0 987(%rsp,%r15,4)
+// CHECK-STDERR: warning: 'prefetchit0' only supports RIP-relative address
+          prefetchit0 987(%rsp,%r15,4)
+
+// CHECK: prefetchit1 -1(%rbp,%rdi,8)
+// CHECK-STDERR: warning: 'prefetchit1' only supports RIP-relative address
+          prefetchit1 -1(%rbp,%rdi,8)
+
+// CHECK: prefetchit0 (%rsp,%rsi,2)
+// CHECK-STDERR: warning: 'prefetchit0' only supports RIP-relative address
+          prefetchit0 (%rsp,%rsi,2)
+
+// CHECK: prefetchit1 (%rdi,%r15,4)
+// CHECK-STDERR: warning: 'prefetchit1' only supports RIP-relative address
+          prefetchit1 (%rdi,%r15,4)
+
+// CHECK: prefetchit0 80(,%r14,8)
+// CHECK-STDERR: warning: 'prefetchit0' only supports RIP-relative address
+          prefetchit0 80(,%r14,8)
+
+// CHECK: prefetchit1 3(,%r8,4)
+// CHECK-STDERR: warning: 'prefetchit1' only supports RIP-relative address
+          prefetchit1 3(,%r8,4)
+
+// CHECK: prefetchit0 (,%rax,2)
+// CHECK-STDERR: warning: 'prefetchit0' only supports RIP-relative address
+          prefetchit0 (,%rax,2)
+
+// CHECK: prefetchit1 (,%rcx,8)
+// CHECK-STDERR: warning: 'prefetchit1' only supports RIP-relative address
+          prefetchit1 (,%rcx,8)


        


More information about the llvm-commits mailing list