[llvm] [GISel] Enforce G_PTR_ADD RHS type matching index size for addr space (PR #84352)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 7 10:08:07 PST 2024


https://github.com/jayfoad created https://github.com/llvm/llvm-project/pull/84352

None

>From 4c7b4b6f1ac4a59b874c9bfde9ad501df6de0ffb Mon Sep 17 00:00:00 2001
From: Jay Foad <jay.foad at amd.com>
Date: Thu, 7 Mar 2024 17:56:54 +0000
Subject: [PATCH] [GISel] Enforce G_PTR_ADD RHS type matching index size for
 addr space

---
 llvm/lib/CodeGen/MachineVerifier.cpp         | 8 ++++++++
 llvm/test/MachineVerifier/test_g_ptr_add.mir | 6 +++++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/CodeGen/MachineVerifier.cpp b/llvm/lib/CodeGen/MachineVerifier.cpp
index 1d0757c5d7f5f5..c29daf7e68a643 100644
--- a/llvm/lib/CodeGen/MachineVerifier.cpp
+++ b/llvm/lib/CodeGen/MachineVerifier.cpp
@@ -1301,6 +1301,14 @@ void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
     if (OffsetTy.isPointerOrPointerVector())
       report("gep offset operand must not be a pointer", MI);
 
+    if (PtrTy.isPointerOrPointerVector()) {
+      const DataLayout &DL = MF->getDataLayout();
+      unsigned AS = PtrTy.getAddressSpace();
+      unsigned IndexSizeInBits = DL.getIndexSize(AS) * 8;
+      if (OffsetTy.getScalarSizeInBits() != IndexSizeInBits)
+        report("gep offset operand must match index size for address space", MI);
+    }
+
     // TODO: Is the offset allowed to be a scalar with a vector?
     break;
   }
diff --git a/llvm/test/MachineVerifier/test_g_ptr_add.mir b/llvm/test/MachineVerifier/test_g_ptr_add.mir
index 07fe6266701d5c..7d1373586c8eb3 100644
--- a/llvm/test/MachineVerifier/test_g_ptr_add.mir
+++ b/llvm/test/MachineVerifier/test_g_ptr_add.mir
@@ -1,4 +1,4 @@
-#RUN: not --crash llc -o - -mtriple=arm64 -run-pass=none -verify-machineinstrs %s 2>&1 | FileCheck %s
+# RUN: not --crash llc -o - -mtriple=arm64 -run-pass=none -verify-machineinstrs %s 2>&1 | FileCheck %s
 # REQUIRES: aarch64-registered-target
 
 ---
@@ -29,4 +29,8 @@ body:             |
     ; CHECK: Bad machine code: gep first operand must be a pointer
     %6:_(s64) = G_PTR_ADD %1, %1
 
+    %7:_(s32) = G_IMPLICIT_DEF
+
+    ; CHECK: Bad machine code: gep offset operand must match index size for address space
+    %8:_(p0) = G_PTR_ADD %0, %7
 ...



More information about the llvm-commits mailing list