https://github.com/tbaederr created 
https://github.com/llvm/llvm-project/pull/137128

And only convert to the target type after that.

>From 00823f8e58ee1b1e8e5660f5e727d9abc72feecf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com>
Date: Thu, 24 Apr 2025 08:16:05 +0200
Subject: [PATCH] [clang][bytecode] Compute pointer differences as 64bit
 integers

And only convert to the target type after that.
---
 clang/lib/AST/ByteCode/Interp.h  | 26 ++++++++++++++++----------
 clang/test/AST/ByteCode/i686.cpp | 14 ++++++++++++++
 2 files changed, 30 insertions(+), 10 deletions(-)
 create mode 100644 clang/test/AST/ByteCode/i686.cpp

diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 588e0502fa88c..99b032bee9e3d 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2161,16 +2161,22 @@ inline bool SubPtr(InterpState &S, CodePtr OpPC) {
     }
   }
 
-  T A = LHS.isBlockPointer()
-            ? (LHS.isElementPastEnd() ? T::from(LHS.getNumElems())
-                                      : T::from(LHS.getIndex()))
-            : T::from(LHS.getIntegerRepresentation());
-  T B = RHS.isBlockPointer()
-            ? (RHS.isElementPastEnd() ? T::from(RHS.getNumElems())
-                                      : T::from(RHS.getIndex()))
-            : T::from(RHS.getIntegerRepresentation());
-
-  return AddSubMulHelper<T, T::sub, std::minus>(S, OpPC, A.bitWidth(), A, B);
+  int64_t A64 =
+      LHS.isBlockPointer()
+          ? (LHS.isElementPastEnd() ? LHS.getNumElems() : LHS.getIndex())
+          : LHS.getIntegerRepresentation();
+
+  int64_t B64 =
+      RHS.isBlockPointer()
+          ? (RHS.isElementPastEnd() ? RHS.getNumElems() : RHS.getIndex())
+          : RHS.getIntegerRepresentation();
+
+  int64_t R64 = A64 - B64;
+  if (static_cast<int64_t>(T::from(R64)) != R64)
+    return handleOverflow(S, OpPC, R64);
+
+  S.Stk.push<T>(T::from(R64));
+  return true;
 }
 
 
//===----------------------------------------------------------------------===//
diff --git a/clang/test/AST/ByteCode/i686.cpp b/clang/test/AST/ByteCode/i686.cpp
new file mode 100644
index 0000000000000..ad914203d3c4b
--- /dev/null
+++ b/clang/test/AST/ByteCode/i686.cpp
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -triple i686-pc-linux-gnu 
-fexperimental-new-constant-interpreter -verify=expected,both %s
+// RUN: %clang_cc1 -triple i686-pc-linux-gnu                                   
      -verify=ref,both      %s
+
+
+char melchizedek[2200000000];
+typedef decltype(melchizedek[1] - melchizedek[0]) ptrdiff_t;
+constexpr ptrdiff_t d1 = &melchizedek[0x7fffffff] - &melchizedek[0];
+constexpr ptrdiff_t d2 = &melchizedek[0x80000000u] - &melchizedek[0]; // 
both-error {{constant expression}} \
+                                                                      // 
both-note {{ 2147483648 }}
+constexpr ptrdiff_t d3 = &melchizedek[0] - &melchizedek[0x80000000u];
+constexpr ptrdiff_t d4 = &melchizedek[0] - &melchizedek[0x80000001u]; // 
both-error {{constant expression}} \
+                                                                      // 
both-note {{ -2147483649 }}
+
+

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to