Skip to content

Conversation

@tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Oct 5, 2024

Checking the decl for every load is rather expensive.

Checking the decl for every load is rather expensive.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 5, 2024

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Checking the decl for every load is rather expensive.


Full diff: https://github.com/llvm/llvm-project/pull/111248.diff

5 Files Affected:

  • (modified) clang/lib/AST/ByteCode/InterpBlock.cpp (+2-2)
  • (modified) clang/lib/AST/ByteCode/InterpBlock.h (+9-6)
  • (modified) clang/lib/AST/ByteCode/Pointer.h (+1-3)
  • (modified) clang/lib/AST/ByteCode/Program.cpp (+10-6)
  • (modified) clang/lib/AST/ByteCode/Program.h (+1-1)
diff --git a/clang/lib/AST/ByteCode/InterpBlock.cpp b/clang/lib/AST/ByteCode/InterpBlock.cpp
index 0ce88ca7e52365..9ef44cd29ff875 100644
--- a/clang/lib/AST/ByteCode/InterpBlock.cpp
+++ b/clang/lib/AST/ByteCode/InterpBlock.cpp
@@ -100,8 +100,8 @@ bool Block::hasPointer(const Pointer *P) const {
 #endif
 
 DeadBlock::DeadBlock(DeadBlock *&Root, Block *Blk)
-    : Root(Root),
-      B(~0u, Blk->Desc, Blk->IsStatic, Blk->IsExtern, /*isDead=*/true) {
+    : Root(Root), B(~0u, Blk->Desc, Blk->IsStatic, Blk->IsExtern, Blk->IsWeak,
+                    /*isDead=*/true) {
   // Add the block to the chain of dead blocks.
   if (Root)
     Root->Prev = this;
diff --git a/clang/lib/AST/ByteCode/InterpBlock.h b/clang/lib/AST/ByteCode/InterpBlock.h
index a5cd58e3a655a0..985e4c152191c6 100644
--- a/clang/lib/AST/ByteCode/InterpBlock.h
+++ b/clang/lib/AST/ByteCode/InterpBlock.h
@@ -50,16 +50,17 @@ class Block final {
 public:
   /// Creates a new block.
   Block(unsigned EvalID, const std::optional<unsigned> &DeclID,
-        const Descriptor *Desc, bool IsStatic = false, bool IsExtern = false)
+        const Descriptor *Desc, bool IsStatic = false, bool IsExtern = false,
+        bool IsWeak = false)
       : EvalID(EvalID), DeclID(DeclID), IsStatic(IsStatic), IsExtern(IsExtern),
-        IsDynamic(false), Desc(Desc) {
+        IsDynamic(false), IsWeak(IsWeak), Desc(Desc) {
     assert(Desc);
   }
 
   Block(unsigned EvalID, const Descriptor *Desc, bool IsStatic = false,
-        bool IsExtern = false)
+        bool IsExtern = false, bool IsWeak = false)
       : EvalID(EvalID), DeclID((unsigned)-1), IsStatic(IsStatic),
-        IsExtern(IsExtern), IsDynamic(false), Desc(Desc) {
+        IsExtern(IsExtern), IsDynamic(false), IsWeak(IsWeak), Desc(Desc) {
     assert(Desc);
   }
 
@@ -73,6 +74,7 @@ class Block final {
   bool isStatic() const { return IsStatic; }
   /// Checks if the block is temporary.
   bool isTemporary() const { return Desc->IsTemporary; }
+  bool isWeak() const { return IsWeak; }
   bool isDynamic() const { return IsDynamic; }
   /// Returns the size of the block.
   unsigned getSize() const { return Desc->getAllocSize(); }
@@ -135,9 +137,9 @@ class Block final {
   friend class DynamicAllocator;
 
   Block(unsigned EvalID, const Descriptor *Desc, bool IsExtern, bool IsStatic,
-        bool IsDead)
+        bool IsWeak, bool IsDead)
       : EvalID(EvalID), IsStatic(IsStatic), IsExtern(IsExtern), IsDead(true),
-        IsDynamic(false), Desc(Desc) {
+        IsDynamic(false), IsWeak(IsWeak), Desc(Desc) {
     assert(Desc);
   }
 
@@ -170,6 +172,7 @@ class Block final {
   /// Flag indicating if this block has been allocated via dynamic
   /// memory allocation (e.g. malloc).
   bool IsDynamic = false;
+  bool IsWeak = false;
   /// Pointer to the stack slot descriptor.
   const Descriptor *Desc;
 };
diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index 08bc4b7e40b636..72e255dba13f6b 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -524,9 +524,7 @@ class Pointer {
       return false;
 
     assert(isBlockPointer());
-    if (const ValueDecl *VD = getDeclDesc()->asValueDecl())
-      return VD->isWeak();
-    return false;
+    return asBlockPointer().Pointee->isWeak();
   }
   /// Checks if an object was initialized.
   bool isInitialized() const;
diff --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp
index 79c645257306e0..969f523db51dfe 100644
--- a/clang/lib/AST/ByteCode/Program.cpp
+++ b/clang/lib/AST/ByteCode/Program.cpp
@@ -152,10 +152,12 @@ std::optional<unsigned> Program::getOrCreateDummy(const DeclTy &D) {
     return It->second;
 
   QualType QT;
+  bool IsWeak = false;
   if (const auto *E = D.dyn_cast<const Expr *>()) {
     QT = E->getType();
   } else {
     const ValueDecl *VD = cast<ValueDecl>(D.get<const Decl *>());
+    IsWeak = VD->isWeak();
     QT = VD->getType();
     if (const auto *RT = QT->getAs<ReferenceType>())
       QT = RT->getPointeeType();
@@ -182,7 +184,7 @@ std::optional<unsigned> Program::getOrCreateDummy(const DeclTy &D) {
 
   auto *G = new (Allocator, Desc->getAllocSize())
       Global(Ctx.getEvalID(), getCurrentDecl(), Desc, /*IsStatic=*/true,
-             /*IsExtern=*/false);
+             /*IsExtern=*/false, IsWeak);
   G->block()->invokeCtor();
 
   Globals.push_back(G);
@@ -193,6 +195,7 @@ std::optional<unsigned> Program::getOrCreateDummy(const DeclTy &D) {
 std::optional<unsigned> Program::createGlobal(const ValueDecl *VD,
                                               const Expr *Init) {
   bool IsStatic, IsExtern;
+  bool IsWeak = VD->isWeak();
   if (const auto *Var = dyn_cast<VarDecl>(VD)) {
     IsStatic = Context::shouldBeGloballyIndexed(VD);
     IsExtern = Var->hasExternalStorage();
@@ -207,7 +210,8 @@ std::optional<unsigned> Program::createGlobal(const ValueDecl *VD,
 
   // Register all previous declarations as well. For extern blocks, just replace
   // the index with the new variable.
-  if (auto Idx = createGlobal(VD, VD->getType(), IsStatic, IsExtern, Init)) {
+  if (auto Idx =
+          createGlobal(VD, VD->getType(), IsStatic, IsExtern, IsWeak, Init)) {
     for (const Decl *P = VD; P; P = P->getPreviousDecl()) {
       if (P != VD) {
         unsigned PIdx = GlobalIndices[P];
@@ -225,7 +229,7 @@ std::optional<unsigned> Program::createGlobal(const Expr *E) {
   if (auto Idx = getGlobal(E))
     return Idx;
   if (auto Idx = createGlobal(E, E->getType(), /*isStatic=*/true,
-                              /*isExtern=*/false)) {
+                              /*isExtern=*/false, /*IsWeak=*/false)) {
     GlobalIndices[E] = *Idx;
     return *Idx;
   }
@@ -234,7 +238,7 @@ std::optional<unsigned> Program::createGlobal(const Expr *E) {
 
 std::optional<unsigned> Program::createGlobal(const DeclTy &D, QualType Ty,
                                               bool IsStatic, bool IsExtern,
-                                              const Expr *Init) {
+                                              bool IsWeak, const Expr *Init) {
   // Create a descriptor for the global.
   Descriptor *Desc;
   const bool IsConst = Ty.isConstQualified();
@@ -251,8 +255,8 @@ std::optional<unsigned> Program::createGlobal(const DeclTy &D, QualType Ty,
   // Allocate a block for storage.
   unsigned I = Globals.size();
 
-  auto *G = new (Allocator, Desc->getAllocSize())
-      Global(Ctx.getEvalID(), getCurrentDecl(), Desc, IsStatic, IsExtern);
+  auto *G = new (Allocator, Desc->getAllocSize()) Global(
+      Ctx.getEvalID(), getCurrentDecl(), Desc, IsStatic, IsExtern, IsWeak);
   G->block()->invokeCtor();
 
   // Initialize InlineDescriptor fields.
diff --git a/clang/lib/AST/ByteCode/Program.h b/clang/lib/AST/ByteCode/Program.h
index bd2672a762b82a..be84c40714a60b 100644
--- a/clang/lib/AST/ByteCode/Program.h
+++ b/clang/lib/AST/ByteCode/Program.h
@@ -152,7 +152,7 @@ class Program final {
 
   std::optional<unsigned> createGlobal(const DeclTy &D, QualType Ty,
                                        bool IsStatic, bool IsExtern,
-                                       const Expr *Init = nullptr);
+                                       bool IsWeak, const Expr *Init = nullptr);
 
   /// Reference to the VM context.
   Context &Ctx;

@tbaederr tbaederr merged commit 4e5f8a8 into llvm:main Oct 5, 2024
10 of 12 checks passed
Kyvangka1610 pushed a commit to Kyvangka1610/llvm-project that referenced this pull request Oct 5, 2024
Checking the decl for every load is rather expensive.
Kyvangka1610 added a commit to Kyvangka1610/llvm-project that referenced this pull request Oct 5, 2024
* commit 'FETCH_HEAD':
  [clang][bytecode] Handle UETT_OpenMPRequiredSimdAlign (llvm#111259)
  [mlir][polynomial] Add and verify constraints of coefficientModulus for ringAttr (llvm#111016)
  [clang][bytecode] Save a per-Block IsWeak bit (llvm#111248)
  [analyzer] Fix wrong `builtin_*_overflow` return type (llvm#111253)
  [SelectOpt] Don't convert constant selects to branches. (llvm#110858)
  [InstCombine] Update and-or-icmps.ll after 574266c. NFC
  [Instcombine] Test for more gep canonicalization
  [NFC][TableGen] Change `CodeGenIntrinsics` to use const references (llvm#111219)
  Add warning message to `session save` when transcript isn't saved. (llvm#109020)
  [RISCV][TTI] Recognize CONCAT_VECTORS if a shufflevector mask is multiple insert subvector. (llvm#110457)
  Revert "[InstCombine] Folding `(icmp eq/ne (and X, -P2), INT_MIN)`" (llvm#111236)
  [NFC][lsan] Add SuspendAllThreads traces
  [lsan] Add `thread_suspend_fail` flag

Signed-off-by: kyvangka1610 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants