[Arm,2/3] Add support for float16 (IEEE and Alternative formats) for Arm backend.

Message ID 853c8195-148e-9c21-7a5a-59b8cdaeeafc@arm.com
State New
Headers show
Series
  • Add float16 directive for assembling 16-bit floating point numbers.
Related show

Commit Message

Barnaby Wilks July 11, 2019, 3:26 p.m.
Hello,

This is part of a patch series that implements a directive for assembling 16-bit floating point constants for Arm and AArch64.

This patch implements the float16 directive for both the IEEE 754 format and the Arm alternative format for the
Arm backend.

The syntax of the directive is:
.float16 <0-n decimal numbers>
e.g.
.float16 12.0
.float16 0.23, 433.1, 0.06

The Arm alternative format is almost identical to the IEEE 754 format, except that it doesn't
encode for NaNs or Infinity (instead an exponent of 0x1F represents a normalized number in the range
65536 to 131008).

The alternative format is documented in the reference manual:
https://static.docs.arm.com/ddi0487/db/DDI0487D_b_armv8_arm.pdf?_ga=2.72318806.49764181.1561632697-999473562.1560847439

Which format is used is controlled by the
.eabi_attribute Tag_ABI_FP_16bit_format, <...>
directive, where if
<...> = 1 then use the IEEE 754 half-precision format else if
<...> = 2 then use the Arm alternative format

Added testcases to verify the correct encoding for both formats (for both big and little endian targets),
as well as tests verifying that warnings are thrown if trying to encode NaN or Infinity when using
the Arm alternative format.
The encodings have been cross referenced with GCC's encoding for __fp16 types to ensure consistency.

Cross compiled and regtested on arm-none-eabi and arm-none-linux-gnueabihf

I don't have write access, so if it's OK then could someone commit on my behalf?

Thanks,
Barney

gas/ChangeLog:

2019-07-11  Barnaby Wilks<barnaby.wilks@arm.com>

	* config/tc-arm.c (md_atof): Set precision for float16 type.
	(enum fp_16bit_format): Add enum to represent the 2 float16 encodings.
	(arm_is_largest_exponent_ok): Check for whether to encode with the IEEE or alternative
	format.
	* config/tc-arm.h (TC_LARGEST_EXPONENT_IS_NORMAL): Macro that expands to
	arm_is_largest_exponent_ok.
	(arm_is_largest_exponent_ok): Add prototype for arm_is_largest_exponent_ok function.
	* testsuite/gas/arm/float16-bad.d: New test.
	* testsuite/gas/arm/float16-bad.l: New test.
	* testsuite/gas/arm/float16-bad.s: New test.
	* testsuite/gas/arm/float16-be.d: New test.
	* testsuite/gas/arm/float16-be.s: New test.
	* testsuite/gas/arm/float16-le.d: New test.
	* testsuite/gas/arm/float16-le.s: New test.

Comments

Richard Earnshaw (lists) July 15, 2019, 9:16 a.m. | #1
On 11/07/2019 16:26, Barnaby Wilks wrote:
> Hello,

> 

> This is part of a patch series that implements a directive for assembling 16-bit floating point constants for Arm and AArch64.

> 

> This patch implements the float16 directive for both the IEEE 754 format and the Arm alternative format for the

> Arm backend >

> The syntax of the directive is:

> .float16 <0-n decimal numbers>

> e.g.

> .float16 12.0

> .float16 0.23, 433.1, 0.06

> 

> The Arm alternative format is almost identical to the IEEE 754 format, except that it doesn't

> encode for NaNs or Infinity (instead an exponent of 0x1F represents a normalized number in the range

> 65536 to 131008).

> 

> The alternative format is documented in the reference manual:

> https://static.docs.arm.com/ddi0487/db/DDI0487D_b_armv8_arm.pdf?_ga=2.72318806.49764181.1561632697-999473562.1560847439

> 

> Which format is used is controlled by the

> .eabi_attribute Tag_ABI_FP_16bit_format, <...>

> directive, where if

> <...> = 1 then use the IEEE 754 half-precision format else if

> <...> = 2 then use the Arm alternative format

> 

> Added testcases to verify the correct encoding for both formats (for both big and little endian targets),

> as well as tests verifying that warnings are thrown if trying to encode NaN or Infinity when using

> the Arm alternative format.

> The encodings have been cross referenced with GCC's encoding for __fp16 types to ensure consistency.

> 

> Cross compiled and regtested on arm-none-eabi and arm-none-linux-gnueabihf

> 

> I don't have write access, so if it's OK then could someone commit on my behalf?

> 


Hi Barney,

thanks for the patch.

I'm not sure that using the build attributes to control selection of the 
format is the best idea.  Firstly, build attributes are not available 
when not assembling for ELF (we have PE-coff support in the assembler as 
well); secondly, build attributes should reflect what was placed in the 
assembly file, not control what goes into it.  So this is a case of the 
coach before the horse.

I think, if we want to support the Arm alternate FP format in GAS at 
all, we need either a new command line option, or a new directive (or 
possibly both).  Then the code in BFD that does automatic 
build-attribute selection should be updated to reflect the tri-state 
situation: no format specified; IEEE format specified; or ARM Alternate 
format specified.

Note that GAS can currently only reflect show build attributes that 
affect the whole file, so switching during assembly is a little dubious 
as things stand.


R.


> Thanks,

> Barney

> 

> gas/ChangeLog:

> 

> 2019-07-11  Barnaby Wilks<barnaby.wilks@arm.com>

> 

> 	* config/tc-arm.c (md_atof): Set precision for float16 type.

> 	(enum fp_16bit_format): Add enum to represent the 2 float16 encodings.

> 	(arm_is_largest_exponent_ok): Check for whether to encode with the IEEE or alternative

> 	format.

> 	* config/tc-arm.h (TC_LARGEST_EXPONENT_IS_NORMAL): Macro that expands to

> 	arm_is_largest_exponent_ok.

> 	(arm_is_largest_exponent_ok): Add prototype for arm_is_largest_exponent_ok function.

> 	* testsuite/gas/arm/float16-bad.d: New test.

> 	* testsuite/gas/arm/float16-bad.l: New test.

> 	* testsuite/gas/arm/float16-bad.s: New test.

> 	* testsuite/gas/arm/float16-be.d: New test.

> 	* testsuite/gas/arm/float16-be.s: New test.

> 	* testsuite/gas/arm/float16-le.d: New test.

> 	* testsuite/gas/arm/float16-le.s: New test.

>
Barnaby Wilks July 15, 2019, 3:44 p.m. | #2
Hi Richard,

Yep agreed, a dedicated directive would make more sense really - originally I did have something
along those lines (.ieee_fp16_format/.alternative_fp16_format directives for switching between formats), but then
changed upon finding a EABI attribute for the same thing, and seeing that was how GCC did it.
Like you say though, for files that contain a mix of float16's in both IEEE and Alternative formats what would be the
EABI attribute for that be?

This could be solved by just restricting to one format (in which
case a command line option may make more sense), or by inventing
a new EABI attribute? (I'm don't really know much about that, so have no idea if it's feasible).

Regards,

Barney

On 7/15/19 10:16 AM, Richard Earnshaw (lists) wrote:
>

>

> On 11/07/2019 16:26, Barnaby Wilks wrote:

>> Hello,

>>

>> This is part of a patch series that implements a directive for 

>> assembling 16-bit floating point constants for Arm and AArch64.

>>

>> This patch implements the float16 directive for both the IEEE 754 

>> format and the Arm alternative format for the

>> Arm backend >

>> The syntax of the directive is:

>> .float16 <0-n decimal numbers>

>> e.g.

>> .float16 12.0

>> .float16 0.23, 433.1, 0.06

>>

>> The Arm alternative format is almost identical to the IEEE 754 

>> format, except that it doesn't

>> encode for NaNs or Infinity (instead an exponent of 0x1F represents a 

>> normalized number in the range

>> 65536 to 131008).

>>

>> The alternative format is documented in the reference manual:

>> https://static.docs.arm.com/ddi0487/db/DDI0487D_b_armv8_arm.pdf?_ga=2.72318806.49764181.1561632697-999473562.1560847439 

>>

>>

>> Which format is used is controlled by the

>> .eabi_attribute Tag_ABI_FP_16bit_format, <...>

>> directive, where if

>> <...> = 1 then use the IEEE 754 half-precision format else if

>> <...> = 2 then use the Arm alternative format

>>

>> Added testcases to verify the correct encoding for both formats (for 

>> both big and little endian targets),

>> as well as tests verifying that warnings are thrown if trying to 

>> encode NaN or Infinity when using

>> the Arm alternative format.

>> The encodings have been cross referenced with GCC's encoding for 

>> __fp16 types to ensure consistency.

>>

>> Cross compiled and regtested on arm-none-eabi and 

>> arm-none-linux-gnueabihf

>>

>> I don't have write access, so if it's OK then could someone commit on 

>> my behalf?

>>

>

> Hi Barney,

>

> thanks for the patch.

>

> I'm not sure that using the build attributes to control selection of 

> the format is the best idea.  Firstly, build attributes are not 

> available when not assembling for ELF (we have PE-coff support in the 

> assembler as well); secondly, build attributes should reflect what was 

> placed in the assembly file, not control what goes into it.  So this 

> is a case of the coach before the horse.

>

> I think, if we want to support the Arm alternate FP format in GAS at 

> all, we need either a new command line option, or a new directive (or 

> possibly both).  Then the code in BFD that does automatic 

> build-attribute selection should be updated to reflect the tri-state 

> situation: no format specified; IEEE format specified; or ARM 

> Alternate format specified.

>

> Note that GAS can currently only reflect show build attributes that 

> affect the whole file, so switching during assembly is a little 

> dubious as things stand.

>

>

> R.

>

>

>> Thanks,

>> Barney

>>

>> gas/ChangeLog:

>>

>> 2019-07-11  Barnaby Wilks<barnaby.wilks@arm.com>

>>

>>     * config/tc-arm.c (md_atof): Set precision for float16 type.

>>     (enum fp_16bit_format): Add enum to represent the 2 float16 

>> encodings.

>>     (arm_is_largest_exponent_ok): Check for whether to encode with 

>> the IEEE or alternative

>>     format.

>>     * config/tc-arm.h (TC_LARGEST_EXPONENT_IS_NORMAL): Macro that 

>> expands to

>>     arm_is_largest_exponent_ok.

>>     (arm_is_largest_exponent_ok): Add prototype for 

>> arm_is_largest_exponent_ok function.

>>     * testsuite/gas/arm/float16-bad.d: New test.

>>     * testsuite/gas/arm/float16-bad.l: New test.

>>     * testsuite/gas/arm/float16-bad.s: New test.

>>     * testsuite/gas/arm/float16-be.d: New test.

>>     * testsuite/gas/arm/float16-be.s: New test.

>>     * testsuite/gas/arm/float16-le.d: New test.

>>     * testsuite/gas/arm/float16-le.s: New test.

>>
Richard Earnshaw (lists) July 15, 2019, 4:11 p.m. | #3
On 15/07/2019 16:44, Barnaby Wilks wrote:
> Hi Richard,

> 

> Yep agreed, a dedicated directive would make more sense really - originally I did have something

> along those lines (.ieee_fp16_format/.alternative_fp16_format directives for switching between formats), but then

> changed upon finding a EABI attribute for the same thing, and seeing that was how GCC did it.

> Like you say though, for files that contain a mix of float16's in both IEEE and Alternative formats what would be the

> EABI attribute for that be?

> 

> This could be solved by just restricting to one format (in which

> case a command line option may make more sense), or by inventing

> a new EABI attribute? (I'm don't really know much about that, so have no idea if it's feasible).

> 


GCC already has the command-line option 
-mfp16-format=(none|ieee|alternative).  I'd suggest we have the same, 
and have an assembly attribute directive with a similar style.

For the directive, however, the default can be a little more lax: I 
think the assembler should start in a mode where the format is 
'tentatively ieee' format.  Then, if a .float16 appears with no format 
override, we'd make that an explicit format selection.  At the end of 
assembly, when then generating build attributes (if doing that 
automatically, we'd update the build attributes if the format is at that 
point explicit.

The tentative nature is important to avoid changing behaviour for 
existing files: we don't want them to start writing out additional build 
attributes that they did not generate previously, however, ieee format 
is far more common than than alternative format, so requiring users to 
always specify this before ever using .float16 would be a little awkward.

R.

> Regards,

> 

> Barney

> 

> On 7/15/19 10:16 AM, Richard Earnshaw (lists) wrote:

>>

>>

>> On 11/07/2019 16:26, Barnaby Wilks wrote:

>>> Hello,

>>>

>>> This is part of a patch series that implements a directive for

>>> assembling 16-bit floating point constants for Arm and AArch64.

>>>

>>> This patch implements the float16 directive for both the IEEE 754

>>> format and the Arm alternative format for the

>>> Arm backend >

>>> The syntax of the directive is:

>>> .float16 <0-n decimal numbers>

>>> e.g.

>>> .float16 12.0

>>> .float16 0.23, 433.1, 0.06

>>>

>>> The Arm alternative format is almost identical to the IEEE 754

>>> format, except that it doesn't

>>> encode for NaNs or Infinity (instead an exponent of 0x1F represents a

>>> normalized number in the range

>>> 65536 to 131008).

>>>

>>> The alternative format is documented in the reference manual:

>>> https://static.docs.arm.com/ddi0487/db/DDI0487D_b_armv8_arm.pdf?_ga=2.72318806.49764181.1561632697-999473562.1560847439

>>>

>>>

>>> Which format is used is controlled by the

>>> .eabi_attribute Tag_ABI_FP_16bit_format, <...>

>>> directive, where if

>>> <...> = 1 then use the IEEE 754 half-precision format else if

>>> <...> = 2 then use the Arm alternative format

>>>

>>> Added testcases to verify the correct encoding for both formats (for

>>> both big and little endian targets),

>>> as well as tests verifying that warnings are thrown if trying to

>>> encode NaN or Infinity when using

>>> the Arm alternative format.

>>> The encodings have been cross referenced with GCC's encoding for

>>> __fp16 types to ensure consistency.

>>>

>>> Cross compiled and regtested on arm-none-eabi and

>>> arm-none-linux-gnueabihf

>>>

>>> I don't have write access, so if it's OK then could someone commit on

>>> my behalf?

>>>

>>

>> Hi Barney,

>>

>> thanks for the patch.

>>

>> I'm not sure that using the build attributes to control selection of

>> the format is the best idea.  Firstly, build attributes are not

>> available when not assembling for ELF (we have PE-coff support in the

>> assembler as well); secondly, build attributes should reflect what was

>> placed in the assembly file, not control what goes into it.  So this

>> is a case of the coach before the horse.

>>

>> I think, if we want to support the Arm alternate FP format in GAS at

>> all, we need either a new command line option, or a new directive (or

>> possibly both).  Then the code in BFD that does automatic

>> build-attribute selection should be updated to reflect the tri-state

>> situation: no format specified; IEEE format specified; or ARM

>> Alternate format specified.

>>

>> Note that GAS can currently only reflect show build attributes that

>> affect the whole file, so switching during assembly is a little

>> dubious as things stand.

>>

>>

>> R.

>>

>>

>>> Thanks,

>>> Barney

>>>

>>> gas/ChangeLog:

>>>

>>> 2019-07-11  Barnaby Wilks<barnaby.wilks@arm.com>

>>>

>>>      * config/tc-arm.c (md_atof): Set precision for float16 type.

>>>      (enum fp_16bit_format): Add enum to represent the 2 float16

>>> encodings.

>>>      (arm_is_largest_exponent_ok): Check for whether to encode with

>>> the IEEE or alternative

>>>      format.

>>>      * config/tc-arm.h (TC_LARGEST_EXPONENT_IS_NORMAL): Macro that

>>> expands to

>>>      arm_is_largest_exponent_ok.

>>>      (arm_is_largest_exponent_ok): Add prototype for

>>> arm_is_largest_exponent_ok function.

>>>      * testsuite/gas/arm/float16-bad.d: New test.

>>>      * testsuite/gas/arm/float16-bad.l: New test.

>>>      * testsuite/gas/arm/float16-bad.s: New test.

>>>      * testsuite/gas/arm/float16-be.d: New test.

>>>      * testsuite/gas/arm/float16-be.s: New test.

>>>      * testsuite/gas/arm/float16-le.d: New test.

>>>      * testsuite/gas/arm/float16-le.s: New test.

>>>
Tamar Christina July 16, 2019, 1:59 a.m. | #4
Introducing a new directive for this seems counter intuitive to me. Particularly you open up some room for user error to happen more easily especially with inline assembly.

You also then have to Orchestrate gcc to emit these as well in order for the file to be logically consistent with the command line options the user specified. If this is a directive gcc has to emit it as well.

Lastly it also duplicates information. Now the user has two directives to set when using fp16 in an arm assembly file and also opens up the weird possibility to have your built attribute be different from the "encoding type" directive which is a bit nonsensical.

Personally I would say If you're really keen on doing this that I'd much rather have either two float16 directives or for the float16 directive to take an optional "format" specifier rather than commandline options which would complicate support in gcc.

The fact that some platforms like PE don't support built attribute aren't an issue, as those platforms already wouldn't be able to switch formats and be stuck at the "default" one. In fact do we even support fp16 on these non-elf platforms.

Regards,
Tamar

________________________________________
From: binutils-owner@sourceware.org <binutils-owner@sourceware.org> on behalf of Richard Earnshaw (lists) <Richard.Earnshaw@arm.com>

Sent: Monday, July 15, 2019 5:11:25 PM
To: Barnaby Wilks; binutils@sourceware.org
Cc: nd; nickc@redhat.com; Ramana Radhakrishnan
Subject: Re: [PATCH][binutils][Arm][2/3] Add support for float16 (IEEE and Alternative formats) for Arm backend.



On 15/07/2019 16:44, Barnaby Wilks wrote:
> Hi Richard,

>

> Yep agreed, a dedicated directive would make more sense really - originally I did have something

> along those lines (.ieee_fp16_format/.alternative_fp16_format directives for switching between formats), but then

> changed upon finding a EABI attribute for the same thing, and seeing that was how GCC did it.

> Like you say though, for files that contain a mix of float16's in both IEEE and Alternative formats what would be the

> EABI attribute for that be?

>

> This could be solved by just restricting to one format (in which

> case a command line option may make more sense), or by inventing

> a new EABI attribute? (I'm don't really know much about that, so have no idea if it's feasible).

>


GCC already has the command-line option
-mfp16-format=(none|ieee|alternative).  I'd suggest we have the same,
and have an assembly attribute directive with a similar style.

For the directive, however, the default can be a little more lax: I
think the assembler should start in a mode where the format is
'tentatively ieee' format.  Then, if a .float16 appears with no format
override, we'd make that an explicit format selection.  At the end of
assembly, when then generating build attributes (if doing that
automatically, we'd update the build attributes if the format is at that
point explicit.

The tentative nature is important to avoid changing behaviour for
existing files: we don't want them to start writing out additional build
attributes that they did not generate previously, however, ieee format
is far more common than than alternative format, so requiring users to
always specify this before ever using .float16 would be a little awkward.

R.

> Regards,

>

> Barney

>

> On 7/15/19 10:16 AM, Richard Earnshaw (lists) wrote:

>>

>>

>> On 11/07/2019 16:26, Barnaby Wilks wrote:

>>> Hello,

>>>

>>> This is part of a patch series that implements a directive for

>>> assembling 16-bit floating point constants for Arm and AArch64.

>>>

>>> This patch implements the float16 directive for both the IEEE 754

>>> format and the Arm alternative format for the

>>> Arm backend >

>>> The syntax of the directive is:

>>> .float16 <0-n decimal numbers>

>>> e.g.

>>> .float16 12.0

>>> .float16 0.23, 433.1, 0.06

>>>

>>> The Arm alternative format is almost identical to the IEEE 754

>>> format, except that it doesn't

>>> encode for NaNs or Infinity (instead an exponent of 0x1F represents a

>>> normalized number in the range

>>> 65536 to 131008).

>>>

>>> The alternative format is documented in the reference manual:

>>> https://static.docs.arm.com/ddi0487/db/DDI0487D_b_armv8_arm.pdf?_ga=2.72318806.49764181.1561632697-999473562.1560847439

>>>

>>>

>>> Which format is used is controlled by the

>>> .eabi_attribute Tag_ABI_FP_16bit_format, <...>

>>> directive, where if

>>> <...> = 1 then use the IEEE 754 half-precision format else if

>>> <...> = 2 then use the Arm alternative format

>>>

>>> Added testcases to verify the correct encoding for both formats (for

>>> both big and little endian targets),

>>> as well as tests verifying that warnings are thrown if trying to

>>> encode NaN or Infinity when using

>>> the Arm alternative format.

>>> The encodings have been cross referenced with GCC's encoding for

>>> __fp16 types to ensure consistency.

>>>

>>> Cross compiled and regtested on arm-none-eabi and

>>> arm-none-linux-gnueabihf

>>>

>>> I don't have write access, so if it's OK then could someone commit on

>>> my behalf?

>>>

>>

>> Hi Barney,

>>

>> thanks for the patch.

>>

>> I'm not sure that using the build attributes to control selection of

>> the format is the best idea.  Firstly, build attributes are not

>> available when not assembling for ELF (we have PE-coff support in the

>> assembler as well); secondly, build attributes should reflect what was

>> placed in the assembly file, not control what goes into it.  So this

>> is a case of the coach before the horse.

>>

>> I think, if we want to support the Arm alternate FP format in GAS at

>> all, we need either a new command line option, or a new directive (or

>> possibly both).  Then the code in BFD that does automatic

>> build-attribute selection should be updated to reflect the tri-state

>> situation: no format specified; IEEE format specified; or ARM

>> Alternate format specified.

>>

>> Note that GAS can currently only reflect show build attributes that

>> affect the whole file, so switching during assembly is a little

>> dubious as things stand.

>>

>>

>> R.

>>

>>

>>> Thanks,

>>> Barney

>>>

>>> gas/ChangeLog:

>>>

>>> 2019-07-11  Barnaby Wilks<barnaby.wilks@arm.com>

>>>

>>>      * config/tc-arm.c (md_atof): Set precision for float16 type.

>>>      (enum fp_16bit_format): Add enum to represent the 2 float16

>>> encodings.

>>>      (arm_is_largest_exponent_ok): Check for whether to encode with

>>> the IEEE or alternative

>>>      format.

>>>      * config/tc-arm.h (TC_LARGEST_EXPONENT_IS_NORMAL): Macro that

>>> expands to

>>>      arm_is_largest_exponent_ok.

>>>      (arm_is_largest_exponent_ok): Add prototype for

>>> arm_is_largest_exponent_ok function.

>>>      * testsuite/gas/arm/float16-bad.d: New test.

>>>      * testsuite/gas/arm/float16-bad.l: New test.

>>>      * testsuite/gas/arm/float16-bad.s: New test.

>>>      * testsuite/gas/arm/float16-be.d: New test.

>>>      * testsuite/gas/arm/float16-be.s: New test.

>>>      * testsuite/gas/arm/float16-le.d: New test.

>>>      * testsuite/gas/arm/float16-le.s: New test.

>>>
Richard Earnshaw (lists) July 17, 2019, 3:29 p.m. | #5
On 16/07/2019 02:59, Tamar Christina wrote:
> 

> Introducing a new directive for this seems counter intuitive to me. Particularly you open up some room for user error to happen more easily especially with inline assembly.


I expect the main usecase for this to be completely hand-written 
assembly.  GCC never outputs any .float type directives but translates 
the values it needs directly into the appropriate decimal constants for 
the encoded values.

> 

> You also then have to Orchestrate gcc to emit these as well in order for the file to be logically consistent with the command line options the user specified. If this is a directive gcc has to emit it as well.


No, see above.

> 

> Lastly it also duplicates information. Now the user has two directives to set when using fp16 in an arm assembly file and also opens up the weird possibility to have your built attribute be different from the "encoding type" directive which is a bit nonsensical.


No, it doesn't.  We can get gas to add the relevant build attributes 
when generating the output, if needed based on what directives the user 
actually put into their code (and whether or not they made use of the 
controlling option).  So no more duplication than we have for, say, .cpu 
or .arch.

> 

> Personally I would say If you're really keen on doing this that I'd much rather have either two float16 directives or for the float16 directive to take an optional "format" specifier rather than commandline options which would complicate support in gcc.

> 

> The fact that some platforms like PE don't support built attribute aren't an issue, as those platforms already wouldn't be able to switch formats and be stuck at the "default" one. In fact do we even support fp16 on these non-elf platforms.


It is an issue.  The attributes are stored in elf-specific extensions 
for the BFD.  They don't exist in COFF.  Furthermore, coff users simply 
don't expect to do this in their assembly code.

Finally, build attributes are pretty non-intuitive in their encoding. 
Expecting users to understand the value system is an insane idea.

R.

> 

> Regards,

> Tamar

> 

> ________________________________________

> From: binutils-owner@sourceware.org <binutils-owner@sourceware.org> on behalf of Richard Earnshaw (lists) <Richard.Earnshaw@arm.com>

> Sent: Monday, July 15, 2019 5:11:25 PM

> To: Barnaby Wilks; binutils@sourceware.org

> Cc: nd; nickc@redhat.com; Ramana Radhakrishnan

> Subject: Re: [PATCH][binutils][Arm][2/3] Add support for float16 (IEEE and Alternative formats) for Arm backend.

> 

> 

> 

> On 15/07/2019 16:44, Barnaby Wilks wrote:

>> Hi Richard,

>>

>> Yep agreed, a dedicated directive would make more sense really - originally I did have something

>> along those lines (.ieee_fp16_format/.alternative_fp16_format directives for switching between formats), but then

>> changed upon finding a EABI attribute for the same thing, and seeing that was how GCC did it.

>> Like you say though, for files that contain a mix of float16's in both IEEE and Alternative formats what would be the

>> EABI attribute for that be?

>>

>> This could be solved by just restricting to one format (in which

>> case a command line option may make more sense), or by inventing

>> a new EABI attribute? (I'm don't really know much about that, so have no idea if it's feasible).

>>

> 

> GCC already has the command-line option

> -mfp16-format=(none|ieee|alternative).  I'd suggest we have the same,

> and have an assembly attribute directive with a similar style.

> 

> For the directive, however, the default can be a little more lax: I

> think the assembler should start in a mode where the format is

> 'tentatively ieee' format.  Then, if a .float16 appears with no format

> override, we'd make that an explicit format selection.  At the end of

> assembly, when then generating build attributes (if doing that

> automatically, we'd update the build attributes if the format is at that

> point explicit.

> 

> The tentative nature is important to avoid changing behaviour for

> existing files: we don't want them to start writing out additional build

> attributes that they did not generate previously, however, ieee format

> is far more common than than alternative format, so requiring users to

> always specify this before ever using .float16 would be a little awkward.

> 

> R.

> 

>> Regards,

>>

>> Barney

>>

>> On 7/15/19 10:16 AM, Richard Earnshaw (lists) wrote:

>>>

>>>

>>> On 11/07/2019 16:26, Barnaby Wilks wrote:

>>>> Hello,

>>>>

>>>> This is part of a patch series that implements a directive for

>>>> assembling 16-bit floating point constants for Arm and AArch64.

>>>>

>>>> This patch implements the float16 directive for both the IEEE 754

>>>> format and the Arm alternative format for the

>>>> Arm backend >

>>>> The syntax of the directive is:

>>>> .float16 <0-n decimal numbers>

>>>> e.g.

>>>> .float16 12.0

>>>> .float16 0.23, 433.1, 0.06

>>>>

>>>> The Arm alternative format is almost identical to the IEEE 754

>>>> format, except that it doesn't

>>>> encode for NaNs or Infinity (instead an exponent of 0x1F represents a

>>>> normalized number in the range

>>>> 65536 to 131008).

>>>>

>>>> The alternative format is documented in the reference manual:

>>>> https://static.docs.arm.com/ddi0487/db/DDI0487D_b_armv8_arm.pdf?_ga=2.72318806.49764181.1561632697-999473562.1560847439

>>>>

>>>>

>>>> Which format is used is controlled by the

>>>> .eabi_attribute Tag_ABI_FP_16bit_format, <...>

>>>> directive, where if

>>>> <...> = 1 then use the IEEE 754 half-precision format else if

>>>> <...> = 2 then use the Arm alternative format

>>>>

>>>> Added testcases to verify the correct encoding for both formats (for

>>>> both big and little endian targets),

>>>> as well as tests verifying that warnings are thrown if trying to

>>>> encode NaN or Infinity when using

>>>> the Arm alternative format.

>>>> The encodings have been cross referenced with GCC's encoding for

>>>> __fp16 types to ensure consistency.

>>>>

>>>> Cross compiled and regtested on arm-none-eabi and

>>>> arm-none-linux-gnueabihf

>>>>

>>>> I don't have write access, so if it's OK then could someone commit on

>>>> my behalf?

>>>>

>>>

>>> Hi Barney,

>>>

>>> thanks for the patch.

>>>

>>> I'm not sure that using the build attributes to control selection of

>>> the format is the best idea.  Firstly, build attributes are not

>>> available when not assembling for ELF (we have PE-coff support in the

>>> assembler as well); secondly, build attributes should reflect what was

>>> placed in the assembly file, not control what goes into it.  So this

>>> is a case of the coach before the horse.

>>>

>>> I think, if we want to support the Arm alternate FP format in GAS at

>>> all, we need either a new command line option, or a new directive (or

>>> possibly both).  Then the code in BFD that does automatic

>>> build-attribute selection should be updated to reflect the tri-state

>>> situation: no format specified; IEEE format specified; or ARM

>>> Alternate format specified.

>>>

>>> Note that GAS can currently only reflect show build attributes that

>>> affect the whole file, so switching during assembly is a little

>>> dubious as things stand.

>>>

>>>

>>> R.

>>>

>>>

>>>> Thanks,

>>>> Barney

>>>>

>>>> gas/ChangeLog:

>>>>

>>>> 2019-07-11  Barnaby Wilks<barnaby.wilks@arm.com>

>>>>

>>>>       * config/tc-arm.c (md_atof): Set precision for float16 type.

>>>>       (enum fp_16bit_format): Add enum to represent the 2 float16

>>>> encodings.

>>>>       (arm_is_largest_exponent_ok): Check for whether to encode with

>>>> the IEEE or alternative

>>>>       format.

>>>>       * config/tc-arm.h (TC_LARGEST_EXPONENT_IS_NORMAL): Macro that

>>>> expands to

>>>>       arm_is_largest_exponent_ok.

>>>>       (arm_is_largest_exponent_ok): Add prototype for

>>>> arm_is_largest_exponent_ok function.

>>>>       * testsuite/gas/arm/float16-bad.d: New test.

>>>>       * testsuite/gas/arm/float16-bad.l: New test.

>>>>       * testsuite/gas/arm/float16-bad.s: New test.

>>>>       * testsuite/gas/arm/float16-be.d: New test.

>>>>       * testsuite/gas/arm/float16-be.s: New test.

>>>>       * testsuite/gas/arm/float16-le.d: New test.

>>>>       * testsuite/gas/arm/float16-le.s: New test.

>>>>

Patch

diff --git a/gas/config/tc-arm.h b/gas/config/tc-arm.h
index 39cc9680b96524c730c15fff1af22b180b9c70d1..8a73bebe6c5b5a027242d02f18b6d51273abcacb 100644
--- a/gas/config/tc-arm.h
+++ b/gas/config/tc-arm.h
@@ -385,3 +385,7 @@  extern char arm_line_separator_chars[];
 
 #define TC_EQUAL_IN_INSN(c, s) arm_tc_equal_in_insn ((c), (s))
 extern bfd_boolean arm_tc_equal_in_insn (int, char *);
+
+#define TC_LARGEST_EXPONENT_IS_NORMAL(PRECISION) \
+	arm_is_largest_exponent_ok ((PRECISION))
+int arm_is_largest_exponent_ok (int precision);
diff --git a/gas/config/tc-arm.c b/gas/config/tc-arm.c
index b70028f7c2e31c179196b3c9ea9665fbf4b0159f..6176fea2bb523990d660fbcc39bc12007a459809 100644
--- a/gas/config/tc-arm.c
+++ b/gas/config/tc-arm.c
@@ -1037,7 +1037,7 @@  const char EXP_CHARS[] = "eE";
 /* As in 0f12.456  */
 /* or	 0d1.2345e12  */
 
-const char FLT_CHARS[] = "rRsSfFdDxXeEpP";
+const char FLT_CHARS[] = "rRsSfFdDxXeEpPHh";
 
 /* Prefix characters that indicate the start of an immediate
    value.  */
@@ -1188,6 +1188,11 @@  md_atof (int type, char * litP, int * sizeP)
 
   switch (type)
     {
+    case 'H':
+    case 'h':
+      prec = 1;
+      break;
+
     case 'f':
     case 'F':
     case 's':
@@ -4925,6 +4930,21 @@  pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
 }
 #endif /* TE_PE */
 
+enum fp_16bit_format
+{
+  ARM_FP16_FORMAT_IEEE        = 0x1,
+  ARM_FP16_FORMAT_ALTERNATIVE = 0x2
+};
+
+int arm_is_largest_exponent_ok (int precision)
+{
+  const int fp16_format = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_PROC,
+		  Tag_ABI_FP_16bit_format);
+
+  /* precision == 1 ensures that this will only return true for 16 bit floats. */
+  return (precision == 1) && (fp16_format == ARM_FP16_FORMAT_ALTERNATIVE);
+}
+
 /* This table describes all the machine specific pseudo-ops the assembler
    has to support.  The fields are:
      pseudo-op name without dot
@@ -5002,9 +5022,11 @@  const pseudo_typeS md_pseudo_table[] =
   {"asmfunc",      s_ccs_asmfunc,    0},
   {"endasmfunc",   s_ccs_endasmfunc, 0},
 
+  {"float16", float_cons, 'h'},
+
   { 0, 0, 0 }
 };
-
+
 /* Parser functions used exclusively in instruction operands.  */
 
 /* Generic immediate-value read function for use in insn parsing.
diff --git a/gas/testsuite/gas/arm/float16-bad.d b/gas/testsuite/gas/arm/float16-bad.d
new file mode 100644
index 0000000000000000000000000000000000000000..8eac0af5cbb8671e61f18939d723599db4f948a6
--- /dev/null
+++ b/gas/testsuite/gas/arm/float16-bad.d
@@ -0,0 +1,3 @@ 
+# name: Invalid float16 literals (IEEE 754 & Alternative)
+# source: float16-bad.s
+# error_output: float16-bad.l
diff --git a/gas/testsuite/gas/arm/float16-bad.l b/gas/testsuite/gas/arm/float16-bad.l
new file mode 100644
index 0000000000000000000000000000000000000000..83978b7571cac2644379d3ca1896f609ba87ce30
--- /dev/null
+++ b/gas/testsuite/gas/arm/float16-bad.l
@@ -0,0 +1,10 @@ 
+[^:]*: Assembler messages:
+[^:]*:2: Error: cannot create floating-point number
+[^:]*:3: Error: cannot create floating-point number
+[^:]*:4: Error: cannot create floating-point number
+[^:]*:7: Warning: NaNs are not supported by this target
+[^:]*:8: Warning: NaNs are not supported by this target
+[^:]*:9: Warning: NaNs are not supported by this target
+[^:]*:11: Warning: Infinities are not supported by this target
+[^:]*:12: Warning: Infinities are not supported by this target
+[^:]*:13: Warning: Infinities are not supported by this target
diff --git a/gas/testsuite/gas/arm/float16-bad.s b/gas/testsuite/gas/arm/float16-bad.s
new file mode 100644
index 0000000000000000000000000000000000000000..cb97c605b8c7dd5bffbf4d13ced71d22124202ed
--- /dev/null
+++ b/gas/testsuite/gas/arm/float16-bad.s
@@ -0,0 +1,13 @@ 
+.data
+	.float16 9999999999999.0
+	.float16 -999999999999.0
+	.float16 131008.0
+
+.eabi_attribute Tag_ABI_FP_16bit_format, 2
+	.float16 NaN
+	.float16 -NaN
+	.float16 +NaN
+
+	.float16 Inf
+	.float16 -Inf
+	.float16 +Inf
diff --git a/gas/testsuite/gas/arm/float16-be.d b/gas/testsuite/gas/arm/float16-be.d
new file mode 100644
index 0000000000000000000000000000000000000000..214c7f7a6ceee423ee505ac38eada9f6c9cf0fbf
--- /dev/null
+++ b/gas/testsuite/gas/arm/float16-be.d
@@ -0,0 +1,12 @@ 
+# name: Big endian float16 literals (IEEE 754 & Alternative)
+# source: float16-be.s
+# objdump: -s --section=.data
+# as: -mbig-endian
+
+.*: +file format .*arm.*
+
+Contents of section \.data:
+ 0000 4a002fdf 1c197bff 000103ff 04003c00.*
+ 0010 3c017fff 7c00fc00 00008000 bc00bbe7.*
+ 0020 fbff4200 4a00603e 38567fff ffff7204.*
+ 0030 7fff7c00.*
diff --git a/gas/testsuite/gas/arm/float16-be.s b/gas/testsuite/gas/arm/float16-be.s
new file mode 100644
index 0000000000000000000000000000000000000000..0cdecbd7c971313e5963a6e158041400a899da8a
--- /dev/null
+++ b/gas/testsuite/gas/arm/float16-be.s
@@ -0,0 +1,29 @@ 
+.data
+	.float16 12.0
+	.float16 0.123
+	.float16 0.004
+	.float16 65504
+	.float16 5.9605e-8
+	.float16 6.0976e-5
+	.float16 6.1035e-5
+	.float16 1
+	.float16 1.001
+	.float16 NaN
+	.float16 +Inf
+	.float16 -Inf
+	.float16 +0
+	.float16 -0
+	.float16 -1
+	.float16 -0.98765
+	.float16 -65504
+	.float16 3.0, 12.0, 543.123
+
+.eabi_attribute Tag_ABI_FP_16bit_format, 2
+	.float16 0.542
+	.float16 131008.0
+	.float16 -131008.0
+	.float16 12323.1234
+
+.eabi_attribute Tag_ABI_FP_16bit_format, 1
+	.float16 NaN
+	.float16 Inf
diff --git a/gas/testsuite/gas/arm/float16-le.d b/gas/testsuite/gas/arm/float16-le.d
new file mode 100644
index 0000000000000000000000000000000000000000..0efb068c5b3a4f5deed3b5ecc9b9aceb08561163
--- /dev/null
+++ b/gas/testsuite/gas/arm/float16-le.d
@@ -0,0 +1,12 @@ 
+# name: Little endian float16 literals (IEEE 754 & Alternative)
+# source: float16-le.s
+# objdump: -s --section=.data
+# as: -mlittle-endian
+
+.*: +file format .*arm.*
+
+Contents of section \.data:
+ 0000 004adf2f 191cff7b 0100ff03 0004003c.*
+ 0010 013cff7f 007c00fc 00000080 00bce7bb.*
+ 0020 fffb0042 004a3e60 5638ff7f ffff0472.*
+ 0030 ff7f007c.*
diff --git a/gas/testsuite/gas/arm/float16-le.s b/gas/testsuite/gas/arm/float16-le.s
new file mode 100644
index 0000000000000000000000000000000000000000..0cdecbd7c971313e5963a6e158041400a899da8a
--- /dev/null
+++ b/gas/testsuite/gas/arm/float16-le.s
@@ -0,0 +1,29 @@ 
+.data
+	.float16 12.0
+	.float16 0.123
+	.float16 0.004
+	.float16 65504
+	.float16 5.9605e-8
+	.float16 6.0976e-5
+	.float16 6.1035e-5
+	.float16 1
+	.float16 1.001
+	.float16 NaN
+	.float16 +Inf
+	.float16 -Inf
+	.float16 +0
+	.float16 -0
+	.float16 -1
+	.float16 -0.98765
+	.float16 -65504
+	.float16 3.0, 12.0, 543.123
+
+.eabi_attribute Tag_ABI_FP_16bit_format, 2
+	.float16 0.542
+	.float16 131008.0
+	.float16 -131008.0
+	.float16 12323.1234
+
+.eabi_attribute Tag_ABI_FP_16bit_format, 1
+	.float16 NaN
+	.float16 Inf