On Thu, Mar 12, 2026 at 6:26 PM Vineet Gupta <[email protected]> wrote:
>
> On 3/12/26 6:01 PM, H.J. Lu wrote:
> >>>>> Please try this enclosed patch.
> >>>> Awesome, it does work perfectly for the tripping tests before. I'll give
> >>>> it more extensive testing but this looks promising already.
> >>> If it works, please update the comment to explain that it is done for
> >>> compatibility
> >>> reasons.
> >> Sorry care to elaborate, comment for which part ?
> >> Compatibilty between gcc and llvm ? That's just the trigger,
> >> conceptually it is for implementing a certain ABI.
> > diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
> > index 4773d789d8e..6f41a9e843c 100644
> > --- a/gcc/config/bpf/bpf.cc
> > +++ b/gcc/config/bpf/bpf.cc
> > @@ -299,13 +299,14 @@ bpf_file_end (void)
> >   static rtx
> >   bpf_function_value (const_tree ret_type,
> >            const_tree fntype_or_decl,
> > -         bool outgoing ATTRIBUTE_UNUSED)
> > +         bool outgoing)
> >   {
> >     enum machine_mode mode;
> >     int unsignedp;
> >
> >     mode = TYPE_MODE (ret_type);
> > -  if (INTEGRAL_TYPE_P (ret_type))
> > +  /* NB: Treat the callee's return value as unpromoted.  */
> > +  if (outgoing && INTEGRAL_TYPE_P (ret_type))
> >       mode = promote_function_mode (ret_type, mode, &unsignedp,
> >                fntype_or_decl, 1);
> >
> > is needed only if callee doesn't always extend the return value.
>
> Right, for BPF we don't want callee to *ever* extend since that caller
> is expected to do that.
> The ABI which we are trying to implement is: Caller to promote/extend
> both args and return.
> I think this is atypical since callers args typically implies callee
> return but BPF has cross calling into and from x86 code thus it needs to
> ensure this on both sides in BPF.
>
>
> > I assume
> > that GCC will be changed to always extend the return value in callee.
>
> You mean common code going fwd or do you imply something about BPF code,
> sorry if I sound dense !
>
> > If
> > it is incorrect, you need to avoid extending the callee return value in
> > TARGET_PROMOTE_FUNCTION_MODE.
>
> The ABI being implemented expects caller to do promote/extend so no
> point in doing it in callee.

Try this patch then.


-- 
H.J.
diff --git a/gcc/config/bpf/bpf.cc b/gcc/config/bpf/bpf.cc
index 4773d789d8e..e74302a928a 100644
--- a/gcc/config/bpf/bpf.cc
+++ b/gcc/config/bpf/bpf.cc
@@ -302,13 +302,7 @@ bpf_function_value (const_tree ret_type,
 		    bool outgoing ATTRIBUTE_UNUSED)
 {
   enum machine_mode mode;
-  int unsignedp;
-
   mode = TYPE_MODE (ret_type);
-  if (INTEGRAL_TYPE_P (ret_type))
-    mode = promote_function_mode (ret_type, mode, &unsignedp,
-				  fntype_or_decl, 1);
-
   return gen_rtx_REG (mode, BPF_R0);
 }
 
@@ -327,6 +321,9 @@ bpf_function_value_regno_p (const unsigned int regno)
 #undef TARGET_FUNCTION_VALUE_REGNO_P
 #define TARGET_FUNCTION_VALUE_REGNO_P bpf_function_value_regno_p
 
+#undef TARGET_PROMOTE_FUNCTION_MODE
+#define TARGET_PROMOTE_FUNCTION_MODE \
+  default_promote_function_mode_sign_extend
 
 /* Determine whether to warn about lack of return statement in a
    function.  */
diff --git a/gcc/config/bpf/bpf.h b/gcc/config/bpf/bpf.h
index c8dad55fd4c..74d07e7f910 100644
--- a/gcc/config/bpf/bpf.h
+++ b/gcc/config/bpf/bpf.h
@@ -49,8 +49,8 @@
   do						\
     {						\
       if (GET_MODE_CLASS (M) == MODE_INT	\
-	  && GET_MODE_SIZE (M) < 8)		\
-	M = DImode;				\
+	  && GET_MODE_SIZE (M) < 4)		\
+	M = SImode;				\
     } while (0)
 
 /* Align argument parameters on the stack to 64-bit, at a minimum.  */

Reply via email to