[clang] [clang][bytecode] Remove unnecessary pointer type checks in CheckLoad/CheckStore (PR #224884)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 19 22:08:47 PDT 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/224884
Only check the pointer type once.
>From 0f757388e89f5fe196542b6d099cfddcec2651df Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 19 Sep 2026 19:22:32 +0200
Subject: [PATCH] checkload/store
---
clang/lib/AST/ByteCode/Interp.cpp | 42 ++++++++++++++++++-------------
clang/lib/AST/ByteCode/Pointer.h | 5 +++-
2 files changed, 28 insertions(+), 19 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index ab6b474503a3b..1761b65afd911 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -729,7 +729,7 @@ bool CheckMutable(InterpState &S, CodePtr OpPC, PtrView Ptr, AccessKinds AK) {
return false;
}
-static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
+static bool CheckVolatile(InterpState &S, CodePtr OpPC, PtrView Ptr,
AccessKinds AK) {
assert(Ptr.isLive());
@@ -745,7 +745,7 @@ static bool CheckVolatile(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
// The reason why Ptr is volatile might be further up the hierarchy.
// Find that pointer.
- Pointer P = Ptr;
+ PtrView P = Ptr;
while (!P.isRoot()) {
if (P.getType().isVolatileQualified())
break;
@@ -960,15 +960,18 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
return false;
if (!Ptr.isInitialized())
return diagnoseUninitialized(S, OpPC, Ptr, AK);
- if (!CheckLifetime(S, OpPC, Ptr, AK))
- return false;
- if (Ptr.isBlockPointer() && !CheckTemporary(S, OpPC, Ptr.block(), AK))
- return false;
- if (!CheckMutable(S, OpPC, Ptr))
- return false;
- if (!CheckVolatile(S, OpPC, Ptr, AK))
- return false;
+ if (Ptr.isBlockPointer()) {
+ if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), Ptr.block(), AK))
+ return false;
+ if (!CheckTemporary(S, OpPC, Ptr.block(), AK))
+ return false;
+
+ if (!CheckMutable(S, OpPC, Ptr.view(), AK))
+ return false;
+ if (!CheckVolatile(S, OpPC, Ptr.view(), AK))
+ return false;
+ }
if (isConstexprUnknown(Ptr))
return false;
@@ -1022,14 +1025,17 @@ bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (!CheckActive(S, OpPC, Ptr, AK_Read))
return false;
- if (!CheckLifetime(S, OpPC, Ptr, AK_Read))
- return false;
if (!Ptr.isInitialized())
return diagnoseUninitialized(S, OpPC, Ptr, AK_Read);
- if (Ptr.isBlockPointer() && !CheckTemporary(S, OpPC, Ptr.block(), AK_Read))
- return false;
- if (!CheckMutable(S, OpPC, Ptr))
- return false;
+
+ if (Ptr.isBlockPointer()) {
+ if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), Ptr.block(), AK_Read))
+ return false;
+ if (!CheckTemporary(S, OpPC, Ptr.block(), AK_Read))
+ return false;
+ if (!CheckMutable(S, OpPC, Ptr.view()))
+ return false;
+ }
if (Ptr.isConstexprUnknown())
return false;
return true;
@@ -1061,9 +1067,9 @@ bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
return false;
if (!CheckConst(S, OpPC, Ptr))
return false;
- if (!CheckVolatile(S, OpPC, Ptr, AK))
+ if (!CheckVolatile(S, OpPC, Ptr.view(), AK))
return false;
- if (!CheckMutable(S, OpPC, Ptr, AK))
+ if (!CheckMutable(S, OpPC, Ptr.view(), AK))
return false;
if (isConstexprUnknown(Ptr))
return false;
diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index d19374acf00c4..54e0f858b4fa0 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 isVolatile() const {
+ return isRoot() ? getDeclDesc()->IsVolatile : getInlineDesc()->IsVolatile;
+ }
bool inUnion() const { return getInlineDesc()->InUnion; };
bool inArray() const { return getFieldDesc()->IsArray; }
bool inPrimitiveArray() const { return getFieldDesc()->isPrimitiveArray(); }
@@ -954,7 +957,7 @@ class Pointer {
bool isVolatile() const {
if (!isBlockPointer())
return false;
- return isRoot() ? getDeclDesc()->IsVolatile : getInlineDesc()->IsVolatile;
+ return view().isVolatile();
}
/// Returns the declaration ID.
More information about the cfe-commits
mailing list