Skip to content

Commit

Permalink
Re-transpile fns with gotos removed now (#425)
Browse files Browse the repository at this point in the history
#421 removed a lot of the simple error/cleanup `goto`s in C. This
re-transpiles those functions and merges the new versions back in with
the relooper state machines now removed.

I only re-transpiled and merged back functions used by the `dav1d`
binary (the others aren't as important).

I also included the `retranspile.sh` script I used to help with this, as
we may want to do this again for something (e.x. more `goto`s, upstream
changes to `dav1d`, etc.).

Also, some upstream changes weren't made, but these were minor, such as
redundant `as` casts and type annotations. It'll be easier to do these
in bulk later (and the reason they were only done sporadically before
was because the code was so heavily indented due to the state machines).
  • Loading branch information
kkysen authored Aug 31, 2023
2 parents bf9f039 + ce29a0a commit 00faf25
Show file tree
Hide file tree
Showing 5 changed files with 3,333 additions and 4,813 deletions.
16 changes: 16 additions & 0 deletions retranspile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# How to Re-Transpile `fn`s After Changes to the C code

1. Make your changes to the C code.
2. Run `./retranspile.sh transpile` to actually re-transpile.
3. For each `fn` `${fn_name}`,
1. Copy `fn ${fn_name}` and any new callees to `retranspile/${fn_name}.fn.new`.
4. Run `./retranspile.sh stash`, `stash`ing all of the re-transpile changes.
5. For each `fn` `${fn_name}`,
1. Run `./retranspile.sh fn-diff ${fn_name}`, saving `fn ${fn_name}`'s diff
(it unfortunately shows a lot extra, too)
from the `initial-transpile` to `retranspile/${fn_name}.fn.diff`.
2. Replace the existing `fn` with the new version.
3. Patch back the changes in `retranspile/${fn_name}.fn.diff`, probably manually.
4. Run `./retranspile.sh commit ${fn_name}`, committing the re-transpiled changes.
6. Run `./retranspile.sh cleanup ${fn_name}` to cleanup intermediate files for `${fn_name}`,
or just `./retranspile.sh cleanup` to cleanup them all.
60 changes: 60 additions & 0 deletions retranspile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env bash

set -euox pipefail

mkdir -p retranspile/

transpile() {
local c2rust_dir="../c2rust-for-rav1d"
local c2rust="${c2rust_dir}/target/release/c2rust"

if [[ ! -x "${c2rust}" ]]; then
rm -rf "${c2rust_dir}"
# need this specific branch, so just build our own copy of c2rust
git clone \
--branch perl/c11_atomics \
--depth 1 \
https://github.com/immunant/c2rust.git "${c2rust_dir}"
(cd "${c2rust_dir}"
cargo build --release
)
fi

export CC=clang
rm -rf build
mkdir build
meson setup build \
--reconfigure \
-Dtest_rust=false \
-Denable_asm=false \
"-Dbitdepths=['8','16']"
bear -- ninja -C build tools/dav1d
"${c2rust}" transpile compile_commands.json --binary dav1d --overwrite-existing
}

stash() {
git add .
git stash push -m 'retranspiled dav1d'
}

fn-diff() {
local fn_name="$1"
git diff "initial-transpile-fmt..$(git branch --show-current)" \
--ignore-space-change \
-G"[^ ] fn ${fn_name}" \
> "retranspile/${fn_name}.fn.diff"
delta < "retranspile/${fn_name}.fn.diff"
}

commit() {
local fn_name="$1"
git commit -m "\`fn ${fn_name}\`: Re-transpile."
}

cleanup() {
local fn_name="${1:-*}"
rm -rf retranspile/${fn_name}.fn.*
rmdir --ignore-fail-on-non-empty retranspile/
}

"${@}"
Loading

0 comments on commit 00faf25

Please sign in to comment.