Skip to content

Commit d8babb0

Browse files
soroshsabzwchilders-nvidia
authored andcommitted
Add support for standard IFC fixed-width floating-point types [GH #409]
1 parent 2a80dfb commit d8babb0

23 files changed

Lines changed: 169 additions & 12 deletions

‎src/ifc_modules_read.c‎

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16497,8 +16497,30 @@ no corresponding type, return an error type.
1649716497
}
1649816498
break;
1649916499
case ifc_tbs_float:
16500-
check_assertion(precision == ifc_tps_default);
16501-
result = float_type((a_float_kind)fk_float);
16500+
switch (precision) {
16501+
case ifc_tps_default:
16502+
result = float_type(fk_float);
16503+
break;
16504+
case ifc_tps_bit16:
16505+
result = float_type(float16_kind());
16506+
break;
16507+
case ifc_tps_bit32:
16508+
result = float_type(fk_std_float32);
16509+
break;
16510+
case ifc_tps_bit64:
16511+
result = float_type(fk_std_float64);
16512+
break;
16513+
case ifc_tps_bit128:
16514+
result = float_type(fk_std_float128);
16515+
break;
16516+
default:
16517+
{ a_string err_msg("Unexpected ", str_for(precision),
16518+
" for ", str_for(basis));
16519+
16520+
ifc_unexpected(module_of(itf), err_msg);
16521+
}
16522+
goto invalid;
16523+
} /* switch */
1650216524
break;
1650316525
case ifc_tbs_double:
1650416526
if (precision == ifc_tps_long) {
@@ -24588,8 +24610,37 @@ this is needed.
2458824610
} /* switch */
2458924611
break;
2459024612
case ifc_tbs_float:
24591-
check_assertion(precision == ifc_tps_default);
24592-
cache_token(cache, tok_float);
24613+
switch (precision) {
24614+
case ifc_tps_default:
24615+
cache_token(cache, tok_float);
24616+
break;
24617+
case ifc_tps_bit16:
24618+
/* These types have no keyword spelling that is correct for
24619+
every dialect, so cache the resolved type directly
24620+
(mirroring the direct type loader). */
24621+
cache_resolved_type_token(cache,
24622+
float_type(float16_kind()));
24623+
break;
24624+
case ifc_tps_bit32:
24625+
cache_resolved_type_token(cache,
24626+
float_type(fk_std_float32));
24627+
break;
24628+
case ifc_tps_bit64:
24629+
cache_resolved_type_token(cache,
24630+
float_type(fk_std_float64));
24631+
break;
24632+
case ifc_tps_bit128:
24633+
cache_resolved_type_token(cache,
24634+
float_type(fk_std_float128));
24635+
break;
24636+
default:
24637+
{ a_string err_msg("Unexpected ", str_for(precision),
24638+
" for ", str_for(basis));
24639+
24640+
ifc_unexpected(module_of(type), err_msg);
24641+
}
24642+
goto invalid;
24643+
} /* switch */
2459324644
break;
2459424645
case ifc_tbs_double:
2459524646
if (precision == ifc_tps_long) {

‎src/ifc_modules_write.c‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,21 +2713,35 @@ for the float type.
27132713
set_ifc_basis(&fund_type, ifc_tbs_double);
27142714
set_ifc_precision(&fund_type, ifc_tps_long);
27152715
break;
2716-
case fk_float128:
2716+
case fk_std_float128:
27172717
set_ifc_basis(&fund_type, ifc_tbs_float);
27182718
set_ifc_precision(&fund_type, ifc_tps_bit128);
27192719
break;
27202720
case fk_float16:
2721+
case fk_std_float16:
2722+
set_ifc_basis(&fund_type, ifc_tbs_float);
2723+
set_ifc_precision(&fund_type, ifc_tps_bit16);
2724+
break;
2725+
case fk_std_float32:
2726+
set_ifc_basis(&fund_type, ifc_tbs_float);
2727+
set_ifc_precision(&fund_type, ifc_tps_bit32);
2728+
break;
2729+
case fk_std_float64:
2730+
set_ifc_basis(&fund_type, ifc_tbs_float);
2731+
set_ifc_precision(&fund_type, ifc_tps_bit64);
2732+
break;
2733+
case fk_float128:
27212734
case fk_fp16:
27222735
case fk_float32x:
27232736
case fk_float64x:
27242737
case fk_float80:
27252738
case fk_std_bfloat16:
2726-
case fk_std_float16:
2727-
case fk_std_float32:
2728-
case fk_std_float64:
2729-
case fk_std_float128:
27302739
case fk_last:
2740+
/* These extended floating-point kinds have no IFC fundamental-type
2741+
encoding that distinguishes them from other floating-point types
2742+
of the same size ("__fp16" and "__float128" in particular have
2743+
different manglings than "_Float16" and "_Float128"); they will
2744+
be covered by an EDG extension type. */
27312745
ifc_write_catastrophe();
27322746
break;
27332747
default_is_unexpected();
@@ -6042,6 +6056,13 @@ Add all the types in the given scope to the given IL -> IFC mapping.
60426056
information. */
60436057
continue;
60446058
} /* if */
6059+
if (type->kind == tk_typeref && type->variant.typeref.predeclared) {
6060+
/* Skip predeclared typedefs (e.g., "_Float16", which is entered in gnu
6061+
and clang emulation modes): they are re-created by the predefined
6062+
type entry processing of an importing translation unit, and the
6063+
types of some of them cannot currently be written to an IFC file. */
6064+
continue;
6065+
} /* if */
60456066
(void)il_map->find_or_enter_type(type);
60466067
} /* for */
60476068
} /* dump_scope_types */

‎src/sys_predef.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3091,9 +3091,7 @@ Enter predeclared symbols as required by the implementation.
30913091
if (float16_enabled) {
30923092
(void)enter_predefined_typedef(
30933093
"_Float16",
3094-
((gnu_version_is(>= 130000) ||
3095-
clang_version_is(>= 150000)) ? float_type(fk_std_float16)
3096-
: float_type(fk_float16)));
3094+
float_type(float16_kind()));
30973095
} /* if */
30983096
if (float80_enabled) {
30993097
(void)enter_predefined_typedef(

‎src/types.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ this routine should not be used when checking type qualifiers.
5454
} /* skip_typerefs */
5555

5656

57+
INLINE a_float_kind float16_kind(void)
58+
/*
59+
Return the floating-point kind used for the type name "_Float16" in the
60+
current mode: GNU versions 13 and later and Clang versions 15 and later
61+
use the standard "_Float16" type, and other modes use the GNU extension
62+
type.
63+
*/
64+
{
65+
return (gnu_version_is(>= 130000) || clang_version_is(>= 150000)) ?
66+
fk_std_float16 : fk_float16;
67+
} /* float16_kind */
68+
69+
5770
INLINE a_boolean is_lexical_typeref(a_type_ptr tp)
5871
/*
5972
Return TRUE if tp is a trk_template_arg_list or trk_name_qualifier typeref,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules --module_interface foo.ixx
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules Test_name.c
2+
2 modules declarations processed (0 failed).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules Test_name.c
2+
2 modules declarations processed (0 failed).
3+
2 modules declarations processed (0 failed).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cpfe -DTEST_NUMBER=1 --module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules Test_name.c
2+
2 modules declarations processed (0 failed).
3+
2 modules declarations processed (0 failed).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export module foo;
2+
3+
export _Float16 half(_Float16 v) {
4+
return v / 2;
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//type:cp
2+
//module_files:foo.ixx
3+
//options_all:--module_import_diagnostics -d-module_report --c++20 --gnu_version=150100 --modules
4+
5+
import foo;
6+
7+
int main() {
8+
_Float16 x = half(8);
9+
return 0;
10+
}

0 commit comments

Comments
 (0)