<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>compiler/rustc_codegen_gcc/Readme.md - fork/rust @ master</title>
<meta name="description" content="compiler/rustc_codegen_gcc/Readme.md in fork/rust at master.">
<meta property="og:title" content="compiler/rustc_codegen_gcc/Readme.md - fork/rust @ master">
<meta property="og:description" content="compiler/rustc_codegen_gcc/Readme.md in fork/rust at master.">
<meta property="og:type" content="website">
<meta property="og:site_name" content="git.julia.blue">
<link rel="stylesheet" href="/css/style.css?v=62">
<script type="module" src="/js/main.js?v=62"></script>
</head>
<body>
<main data-ref="master" data-tip="3bcad65fbf67b6edf87c949531fbc13435882e6b">
<noscript>
  <div class="callout">
    <p>
      hey! this is a static git repo viewer. commit diffs, file history,
      older commits and other revisions won't load without javascript.
    </p>
    <p>
      you are still able to view source files on branch/tag tips,
      but the experience is degraded.
    </p>
  </div>
</noscript>
<header class="repo"><div>
<h1>fork/rust</h1>
</div>
<pre class="clone">git clone https://git.julia.blue/fork/rust</pre>
</header>
<p class="snapshot-commit"><span class="commit-author">bors</span><span class="commit-message" data-commit="3bcad65fbf67b6edf87c949531fbc13435882e6b">Auto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee</span><span class="snapshot-sha sha" data-commit="3bcad65fbf67b6edf87c949531fbc13435882e6b">3bcad65fbf6</span><time datetime="2023-10-05T15:24:54+00:00" title="2023-10-05 15:24:54 +0000">2023-10-05</time></p>
<header class="topbar"><details class="switcher"><summary>master</summary><div class="menu"><strong>branches</strong><a class="current" href="/fork/rust/ref/master/">master</a><strong>tags</strong><a href="/fork/rust/ref/1.39.0/">1.39.0</a><a href="/fork/rust/ref/1.40.0/">1.40.0</a><a href="/fork/rust/ref/1.41.0/">1.41.0</a><a href="/fork/rust/ref/1.41.1/">1.41.1</a><a href="/fork/rust/ref/1.42.0/">1.42.0</a><a href="/fork/rust/ref/1.43.0/">1.43.0</a><a href="/fork/rust/ref/1.43.1/">1.43.1</a><a href="/fork/rust/ref/1.44.0/">1.44.0</a><a class="all" href="/fork/rust/refs">all refs →</a></div></details><nav class="crumbs"><a href="/fork/rust/">rust</a> / <a href="/fork/rust/ref/master/tree/compiler/">compiler</a> / <a href="/fork/rust/ref/master/tree/compiler/rustc_codegen_gcc/">rustc_codegen_gcc</a> / <span class="cur">Readme.md</span></nav>
<span class="view-stats"><span>8.5 KiB</span><span>242 lines</span></span><span class="actions"><a class="raw" href="/fork/rust/raw/a93637d9038dc89551f6c98da9b022346a028937/compiler/rustc_codegen_gcc/Readme.md">raw</a></span></header>
<div class="markdown-view"><input type="radio" name="markdown-view" id="markdown-rendered" checked><label for="markdown-rendered">rendered</label><input type="radio" name="markdown-view" id="markdown-code"><label for="markdown-code">code</label><section class="readme rendered"><h1>WIP libgccjit codegen backend for rust</h1>
<p><a href="https://web.libera.chat/#rustc_codegen_gcc"><img src="https://img.shields.io/badge/irc.libera.chat-%23rustc__codegen__gcc-blue.svg" alt="Chat on IRC" /></a></p>
<p>This is a GCC codegen for rustc, which means it can be loaded by the existing rustc frontend, but benefits from GCC: more architectures are supported and GCC's optimizations are used.</p>
<p><strong>Despite its name, libgccjit can be used for ahead-of-time compilation, as is used here.</strong></p>
<h2>Motivation</h2>
<p>The primary goal of this project is to be able to compile Rust code on platforms unsupported by LLVM.
A secondary goal is to check if using the gcc backend will provide any run-time speed improvement for the programs compiled using rustc.</p>
<h2>Building</h2>
<p><strong>This requires a patched libgccjit in order to work.
The patches in <a href="https://github.com/antoyo/libgccjit-patches">this repository</a> need to be applied.
(Those patches should work when applied on master, but in case it doesn't work, they are known to work when applied on 079c23cfe079f203d5df83fea8e92a60c7d7e878.)
You can also use my <a href="https://github.com/antoyo/gcc">fork of gcc</a> which already includes these patches.</strong></p>
<p>To build it (most of these instructions come from <a href="https://gcc.gnu.org/onlinedocs/jit/internals/index.html">here</a>, so don't hesitate to take a look there if you encounter an issue):</p>
<pre class="language-bash"><a-o>$</a-o><a-f> </a-f><a-pr>git</a-pr> clone https://github.com/antoyo/gcc
<a-o>$</a-o><a-f> </a-f><a-pr>sudo</a-pr> apt install flex libmpfr-dev libgmp-dev libmpc3 libmpc-dev
<a-o>$</a-o><a-f> </a-f><a-pr>mkdir</a-pr> gcc-build gcc-install
<a-o>$</a-o><a-f> </a-f><a-pr>cd</a-pr> gcc-build
<a-o>$</a-o> <a-f>../gcc/configure</a-f> \
    <a-co>--enable-host-shared</a-co> \
    <a-co>--enable-languages=jit</a-co> \
    <a-co>--enable-checking=release</a-co> \ <a-c># it enables extra checks which allow to find bugs</a-c>
    <a-co>--disable-bootstrap</a-co> \
    <a-co>--disable-multilib</a-co> \
    <a-co>--prefix=</a-co><a-eb>$(</a-eb><a-f>pwd</a-f><a-eb>)</a-eb><a-co>/../gcc-install</a-co>
<a-o>$</a-o><a-f> </a-f><a-pr>make</a-pr> <a-co>-j4</a-co> <a-c># You can replace `4` with another number depending on how many cores you have.</a-c></pre>
<p>If you want to run libgccjit tests, you will need to also enable the C++ language in the <code>configure</code>:</p>
<pre class="language-bash"><a-co>--enable-languages=jit,c++</a-co></pre>
<p>Then to run libgccjit tests:</p>
<pre class="language-bash"><a-o>$</a-o><a-f> </a-f><a-pr>cd</a-pr> gcc <a-c># from the `gcc-build` folder</a-c>
<a-o>$</a-o><a-f> </a-f><a-pr>make</a-pr> check-jit
<a-c># To run one specific test:</a-c>
<a-o>$</a-o><a-f> </a-f><a-pr>make</a-pr> check-jit RUNTESTFLAGS=<a-s>&quot;-v -v -v jit.exp=jit.dg/test-asm.cc&quot;</a-s></pre>
<p><strong>Put the path to your custom build of libgccjit in the file <code>gcc_path</code>.</strong></p>
<pre class="language-bash"><a-o>$</a-o><a-f> </a-f><a-pr>dirname</a-pr> <a-eb>$(</a-eb><a-f>readlink</a-f><a-eb> </a-eb><a-co>-f</a-co><a-eb> </a-eb><a-eb>`</a-eb><a-f>find</a-f><a-eb> . </a-eb><a-co>-name</a-co><a-eb> libgccjit.so`</a-eb><a-eb>)</a-eb> <a-o>&gt;</a-o> gcc_path</pre>
<p>You also need to set RUST_COMPILER_RT_ROOT:</p>
<pre class="language-bash"><a-o>$</a-o><a-f> </a-f><a-pr>git</a-pr> clone https://github.com/llvm/llvm-project llvm <a-co>--depth</a-co> 1 <a-co>--single-branch</a-co>
<a-o>$</a-o><a-f> </a-f><a-pr>export</a-pr> RUST_COMPILER_RT_ROOT=<a-s>&quot;</a-s><a-o>$</a-o><a-pr>PWD</a-pr><a-s>/llvm/compiler-rt&quot;</a-s></pre>
<p>Then you can run commands like this:</p>
<pre class="language-bash"><a-o>$</a-o> <a-f>./prepare.sh</a-f> <a-c># download and patch sysroot src and install hyperfine for benchmarking</a-c>
<a-o>$</a-o><a-f> </a-f><a-pr>LIBRARY_PATH</a-pr><a-f>=</a-f><a-eb>$(</a-eb><a-f>cat</a-f><a-eb> gcc_path)</a-eb> LD_LIBRARY_PATH=<a-eb>$(</a-eb><a-f>cat</a-f><a-eb> gcc_path)</a-eb> ./build.sh <a-co>--release</a-co></pre>
<p>To run the tests:</p>
<pre class="language-bash"><a-o>$</a-o> <a-f>./test.sh</a-f> <a-co>--release</a-co></pre>
<h2>Usage</h2>
<p><code>$cg_gccjit_dir</code> is the directory you cloned this repo into in the following instructions.</p>
<h3>Cargo</h3>
<pre class="language-bash"><a-o>$</a-o><a-f> </a-f><a-pr>CHANNEL</a-pr><a-f>=</a-f><a-s>&quot;release&quot;</a-s> <a-o>$</a-o><a-pr>cg_gccjit_dir</a-pr>/cargo.sh run</pre>
<p>If you compiled cg_gccjit in debug mode (aka you didn't pass <code>--release</code> to <code>./test.sh</code>) you should use <code>CHANNEL="debug"</code> instead or omit <code>CHANNEL="release"</code> completely.</p>
<h3>Rustc</h3>
<blockquote>
<p>You should prefer using the Cargo method.</p>
</blockquote>
<pre class="language-bash"><a-o>$</a-o><a-f> </a-f><a-pr>rustc</a-pr> +<a-eb>$(</a-eb><a-f>cat</a-f><a-eb> </a-eb><a-o>$</a-o><a-pr>cg_gccjit_dir</a-pr><a-eb>/rust-toolchain)</a-eb> <a-co>-Cpanic=abort</a-co> <a-co>-Zcodegen-backend=</a-co><a-o>$</a-o><a-pr>cg_gccjit_dir</a-pr><a-co>/target/release/librustc_codegen_gcc.so</a-co> <a-co>--sysroot</a-co> <a-o>$</a-o><a-pr>cg_gccjit_dir</a-pr>/build_sysroot/sysroot my_crate.rs</pre>
<h2>Env vars</h2>
&lt;dl&gt;
    &lt;dt&gt;CG_GCCJIT_INCR_CACHE_DISABLED&lt;/dt&gt;
    &lt;dd&gt;Don't cache object files in the incremental cache. Useful during development of cg_gccjit
    to make it possible to use incremental mode for all analyses performed by rustc without caching
    object files when their content should have been changed by a change to cg_gccjit.&lt;/dd&gt;
    &lt;dt&gt;CG_GCCJIT_DISPLAY_CG_TIME&lt;/dt&gt;
    &lt;dd&gt;Display the time it took to perform codegen for a crate&lt;/dd&gt;
&lt;/dl&gt;
<h2>Debugging</h2>
<p>Sometimes, libgccjit will crash and output an error like this:</p>
<pre>during RTL pass: expand
libgccjit.so: error: in expmed_mode_index, at expmed.h:249
0x7f0da2e61a35 expmed_mode_index
	../../../gcc/gcc/expmed.h:249
0x7f0da2e61aa4 expmed_op_cost_ptr
	../../../gcc/gcc/expmed.h:271
0x7f0da2e620dc sdiv_cost_ptr
	../../../gcc/gcc/expmed.h:540
0x7f0da2e62129 sdiv_cost
	../../../gcc/gcc/expmed.h:558
0x7f0da2e73c12 expand_divmod(int, tree_code, machine_mode, rtx_def*, rtx_def*, rtx_def*, int)
	../../../gcc/gcc/expmed.c:4335
0x7f0da2ea1423 expand_expr_real_2(separate_ops*, rtx_def*, machine_mode, expand_modifier)
	../../../gcc/gcc/expr.c:9240
0x7f0da2cd1a1e expand_gimple_stmt_1
	../../../gcc/gcc/cfgexpand.c:3796
0x7f0da2cd1c30 expand_gimple_stmt
	../../../gcc/gcc/cfgexpand.c:3857
0x7f0da2cd90a9 expand_gimple_basic_block
	../../../gcc/gcc/cfgexpand.c:5898
0x7f0da2cdade8 execute
	../../../gcc/gcc/cfgexpand.c:6582
</pre>
<p>To see the code which causes this error, call the following function:</p>
<pre class="language-c"><a-f>gcc_jit_context_dump_to_file</a-f>(<a-v>ctxt</a-v>, <a-s>&quot;/tmp/output.c&quot;</a-s>, <a-n>1</a-n> <a-c>/* update_locations */</a-c>)</pre>
<p>This will create a C-like file and add the locations into the IR pointing to this C file.
Then, rerun the program and it will output the location in the second line:</p>
<pre>libgccjit.so: /tmp/something.c:61322:0: error: in expmed_mode_index, at expmed.h:249
</pre>
<p>Or add a breakpoint to <code>add_error</code> in gdb and print the line number using:</p>
<pre>p loc-&gt;m_line
p loc-&gt;m_filename-&gt;m_buffer
</pre>
<p>To print a debug representation of a tree:</p>
<pre class="language-c"><a-f>debug_tree</a-f>(<a-v>expr</a-v>);</pre>
<p>(defined in print-tree.h)</p>
<p>To print a debug reprensentation of a gimple struct:</p>
<pre class="language-c"><a-v>debug_gimple_stmt</a-v>(<a-t>gimple_struct</a-t>)</pre>
<p>To get the <code>rustc</code> command to run in <code>gdb</code>, add the <code>--verbose</code> flag to <code>cargo build</code>.</p>
<p>To have the correct file paths in <code>gdb</code> instead of <code>/usr/src/debug/gcc/libstdc++-v3/libsupc++/eh_personality.cc</code>:</p>
<p>Maybe by calling the following at the beginning of gdb:</p>
<pre>set substitute-path /usr/src/debug/gcc /path/to/gcc-repo/gcc
</pre>
<p>TODO(antoyo): but that's not what I remember I was doing.</p>
<h3>How to use a custom-build rustc</h3>
<ul>
<li>Build the stage2 compiler (<code>rustup toolchain link debug-current build/x86_64-unknown-linux-gnu/stage2</code>).</li>
<li>Clean and rebuild the codegen with <code>debug-current</code> in the file <code>rust-toolchain</code>.</li>
</ul>
<h3>How to install a forked git-subtree</h3>
<p>Using git-subtree with <code>rustc</code> requires a patched git to make it work.
The PR that is needed is <a href="https://github.com/gitgitgadget/git/pull/493">here</a>.
Use the following instructions to install it:</p>
<pre class="language-bash"><a-f>git</a-f> clone git@github.com:tqc/git.git
<a-f>cd</a-f> git
<a-f>git</a-f> checkout tqc/subtree
<a-f>make</a-f>
<a-f>make</a-f> install
<a-f>cd</a-f> contrib/subtree
<a-f>make</a-f>
<a-f>cp</a-f> git-subtree ~/bin</pre>
<p>Then, do a sync with this command:</p>
<pre class="language-bash"><a-pr>PATH</a-pr>=<a-s>&quot;</a-s><a-o>$</a-o><a-pr>HOME</a-pr><a-s>/bin:</a-s><a-o>$</a-o><a-pr>PATH</a-pr><a-s>&quot;</a-s> <a-f>~/bin/git-subtree</a-f> push <a-co>-P</a-co> compiler/rustc_codegen_gcc/ ../rustc_codegen_gcc/ sync_branch_name
<a-f>cd</a-f> ../rustc_codegen_gcc
<a-f>git</a-f> checkout master
<a-f>git</a-f> pull
<a-f>git</a-f> checkout sync_branch_name
<a-f>git</a-f> merge master</pre>
<p>TODO: write a script that does the above.</p>
<p>https://rust-lang.zulipchat.com/#narrow/stream/301329-t-devtools/topic/subtree.20madness/near/258877725</p>
<h3>How to use <a href="https://github.com/antoyo/mem-trace">mem-trace</a></h3>
<p><code>rustc</code> needs to be built without <code>jemalloc</code> so that <code>mem-trace</code> can overload <code>malloc</code> since <code>jemalloc</code> is linked statically, so a <code>LD_PRELOAD</code>-ed library won't a chance to intercept the calls to <code>malloc</code>.</p>
<h3>How to build a cross-compiling libgccjit</h3>
<h4>Building libgccjit</h4>
<ul>
<li>Follow these instructions: https://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/ with the following changes:</li>
<li>Configure gcc with <code>../gcc/configure --enable-host-shared --disable-multilib --enable-languages=c,jit,c++ --disable-bootstrap --enable-checking=release --prefix=/opt/m68k-gcc/ --target=m68k-linux --without-headers</code>.</li>
<li>Some shells, like fish, don't define the environment variable <code>$MACHTYPE</code>.</li>
<li>Add <code>CFLAGS="-Wno-error=attributes -g -O2"</code> at the end of the configure command for building glibc (<code>CFLAGS="-Wno-error=attributes -Wno-error=array-parameter -Wno-error=stringop-overflow -Wno-error=array-bounds -g -O2"</code> for glibc 2.31, which is useful for Debian).</li>
</ul>
<h4>Configuring rustc_codegen_gcc</h4>
<ul>
<li>Set <code>TARGET_TRIPLE="m68k-unknown-linux-gnu"</code> in config.sh.</li>
<li>Since rustc doesn't support this architecture yet, set it back to <code>TARGET_TRIPLE="mips-unknown-linux-gnu"</code> (or another target having the same attributes). Alternatively, create a <a href="https://book.avr-rust.com/005.1-the-target-specification-json-file.html">target specification file</a> (note that the <code>arch</code> specified in this file must be supported by the rust compiler).</li>
<li>Set <code>linker='-Clinker=m68k-linux-gcc'</code>.</li>
<li>Set the path to the cross-compiling libgccjit in <code>gcc_path</code>.</li>
<li>Comment the line: <code>context.add_command_line_option("-masm=intel");</code> in src/base.rs.</li>
<li>(might not be necessary) Disable the compilation of libstd.so (and possibly libcore.so?).</li>
</ul>
</section><div class="code-panel"><pre class="code src" data-hl="compiler/rustc_codegen_gcc/Readme.md"><span class="code-line"><a class="ln" id="L1" href="#L1">1</a><span class="code-text"># WIP libgccjit codegen backend for rust</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L2" href="#L2">2</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L3" href="#L3">3</a><span class="code-text">[![Chat on IRC](https://img.shields.io/badge/irc.libera.chat-%23rustc__codegen__gcc-blue.svg)](https://web.libera.chat/#rustc_codegen_gcc)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L4" href="#L4">4</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L5" href="#L5">5</a><span class="code-text">This is a GCC codegen for rustc, which means it can be loaded by the existing rustc frontend, but benefits from GCC: more architectures are supported and GCC's optimizations are used.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L6" href="#L6">6</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L7" href="#L7">7</a><span class="code-text">**Despite its name, libgccjit can be used for ahead-of-time compilation, as is used here.**</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L8" href="#L8">8</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L9" href="#L9">9</a><span class="code-text">## Motivation</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L10" href="#L10">10</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L11" href="#L11">11</a><span class="code-text">The primary goal of this project is to be able to compile Rust code on platforms unsupported by LLVM.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L12" href="#L12">12</a><span class="code-text">A secondary goal is to check if using the gcc backend will provide any run-time speed improvement for the programs compiled using rustc.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L13" href="#L13">13</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L14" href="#L14">14</a><span class="code-text">## Building</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L15" href="#L15">15</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L16" href="#L16">16</a><span class="code-text">**This requires a patched libgccjit in order to work.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L17" href="#L17">17</a><span class="code-text">The patches in [this repository](https://github.com/antoyo/libgccjit-patches) need to be applied.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L18" href="#L18">18</a><span class="code-text">(Those patches should work when applied on master, but in case it doesn't work, they are known to work when applied on 079c23cfe079f203d5df83fea8e92a60c7d7e878.)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L19" href="#L19">19</a><span class="code-text">You can also use my [fork of gcc](https://github.com/antoyo/gcc) which already includes these patches.**</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L20" href="#L20">20</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L21" href="#L21">21</a><span class="code-text">To build it (most of these instructions come from [here](https://gcc.gnu.org/onlinedocs/jit/internals/index.html), so don't hesitate to take a look there if you encounter an issue):</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L22" href="#L22">22</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L23" href="#L23">23</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L24" href="#L24">24</a><span class="code-text">$ git clone https://github.com/antoyo/gcc</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L25" href="#L25">25</a><span class="code-text">$ sudo apt install flex libmpfr-dev libgmp-dev libmpc3 libmpc-dev</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L26" href="#L26">26</a><span class="code-text">$ mkdir gcc-build gcc-install</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L27" href="#L27">27</a><span class="code-text">$ cd gcc-build</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L28" href="#L28">28</a><span class="code-text">$ ../gcc/configure \</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L29" href="#L29">29</a><span class="code-text">    --enable-host-shared \</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L30" href="#L30">30</a><span class="code-text">    --enable-languages=jit \</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L31" href="#L31">31</a><span class="code-text">    --enable-checking=release \ # it enables extra checks which allow to find bugs</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L32" href="#L32">32</a><span class="code-text">    --disable-bootstrap \</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L33" href="#L33">33</a><span class="code-text">    --disable-multilib \</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L34" href="#L34">34</a><span class="code-text">    --prefix=$(pwd)/../gcc-install</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L35" href="#L35">35</a><span class="code-text">$ make -j4 # You can replace `4` with another number depending on how many cores you have.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L36" href="#L36">36</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L37" href="#L37">37</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L38" href="#L38">38</a><span class="code-text">If you want to run libgccjit tests, you will need to also enable the C++ language in the `configure`:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L39" href="#L39">39</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L40" href="#L40">40</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L41" href="#L41">41</a><span class="code-text">--enable-languages=jit,c++</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L42" href="#L42">42</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L43" href="#L43">43</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L44" href="#L44">44</a><span class="code-text">Then to run libgccjit tests:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L45" href="#L45">45</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L46" href="#L46">46</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L47" href="#L47">47</a><span class="code-text">$ cd gcc # from the `gcc-build` folder</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L48" href="#L48">48</a><span class="code-text">$ make check-jit</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L49" href="#L49">49</a><span class="code-text"># To run one specific test:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L50" href="#L50">50</a><span class="code-text">$ make check-jit RUNTESTFLAGS=&quot;-v -v -v jit.exp=jit.dg/test-asm.cc&quot;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L51" href="#L51">51</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L52" href="#L52">52</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L53" href="#L53">53</a><span class="code-text">**Put the path to your custom build of libgccjit in the file `gcc_path`.**</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L54" href="#L54">54</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L55" href="#L55">55</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L56" href="#L56">56</a><span class="code-text">$ dirname $(readlink -f `find . -name libgccjit.so`) &gt; gcc_path</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L57" href="#L57">57</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L58" href="#L58">58</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L59" href="#L59">59</a><span class="code-text">You also need to set RUST_COMPILER_RT_ROOT:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L60" href="#L60">60</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L61" href="#L61">61</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L62" href="#L62">62</a><span class="code-text">$ git clone https://github.com/llvm/llvm-project llvm --depth 1 --single-branch</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L63" href="#L63">63</a><span class="code-text">$ export RUST_COMPILER_RT_ROOT=&quot;$PWD/llvm/compiler-rt&quot;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L64" href="#L64">64</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L65" href="#L65">65</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L66" href="#L66">66</a><span class="code-text">Then you can run commands like this:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L67" href="#L67">67</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L68" href="#L68">68</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L69" href="#L69">69</a><span class="code-text">$ ./prepare.sh # download and patch sysroot src and install hyperfine for benchmarking</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L70" href="#L70">70</a><span class="code-text">$ LIBRARY_PATH=$(cat gcc_path) LD_LIBRARY_PATH=$(cat gcc_path) ./build.sh --release</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L71" href="#L71">71</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L72" href="#L72">72</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L73" href="#L73">73</a><span class="code-text">To run the tests:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L74" href="#L74">74</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L75" href="#L75">75</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L76" href="#L76">76</a><span class="code-text">$ ./test.sh --release</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L77" href="#L77">77</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L78" href="#L78">78</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L79" href="#L79">79</a><span class="code-text">## Usage</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L80" href="#L80">80</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L81" href="#L81">81</a><span class="code-text">`$cg_gccjit_dir` is the directory you cloned this repo into in the following instructions.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L82" href="#L82">82</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L83" href="#L83">83</a><span class="code-text">### Cargo</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L84" href="#L84">84</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L85" href="#L85">85</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L86" href="#L86">86</a><span class="code-text">$ CHANNEL=&quot;release&quot; $cg_gccjit_dir/cargo.sh run</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L87" href="#L87">87</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L88" href="#L88">88</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L89" href="#L89">89</a><span class="code-text">If you compiled cg_gccjit in debug mode (aka you didn't pass `--release` to `./test.sh`) you should use `CHANNEL=&quot;debug&quot;` instead or omit `CHANNEL=&quot;release&quot;` completely.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L90" href="#L90">90</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L91" href="#L91">91</a><span class="code-text">### Rustc</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L92" href="#L92">92</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L93" href="#L93">93</a><span class="code-text">&gt; You should prefer using the Cargo method.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L94" href="#L94">94</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L95" href="#L95">95</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L96" href="#L96">96</a><span class="code-text">$ rustc +$(cat $cg_gccjit_dir/rust-toolchain) -Cpanic=abort -Zcodegen-backend=$cg_gccjit_dir/target/release/librustc_codegen_gcc.so --sysroot $cg_gccjit_dir/build_sysroot/sysroot my_crate.rs</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L97" href="#L97">97</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L98" href="#L98">98</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L99" href="#L99">99</a><span class="code-text">## Env vars</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L100" href="#L100">100</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L101" href="#L101">101</a><span class="code-text">&lt;dl&gt;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L102" href="#L102">102</a><span class="code-text">    &lt;dt&gt;CG_GCCJIT_INCR_CACHE_DISABLED&lt;/dt&gt;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L103" href="#L103">103</a><span class="code-text">    &lt;dd&gt;Don't cache object files in the incremental cache. Useful during development of cg_gccjit</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L104" href="#L104">104</a><span class="code-text">    to make it possible to use incremental mode for all analyses performed by rustc without caching</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L105" href="#L105">105</a><span class="code-text">    object files when their content should have been changed by a change to cg_gccjit.&lt;/dd&gt;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L106" href="#L106">106</a><span class="code-text">    &lt;dt&gt;CG_GCCJIT_DISPLAY_CG_TIME&lt;/dt&gt;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L107" href="#L107">107</a><span class="code-text">    &lt;dd&gt;Display the time it took to perform codegen for a crate&lt;/dd&gt;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L108" href="#L108">108</a><span class="code-text">&lt;/dl&gt;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L109" href="#L109">109</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L110" href="#L110">110</a><span class="code-text">## Debugging</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L111" href="#L111">111</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L112" href="#L112">112</a><span class="code-text">Sometimes, libgccjit will crash and output an error like this:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L113" href="#L113">113</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L114" href="#L114">114</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L115" href="#L115">115</a><span class="code-text">during RTL pass: expand</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L116" href="#L116">116</a><span class="code-text">libgccjit.so: error: in expmed_mode_index, at expmed.h:249</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L117" href="#L117">117</a><span class="code-text">0x7f0da2e61a35 expmed_mode_index</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L118" href="#L118">118</a><span class="code-text">	../../../gcc/gcc/expmed.h:249</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L119" href="#L119">119</a><span class="code-text">0x7f0da2e61aa4 expmed_op_cost_ptr</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L120" href="#L120">120</a><span class="code-text">	../../../gcc/gcc/expmed.h:271</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L121" href="#L121">121</a><span class="code-text">0x7f0da2e620dc sdiv_cost_ptr</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L122" href="#L122">122</a><span class="code-text">	../../../gcc/gcc/expmed.h:540</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L123" href="#L123">123</a><span class="code-text">0x7f0da2e62129 sdiv_cost</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L124" href="#L124">124</a><span class="code-text">	../../../gcc/gcc/expmed.h:558</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L125" href="#L125">125</a><span class="code-text">0x7f0da2e73c12 expand_divmod(int, tree_code, machine_mode, rtx_def*, rtx_def*, rtx_def*, int)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L126" href="#L126">126</a><span class="code-text">	../../../gcc/gcc/expmed.c:4335</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L127" href="#L127">127</a><span class="code-text">0x7f0da2ea1423 expand_expr_real_2(separate_ops*, rtx_def*, machine_mode, expand_modifier)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L128" href="#L128">128</a><span class="code-text">	../../../gcc/gcc/expr.c:9240</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L129" href="#L129">129</a><span class="code-text">0x7f0da2cd1a1e expand_gimple_stmt_1</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L130" href="#L130">130</a><span class="code-text">	../../../gcc/gcc/cfgexpand.c:3796</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L131" href="#L131">131</a><span class="code-text">0x7f0da2cd1c30 expand_gimple_stmt</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L132" href="#L132">132</a><span class="code-text">	../../../gcc/gcc/cfgexpand.c:3857</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L133" href="#L133">133</a><span class="code-text">0x7f0da2cd90a9 expand_gimple_basic_block</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L134" href="#L134">134</a><span class="code-text">	../../../gcc/gcc/cfgexpand.c:5898</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L135" href="#L135">135</a><span class="code-text">0x7f0da2cdade8 execute</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L136" href="#L136">136</a><span class="code-text">	../../../gcc/gcc/cfgexpand.c:6582</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L137" href="#L137">137</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L138" href="#L138">138</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L139" href="#L139">139</a><span class="code-text">To see the code which causes this error, call the following function:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L140" href="#L140">140</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L141" href="#L141">141</a><span class="code-text">```c</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L142" href="#L142">142</a><span class="code-text">gcc_jit_context_dump_to_file(ctxt, &quot;/tmp/output.c&quot;, 1 /* update_locations */)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L143" href="#L143">143</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L144" href="#L144">144</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L145" href="#L145">145</a><span class="code-text">This will create a C-like file and add the locations into the IR pointing to this C file.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L146" href="#L146">146</a><span class="code-text">Then, rerun the program and it will output the location in the second line:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L147" href="#L147">147</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L148" href="#L148">148</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L149" href="#L149">149</a><span class="code-text">libgccjit.so: /tmp/something.c:61322:0: error: in expmed_mode_index, at expmed.h:249</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L150" href="#L150">150</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L151" href="#L151">151</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L152" href="#L152">152</a><span class="code-text">Or add a breakpoint to `add_error` in gdb and print the line number using:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L153" href="#L153">153</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L154" href="#L154">154</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L155" href="#L155">155</a><span class="code-text">p loc-&gt;m_line</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L156" href="#L156">156</a><span class="code-text">p loc-&gt;m_filename-&gt;m_buffer</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L157" href="#L157">157</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L158" href="#L158">158</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L159" href="#L159">159</a><span class="code-text">To print a debug representation of a tree:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L160" href="#L160">160</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L161" href="#L161">161</a><span class="code-text">```c</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L162" href="#L162">162</a><span class="code-text">debug_tree(expr);</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L163" href="#L163">163</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L164" href="#L164">164</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L165" href="#L165">165</a><span class="code-text">(defined in print-tree.h)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L166" href="#L166">166</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L167" href="#L167">167</a><span class="code-text">To print a debug reprensentation of a gimple struct:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L168" href="#L168">168</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L169" href="#L169">169</a><span class="code-text">```c</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L170" href="#L170">170</a><span class="code-text">debug_gimple_stmt(gimple_struct)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L171" href="#L171">171</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L172" href="#L172">172</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L173" href="#L173">173</a><span class="code-text">To get the `rustc` command to run in `gdb`, add the `--verbose` flag to `cargo build`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L174" href="#L174">174</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L175" href="#L175">175</a><span class="code-text">To have the correct file paths in `gdb` instead of `/usr/src/debug/gcc/libstdc++-v3/libsupc++/eh_personality.cc`:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L176" href="#L176">176</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L177" href="#L177">177</a><span class="code-text">Maybe by calling the following at the beginning of gdb:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L178" href="#L178">178</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L179" href="#L179">179</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L180" href="#L180">180</a><span class="code-text">set substitute-path /usr/src/debug/gcc /path/to/gcc-repo/gcc</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L181" href="#L181">181</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L182" href="#L182">182</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L183" href="#L183">183</a><span class="code-text">TODO(antoyo): but that's not what I remember I was doing.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L184" href="#L184">184</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L185" href="#L185">185</a><span class="code-text">### How to use a custom-build rustc</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L186" href="#L186">186</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L187" href="#L187">187</a><span class="code-text"> * Build the stage2 compiler (`rustup toolchain link debug-current build/x86_64-unknown-linux-gnu/stage2`).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L188" href="#L188">188</a><span class="code-text"> * Clean and rebuild the codegen with `debug-current` in the file `rust-toolchain`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L189" href="#L189">189</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L190" href="#L190">190</a><span class="code-text">### How to install a forked git-subtree</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L191" href="#L191">191</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L192" href="#L192">192</a><span class="code-text">Using git-subtree with `rustc` requires a patched git to make it work.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L193" href="#L193">193</a><span class="code-text">The PR that is needed is [here](https://github.com/gitgitgadget/git/pull/493).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L194" href="#L194">194</a><span class="code-text">Use the following instructions to install it:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L195" href="#L195">195</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L196" href="#L196">196</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L197" href="#L197">197</a><span class="code-text">git clone git@github.com:tqc/git.git</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L198" href="#L198">198</a><span class="code-text">cd git</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L199" href="#L199">199</a><span class="code-text">git checkout tqc/subtree</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L200" href="#L200">200</a><span class="code-text">make</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L201" href="#L201">201</a><span class="code-text">make install</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L202" href="#L202">202</a><span class="code-text">cd contrib/subtree</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L203" href="#L203">203</a><span class="code-text">make</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L204" href="#L204">204</a><span class="code-text">cp git-subtree ~/bin</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L205" href="#L205">205</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L206" href="#L206">206</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L207" href="#L207">207</a><span class="code-text">Then, do a sync with this command:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L208" href="#L208">208</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L209" href="#L209">209</a><span class="code-text">```bash</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L210" href="#L210">210</a><span class="code-text">PATH=&quot;$HOME/bin:$PATH&quot; ~/bin/git-subtree push -P compiler/rustc_codegen_gcc/ ../rustc_codegen_gcc/ sync_branch_name</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L211" href="#L211">211</a><span class="code-text">cd ../rustc_codegen_gcc</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L212" href="#L212">212</a><span class="code-text">git checkout master</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L213" href="#L213">213</a><span class="code-text">git pull</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L214" href="#L214">214</a><span class="code-text">git checkout sync_branch_name</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L215" href="#L215">215</a><span class="code-text">git merge master</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L216" href="#L216">216</a><span class="code-text">```</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L217" href="#L217">217</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L218" href="#L218">218</a><span class="code-text">TODO: write a script that does the above.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L219" href="#L219">219</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L220" href="#L220">220</a><span class="code-text">https://rust-lang.zulipchat.com/#narrow/stream/301329-t-devtools/topic/subtree.20madness/near/258877725</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L221" href="#L221">221</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L222" href="#L222">222</a><span class="code-text">### How to use [mem-trace](https://github.com/antoyo/mem-trace)</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L223" href="#L223">223</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L224" href="#L224">224</a><span class="code-text">`rustc` needs to be built without `jemalloc` so that `mem-trace` can overload `malloc` since `jemalloc` is linked statically, so a `LD_PRELOAD`-ed library won't a chance to intercept the calls to `malloc`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L225" href="#L225">225</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L226" href="#L226">226</a><span class="code-text">### How to build a cross-compiling libgccjit</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L227" href="#L227">227</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L228" href="#L228">228</a><span class="code-text">#### Building libgccjit</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L229" href="#L229">229</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L230" href="#L230">230</a><span class="code-text"> * Follow these instructions: https://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/ with the following changes:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L231" href="#L231">231</a><span class="code-text"> * Configure gcc with `../gcc/configure --enable-host-shared --disable-multilib --enable-languages=c,jit,c++ --disable-bootstrap --enable-checking=release --prefix=/opt/m68k-gcc/ --target=m68k-linux --without-headers`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L232" href="#L232">232</a><span class="code-text"> * Some shells, like fish, don't define the environment variable `$MACHTYPE`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L233" href="#L233">233</a><span class="code-text"> * Add `CFLAGS=&quot;-Wno-error=attributes -g -O2&quot;` at the end of the configure command for building glibc (`CFLAGS=&quot;-Wno-error=attributes -Wno-error=array-parameter -Wno-error=stringop-overflow -Wno-error=array-bounds -g -O2&quot;` for glibc 2.31, which is useful for Debian).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L234" href="#L234">234</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L235" href="#L235">235</a><span class="code-text">#### Configuring rustc_codegen_gcc</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L236" href="#L236">236</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L237" href="#L237">237</a><span class="code-text"> * Set `TARGET_TRIPLE=&quot;m68k-unknown-linux-gnu&quot;` in config.sh.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L238" href="#L238">238</a><span class="code-text"> * Since rustc doesn't support this architecture yet, set it back to `TARGET_TRIPLE=&quot;mips-unknown-linux-gnu&quot;` (or another target having the same attributes). Alternatively, create a [target specification file](https://book.avr-rust.com/005.1-the-target-specification-json-file.html) (note that the `arch` specified in this file must be supported by the rust compiler).</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L239" href="#L239">239</a><span class="code-text"> * Set `linker='-Clinker=m68k-linux-gcc'`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L240" href="#L240">240</a><span class="code-text"> * Set the path to the cross-compiling libgccjit in `gcc_path`.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L241" href="#L241">241</a><span class="code-text"> * Comment the line: `context.add_command_line_option(&quot;-masm=intel&quot;);` in src/base.rs.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L242" href="#L242">242</a><span class="code-text"> * (might not be necessary) Disable the compilation of libstd.so (and possibly libcore.so?).</span></span></pre></div></div>
</main>
</body>
</html>
