RISC-V: fix TARGET_PROMOTE_FUNCTION_MODE hook for libcalls

Message ID 20231031183504.832611-1-vineetg@rivosinc.com
State Accepted
Headers
Series RISC-V: fix TARGET_PROMOTE_FUNCTION_MODE hook for libcalls |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Vineet Gupta Oct. 31, 2023, 6:35 p.m. UTC
  riscv_promote_function_mode doesn't promote a SI to DI for libcalls
case.

The fix is what generic promote_mode () in explow.cc does. I really
don't understand why the old code didn't work, but stepping thru the
debugger shows old code didn't and fixed does.

This showed up when testing Ajit's REE ABI extension series which probes
the ABI (using a NULL tree type) and ends up hitting the libcall code path.

[Usual caveat, I'll wait for Pre-commit CI to run the tests and report]

gcc/ChangeLog:
	* config/riscv/riscv.cc (riscv_promote_function_mode): Fix mode
	  returned for libcall case.

Signed-off-by: Vineet Gupta <vineetg@rivosinc.com>
---
 gcc/config/riscv/riscv.cc | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

Jeff Law Oct. 31, 2023, 11:18 p.m. UTC | #1
On 10/31/23 12:35, Vineet Gupta wrote:
> riscv_promote_function_mode doesn't promote a SI to DI for libcalls
> case.
> 
> The fix is what generic promote_mode () in explow.cc does. I really
> don't understand why the old code didn't work, but stepping thru the
> debugger shows old code didn't and fixed does.
> 
> This showed up when testing Ajit's REE ABI extension series which probes
> the ABI (using a NULL tree type) and ends up hitting the libcall code path.
> 
> [Usual caveat, I'll wait for Pre-commit CI to run the tests and report]
> 
> gcc/ChangeLog:
> 	* config/riscv/riscv.cc (riscv_promote_function_mode): Fix mode
> 	  returned for libcall case.
Hmm.  There may be dragons in here.  I'll need to find and review an old 
conversation in this space (libcalls and argument promotions).

Jeff
  
Palmer Dabbelt Oct. 31, 2023, 11:41 p.m. UTC | #2
On Tue, 31 Oct 2023 16:18:35 PDT (-0700), jeffreyalaw@gmail.com wrote:
>
>
> On 10/31/23 12:35, Vineet Gupta wrote:
>> riscv_promote_function_mode doesn't promote a SI to DI for libcalls
>> case.
>>
>> The fix is what generic promote_mode () in explow.cc does. I really
>> don't understand why the old code didn't work, but stepping thru the
>> debugger shows old code didn't and fixed does.
>>
>> This showed up when testing Ajit's REE ABI extension series which probes
>> the ABI (using a NULL tree type) and ends up hitting the libcall code path.
>>
>> [Usual caveat, I'll wait for Pre-commit CI to run the tests and report]
>>
>> gcc/ChangeLog:
>> 	* config/riscv/riscv.cc (riscv_promote_function_mode): Fix mode
>> 	  returned for libcall case.
> Hmm.  There may be dragons in here.  I'll need to find and review an old
> conversation in this space (libcalls and argument promotions).

We also have a non-orthogonality in the ABI sign extension rules between 
SI and DI, a few of us were talking about it on the internal slack 
(though the specifics were for a different patch, Vineet has a few in 
flight).
  
Jeff Law Nov. 1, 2023, 12:51 a.m. UTC | #3
On 10/31/23 17:41, Palmer Dabbelt wrote:
> On Tue, 31 Oct 2023 16:18:35 PDT (-0700), jeffreyalaw@gmail.com wrote:
>>
>>
>> On 10/31/23 12:35, Vineet Gupta wrote:
>>> riscv_promote_function_mode doesn't promote a SI to DI for libcalls
>>> case.
>>>
>>> The fix is what generic promote_mode () in explow.cc does. I really
>>> don't understand why the old code didn't work, but stepping thru the
>>> debugger shows old code didn't and fixed does.
>>>
>>> This showed up when testing Ajit's REE ABI extension series which probes
>>> the ABI (using a NULL tree type) and ends up hitting the libcall code 
>>> path.
>>>
>>> [Usual caveat, I'll wait for Pre-commit CI to run the tests and report]
>>>
>>> gcc/ChangeLog:
>>>     * config/riscv/riscv.cc (riscv_promote_function_mode): Fix mode
>>>       returned for libcall case.
>> Hmm.  There may be dragons in here.  I'll need to find and review an old
>> conversation in this space (libcalls and argument promotions).
> 
> We also have a non-orthogonality in the ABI sign extension rules between 
> SI and DI, a few of us were talking about it on the internal slack 
> (though the specifics were for a different patch, Vineet has a few in 
> flight).
So the old issue I was thinking of really only affects targets that push 
arguments on the stack and when a sub-word push actually allocates a 
full word on the stack (m68k, but !coldfire, h8 and probably others of 
that era).

Point being, those issues don't apply here.

jeff
  
Vineet Gupta Nov. 1, 2023, 1:37 a.m. UTC | #4
On 10/31/23 17:51, Jeff Law wrote:
>>>
>>
>> We also have a non-orthogonality in the ABI sign extension rules 
>> between SI and DI, a few of us were talking about it on the internal 
>> slack (though the specifics were for a different patch, Vineet has a 
>> few in flight).
> So the old issue I was thinking of really only affects targets that 
> push arguments on the stack and when a sub-word push actually 
> allocates a full word on the stack (m68k, but !coldfire, h8 and 
> probably others of that era).
>
> Point being, those issues don't apply here.

OK, I think Palmer was conflating this with the discussion in other 
thread/patch.

-Vineet
  
Jeff Law Nov. 1, 2023, 7:11 p.m. UTC | #5
On 10/31/23 12:35, Vineet Gupta wrote:
> riscv_promote_function_mode doesn't promote a SI to DI for libcalls
> case.
> 
> The fix is what generic promote_mode () in explow.cc does. I really
> don't understand why the old code didn't work, but stepping thru the
> debugger shows old code didn't and fixed does.
> 
> This showed up when testing Ajit's REE ABI extension series which probes
> the ABI (using a NULL tree type) and ends up hitting the libcall code path.
> 
> [Usual caveat, I'll wait for Pre-commit CI to run the tests and report]
> 
> gcc/ChangeLog:
> 	* config/riscv/riscv.cc (riscv_promote_function_mode): Fix mode
> 	  returned for libcall case.
Macro that change their arguments are evil ;(   I think Juzhe's patch in 
this space a few months back wasn't supposed to change behavior.

OK once CI finishes without regressions.

jeff
  
Vineet Gupta Nov. 1, 2023, 7:19 p.m. UTC | #6
On 11/1/23 12:11, Jeff Law wrote:
>
>
> On 10/31/23 12:35, Vineet Gupta wrote:
>> riscv_promote_function_mode doesn't promote a SI to DI for libcalls
>> case.
>>
>> The fix is what generic promote_mode () in explow.cc does. I really
>> don't understand why the old code didn't work, but stepping thru the
>> debugger shows old code didn't and fixed does.
>>
>> This showed up when testing Ajit's REE ABI extension series which probes
>> the ABI (using a NULL tree type) and ends up hitting the libcall code 
>> path.
>>
>> [Usual caveat, I'll wait for Pre-commit CI to run the tests and report]
>>
>> gcc/ChangeLog:
>>     * config/riscv/riscv.cc (riscv_promote_function_mode): Fix mode
>>       returned for libcall case.
> Macro that change their arguments are evil ;(   I think Juzhe's patch 
> in this space a few months back wasn't supposed to change behavior.

Oh, its a regression. I can add a Fixes: tag

>
> OK once CI finishes without regressions.

Thx,
-Vineet
  
Patrick O'Neill Nov. 1, 2023, 9:24 p.m. UTC | #7
On 11/1/23 12:19, Vineet Gupta wrote:
>
>
> On 11/1/23 12:11, Jeff Law wrote:
>>
>>
>> On 10/31/23 12:35, Vineet Gupta wrote:
>>> riscv_promote_function_mode doesn't promote a SI to DI for libcalls
>>> case.
>>>
>>> The fix is what generic promote_mode () in explow.cc does. I really
>>> don't understand why the old code didn't work, but stepping thru the
>>> debugger shows old code didn't and fixed does.
>>>
>>> This showed up when testing Ajit's REE ABI extension series which 
>>> probes
>>> the ABI (using a NULL tree type) and ends up hitting the libcall 
>>> code path.
>>>
>>> [Usual caveat, I'll wait for Pre-commit CI to run the tests and report]
>>>
>>> gcc/ChangeLog:
>>>     * config/riscv/riscv.cc (riscv_promote_function_mode): Fix mode
>>>       returned for libcall case.
>> Macro that change their arguments are evil ;(   I think Juzhe's patch 
>> in this space a few months back wasn't supposed to change behavior.
>
> Oh, its a regression. I can add a Fixes: tag
>
>>
>> OK once CI finishes without regressions.
>
> Thx,
> -Vineet
>
It passes precommit CI without any new failures:
https://github.com/ewlu/gcc-precommit-ci/issues/526#issuecomment-1787891174

Tested-by: Patrick O'Neill <patrick@rivosinc.com>

Thanks,
Patrick
  

Patch

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 3e27897d6d30..7b8e9af0a5af 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -8630,9 +8630,10 @@  riscv_promote_function_mode (const_tree type ATTRIBUTE_UNUSED,
     return promote_mode (type, mode, punsignedp);
 
   unsignedp = *punsignedp;
-  PROMOTE_MODE (as_a <scalar_mode> (mode), unsignedp, type);
+  scalar_mode smode = as_a <scalar_mode> (mode);
+  PROMOTE_MODE (smode, unsignedp, type);
   *punsignedp = unsignedp;
-  return mode;
+  return smode;
 }
 
 /* Implement TARGET_MACHINE_DEPENDENT_REORG.  */