Skip to content

[clang][bytecode] Make some builtins no-ops #141952

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
May 29, 2025
Merged

Conversation

tbaederr
Copy link
Contributor

For some builtins, we dont' need to do anything, but due to the cleanup code being the same for all builtins, we still had to duplicate the value on the stack. Remove that and get rid of all the unnecessary pressure on the InterpStack.

@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 May 29, 2025
@llvmbot
Copy link
Member

llvmbot commented May 29, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

For some builtins, we dont' need to do anything, but due to the cleanup code being the same for all builtins, we still had to duplicate the value on the stack. Remove that and get rid of all the unnecessary pressure on the InterpStack.


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

1 Files Affected:

  • (modified) clang/lib/AST/ByteCode/InterpBuiltin.cpp (+25-21)
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 8edc6248dcbfd..ba38fc505e3b4 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -23,6 +23,24 @@
 namespace clang {
 namespace interp {
 
+static bool isNoopBuiltin(unsigned ID) {
+  switch (ID) {
+  case Builtin::BIas_const:
+  case Builtin::BIforward:
+  case Builtin::BIforward_like:
+  case Builtin::BImove:
+  case Builtin::BImove_if_noexcept:
+  case Builtin::BIaddressof:
+  case Builtin::BI__addressof:
+  case Builtin::BI__builtin_addressof:
+  case Builtin::BI__builtin_launder:
+    return true;
+  default:
+    return false;
+  }
+  return false;
+}
+
 static unsigned callArgSize(const InterpState &S, const CallExpr *C) {
   unsigned O = 0;
 
@@ -100,6 +118,8 @@ static bool retBI(InterpState &S, const CallExpr *Call, unsigned BuiltinID) {
 static bool retPrimValue(InterpState &S, CodePtr OpPC,
                          std::optional<PrimType> &T, const CallExpr *Call,
                          unsigned BuiltinID) {
+  if (isNoopBuiltin(BuiltinID))
+    return true;
 
   if (!T) {
     if (!Context::isUnevaluatedBuiltin(BuiltinID)) {
@@ -742,24 +762,14 @@ static bool interp__builtin_addressof(InterpState &S, CodePtr OpPC,
                                       const CallExpr *Call) {
   assert(Call->getArg(0)->isLValue());
   PrimType PtrT = S.getContext().classify(Call->getArg(0)).value_or(PT_Ptr);
-
-  if (PtrT == PT_Ptr) {
-    const Pointer &Arg = S.Stk.peek<Pointer>();
-    S.Stk.push<Pointer>(Arg);
-  } else {
-    assert(false && "Unsupported pointer type passed to __builtin_addressof()");
-  }
+  assert(PtrT == PT_Ptr &&
+         "Unsupported pointer type passed to __builtin_addressof()");
   return true;
 }
 
 static bool interp__builtin_move(InterpState &S, CodePtr OpPC,
                                  const InterpFrame *Frame,
                                  const CallExpr *Call) {
-
-  PrimType ArgT = S.getContext().classify(Call->getArg(0)).value_or(PT_Ptr);
-
-  TYPE_SWITCH(ArgT, const T &Arg = S.Stk.peek<T>(); S.Stk.push<T>(Arg););
-
   return Call->getDirectCallee()->isConstexpr();
 }
 
@@ -775,13 +785,6 @@ static bool interp__builtin_eh_return_data_regno(InterpState &S, CodePtr OpPC,
   return true;
 }
 
-/// Just takes the first Argument to the call and puts it on the stack.
-static bool noopPointer(InterpState &S) {
-  const Pointer &Arg = S.Stk.peek<Pointer>();
-  S.Stk.push<Pointer>(Arg);
-  return true;
-}
-
 // Two integral values followed by a pointer (lhs, rhs, resultOut)
 static bool interp__builtin_overflowop(InterpState &S, CodePtr OpPC,
                                        const CallExpr *Call,
@@ -2504,6 +2507,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
   case Builtin::BIaddressof:
   case Builtin::BI__addressof:
   case Builtin::BI__builtin_addressof:
+    assert(isNoopBuiltin(BuiltinID));
     if (!interp__builtin_addressof(S, OpPC, Frame, Call))
       return false;
     break;
@@ -2513,6 +2517,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
   case Builtin::BIforward_like:
   case Builtin::BImove:
   case Builtin::BImove_if_noexcept:
+    assert(isNoopBuiltin(BuiltinID));
     if (!interp__builtin_move(S, OpPC, Frame, Call))
       return false;
     break;
@@ -2523,8 +2528,7 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
     break;
 
   case Builtin::BI__builtin_launder:
-    if (!noopPointer(S))
-      return false;
+    assert(isNoopBuiltin(BuiltinID));
     break;
 
   case Builtin::BI__builtin_add_overflow:

For some builtins, we dont' need to do anything, but due to the cleanup
code being the same for all builtins, we still had to duplicate the
value on the stack. Remove that and get rid of all the unnecessary
pressure on the InterpStack.
@tbaederr tbaederr merged commit b411f70 into llvm:main May 29, 2025
11 checks passed
svkeerthy pushed a commit that referenced this pull request May 29, 2025
For some builtins, we dont' need to do anything, but due to the cleanup
code being the same for all builtins, we still had to duplicate the
value on the stack. Remove that and get rid of all the unnecessary
pressure on the InterpStack.
google-yfyang pushed a commit to google-yfyang/llvm-project that referenced this pull request May 29, 2025
For some builtins, we dont' need to do anything, but due to the cleanup
code being the same for all builtins, we still had to duplicate the
value on the stack. Remove that and get rid of all the unnecessary
pressure on the InterpStack.
@kazutakahirata
Copy link
Contributor

@tbaederr I've landed 882e733 to fix a warning from this PR. Thanks!

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