On Mon, Sep 14, 2020 at 9:46 AM Eric Botcazou <botca...@adacore.com> wrote: > > Hi, > > when a thunk cannot be emitted in assembly directly, cgraph_node::expand_thunk > generates regular GIMPLE code but unconditionally forces a tail call to the > target of the thunk. That's theoretically OK because the thunk essentially > forwards its parameters to the target, but in practice the RTL expander can > spill parameters passed by reference on the stack, see assign_parm_setup_reg: > > /* If we were passed a pointer but the actual value can safely live > in a register, retrieve it and use it directly. */ > if (data->arg.pass_by_reference && TYPE_MODE (TREE_TYPE (parm)) != BLKmode) > { > /* We can't use nominal_mode, because it will have been set to > Pmode above. We must use the actual mode of the parm. */ > if (use_register_for_decl (parm)) > { > parmreg = gen_reg_rtx (TYPE_MODE (TREE_TYPE (parm))); > mark_user_reg (parmreg); > } > else > { > int align = STACK_SLOT_ALIGNMENT (TREE_TYPE (parm), > TYPE_MODE (TREE_TYPE (parm)), > TYPE_ALIGN (TREE_TYPE (parm))); > parmreg > = assign_stack_local (TYPE_MODE (TREE_TYPE (parm)), > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (parm))), > align); > set_mem_attributes (parmreg, parm, 1); > } > > use_register_for_decl always return false at -O0 so, in this case, the thunk > will pass an address within its frame to its target, so it cannot use a tail > call to invoke it. > > Tested on x86_64-suse-linux, OK for the mainline?
ISTR the tailcall flag is only a hint and RTL expansion can decide to not tailcall based on targets. So to me it looks like a missed disqualification on the RTL expansion side. Or do we, besides from this very single spot, simply never tailcall at -O0 and thus never hit this latent issue? How does this change the debug experience at -O0 when GIMPLE thunks are used? Thanks, Richard. > > 2020-09-14 Eric Botcazou <ebotca...@adacore.com> > > * cgraphunit.c (cgraph_node::expand_thunk): Force a tail call only > when optimizing. > > > 2020-09-14 Eric Botcazou <ebotca...@adacore.com> > > * gnat.dg/thunk1.adb: New test. > * gnat.dg/thunk1_pkg1.ads: New helper. > * gnat.dg/thunk1_pkg2.ads: Likewise. > * gnat.dg/thunk1_pkg2.adb: Likewise. > > -- > Eric Botcazou