Skip to content

[clang][bytecode][NFC] Only collect non-null args if we have to #152074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2025

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Aug 5, 2025

Only do this if the function really has a NonNullArg.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:bytecode Issues for the clang bytecode constexpr interpreter labels Aug 5, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 5, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Only do this if the function really has a NonNullArg.


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

2 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Compiler.cpp (+7-2)
  • (modified) clang/lib/AST/ByteCode/InterpShared.cpp (+9-9)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 7a5bd4dfc0bed..ee9c69bf2748e 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2038,7 +2038,12 @@ bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
                                       const FunctionDecl *FuncDecl,
                                       bool Activate) {
   assert(VarScope->getKind() == ScopeKind::Call);
-  llvm::BitVector NonNullArgs = collectNonNullArgs(FuncDecl, Args);
+  bool HasNonNullAttr = false;
+  llvm::BitVector NonNullArgs;
+  if (FuncDecl && FuncDecl->hasAttr<NonNullAttr>()) {
+    HasNonNullAttr = true;
+    NonNullArgs = collectNonNullArgs(FuncDecl, Args);
+  }
 
   unsigned ArgIndex = 0;
   for (const Expr *Arg : Args) {
@@ -2064,7 +2069,7 @@ bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
         return false;
     }
 
-    if (FuncDecl && NonNullArgs[ArgIndex]) {
+    if (FuncDecl && HasNonNullAttr && NonNullArgs[ArgIndex]) {
       PrimType ArgT = classify(Arg).value_or(PT_Ptr);
       if (ArgT == PT_Ptr) {
         if (!this->emitCheckNonNullArg(ArgT, Arg))
diff --git a/clang/lib/AST/ByteCode/InterpShared.cpp b/clang/lib/AST/ByteCode/InterpShared.cpp
index 1e94dc19d03c1..e01b32bc63a2a 100644
--- a/clang/lib/AST/ByteCode/InterpShared.cpp
+++ b/clang/lib/AST/ByteCode/InterpShared.cpp
@@ -16,23 +16,23 @@ namespace interp {
 llvm::BitVector collectNonNullArgs(const FunctionDecl *F,
                                    ArrayRef<const Expr *> Args) {
   llvm::BitVector NonNullArgs;
-  if (!F)
-    return NonNullArgs;
 
   assert(F);
+  assert(F->hasAttr<NonNullAttr>());
   NonNullArgs.resize(Args.size());
 
   for (const auto *Attr : F->specific_attrs<NonNullAttr>()) {
     if (!Attr->args_size()) {
       NonNullArgs.set();
       break;
-    } else
-      for (auto Idx : Attr->args()) {
-        unsigned ASTIdx = Idx.getASTIndex();
-        if (ASTIdx >= Args.size())
-          continue;
-        NonNullArgs[ASTIdx] = true;
-      }
+    }
+
+    for (auto Idx : Attr->args()) {
+      unsigned ASTIdx = Idx.getASTIndex();
+      if (ASTIdx >= Args.size())
+        continue;
+      NonNullArgs[ASTIdx] = true;
+    }
   }
 
   return NonNullArgs;

Only do this if the function really has a NonNullArg.
@tbaederr tbaederr merged commit df3f629 into llvm:main Aug 5, 2025
9 checks passed
@@ -2064,7 +2069,7 @@ bool Compiler<Emitter>::visitCallArgs(ArrayRef<const Expr *> Args,
return false;
}

if (FuncDecl && NonNullArgs[ArgIndex]) {
if (HasNonNullAttr && NonNullArgs[ArgIndex]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just use !NonNullArgs.empty() rather than have a flag boolean?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that should work too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:bytecode Issues for the clang bytecode constexpr interpreter 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.

3 participants