Skip to content

Adds Out-Of-Process JIT execution for CppInterOp #635

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

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

kr-2003
Copy link
Contributor

@kr-2003 kr-2003 commented Jun 17, 2025

Description

Please include a summary of changes, motivation and context for this PR.

Fixes # (issue)

Type of change

Please tick all options which are relevant.

  • Bug fix
  • New feature
  • Requires documentation updates

Testing

Please describe the test(s) that you added and ran to verify your changes.

Checklist

  • I have read the contribution guide recently

@Vipul-Cariappa Vipul-Cariappa self-requested a review June 17, 2025 12:14
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Copy link
Collaborator

@Vipul-Cariappa Vipul-Cariappa left a comment

Choose a reason for hiding this comment

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

Nice work. I guess there will be multiple rounds of review on this.

Comment on lines +312 to +313
string(REGEX REPLACE "/build/lib/cmake/llvm$" "" LLVM_SOURCE_DIR "${LLVM_DIR}")
add_definitions(-DLLVM_SOURCE_DIR="${LLVM_SOURCE_DIR}")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
string(REGEX REPLACE "/build/lib/cmake/llvm$" "" LLVM_SOURCE_DIR "${LLVM_DIR}")
add_definitions(-DLLVM_SOURCE_DIR="${LLVM_SOURCE_DIR}")
string(REGEX REPLACE "/build/lib/cmake/llvm$" "" LLVM_BINARY_DIR "${LLVM_DIR}")
add_definitions(-DLLVM_SOURCE_DIR="${LLVM_BINARY_DIR}")

I guess is a better name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LLVM_BINARY_DIR is already an env variable

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we reuse LLVM_BINARY_DIR?

Comment on lines +149 to +165
#ifdef CPPINTEROP_WITH_OOP_JIT
TEST(InterpreterTest, OopJITProcess) {
#ifdef _WIN32
GTEST_SKIP() << "Disabled on Windows. Needs fixing.";
#endif
if (llvm::sys::RunningOnValgrind())
GTEST_SKIP() << "XFAIL due to Valgrind report";
std::vector<const char*> interpreter_args = { "-include", "new" };
auto* I = Cpp::CreateInterpreter(interpreter_args, {}, true);
EXPECT_TRUE(Cpp::Process("") == 0);
EXPECT_TRUE(Cpp::Process("int a = 12;") == 0);
EXPECT_FALSE(Cpp::Process("error_here;") == 0);
// Linker/JIT error.
EXPECT_FALSE(Cpp::Process("int f(); int res = f();") == 0);
}
#endif

Copy link
Collaborator

Choose a reason for hiding this comment

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

According to this, we only run one test with out-of-process, but we should run all the tests with out-of-process. This should be done through the CI matrix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, i added only this for now to test it out. In the future, all the tests should be ran against out-of-process JIT.

std::unique_ptr<llvm::orc::LLJITBuilder> JB;

if(outOfProcess) {
std::string OOPExecutor = std::string(LLVM_SOURCE_DIR) + "/build/bin/llvm-jitlink-executor";
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this work if LLVM is installed with conda or is installed with the system's package installer.
Could you make sure if working in the other cases too.
You can first check if the file exists; if not, try other paths like the CONDA_PREFIX or /usr/bin.

@Vipul-Cariappa Vipul-Cariappa requested a review from mcbarton June 17, 2025 12:40
@Vipul-Cariappa
Copy link
Collaborator

Hi @mcbarton, do we have cache to add another version of LLVM? For the out-of-process.patch. We need the normal LLVM-20 and LLVM-20-OOP where OOP is for out-of-process?
Could you please help @kr-2003 achieve this?

@Vipul-Cariappa Vipul-Cariappa marked this pull request as draft June 17, 2025 12:42
@Vipul-Cariappa
Copy link
Collaborator

I would prefer if we could keep this as a draft PR.

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

elif [[ "${{ runner.os }}" == "macOS" ]]; then
ninja clang clang-repl llvm-jitlink-executor orc_rt_osx -j ${{ env.ncpus }}
fi
fi
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we have target name for orc_rt difference for MacOS? The target name should be independent of the system your building on.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is different in LLVM itself. I guess it might have to do with cross-compiling.

if [[ "${{ matrix.oop-jit }}" == "On" ]]; then
git apply -v ../patches/llvm/clang20-1-out-of-process-jit-execution.patch
echo "Apply out-of-process-jit-execution.patch:"
fi
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should apply this patch regardless of whether matrix.oop-jit=on. If you only apply it if CPPINTEROP_WITH_OOP_JIT=ON , this would imply if you apply the patch, DCPPINTEROP_WITH_OOP_JIT=OFF, then somehow something will go wrong.

Also, why are we not applying this patch on Windows, and testing CPPINTEROP_WITH_OOP_JIT=ON/OFF?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok. The thing is, I would want CppInterOp to be compatible with upstream LLVM as well as the patched LLVM. Therefore, I was thinking about having 2 different CI workflows for version 20 with OOP-JIT and without OOP-JIT.
OOP-JIT is not supported on Windows.


Ideally, this is temporary till the patch is directly merged into LLVM and a new version is released.

Copy link
Collaborator

@mcbarton mcbarton Jun 18, 2025

Choose a reason for hiding this comment

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

I could be wrong, and @vgvassilev can correct me if I am wrong, but for such large patches, we have had them merged into llvm first, before applying them to previous versions in CppInterOp.

You could have a new llvm cache like you want. One where you build OOP-JIT and without OOP-JIT. This could only really be done after I find a way to make space in the cache, since we currently are unlikely to have enough space for all these new llvm cache builds. I have an idea on how to free up some space, and just need to find some time to do. I will try and do it tomorrow, as I am busy today.

-DCODE_COVERAGE=${{ env.CODE_COVERAGE }} \
-DCMAKE_INSTALL_PREFIX=$CPPINTEROP_DIR \
-DLLVM_ENABLE_WERROR=On \
-DCPPINTEROP_WITH_OOP_JIT=OFF \
Copy link
Collaborator

Choose a reason for hiding this comment

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

Feels like CPPINTEROP_WITH_OOP_JIT should have a default value, like we have for cppinterop_use_repl/cppinterop_use_cling

@@ -0,0 +1,710 @@
diff --git a/clang/include/clang/Interpreter/Interpreter.h b/clang/include/clang/Interpreter/Interpreter.h
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is quite a large patch. Can you confirm that it has been upsteamed so will appear in llvm 21?

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

Copy link

codecov bot commented Jun 20, 2025

Codecov Report

Attention: Patch coverage is 44.44444% with 5 lines in your changes missing coverage. Please review.

Project coverage is 78.59%. Comparing base (130dd30) to head (43220d7).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/CppInterOp/Compatibility.h 28.57% 5 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #635      +/-   ##
==========================================
- Coverage   78.69%   78.59%   -0.10%     
==========================================
  Files           9        9              
  Lines        3835     3841       +6     
==========================================
+ Hits         3018     3019       +1     
- Misses        817      822       +5     
Files with missing lines Coverage Δ
include/CppInterOp/CppInterOp.h 95.34% <ø> (ø)
lib/CppInterOp/CppInterOp.cpp 86.58% <100.00%> (ø)
lib/CppInterOp/CppInterOpInterpreter.h 86.97% <100.00%> (ø)
lib/CppInterOp/Compatibility.h 88.70% <28.57%> (-3.67%) ⬇️
Files with missing lines Coverage Δ
include/CppInterOp/CppInterOp.h 95.34% <ø> (ø)
lib/CppInterOp/CppInterOp.cpp 86.58% <100.00%> (ø)
lib/CppInterOp/CppInterOpInterpreter.h 86.97% <100.00%> (ø)
lib/CppInterOp/Compatibility.h 88.70% <28.57%> (-3.67%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +62 to +63
+launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,
+ llvm::StringRef SlabAllocateSizeString);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
+launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,
+ llvm::StringRef SlabAllocateSizeString);
+launchExecutor(llvm::StringRef ExecutablePath, bool UseSharedMemory,
+ llvm::StringRef SlabAllocateSizeString, int stdin_fd = 0, int stdout_fd = 1, int stderr_fd = 2);

Comment on lines +321 to +322
+ dup2(StdoutPipe[WriteEnd], STDOUT_FILENO);
+ dup2(StderrPipe[WriteEnd], STDERR_FILENO);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
+ dup2(StdoutPipe[WriteEnd], STDOUT_FILENO);
+ dup2(StderrPipe[WriteEnd], STDERR_FILENO);
+ if (stdout_fd != 1) ...
+ dup2(stdout_fd, STDOUT_FILENO);
+ dup2(StderrPipe[WriteEnd], STDERR_FILENO);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants