feat: merging changes related to f16 formatting

This commit is contained in:
Madhav Madhusoodanan 2025-05-21 13:50:33 +05:30 committed by Amanieu d'Antras
parent 2a5e678a84
commit 6de5b7bef8
7 changed files with 105 additions and 7 deletions

View file

@ -21,6 +21,93 @@ pub const POLY128_OSTREAM_DEF: &str = r#"std::ostream& operator<<(std::ostream&
return os;
}"#;
// Format f16 values (and vectors containing them) in a way that is consistent with C.
pub const F16_FORMATTING_DEF: &str = r#"
/// Used to continue `Debug`ging SIMD types as `MySimd(1, 2, 3, 4)`, as they
/// were before moving to array-based simd.
#[inline]
fn debug_simd_finish<T: core::fmt::Debug, const N: usize>(
formatter: &mut core::fmt::Formatter<'_>,
type_name: &str,
array: &[T; N],
) -> core::fmt::Result {
core::fmt::Formatter::debug_tuple_fields_finish(
formatter,
type_name,
&core::array::from_fn::<&dyn core::fmt::Debug, N, _>(|i| &array[i]),
)
}
#[repr(transparent)]
struct Hex<T>(T);
impl<T: DebugHexF16> core::fmt::Debug for Hex<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
<T as DebugHexF16>::fmt(&self.0, f)
}
}
fn debug_f16<T: DebugHexF16>(x: T) -> impl core::fmt::Debug {
Hex(x)
}
trait DebugHexF16 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
}
impl DebugHexF16 for f16 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:#06x?}", self.to_bits())
}
}
impl DebugHexF16 for float16x4_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let array = unsafe { core::mem::transmute::<_, [Hex<f16>; 4]>(*self) };
debug_simd_finish(f, "float16x4_t", &array)
}
}
impl DebugHexF16 for float16x8_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let array = unsafe { core::mem::transmute::<_, [Hex<f16>; 8]>(*self) };
debug_simd_finish(f, "float16x8_t", &array)
}
}
impl DebugHexF16 for float16x4x2_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x4x2_t", &[Hex(self.0), Hex(self.1)])
}
}
impl DebugHexF16 for float16x4x3_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x4x3_t", &[Hex(self.0), Hex(self.1), Hex(self.2)])
}
}
impl DebugHexF16 for float16x4x4_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x4x4_t", &[Hex(self.0), Hex(self.1), Hex(self.2), Hex(self.3)])
}
}
impl DebugHexF16 for float16x8x2_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x8x2_t", &[Hex(self.0), Hex(self.1)])
}
}
impl DebugHexF16 for float16x8x3_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x8x3_t", &[Hex(self.0), Hex(self.1), Hex(self.2)])
}
}
impl DebugHexF16 for float16x8x4_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x8x4_t", &[Hex(self.0), Hex(self.1), Hex(self.2), Hex(self.3)])
}
}
"#;
pub const AARCH_CONFIGURATIONS: &str = r#"
#![cfg_attr(target_arch = "arm", feature(stdarch_arm_neon_intrinsics))]
#![cfg_attr(target_arch = "arm", feature(stdarch_aarch32_crc32))]
@ -30,5 +117,6 @@ pub const AARCH_CONFIGURATIONS: &str = r#"
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_sha3))]
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_sm4))]
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_ftts))]
#![feature(fmt_helpers_for_derive)]
#![feature(stdarch_neon_f16)]
"#;

View file

@ -87,7 +87,7 @@ impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
)
}
pub fn generate_loop_c(
fn generate_loop_c(
&self,
indentation: Indentation,
additional: &str,
@ -108,7 +108,7 @@ impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
)
}
pub fn generate_loop_rust(
fn generate_loop_rust(
&self,
indentation: Indentation,
additional: &str,

View file

@ -13,7 +13,7 @@ use crate::common::gen_rust::compile_rust;
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
use crate::common::intrinsic_helpers::{BaseIntrinsicTypeDefinition, TypeKind};
use crate::common::write_file::{write_c_testfiles, write_rust_testfiles};
use config::{AARCH_CONFIGURATIONS, POLY128_OSTREAM_DEF, build_notices};
use config::{AARCH_CONFIGURATIONS, F16_FORMATTING_DEF, POLY128_OSTREAM_DEF, build_notices};
use json_parser::get_neon_intrinsics;
pub struct ArmArchitectureTest {
@ -91,6 +91,7 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
.collect::<Vec<_>>(),
final_target,
&build_notices("// "),
F16_FORMATTING_DEF,
AARCH_CONFIGURATIONS,
);

View file

@ -7,6 +7,7 @@ use std::process::Command;
pub fn generate_rust_program(
notices: &str,
definitions: &str,
configurations: &str,
arch_definition: &str,
arglists: &str,
@ -17,7 +18,8 @@ pub fn generate_rust_program(
#![feature(link_llvm_intrinsics)]
#![feature(f16)]
{configurations}
#![allow(non_upper_case_globals)]
{definitions}
use core_arch::arch::{arch_definition}::*;
fn main() {{

View file

@ -202,7 +202,13 @@ where
}
}
fn generate_rust_program(&self, target: &str, notice: &str, cfg: &str) -> String {
fn generate_rust_program(
&self,
target: &str,
notice: &str,
definitions: &str,
cfg: &str,
) -> String {
let arguments = self.arguments();
let constraints = arguments
.iter()
@ -212,6 +218,7 @@ where
let indentation = Indentation::default();
generate_rust_program(
notice,
definitions,
cfg,
target,
self.arguments()

View file

@ -32,6 +32,7 @@ pub fn write_rust_testfiles<T: IntrinsicTypeDefinition>(
intrinsics: Vec<&dyn IntrinsicDefinition<T>>,
rust_target: &str,
notice: &str,
definitions: &str,
cfg: &str,
) -> Vec<String> {
let intrinsics_name_list = intrinsics
@ -41,7 +42,7 @@ pub fn write_rust_testfiles<T: IntrinsicTypeDefinition>(
let filename_mapping = create_rust_filenames(&intrinsics_name_list);
intrinsics.iter().for_each(|i| {
let rust_code = i.generate_rust_program(rust_target, notice, cfg);
let rust_code = i.generate_rust_program(rust_target, notice, definitions, cfg);
match filename_mapping.get(&i.name()) {
Some(filename) => write_file(filename, rust_code),
None => {}

View file

@ -1,4 +1,3 @@
#![feature(slice_partition_dedup)]
#[macro_use]
extern crate log;