|
| 1 | +--- |
| 2 | +title: "Wrapping Up GSoC 2024: Out-Of-Process Execution for Clang-REPL" |
| 3 | +layout: post |
| 4 | +excerpt: "Out of Process Execution for Clang-Repl project, part of Google Summer of Code 2024, aims to enhance Clang-Repl by implementing out-of-process execution. This will address issues of high resource consumption and instability, making Clang-Repl more suitable for low-power devices and more stable for developers" |
| 5 | +sitemap: false |
| 6 | +author: Sahil Patidar |
| 7 | +permalink: blogs/gsoc24_sahil_wrapup_blog/ |
| 8 | +banner_image: /images/blog/gsoc-banner.png |
| 9 | +date: 2024-10-14 |
| 10 | +tags: gsoc llvm clang-repl orc-jit llvm-jitlink-executor |
| 11 | +--- |
| 12 | + |
| 13 | +### **Introduction** |
| 14 | + |
| 15 | +Hello! I'm Sahil Patidar, and this summer I had the exciting opportunity to |
| 16 | +participate in Google Summer of Code (GSoC) 2024. My project revolved around |
| 17 | +enhancing Clang-Repl by introducing Out-Of-Process Execution. |
| 18 | + |
| 19 | +Mentors: Vassil Vassilev and Matheus Izvekov |
| 20 | + |
| 21 | +### **Project Overview** |
| 22 | + |
| 23 | +**Clang** is a widely-used compiler front-end within the **LLVM** project, capable of |
| 24 | +compiling languages like C++. A powerful tool, **Clang-Repl**, acts as an interactive |
| 25 | +C++ interpreter using **Just-In-Time (JIT)** compilation. It allows users to write |
| 26 | +compile, and execute C++ code interactively, making it an invaluable resource for |
| 27 | +learning, prototyping, and debugging. |
| 28 | + |
| 29 | +Despite its benefits, Clang-Repl had certain limitations: |
| 30 | + |
| 31 | +1. **High Resource Usage**: Running the Clang-Repl environment and JIT in the same |
| 32 | + process consumed a lot of system resources. |
| 33 | +2. **Instability**: If the user's code crashed, it would terminate the entire |
| 34 | + Clang-Repl session, causing disruptions. |
| 35 | + |
| 36 | +### **Out-Of-Process Execution** |
| 37 | + |
| 38 | +The solution to these challenges was **Out-Of-Process Execution**—executing user code |
| 39 | +in a separate process. This offers two major advantages: |
| 40 | + |
| 41 | +- **Efficient Resource Management**: By isolating code execution, Clang-Repl reduces |
| 42 | + its system resource footprint, crucial for devices with limited memory or processing |
| 43 | + power. |
| 44 | +- **Enhanced Stability**: Crashes in user code no longer affect the main Clang-Repl |
| 45 | + session, improving reliability and user experience. |
| 46 | + |
| 47 | +This improvement makes Clang-Repl more reliable, especially on low-power or embedded |
| 48 | +systems, and suitable for broader use cases. |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## **Summary of accomplished tasks** |
| 53 | + |
| 54 | +#### **Add Out-Of-Process Execution Support for Clang-Repl** [#110418](https://github.com/llvm/llvm-project/pull/110418) |
| 55 | + |
| 56 | +To implement out-of-process execution, I leveraged **ORC JIT’s remote execution capabilities** with `llvm-jitlink-executor`. Key features included: |
| 57 | + |
| 58 | +- **New Command Flags**: |
| 59 | + - `--oop-executor`: Launches the JIT executor in a separate process. |
| 60 | + - `--oop-executor-connect`: Connects Clang-Repl to the external process for out-of-process execution. |
| 61 | + |
| 62 | +These flags enable Clang-Repl to use `llvm-jitlink-executor` for isolated code execution. |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +### **Key ORC JIT Enhancements** |
| 67 | + |
| 68 | +To support Clang-Repl’s out-of-process execution, I contributed several improvements to **ORC JIT**: |
| 69 | + |
| 70 | +#### **Incremental Initializer Execution for Mach-O and ELF** [#97441](https://github.com/llvm/llvm-project/pull/97441), [#110406](https://github.com/llvm/llvm-project/pull/110406) |
| 71 | + |
| 72 | +The `dlupdate` function was introduced in the ORC runtime to enable the incremental |
| 73 | +execution of newly added initializers within the REPL environment. Unlike the |
| 74 | +traditional `dlopen` function, which handles initializers, code mapping, and library |
| 75 | +reference counts, `dlupdate` is focused solely on executing new initializers, |
| 76 | +ensuring that only the newly introduced sections are processed. This targeted |
| 77 | +approach improves efficiency, especially in interactive REPL sessions like `clang-repl`. |
| 78 | + |
| 79 | +#### **Push-Request Model for ELF Initializers** [#102846](https://github.com/llvm/llvm-project/pull/102846) |
| 80 | + |
| 81 | +I introduced a push-request model to manage ELF initializers within the runtime |
| 82 | +state for each JITDylib, similar to how Mach-O and COFF handle initializers. |
| 83 | +Previously, ELF had to request initializers every time it needed them, but |
| 84 | +lacked the ability to `register`, `deregister`, or `retain` these initializers. |
| 85 | +This led to issues when re-running `dlopen` because, once `rt_getInitializers` |
| 86 | +was invoked, the initializers were erased, making subsequent executions impossible. |
| 87 | + |
| 88 | +To address this, I introduced the following functions: |
| 89 | + |
| 90 | + - **`__orc_rt_elfnix_register_init_sections`**: Registers ELF initializer sections. |
| 91 | + - **`__orc_rt_elfnix_register_jitdylib`**: Registers JITDylib-specific initializers. |
| 92 | + |
| 93 | +The push-request model effectively tracks and manages initializers for each |
| 94 | +`JITDylib` state. By utilizing Mach-O’s `RecordSectionsTracker`, the system selectively executes only newly |
| 95 | +registered initializers, thereby enhancing both efficiency and reliability |
| 96 | +when working with ELF targets in `clang-repl`. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +### **Additional Improvements** |
| 101 | + |
| 102 | +#### **Auto-loading Dynamic Libraries in ORC JIT** [#109913](https://github.com/llvm/llvm-project/pull/109913) (On-going) |
| 103 | + |
| 104 | +I added an auto-loading dynamic library feature for ORC JIT, aimed at |
| 105 | +enhancing the efficiency of symbol resolution for both loaded and unloaded |
| 106 | +libraries. A key component of this improvement is the global bloom filter, |
| 107 | +which filters out symbols unlikely to be present, thereby reducing unnecessary |
| 108 | +search attempts and speeding up lookups. |
| 109 | + |
| 110 | +#### **Refactor `dlupdate`** [#110491](https://github.com/llvm/llvm-project/pull/110491) |
| 111 | + |
| 112 | +I refactored the `dlupdate` function to remove the mode argument, simplifying the |
| 113 | +function's interface. |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +### **Benchmarks: In-Process vs Out-of-Process Execution** |
| 118 | + |
| 119 | +- [Prime Finder](https://gist.github.com/SahilPatidar/4870bf9968b1b0cb3dabcff7281e6135) |
| 120 | +- [Fibonacci Sequence](https://gist.github.com/SahilPatidar/2191963e59feb7dfa1314509340f95a1) |
| 121 | +- [Matrix Multiplication](https://gist.github.com/SahilPatidar/1df9e219d0f8348bd126f1e01658b3fa) |
| 122 | +- [Sorting Algorithms](https://gist.github.com/SahilPatidar/c814634b2f863fc167b8d16b573f88ec) |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +### **Conclusion** |
| 127 | + |
| 128 | +Thanks to this project, **Clang-Repl** now supports **out-of-process execution** for |
| 129 | +both `ELF` and `Mach-O`, vastly improving its resource efficiency and stability. These |
| 130 | +changes make Clang-Repl more robust, especially on low-resource devices. |
| 131 | + |
| 132 | +Looking ahead, I plan to focus on automating library loading and further enhancing |
| 133 | +ORC-JIT to optimize Clang-Repl's out-of-process execution. |
| 134 | + |
| 135 | +Thank you for being a part of my **GSoC 2024** journey! |
| 136 | + |
| 137 | +### **Acknowledgements** |
| 138 | + |
| 139 | +I would like to extend my deepest gratitude to Google Summer of Code (GSoC) |
| 140 | +for the incredible opportunity to work on this project. A special thanks to |
| 141 | +my mentors, Vassil Vassilev and Matheus Izvekov, for their invaluable guidance |
| 142 | +and support throughout this journey. I am also immensely grateful to Lang Hames |
| 143 | +for their expert insights on ORC-JIT enhancements for `clang-repl`. This experience |
| 144 | +has been a pivotal moment in my development, and I am excited to continue |
| 145 | +contributing to the open-source community. |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +### **Related Links** |
| 150 | + |
| 151 | +- [LLVM Repository](https://github.com/llvm/llvm-project) |
| 152 | +- [Project Description](https://discourse.llvm.org/t/clang-out-of-process-execution-for-clang-repl/68225) |
| 153 | +- [My GitHub Profile](https://github.com/SahilPatidar) |
| 154 | + |
| 155 | +--- |
0 commit comments