Skip to content

Commit f018f98

Browse files
f15hrmkannwischer
authored andcommitted
HOL-Light: Allow cross-generation of byte code
- Ported from pq-code-package/mlkem-native#1444 Signed-off-by: Liam Fisher <liam.louis.fisher@gmail.com>
1 parent d1304ec commit f018f98

File tree

5 files changed

+100
-40
lines changed

5 files changed

+100
-40
lines changed

.github/workflows/ci.yml

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -725,15 +725,63 @@ jobs:
725725
strategy:
726726
fail-fast: false
727727
matrix:
728-
system: [ubuntu-latest, ubuntu-24.04-arm]
729-
runs-on: ${{ matrix.system }}
728+
target:
729+
- system: macos-latest
730+
nix_shell: 'ci-cross-x86_64'
731+
nix_cache: 'true'
732+
extra_args: '--force-cross'
733+
# TODO: This does not yet work (#1304) <- MLKEM ISSUE, Port when fixed
734+
# - system: macos-15-intel
735+
# nix_cache: 'false'
736+
# nix_shell: 'ci'
737+
- system: ubuntu-latest
738+
nix_shell: 'ci-cross-aarch64'
739+
nix_cache: 'true'
740+
extra_args: '--force-cross'
741+
- system: ubuntu-24.04-arm
742+
nix_shell: 'ci-cross-x86_64'
743+
nix_cache: 'true'
744+
extra_args: '--force-cross'
745+
runs-on: ${{ matrix.target.system }}
730746
name: Check autogenerated files
731747
steps:
732748
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
733749
- uses: ./.github/actions/setup-shell
734750
with:
735-
nix-shell: 'ci-cross' # Need cross-compiler for ASM simplification
736-
nix-cache: 'true'
751+
nix-shell: ${{ matrix.target.nix_shell }}
752+
nix-cache: ${{ matrix.target.nix_cache }}
753+
gh_token: ${{ secrets.GITHUB_TOKEN }}
754+
script: |
755+
python3 ./scripts/autogen --dry-run ${{ matrix.target.extra_args }}
756+
check_hol_light_object_code:
757+
strategy:
758+
fail-fast: false
759+
matrix:
760+
target:
761+
- system: macos-latest
762+
nix_cache: 'true'
763+
nix_shell: 'hol_light-cross-x86_64'
764+
extra_args: '--force-cross'
765+
# TODO: autogen does not yet work on macos15-intel (#1304)
766+
# - system: macos-15-intel
767+
# nix_cache: 'false'
768+
# nix_shell: 'ci'
769+
- system: ubuntu-latest
770+
nix_shell: 'hol_light-cross-aarch64'
771+
nix_cache: 'true'
772+
extra_args: '--force-cross'
773+
- system: ubuntu-24.04-arm
774+
nix_shell: 'hol_light-cross-x86_64'
775+
nix_cache: 'true'
776+
extra_args: '--force-cross'
777+
runs-on: ${{ matrix.target.system }}
778+
name: Check object code in HOL-Light proofs
779+
steps:
780+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
781+
- uses: ./.github/actions/setup-shell
782+
with:
783+
nix-shell: ${{ matrix.target.nix_shell }}
784+
nix-cache: ${{ matrix.target.nix_cache }}
737785
gh_token: ${{ secrets.GITHUB_TOKEN }}
738786
script: |
739-
python3 ./scripts/autogen --dry-run --force-cross
787+
python3 ./scripts/autogen --dry-run --update-hol-light-bytecode ${{ matrix.target.extra_args }}

.github/workflows/nix.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ jobs:
6969
devShell: ci
7070
gh_token: ${{ secrets.GITHUB_TOKEN }}
7171
script: |
72-
# NOTE: we're not running cross compilation tests on macOS currently
73-
# and building cross compilation toolchain on macOS runner took
74-
# extremely long time
75-
if [[ ${{ runner.os }} != 'macOS' ]]; then
72+
# We only run cross-compilation checks for x86 on macos-latest,
73+
# so restrict caching to the corresponding cross shell.
74+
if [[ ${{ runner.os }} == 'macOS' ]]; then
75+
nix develop .#ci-cross-x86_64 --profile tmp-cross
76+
else
7677
nix develop .#ci-cross --profile tmp-cross
7778
# GH ubuntu-24.04 image tend to run outof space
7879
if [[ ${{ matrix.runner }} == 'ubuntu-24.04' ]]; then

proofs/hol_light/x86_64/Makefile

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,19 @@ ARCHTYPE_RESULT=$(shell uname -m)
1919
SRC ?= $(S2N_BIGNUM_DIR)
2020
SRC_X86 ?= $(SRC)/x86
2121

22+
# If actually on an x86_64 machine, just use the assembler (as). Otherwise
23+
# use a cross-assembling version so that the code can still be assembled
24+
# and the proofs checked against the object files (though you won't be able
25+
# to run code without additional emulation infrastructure).
26+
ifeq ($(filter $(ARCHTYPE_RESULT),x86_64),)
27+
CROSS_PREFIX=x86_64-unknown-linux-gnu-
28+
ifeq ($(shell command -v $(ASSEMBLE) >/dev/null 2>&1 && echo yes || echo no),no)
29+
$(error Cross-toolchain not found. Please run in the 'hol_light-cross-x86_64' nix shell via: nix develop .#hol_light-cross-x86_64)
30+
endif
31+
endif
32+
ASSEMBLE=$(CROSS_PREFIX)as $(ARCHFLAGS)
33+
OBJDUMP=$(CROSS_PREFIX)objdump -d
34+
2235
# Add explicit language input parameter to cpp, otherwise the use of #n for
2336
# numeric literals in x86 code is a problem when used inside #define macros
2437
# since normally that means stringization.
@@ -37,29 +50,6 @@ endif
3750

3851
SPLIT=tr ';' '\n'
3952

40-
# If actually on an x86_64 machine, just use the assembler (as). Otherwise
41-
# use a cross-assembling version so that the code can still be assembled
42-
# and the proofs checked against the object files (though you won't be able
43-
# to run code without additional emulation infrastructure). For the clang
44-
# version on OS X we just add the "-arch x86_64" option. For the Linux/gcc
45-
# toolchain we assume the presence of the special cross-assembler. This
46-
# can be installed via something like:
47-
#
48-
# sudo apt-get install binutils-x86-64-linux-gnu
49-
50-
ifeq ($(ARCHTYPE_RESULT),x86_64)
51-
ASSEMBLE=as
52-
OBJDUMP=objdump -d
53-
else
54-
ifeq ($(OSTYPE_RESULT),Darwin)
55-
ASSEMBLE=as -arch x86_64
56-
OBJDUMP=otool -tvV
57-
else
58-
ASSEMBLE=x86_64-linux-gnu-as
59-
OBJDUMP=x86_64-linux-gnu-objdump -d
60-
endif
61-
endif
62-
6353
OBJ = mldsa/mldsa_ntt.o
6454

6555
# Build object files from assembly sources

scripts/autogen

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,16 +2914,30 @@ def update_bytecode_in_proof_script(filepath, bytecode):
29142914
update_file(filepath, updated_content)
29152915

29162916

2917-
def update_hol_light_bytecode():
2918-
"""Update HOL Light proof files with bytecode from make dump_bytecode."""
2917+
def update_hol_light_bytecode_for_arch(arch, force_cross=False):
29192918
status_update(
2920-
"HOL Light bytecode",
2921-
"Running make dump_bytecode ... (this may take a few minutes)",
2919+
f"HOL Light bytecode ({arch})",
2920+
f"Running make dump_bytecode ({arch})... (this may take a few minutes)",
29222921
)
29232922

2923+
source_arch = arch
2924+
if platform.machine().lower() in ["arm64", "aarch64"]:
2925+
native_arch = "aarch64"
2926+
else:
2927+
native_arch = "x86_64"
2928+
2929+
if native_arch != source_arch:
2930+
cross_prefix = f"{source_arch}-unknown-linux-gnu-"
2931+
cross_gcc = cross_prefix + "gcc"
2932+
# Check if cross-compiler is present
2933+
if shutil.which(cross_gcc) is None:
2934+
if force_cross is False:
2935+
return
2936+
raise Exception(f"Could not find cross toolchain {cross_prefix}")
2937+
29242938
# Run make to get bytecode output
29252939
result = subprocess.run(
2926-
["make", "-C", "proofs/hol_light/x86_64", "dump_bytecode"],
2940+
["make", "-C", "proofs/hol_light/" + arch, "dump_bytecode"],
29272941
capture_output=True,
29282942
text=True,
29292943
check=True,
@@ -2935,10 +2949,17 @@ def update_hol_light_bytecode():
29352949

29362950
# Update each .ml file
29372951
for obj_name, bytecode in bytecode_dict.items():
2938-
ml_file = "proofs/hol_light/x86_64/proofs/" + obj_name + ".ml"
2952+
ml_file = "proofs/hol_light/" + arch + "/proofs/" + obj_name + ".ml"
29392953
update_bytecode_in_proof_script(ml_file, bytecode)
29402954

29412955

2956+
def update_hol_light_bytecode(force_cross=False):
2957+
"""Update HOL Light proof files with bytecode from make dump_bytecode."""
2958+
# NOTE: The following line is commented out until there are hol_light aarch64 proofs.
2959+
# update_hol_light_bytecode_for_arch("aarch64", force_cross=force_cross)
2960+
update_hol_light_bytecode_for_arch("x86_64", force_cross=force_cross)
2961+
2962+
29422963
def gen_test_config(config_path, config_spec, default_config_content):
29432964
"""Generate a config file by modifying the default config."""
29442965
status_update("test configs", config_path)
@@ -3164,7 +3185,7 @@ def _main():
31643185
high_level_status("Completed final backend synchronization")
31653186

31663187
if args.update_hol_light_bytecode:
3167-
update_hol_light_bytecode()
3188+
update_hol_light_bytecode(args.force_cross)
31683189
high_level_status("Updated HOL Light bytecode")
31693190

31703191
gen_monolithic_source_file()

scripts/simpasm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def simplify(logger, args, asm_input, asm_output=None):
247247
logger.debug(f"Using raw global symbol {sym} going forward ...")
248248

249249
cmd = [args.objdump, "--disassemble", tmp_objfile0]
250-
if platform.system() == "Darwin":
250+
if platform.system() == "Darwin" and args.arch == "aarch64":
251251
cmd += ["--triple=aarch64"]
252252

253253
# Add syntax option if specified

0 commit comments

Comments
 (0)