[clang] 2b19c3f - [clang][bytecode] Optimize `CheckStore()` (#241345)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 11 22:13:05 PST 2026


Author: Timm Baeder
Date: 2026-11-12T07:12:59+01:00
New Revision: 2b19c3f3cdb3ea11052abcc09db948f8644c289d

URL: https://github.com/llvm/llvm-project/commit/2b19c3f3cdb3ea11052abcc09db948f8644c289d
DIFF: https://github.com/llvm/llvm-project/commit/2b19c3f3cdb3ea11052abcc09db948f8644c289d.diff

LOG: [clang][bytecode] Optimize `CheckStore()` (#241345)

Block pointers are the only writable pointer type, which is why we're
checking for them very early in `CheckStore`. Make the distinction more
explicit and use the block and the `PtrView` of the pointer explicitly.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.cpp
    clang/lib/AST/ByteCode/Pointer.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 2f4e8ee0fe59b..0377dbe6c130f 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -199,9 +199,9 @@ static void diagnoseNonConstVariable(InterpState &S, CodePtr OpPC,
   S.Note(VD->getLocation(), diag::note_declared_at);
 }
 
-static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
-  if (auto ID = Ptr.getDeclID()) {
-    if (!Ptr.isStatic())
+static bool CheckGlobal(InterpState &S, CodePtr OpPC, const Block *B) {
+  if (auto ID = B->getDeclID()) {
+    if (!B->isGlobal())
       return true;
 
     if (S.P.getCurrentDecl() == ID)
@@ -625,7 +625,7 @@ bool CheckSubobject(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
   return false;
 }
 
-bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
+static bool CheckConst(InterpState &S, CodePtr OpPC, PtrView Ptr) {
   assert(Ptr.isLive() && "Pointer is not live");
   if (!Ptr.isConst())
     return true;
@@ -633,9 +633,6 @@ bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
   if (Ptr.isMutable() && !Ptr.isConstInMutable())
     return true;
 
-  if (!Ptr.isBlockPointer())
-    return false;
-
   // The This pointer is writable in constructors and destructors,
   // even if isConst() returns true.
   for (PtrView V : llvm::reverse(S.InitializingPtrs)) {
@@ -657,7 +654,7 @@ bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
     // that is marked const but NOT in InitializingPtrs. If that is the case,
     // Ptr is currently not writable.
     bool FoundProblem = false;
-    for (PtrView P = Ptr.view(); P != V; P = P.getBase()) {
+    for (PtrView P = Ptr; P != V; P = P.getBase()) {
       if (P.getFieldDesc()->IsConst) {
         FoundProblem = true;
         break;
@@ -682,6 +679,17 @@ bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
   return false;
 }
 
+bool CheckConst(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
+  assert(Ptr.isLive() && "Pointer is not live");
+
+  if (!Ptr.isConst())
+    return true;
+
+  if (!Ptr.isBlockPointer())
+    return false;
+  return CheckConst(S, OpPC, Ptr.view());
+}
+
 bool CheckMutable(InterpState &S, CodePtr OpPC, PtrView Ptr, AccessKinds AK) {
   assert(Ptr.isLive() && "Pointer is not live");
   if (!Ptr.isMutable())
@@ -1082,26 +1090,29 @@ bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
   if (!Ptr.isBlockPointer())
     return false;
 
-  if (!Ptr.block()->isAccessible()) {
+  PtrView P = Ptr.view();
+  const Block *B = Ptr.block();
+
+  if (!B->isAccessible()) {
     if (!CheckLive(S, OpPC, Ptr, AK))
       return false;
     if (!CheckExtern(S, OpPC, Ptr))
       return false;
     return false;
   }
-  if (!WillBeActivated && !CheckLifetime(S, OpPC, Ptr, AK))
+  if (!WillBeActivated && !CheckLifetime(S, OpPC, P.getLifetime(), B, AK))
     return false;
   if (!CheckRange(S, OpPC, Ptr, AK))
     return false;
-  if (!CheckActive(S, OpPC, Ptr, AK, WillBeActivated))
+  if (!CheckActive(S, OpPC, P, AK, WillBeActivated))
     return false;
-  if (!CheckGlobal(S, OpPC, Ptr))
+  if (!CheckGlobal(S, OpPC, B))
     return false;
-  if (!CheckConst(S, OpPC, Ptr))
+  if (!CheckConst(S, OpPC, P))
     return false;
-  if (!CheckVolatile(S, OpPC, Ptr.view(), AK))
+  if (!CheckVolatile(S, OpPC, P, AK))
     return false;
-  if (!CheckMutable(S, OpPC, Ptr.view(), AK))
+  if (!CheckMutable(S, OpPC, P, AK))
     return false;
   return true;
 }
@@ -2870,9 +2881,9 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
 
   if (!CheckRange(S, OpPC, Ptr, AK_Construct))
     return false;
-  if (!CheckGlobal(S, OpPC, Ptr))
+  if (!CheckGlobal(S, OpPC, Ptr.block()))
     return false;
-  if (!CheckConst(S, OpPC, Ptr))
+  if (!CheckConst(S, OpPC, Ptr.view()))
     return false;
 
   const auto *NewExpr = cast<CXXNewExpr>(E);

diff  --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index d9123a7658d10..04aaacd5d9651 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -49,6 +49,9 @@ struct PtrView {
   bool isMutable() const {
     return !isRoot() && getInlineDesc()->IsFieldMutable;
   }
+  bool isConstInMutable() const {
+    return !isRoot() && getInlineDesc()->IsConstInMutable;
+  }
   bool isExtern() const { return Pointee && Pointee->isExtern(); }
   bool isVolatile() const {
     return isRoot() ? getDeclDesc()->IsVolatile : getInlineDesc()->IsVolatile;
@@ -58,8 +61,6 @@ struct PtrView {
   bool inPrimitiveArray() const { return getFieldDesc()->isPrimitiveArray(); }
   const Block *block() const { return Pointee; }
 
-  unsigned getEvalID() { return Pointee->getEvalID(); }
-
   bool isRoot() const { return Base == 0; }
 
   bool isConst() const {
@@ -971,11 +972,6 @@ class Pointer {
       return false;
     return view().isConst();
   }
-  bool isConstInMutable() const {
-    if (!isBlockPointer())
-      return false;
-    return isRoot() ? false : getInlineDesc()->IsConstInMutable;
-  }
 
   /// Checks if an object or a subfield is volatile.
   bool isVolatile() const {


        


More information about the cfe-commits mailing list