Skip to content

[clang][repl] fix new on Mac M1 #69072

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

Closed
wants to merge 1 commit into from

Conversation

makslevental
Copy link
Contributor

On Mac M1, if you load something that transitively loads #include <new>, it will fail because

In file included from <<< inputs >>>:1:
In file included from input_line_22:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/memory:671:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__functional_base:23:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/new:214:70: error: 'internal_linkage' attribute does not appear on the first declaration
  214 | _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
      |                                                                      ^
input_line_0:3:11: note: previous declaration is here
    3 |     void* operator new(__SIZE_TYPE__, void* __p) noexcept;
      |           ^
Assertion failed: (Ptr && "dereferencing end() iterator"), function operator*, file DeclBase.h, line 1315.

This is because MacOSX12.3.sdk/usr/include/c++/v1/__config sets _LIBCPP_INLINE_VISIBILITY to be __attribute__((__visibility__("hidden"))) __attribute__((internal_linkage)), which makes the new in <new> incompatible with the hardcoded decl void* operator new(__SIZE_TYPE__, void* __p) noexcept; in clang/lib/Interpreter/Interpreter::Runtimes. I believe the correct thing to do is replace that decl with #include <new> (as I've done here).

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

llvmbot commented Oct 14, 2023

@llvm/pr-subscribers-clang

Author: Maksim Levental (makslevental)

Changes

On Mac M1, if you load something that transitively loads #include &lt;new&gt;, it will fail because

In file included from &lt;&lt;&lt; inputs &gt;&gt;&gt;:1:
In file included from input_line_22:1:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/memory:671:
In file included from /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/__functional_base:23:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/new:214:70: error: 'internal_linkage' attribute does not appear on the first declaration
  214 | _LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_INLINE_VISIBILITY void* operator new  (std::size_t, void* __p) _NOEXCEPT {return __p;}
      |                                                                      ^
input_line_0:3:11: note: previous declaration is here
    3 |     void* operator new(__SIZE_TYPE__, void* __p) noexcept;
      |           ^
Assertion failed: (Ptr &amp;&amp; "dereferencing end() iterator"), function operator*, file DeclBase.h, line 1315.

This is because MacOSX12.3.sdk/usr/include/c++/v1/__config sets _LIBCPP_INLINE_VISIBILITY to be __attribute__((__visibility__("hidden"))) __attribute__((internal_linkage)), which makes the new in &lt;new&gt; incompatible with the hardcoded decl void* operator new(__SIZE_TYPE__, void* __p) noexcept; in clang/lib/Interpreter/Interpreter::Runtimes. I believe the correct thing to do is replace that decl with #include &lt;new&gt; (as I've done here).


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

2 Files Affected:

  • (modified) clang/lib/Interpreter/Interpreter.cpp (+1-1)
  • (modified) clang/test/Interpreter/execute.cpp (+2)
diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 7968c62cbd3e7b3..ddfbc9ac01c6743 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -248,7 +248,7 @@ Interpreter::~Interpreter() {
 // can't find the precise resource directory in unittests so we have to hard
 // code them.
 const char *const Runtimes = R"(
-    void* operator new(__SIZE_TYPE__, void* __p) noexcept;
+    #include <new>
     void *__clang_Interpreter_SetValueWithAlloc(void*, void*, void*);
     void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*);
     void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*, void*);
diff --git a/clang/test/Interpreter/execute.cpp b/clang/test/Interpreter/execute.cpp
index 6e73ed3927e8155..d54ab99749c1bac 100644
--- a/clang/test/Interpreter/execute.cpp
+++ b/clang/test/Interpreter/execute.cpp
@@ -20,4 +20,6 @@ auto r2 = printf("S[f=%f, m=0x%llx]\n", s.f, reinterpret_cast<unsigned long long
 inline int foo() { return 42; }
 int r3 = foo();
 
+#include <memory>
+
 %quit

@makslevental makslevental force-pushed the clang_repl_include_path branch 4 times, most recently from 737bef4 to 940da2d Compare October 20, 2023 05:30
@makslevental
Copy link
Contributor Author

@vgvassilev this seems acceptable?

@makslevental makslevental force-pushed the clang_repl_include_path branch from 940da2d to 891cdd5 Compare October 20, 2023 05:35
@@ -24,6 +24,7 @@
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/TargetParser/Host.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

The changes in this file should not be needed.

@@ -148,12 +161,12 @@ TEST(InterpreterTest, UndoCommand) {
auto Interp = createInterpreter(ExtraArgs, DiagPrinter.get());

// Fail to undo.
auto Err1 = Interp->Undo();
auto Err1 = Interp->Undo(2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Likewise.

@@ -257,14 +256,9 @@ const char *const Runtimes = R"(
void __clang_Interpreter_SetValueNoAlloc(void*, void*, void*, long double);
void __clang_Interpreter_SetValueNoAlloc(void*,void*,void*,unsigned long long);
template <class T, class = T (*)() /*disable for arrays*/>
void __clang_Interpreter_SetValueCopyArr(T* Src, void* Placement, unsigned long Size) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can’t outline templates because we are introducing an odr violation. We should move new behind a new non-templates operation which we can forward declare and use within the template.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can’t outline templates because we are introducing an odr violation.

This is not correct - templates (subject to conditions satisfied here) are exempt from odr:

There can be more than one definition of a [...] templated entity ([temp.pre]) [...] in a program provided that each definition appears in a different translation unit and the definitions satisfy the following requirements.

https://timsong-cpp.github.io/cppwp/n4861/basic.def.odr#13

We should move new behind a new non-templates operation which we can forward declare and use within the template.

I don't know what you're describing here; feel free to take over this PR if you have a concrete design in mind.

vgvassilev added a commit to vgvassilev/llvm-project that referenced this pull request Dec 22, 2023
…C++.

This patch brings back the basic support for C by inserting the required for
value printing runtime only when we are in C++ mode. Additionally, it defines
a new overload of operator placement new because we can't really forward declare
it in a library-agnostic way.

Fixes the issue described in llvm#69072.
vgvassilev added a commit to vgvassilev/llvm-project that referenced this pull request Jan 17, 2024
…C++.

This patch brings back the basic support for C by inserting the required for
value printing runtime only when we are in C++ mode. Additionally, it defines
a new overload of operator placement new because we can't really forward declare
it in a library-agnostic way.

Fixes the issue described in llvm#69072.
vgvassilev added a commit that referenced this pull request Jan 18, 2024
…C++ (#76218)

This patch brings back the basic support for C by inserting the required
for value printing runtime only when we are in C++ mode. Additionally,
it defines a new overload of operator placement new because we can't
really forward declare it in a library-agnostic way.

Fixes the issue described in #69072.
giordano pushed a commit to JuliaLang/llvm-project that referenced this pull request May 25, 2024
…C++ (llvm#76218)

This patch brings back the basic support for C by inserting the required
for value printing runtime only when we are in C++ mode. Additionally,
it defines a new overload of operator placement new because we can't
really forward declare it in a library-agnostic way.

Fixes the issue described in llvm#69072.

(cherry picked from commit 1566f1f)
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.

3 participants