Skip to content

[clang][Interp] Handle std::move etc. builtins #70772

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
Jan 31, 2024
Merged

Conversation

tbaederr
Copy link
Contributor

Only light testing here since I'm not sure how to properly test this.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 31, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 31, 2023

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Only light testing here since I'm not sure how to properly test this.


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

2 Files Affected:

  • (modified) clang/lib/AST/Interp/InterpBuiltin.cpp (+17)
  • (modified) clang/test/AST/Interp/functions.cpp (+16)
diff --git a/clang/lib/AST/Interp/InterpBuiltin.cpp b/clang/lib/AST/Interp/InterpBuiltin.cpp
index e329794cb79243d..10e703d868f168a 100644
--- a/clang/lib/AST/Interp/InterpBuiltin.cpp
+++ b/clang/lib/AST/Interp/InterpBuiltin.cpp
@@ -412,6 +412,15 @@ static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
   return true;
 }
 
+static bool interp__builtin_move(InterpState &S, CodePtr OpPC,
+                                 const InterpFrame *Frame, const Function *Func,
+                                 const CallExpr *Call) {
+
+  const Pointer &Arg = S.Stk.peek<Pointer>();
+  S.Stk.push<Pointer>(Arg);
+  return Func->getDecl()->isConstexpr();
+}
+
 bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
                       const CallExpr *Call) {
   InterpFrame *Frame = S.Current;
@@ -537,6 +546,14 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const Function *F,
       return retInt(S, OpPC, Dummy);
     break;
 
+  case Builtin::BIas_const:
+  case Builtin::BIforward:
+  case Builtin::BIforward_like:
+  case Builtin::BImove:
+    if (interp__builtin_move(S, OpPC, Frame, F, Call))
+      return Ret<PT_Ptr>(S, OpPC, Dummy);
+    break;
+
   default:
     return false;
   }
diff --git a/clang/test/AST/Interp/functions.cpp b/clang/test/AST/Interp/functions.cpp
index 4bef9c2f7c0d1fa..364744203424380 100644
--- a/clang/test/AST/Interp/functions.cpp
+++ b/clang/test/AST/Interp/functions.cpp
@@ -371,3 +371,19 @@ namespace Variadic {
   constexpr int (*VFP)(...) = variadic_function2;
   static_assert(VFP() == 12, "");
 }
+
+
+namespace std {
+template <typename T> struct remove_reference { using type = T; };
+template <typename T> struct remove_reference<T &> { using type = T; };
+template <typename T> struct remove_reference<T &&> { using type = T; };
+template <typename T>
+constexpr typename std::remove_reference<T>::type&& move(T &&t) noexcept {
+  return static_cast<typename std::remove_reference<T>::type &&>(t);
+}
+}
+/// The std::move declaration above gets translated to a builtin function.
+namespace Move {
+  constexpr int A = std::move(5);
+  static_assert(A == 5, "");
+}

@hnrklssn
Copy link
Member

hnrklssn commented Nov 1, 2023

Would making a class with custom move and copy constructors with side effects, and moving/copying an instance around a bit to affect the results of static_asserts, work for testing?

@hnrklssn
Copy link
Member

hnrklssn commented Nov 2, 2023

Nice. I realise you're following the convention already established in the test file, but have you considered compiling with -verify=new,both and -verify=ref,both, respectively, and combining the shared diagnostics into both-error etc. to make it easier to see when they do and don't align?

@tbaederr
Copy link
Contributor Author

tbaederr commented Nov 2, 2023

Nice. I realise you're following the convention already established in the test file, but have you considered compiling with -verify=new,both and -verify=ref,both, respectively, and combining the shared diagnostics into both-error etc. to make it easier to see when they do and don't align?

That might be an interesting approach for the next new test file I add, thanks.

@tbaederr
Copy link
Contributor Author

tbaederr commented Nov 8, 2023

Ping

1 similar comment
@tbaederr
Copy link
Contributor Author

Ping

@hnrklssn
Copy link
Member

LGTM, but someone else should approve also.

@tbaederr
Copy link
Contributor Author

Ping

1 similar comment
@tbaederr
Copy link
Contributor Author

tbaederr commented Dec 8, 2023

Ping

}
}
/// The std::move declaration above gets translated to a builtin function.
namespace Move {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd like to see test coverage involving a move constructor, a move assignment operator, a direct call to __builtin_move, and some testing for std::as_const and std::forward. Of special interest would be times when there's UB in the move constructor/move assignment that should be caught or a non-copyable object where move semantics are the only thing that should work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've enabled test/SemaCXX/builtin-std-move.cpp with the new interpreter.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

LGTM assuming no surprises come up from the additional test coverage.

Copy link

github-actions bot commented Jan 15, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@tbaederr tbaederr force-pushed the stdmove branch 2 times, most recently from c6c7d24 to 7f4cb1f Compare January 26, 2024 05:37
@tbaederr tbaederr merged commit 47df391 into llvm:main Jan 31, 2024
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.

4 participants