Skip to content

Commit 08dbe9c

Browse files
committed
autogen: Prepare for rv64 and ppc64le backends
This commit prepares scripts/autogen and scripts/cfify for the work-in-progress addition of riscv64 and ppc64le backends. Specifically, simpasm needs to be invoked with the right cross compiler for those architectures, and scripts/cfify needs to accept riscv64 and ppc64le architecture parameters. Signed-off-by: Hanno Becker <[email protected]>
1 parent b15ba6b commit 08dbe9c

File tree

3 files changed

+88
-49
lines changed

3 files changed

+88
-49
lines changed

scripts/autogen

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,10 @@ def gen_monolithic_source_file(dry_run=False):
14311431
for c in filter(native_arith_x86_64, c_sources):
14321432
yield f'#include "{c}"'
14331433
yield "#endif"
1434+
yield "#if defined(MLK_SYS_PPC64LE)"
1435+
for c in filter(native_arith_ppc64le, c_sources):
1436+
yield f'#include "{c}"'
1437+
yield "#endif"
14341438
yield "#endif"
14351439
yield ""
14361440
yield "#if defined(MLK_CONFIG_USE_NATIVE_BACKEND_FIPS202)"
@@ -1766,6 +1770,10 @@ def update_via_simpasm(
17661770
source_arch = "aarch64"
17671771
elif "x86_64" in infile_full:
17681772
source_arch = "x86_64"
1773+
elif "ppc64le" in infile_full:
1774+
source_arch = "ppc64le"
1775+
elif "riscv64" in infile_full:
1776+
source_arch = "riscv64"
17691777
else:
17701778
raise Exception(f"Could not detect architecture of source file {infile_full}.")
17711779
# Check native architecture
@@ -1775,7 +1783,14 @@ def update_via_simpasm(
17751783
native_arch = "x86_64"
17761784

17771785
if native_arch != source_arch:
1778-
cross_prefix = f"{source_arch}-unknown-linux-gnu-"
1786+
arch_to_cross_prefix = {
1787+
"aarch64": "aarch64-unknown-linux-gnu-",
1788+
"x86_64": "x86_64-unknown-linux-gnu-",
1789+
"ppc64le": "powerpc64le-unknown-linux-gnu-",
1790+
"riscv64": "riscv64-unknown-linux-gnu-",
1791+
}
1792+
1793+
cross_prefix = arch_to_cross_prefix[source_arch]
17791794
cross_gcc = cross_prefix + "gcc"
17801795
# Check if cross-compiler is present
17811796
if shutil.which(cross_gcc) is None:
@@ -1788,13 +1803,12 @@ def update_via_simpasm(
17881803
with tempfile.NamedTemporaryFile(suffix=".S") as tmp:
17891804
try:
17901805
# Determine architecture from filename
1791-
arch = "aarch64" if "aarch64" in infile_full else "x86_64"
17921806

17931807
cmd = [
17941808
"./scripts/simpasm",
17951809
"--objdump=llvm-objdump",
17961810
"--cfify",
1797-
"--arch=" + arch,
1811+
"--arch=" + source_arch,
17981812
"-i",
17991813
infile_full,
18001814
"-o",
@@ -2058,49 +2072,55 @@ def synchronize_backends(
20582072
),
20592073
)
20602074

2061-
synchronize_backend(
2062-
f"dev/aarch64_{ty}/src",
2063-
"mlkem/src/native/aarch64/src",
2064-
dry_run=dry_run,
2065-
delete=delete,
2066-
force_cross=force_cross,
2067-
no_simplify=no_simplify,
2068-
cflags="-Imlkem/src/native/aarch64/src",
2069-
)
2070-
synchronize_backend(
2071-
"dev/fips202/aarch64/src",
2072-
"mlkem/src/fips202/native/aarch64/src",
2073-
dry_run=dry_run,
2074-
delete=delete,
2075-
force_cross=force_cross,
2076-
no_simplify=no_simplify,
2077-
cflags="-Imlkem/src/fips202/native/aarch64/src -march=armv8.4-a+sha3",
2078-
)
2079-
synchronize_backend(
2080-
"dev/fips202/aarch64",
2081-
"mlkem/src/fips202/native/aarch64",
2082-
dry_run=dry_run,
2083-
delete=delete,
2084-
force_cross=force_cross,
2085-
no_simplify=no_simplify,
2086-
cflags="-Imlkem/src/fips202/native/aarch64 -march=armv8.4-a+sha3",
2087-
)
2088-
synchronize_backend(
2089-
"dev/x86_64/src",
2090-
"mlkem/src/native/x86_64/src",
2091-
dry_run=dry_run,
2092-
delete=delete,
2093-
force_cross=force_cross,
2094-
no_simplify=no_simplify,
2095-
# Turn off control-flow protection (CET) explicitly. Newer versions of
2096-
# clang turn it on by default and insert endbr64 instructions at every
2097-
# global symbol.
2098-
# We insert endbr64 instruction manually via the MLK_ASM_FN_SYMBOL
2099-
# macro.
2100-
# This leads to duplicate endbr64 instructions causing a failure when
2101-
# comparing the object code before and after simplification.
2102-
cflags="-Imlkem/src/native/x86_64/src/ -mavx2 -mbmi2 -msse4 -fcf-protection=none",
2103-
)
2075+
# Triples of
2076+
# - input backend directory under dev/
2077+
# - output backend directory under mlkem/
2078+
# - cflags
2079+
worklist = [
2080+
(
2081+
f"dev/aarch64_{ty}/src",
2082+
"mlkem/src/native/aarch64/src",
2083+
"-Imlkem/src/native/aarch64/src",
2084+
),
2085+
(
2086+
"dev/fips202/aarch64/src",
2087+
"mlkem/src/fips202/native/aarch64/src",
2088+
"-Imlkem/src/fips202/native/aarch64/src -march=armv8.4-a+sha3",
2089+
),
2090+
(
2091+
"dev/fips202/aarch64",
2092+
"mlkem/src/fips202/native/aarch64",
2093+
"-Imlkem/src/fips202/native/aarch64 -march=armv8.4-a+sha3",
2094+
),
2095+
(
2096+
"dev/x86_64/src",
2097+
"mlkem/src/native/x86_64/src",
2098+
# Turn off control-flow protection (CET) explicitly. Newer versions of
2099+
# clang turn it on by default and insert endbr64 instructions at every
2100+
# global symbol.
2101+
# We insert endbr64 instruction manually via the MLK_ASM_FN_SYMBOL
2102+
# macro.
2103+
# This leads to duplicate endbr64 instructions causing a failure when
2104+
# comparing the object code before and after simplification.
2105+
"-Imlkem/src/native/x86_64/src/ -mavx2 -mbmi2 -msse4 -fcf-protection=none",
2106+
),
2107+
(
2108+
"dev/ppc64le/src",
2109+
"mlkem/src/native/ppc64le/src",
2110+
"-Imlkem/src/native/ppc64le/src -mvsx",
2111+
),
2112+
]
2113+
2114+
for in_dir, out_dir, cflags in worklist:
2115+
synchronize_backend(
2116+
in_dir,
2117+
out_dir,
2118+
dry_run=dry_run,
2119+
delete=delete,
2120+
force_cross=force_cross,
2121+
no_simplify=no_simplify,
2122+
cflags=cflags,
2123+
)
21042124

21052125

21062126
def adjust_header_guard_for_filename(content, header_file):

scripts/cfify

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,19 @@ def add_cfi_directives(text, arch):
226226
i += 1
227227
continue
228228

229+
elif arch == "riscv64":
230+
# No special handling of riscv64 for now
231+
pass
232+
elif arch == "ppc64le":
233+
# ppc64le: blr -> .cfi_endproc after blr
234+
match = re.match(r"(\s*)blr\s*$", line, re.IGNORECASE)
235+
if match:
236+
indent = match.group(1)
237+
result.append(line)
238+
result.append(f"{indent}.cfi_endproc")
239+
i += 1
240+
continue
241+
229242
result.append(line)
230243
i += 1
231244

@@ -246,7 +259,7 @@ def main():
246259
)
247260
parser.add_argument(
248261
"--arch",
249-
choices=["aarch64", "x86_64"],
262+
choices=["aarch64", "x86_64", "riscv64", "ppc64le"],
250263
default="aarch64",
251264
help="Target architecture (default: aarch64)",
252265
)

scripts/simpasm

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def simplify(logger, args, asm_input, asm_output=None):
246246
logger.debug(f"Using raw global symbol {sym} going forward ...")
247247

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

252252
logger.debug(f"Disassembling temporary object file {tmp_objfile0} ...")
@@ -255,6 +255,12 @@ def simplify(logger, args, asm_input, asm_output=None):
255255
logger.debug("Patching up disassembly ...")
256256
simplified = patchup_disasm(disasm, cfify=args.cfify)
257257

258+
# On ppc64le we're using 16 byte alignment
259+
if args.arch == "ppc64le":
260+
align = 16
261+
else:
262+
align = 4
263+
258264
autogen_header = [
259265
"",
260266
"/*",
@@ -264,7 +270,7 @@ def simplify(logger, args, asm_input, asm_output=None):
264270
"",
265271
"",
266272
".text",
267-
".balign 4",
273+
f".balign {align}",
268274
]
269275

270276
if args.preserve_preprocessor_directives is False:

0 commit comments

Comments
 (0)