compiler-research / CppInterOp

A Clang-based C++ Interoperability Library
Other
38 stars 20 forks source link

Clang-repl=ON issue for both clang 16 & 17 on Apple Silicon for cppyy #175

Closed mcbarton closed 5 months ago

mcbarton commented 6 months ago

Currently if you build CppInterOp with Clang-Repl=ON , you encounter the following error when performing the command python -c "import cppyy" (with both a patched clang 16 or 17 on Apple Silicon)

<built-in>:1:10: fatal error: 'new' file not found
    1 | #include "new"
      |          ^~~~~
In file included from <<< inputs >>>:1:
input_line_2:1:10: fatal error: 'iostream' file not found
    1 | #include <iostream>
      |          ^~~~~~~~~~
Failed to parse via ::process:Parsing failed.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/wf5617/Library/Python/3.12/lib/python/site-packages/cppyy/__init__.py", line 80, in <module>
    from ._cpython_cppyy import *
  File "/Users/wf5617/Library/Python/3.12/lib/python/site-packages/cppyy/_cpython_cppyy.py", line 152, in <module>
    gbl.std =  _backend.CreateScopeProxy('std')
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'std' is not a known C++ class

I get no errors when I use CppInterOp with Cling. After a fix to this issue is found then a ci for osx on arm should be able to pass to the same level as ubuntu and osx on x86, once the free runners become available. My initial attempts to try and find the cause of this issue on my local machine have failed. @vgvassilev @alexander-penev any suggestions on what the issue may be?

vgvassilev commented 6 months ago

The problem is that Cling has a robust way to find the relevant include paths to pass to the interpreter by asking the underlying compiler: https://github.com/root-project/root/blob/798b9b079f7b6c136bdaaf6787729a1bef865429/interpreter/cling/lib/Interpreter/CIFactory.cpp#L109

One solution would be to get that code and add it right after we create the clang-repl instance.

mcbarton commented 6 months ago

@vgvassilev Until I can find the time add add that code you suggest I worked around the issue by appending to CPLUS_INCLUDE_PATH the paths it needed to find the header files. After doing so I a new error arose

M0988-Mac:~ wf5617$ python -c "import cppyy"
[GetVariableOffset] Failed to execute PTU:Duplicate definition of symbol '__ZN5cling7runtime6gClingE'

Any suggestions on how to fix this issue?

Just so there is a note. This was with a patched version of clang 16, but I assume that same happens with clang 17, but am yet to test.

mcbarton commented 6 months ago

@Krishna-13-cyber I see you currently have a PR to add support for GetIncludePaths. Would it be possible to change this PR to also add support for the ReadCompilerIncludePaths suggested to partially fix this issue? Alternatively would it be possible for you to assist me in implementing support for this in a separate PR?

Krishna-13-cyber commented 6 months ago

Hi @mcbarton, Yes, I will coordinate with you accordingly.

Krishna-13-cyber commented 6 months ago

Hi @mcbarton, When can we meet and discuss?

mcbarton commented 6 months ago

@Krishna-13-cyber I don't really have availability to meet up and discuss. This project is something I am just working on helping in my spare time when not working, just because I find it interesting. When I asked if you could support the implementation of ReadCompilerIncludePaths it was more asking if you could offer advice on a PR which tries to add support (which I'm hoping I might find time to put in the next week or possibly 2). I suspected you may know the best way given your PR to add support for GetIncludePaths. I have a coding background (C++,Fortran,Python,etc) , but nothing that relates to cling , clang and llvm so my first attempt at this PR may not be great (my background is in computational plasma physics, and computational nuclear physics).

mcbarton commented 6 months ago

@vgvassilev Until I can find the time add add that code you suggest I worked around the issue by appending to CPLUS_INCLUDE_PATH the paths it needed to find the header files. After doing so I a new error arose

M0988-Mac:~ wf5617$ python -c "import cppyy"
[GetVariableOffset] Failed to execute PTU:Duplicate definition of symbol '__ZN5cling7runtime6gClingE'

Any suggestions on how to fix this issue?

Just so there is a note. This was with a patched version of clang 16, but I assume that same happens with clang 17, but am yet to test.

@alexander-penev Do you know what this error message is telling me? I know the error is being logged either here

https://github.com/compiler-research/CppInterOp/blob/d294b0790318522fd1eb0b0a2c786a4ba0347a58/lib/Interpreter/CppInterOp.cpp#L1107

or here

https://github.com/compiler-research/CppInterOp/blob/d294b0790318522fd1eb0b0a2c786a4ba0347a58/lib/Interpreter/CppInterOp.cpp#L1121

but don't know which one, or what could possibly fix it.

vgvassilev commented 6 months ago

@vgvassilev Until I can find the time add add that code you suggest I worked around the issue by appending to CPLUS_INCLUDE_PATH the paths it needed to find the header files. After doing so I a new error arose

M0988-Mac:~ wf5617$ python -c "import cppyy"
[GetVariableOffset] Failed to execute PTU:Duplicate definition of symbol '__ZN5cling7runtime6gClingE'

Any suggestions on how to fix this issue?

Just so there is a note. This was with a patched version of clang 16, but I assume that same happens with clang 17, but am yet to test.

Can we try export SDKROOT=`xcrun --show-sdk-path`

mcbarton commented 6 months ago

Can we try export SDKROOT=xcrun --show-sdk-path

Adding this environment variable is an alternative to me appending to CPLUS_INCLUDE_PATH, but still leaves me with the same error.

Krishna-13-cyber commented 6 months ago

@Krishna-13-cyber I don't really have availability to meet up and discuss. This project is something I am just working on helping in my spare time when not working, just because I find it interesting. When I asked if you could support the implementation of ReadCompilerIncludePaths it was more asking if you could offer advice on a PR which tries to add support (which I'm hoping I might find time to put in the next week or possibly 2). I suspected you may know the best way given your PR to add support for GetIncludePaths. I have a coding background (C++,Fortran,Python,etc) , but nothing that relates to cling , clang and llvm so my first attempt at this PR may not be great (my background is in computational plasma physics, and computational nuclear physics).

Oh, got it. I will get back to you when I have some implementation plan or will ping you when I open PR for ReadCompilerIncludePaths!

Krishna-13-cyber commented 5 months ago

The problem is that Cling has a robust way to find the relevant include paths to pass to the interpreter by asking the underlying compiler: https://github.com/root-project/root/blob/798b9b079f7b6c136bdaaf6787729a1bef865429/interpreter/cling/lib/Interpreter/CIFactory.cpp#L109

One solution would be to get that code and add it right after we create the clang-repl instance.

I gave this a try, we need ArgumentList to append all the flags and arguments we encounter synonymous to how it's implemented in Cling. Or we can get the fetch and store the paths by some means given by the directory given and with the help of AddIncludePaths we can tell interpeter about the same.

CC: @vgvassilev Please do let me know if I am on the right track.

Gnimuc commented 2 months ago

I hit this with 1.2.0. How did #195 fix the issue? The diff seems to be unrelated.

julia> Cpp.CreateInterpreter(args)
clang version 17.0.6 (/cache/yggdrasil/downloads/clones/llvm-project.git-5a9787eb535c2edc5dea030cc221c1d60f38c9f42344f410e425ea2139e233aa 0424864b3ac348210aab171668eea1a669956a31)
Target: aarch64-apple-darwin20
Thread model: posix
InstalledDir: /Users/gnimuc/Code/juliasrc/usr/bin
 "/Users/gnimuc/Code/juliasrc/usr/bin/julia" -cc1 -triple arm64-apple-macosx11.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name "<<< inputs >>>" -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +aes -target-feature +crc -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +lse -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sha2 -target-feature +sha3 -target-feature +neon -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-abi darwinpcs -debugger-tuning=lldb -v -fcoverage-compilation-dir=/Users/gnimuc/.julia/dev/CppInterOp -nostdsysteminc -nobuiltininc -resource-dir /Users/gnimuc/.julia/artifacts/8073d26681313e3bc6911514633c69424de161e4/lib/clang/17 -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/lib/gcc/aarch64-apple-darwin20/11.0.0/include -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/lib/gcc/aarch64-apple-darwin20/11.0.0/include-fixed -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/include -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/sys-root/usr/include -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/sys-root/System/Library/Frameworks -isystem /Users/gnimuc/.julia/artifacts/8f85d73f56f6ee32595fc24f13feccf9bcc6d8fa/lib/clang/17/include -include new -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/Users/gnimuc/.julia/dev/CppInterOp -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -fincremental-extensions -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o "<<< inputs >>>.o" -x c++ "<<< inputs >>>"
clang -cc1 version 17.0.6 based upon LLVM 17.0.6jl default target aarch64-apple-darwin23.4.0
#include "..." search starts here:
#include <...> search starts here:
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/lib/gcc/aarch64-apple-darwin20/11.0.0/include
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/lib/gcc/aarch64-apple-darwin20/11.0.0/include-fixed
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/include
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/sys-root/usr/include
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/sys-root/System/Library/Frameworks
 /Users/gnimuc/.julia/artifacts/8f85d73f56f6ee32595fc24f13feccf9bcc6d8fa/lib/clang/17/include
End of search list.
<built-in>:1:10: fatal error: 'new' file not found
    1 | #include "new"
      |          ^~~~~

[51392] signal 11 (2): Segmentation fault: 11
in expression starting at REPL[7]:1
_ZN4llvm15AnalysisManagerINS_6ModuleEJEE13getResultImplEPNS_11AnalysisKeyERS1_ at /Users/gnimuc/Code/juliasrc/usr/lib/libLLVM.dylib (unknown line)
_ZN4llvm11PassManagerINS_6ModuleENS_15AnalysisManagerIS1_JEEEJEE3runERS1_RS3_ at /Users/gnimuc/Code/juliasrc/usr/lib/libLLVM.dylib (unknown line)
_ZN12_GLOBAL__N_118EmitAssemblyHelper23RunOptimizationPipelineEN5clang13BackendActionERNSt3__110unique_ptrIN4llvm17raw_pwrite_streamENS3_14default_deleteIS6_EEEERNS4_INS5_14ToolOutputFileENS7_ISB_EEEE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang17EmitBackendOutputERNS_17DiagnosticsEngineERKNS_19HeaderSearchOptionsERKNS_14CodeGenOptionsERKNS_13TargetOptionsERKNS_11LangOptionsEN4llvm9StringRefEPNSE_6ModuleENS_13BackendActionENSE_18IntrusiveRefCntPtrINSE_3vfs10FileSystemEEENSt3__110unique_ptrINSE_17raw_pwrite_streamENSN_14default_deleteISP_EEEE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang15BackendConsumer21HandleTranslationUnitERNS_10ASTContextE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang17IncrementalParser23ParseOrWrapTopLevelDeclEv at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang17IncrementalParser5ParseEN4llvm9StringRefE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang11Interpreter6createENSt3__110unique_ptrINS_16CompilerInstanceENS1_14default_deleteIS3_EEEE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN6compat22createClangInterpreterERNSt3__16vectorIPKcNS0_9allocatorIS3_EEEE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN3Cpp11InterpreterC2EiPKPKcS2_RKNSt3__16vectorINS5_10shared_ptrIN5clang19ModuleFileExtensionEEENS5_9allocatorISA_EEEEPvb at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN3Cpp11InterpreterC1EiPKPKcS2_RKNSt3__16vectorINS5_10shared_ptrIN5clang19ModuleFileExtensionEEENS5_9allocatorISA_EEEEPvb at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
CreateInterpreter at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
clang_CppInterOp_CreateInterpreter at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
mcbarton commented 2 months ago

I hit this with 1.2.0. How did #195 fix the issue? The diff seems to be unrelated.

julia> Cpp.CreateInterpreter(args)
clang version 17.0.6 (/cache/yggdrasil/downloads/clones/llvm-project.git-5a9787eb535c2edc5dea030cc221c1d60f38c9f42344f410e425ea2139e233aa 0424864b3ac348210aab171668eea1a669956a31)
Target: aarch64-apple-darwin20
Thread model: posix
InstalledDir: /Users/gnimuc/Code/juliasrc/usr/bin
 "/Users/gnimuc/Code/juliasrc/usr/bin/julia" -cc1 -triple arm64-apple-macosx11.0.0 -Wundef-prefix=TARGET_OS_ -Werror=undef-prefix -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name "<<< inputs >>>" -mrelocation-model pic -pic-level 2 -mframe-pointer=non-leaf -ffp-contract=on -fno-rounding-math -funwind-tables=1 -fcompatibility-qualified-id-block-type-checking -fvisibility-inlines-hidden-static-local-var -target-cpu apple-m1 -target-feature +v8.5a -target-feature +aes -target-feature +crc -target-feature +dotprod -target-feature +fp-armv8 -target-feature +fp16fml -target-feature +lse -target-feature +ras -target-feature +rcpc -target-feature +rdm -target-feature +sha2 -target-feature +sha3 -target-feature +neon -target-feature +zcm -target-feature +zcz -target-feature +fullfp16 -target-abi darwinpcs -debugger-tuning=lldb -v -fcoverage-compilation-dir=/Users/gnimuc/.julia/dev/CppInterOp -nostdsysteminc -nobuiltininc -resource-dir /Users/gnimuc/.julia/artifacts/8073d26681313e3bc6911514633c69424de161e4/lib/clang/17 -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/lib/gcc/aarch64-apple-darwin20/11.0.0/include -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/lib/gcc/aarch64-apple-darwin20/11.0.0/include-fixed -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/include -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/sys-root/usr/include -isystem /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/sys-root/System/Library/Frameworks -isystem /Users/gnimuc/.julia/artifacts/8f85d73f56f6ee32595fc24f13feccf9bcc6d8fa/lib/clang/17.0.6/include -include new -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/Users/gnimuc/.julia/dev/CppInterOp -ferror-limit 19 -stack-protector 1 -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcolor-diagnostics -fincremental-extensions -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o "<<< inputs >>>.o" -x c++ "<<< inputs >>>"
clang -cc1 version 17.0.6 based upon LLVM 17.0.6jl default target aarch64-apple-darwin23.4.0
ignoring nonexistent directory "/Users/gnimuc/.julia/artifacts/8f85d73f56f6ee32595fc24f13feccf9bcc6d8fa/lib/clang/17.0.6/include"
#include "..." search starts here:
#include <...> search starts here:
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/lib/gcc/aarch64-apple-darwin20/11.0.0/include
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/lib/gcc/aarch64-apple-darwin20/11.0.0/include-fixed
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/include
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/sys-root/usr/include
 /Users/gnimuc/.julia/artifacts/fbb170fa64a06b66dd9a16aaa21bf12848042d9a/aarch64-apple-darwin20/sys-root/System/Library/Frameworks
End of search list.
<built-in>:1:10: fatal error: 'new' file not found
    1 | #include "new"
      |          ^~~~~

[47168] signal 11 (2): Segmentation fault: 11
in expression starting at REPL[7]:1
_ZN4llvm15AnalysisManagerINS_6ModuleEJEE13getResultImplEPNS_11AnalysisKeyERS1_ at /Users/gnimuc/Code/juliasrc/usr/lib/libLLVM.dylib (unknown line)
_ZN4llvm11PassManagerINS_6ModuleENS_15AnalysisManagerIS1_JEEEJEE3runERS1_RS3_ at /Users/gnimuc/Code/juliasrc/usr/lib/libLLVM.dylib (unknown line)
_ZN12_GLOBAL__N_118EmitAssemblyHelper23RunOptimizationPipelineEN5clang13BackendActionERNSt3__110unique_ptrIN4llvm17raw_pwrite_streamENS3_14default_deleteIS6_EEEERNS4_INS5_14ToolOutputFileENS7_ISB_EEEE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang17EmitBackendOutputERNS_17DiagnosticsEngineERKNS_19HeaderSearchOptionsERKNS_14CodeGenOptionsERKNS_13TargetOptionsERKNS_11LangOptionsEN4llvm9StringRefEPNSE_6ModuleENS_13BackendActionENSE_18IntrusiveRefCntPtrINSE_3vfs10FileSystemEEENSt3__110unique_ptrINSE_17raw_pwrite_streamENSN_14default_deleteISP_EEEE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang15BackendConsumer21HandleTranslationUnitERNS_10ASTContextE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang17IncrementalParser23ParseOrWrapTopLevelDeclEv at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang17IncrementalParser5ParseEN4llvm9StringRefE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN5clang11Interpreter6createENSt3__110unique_ptrINS_16CompilerInstanceENS1_14default_deleteIS3_EEEE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN6compat22createClangInterpreterERNSt3__16vectorIPKcNS0_9allocatorIS3_EEEE at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN3Cpp11InterpreterC2EiPKPKcS2_RKNSt3__16vectorINS5_10shared_ptrIN5clang19ModuleFileExtensionEEENS5_9allocatorISA_EEEEPvb at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
_ZN3Cpp11InterpreterC1EiPKPKcS2_RKNSt3__16vectorINS5_10shared_ptrIN5clang19ModuleFileExtensionEEENS5_9allocatorISA_EEEEPvb at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
CreateInterpreter at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)
clang_CppInterOp_CreateInterpreter at /Users/gnimuc/Code/CppInterOp/install/lib/libclangCppInterOp.dylib (unknown line)

@Gnimuc The issue was resolved in a PR before 195. 195 was the first time I ran locally and noticed the issue no longer existed. I actually don't know which change fixed it, but it was definitely fixed by version 1.2.0

mcbarton commented 2 months ago

@Gnimuc Did you remember to define the envirnoment variable SDKROOT? From the output that is what appears to be missing. If not you can define it by export SDKROOT="xcrun --show-sdk-path"

Gnimuc commented 2 months ago

That does fix the issue. Thanks!