Re: What does "return" keyword mean in INFO_TABLE_RET declarations?

2018-03-18 Thread Rahul Muttineni
Hi Omer,

An INFO_TABLE_RET is a frame that "can be returned to" and the return
keyword allows you to provide a name for the value(s) that was(were)
returned to this frame and do something with it if you wish. If you didn't
have this keyword, you would have to do low-level stack manipulations
yourself to get a handle on the return value and it's easy to mess up.

You can think of INFO_TABLE_RET as a traditional stack frame in languages
like C, except it's powerful because you can specify custom logic on how
you deal with the returned value. In some cases, like stg_atomically_frame,
you may not even return the value further down into the stack until certain
conditions are met (the transaction is valid).

Hope that helps,
Rahul

On Sun, Mar 18, 2018 at 8:18 PM, Ömer Sinan Ağacan 
wrote:

> Hi,
>
> I'm trying to understand what a "return" list in INFO_TABLE_RET declaration
> line specifies. As far as I understand a "return" in the declaration line
> is
> something different than a "return" in the body. For example, in this
> definition: (in HeapStackCheck.cmm)
>
> INFO_TABLE_RET ( stg_ret_p, RET_SMALL, W_ info_ptr, P_ ptr )
> return (/* no return values */)
> {
> return (ptr);
> }
>
> The return list is empty and it even says "no return values" explicitly,
> yet it
> returns something.
>
> My guess is that the "return" list in the header is actually for
> arguments. I
> found this info table which has an argument: (in StgMiscClosures.cmm)
>
> INFO_TABLE_RET (stg_restore_cccs_eval, RET_SMALL, W_ info_ptr, W_ cccs)
> return (P_ ret)
> {
> unwind Sp = Sp + WDS(2);
> #if defined(PROFILING)
> CCCS = cccs;
> #endif
> jump stg_ap_0_fast(ret);
> }
>
> This is the use site: (in Interpreter.c)
>
> #if defined(PROFILING)
> // restore the CCCS after evaluating the closure
> Sp_subW(2);
> SpW(1) = (W_)cap->r.rCCCS;
> SpW(0) = (W_)_restore_cccs_eval_info;
> #endif
> Sp_subW(2);
> SpW(1) = (W_)tagged_obj;
> SpW(0) = (W_)_enter_info;
> RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);
>
> If I understand this correctly, the "tagged_obj" code will put the return
> value
> in R1, pop the stack (which will have stg_restore_ccs_eval_info at the
> bottom)
> and jump to this the info table code shown above. So `P_ ret` is the value
> of
> `tagged_obj`, and the "return" list is actually for parameters.
>
> Did I get this right? If I did, I'm curious why it's called "return" and
> not
> "args" or something like that.
>
> Thanks,
>
> Ömer
> ___
> ghc-devs mailing list
> ghc-devs@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>



-- 
Rahul Muttineni
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Re: Is "cml_cont" of CmmCall used in practice?

2018-03-18 Thread Kavon Farvardin
> Is the "cml_cont" field of the CmmCall variant is really used in practice?


Seconding what Michal has already said, yes, the `cml_cont` field is used quite 
a bit in Cmm.

Any non-tail call in the program will have this field populated with 
information about where control-flow continues after the call returns.

This field is used for some important steps, such as Proc Point analysis ( 
CmmProcPoint.callProcPoints ), the result of which is used to layout the stack. 
Stack layout uses the information about what values are live in the 
continuation block of a non-tail call to determine what values need to be saved 
to the stack.

Thus, the control-flow graph structure of a CmmProc is defined in part by the 
`cml_cont` field (see the Cmm specific instance of Hoopl's NonLocal class in 
CmmNode, where it is used to indicate successors of a block).

> I traversed the output of raw Cmm produced by ghc compiling the whole base 
> package, but the value of cml_cont is always Nothing.

Hrm, were you looking for "returns to" in that output?

~kavon


> On Mar 18, 2018, at 8:52 AM, Michal Terepeta  
> wrote:
> 
> On Sun, Mar 18, 2018 at 6:38 AM Shao, Cheng  > wrote:
> Hi all,
> 
> Is the "cml_cont" field of the CmmCall variant is really used in practice? I 
> traversed the output of raw Cmm produced by ghc compiling the whole base 
> package, but the value of cml_cont is always Nothing.
> 
> Regards,
> Shao Cheng
> 
> 
> Hi,
> 
> I'm not a GHC expert, so please don't trust everything I say ;)
> 
> That being said, I think `cml_cont` is used a lot. If you look at the
> `compiler/codeGen` directory (that's what turns STG to cmm), you'll
> see that `MkGraph.mkCallReturnsTo` is called a few times. That's the
> function that will construct a `CmmCall` with the continuation block.
> 
> When dumping cmm, you'll often see all those `returns to` notes. For
> instance, compiling:
> 
> ```
> foo :: Int -> Int
> foo x =
>   case x of
> 42 -> 11
> _  -> 00
> ```
> 
> results in:
> 
> ```
>[...]
>c2cN: // global
>I64[Sp - 8] = c2cI;
>R1 = R2;
>Sp = Sp - 8;
>if (R1 & 7 != 0) goto c2cI; else goto c2cJ;
> 
>// Evaluate the parameter.
>c2cJ: // global
>call (I64[R1])(R1) returns to c2cI, args: 8, res: 8, upd: 8;
>// ^^^
>// this specifies the continuation block
>// see also PprCmm.pprNode
> 
>// Now check if it's 42.
>c2cI: // global
>if (I64[R1 + 7] == 42) goto c2cU; else goto c2cT;
>c2cU: // global
>[...]
> ```
> 
> As far as I understand it, this allows the code above to jump to the
> `x` closure (to evalutae it), and have the closure jump back to the
> continuation block (note that it's address is stored before we jump to
> closure). AFAICS this particular code is created by
> `StgCmmExpr.emitEnter`.
> 
> Hope this helps!
> 
> - Michal
> 
> ___
> ghc-devs mailing list
> ghc-devs@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs



signature.asc
Description: Message signed with OpenPGP
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


What does "return" keyword mean in INFO_TABLE_RET declarations?

2018-03-18 Thread Ömer Sinan Ağacan
Hi,

I'm trying to understand what a "return" list in INFO_TABLE_RET declaration
line specifies. As far as I understand a "return" in the declaration line is
something different than a "return" in the body. For example, in this
definition: (in HeapStackCheck.cmm)

INFO_TABLE_RET ( stg_ret_p, RET_SMALL, W_ info_ptr, P_ ptr )
return (/* no return values */)
{
return (ptr);
}

The return list is empty and it even says "no return values" explicitly, yet it
returns something.

My guess is that the "return" list in the header is actually for arguments. I
found this info table which has an argument: (in StgMiscClosures.cmm)

INFO_TABLE_RET (stg_restore_cccs_eval, RET_SMALL, W_ info_ptr, W_ cccs)
return (P_ ret)
{
unwind Sp = Sp + WDS(2);
#if defined(PROFILING)
CCCS = cccs;
#endif
jump stg_ap_0_fast(ret);
}

This is the use site: (in Interpreter.c)

#if defined(PROFILING)
// restore the CCCS after evaluating the closure
Sp_subW(2);
SpW(1) = (W_)cap->r.rCCCS;
SpW(0) = (W_)_restore_cccs_eval_info;
#endif
Sp_subW(2);
SpW(1) = (W_)tagged_obj;
SpW(0) = (W_)_enter_info;
RETURN_TO_SCHEDULER_NO_PAUSE(ThreadRunGHC, ThreadYielding);

If I understand this correctly, the "tagged_obj" code will put the return value
in R1, pop the stack (which will have stg_restore_ccs_eval_info at the bottom)
and jump to this the info table code shown above. So `P_ ret` is the value of
`tagged_obj`, and the "return" list is actually for parameters.

Did I get this right? If I did, I'm curious why it's called "return" and not
"args" or something like that.

Thanks,

Ömer
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Re: Is "cml_cont" of CmmCall used in practice?

2018-03-18 Thread Michal Terepeta
On Sun, Mar 18, 2018 at 6:38 AM Shao, Cheng  wrote:

> Hi all,
>
> Is the "cml_cont" field of the CmmCall variant is really used in practice?
> I traversed the output of raw Cmm produced by ghc compiling the whole base
> package, but the value of cml_cont is always Nothing.
>
> Regards,
> Shao Cheng
>


Hi,

I'm not a GHC expert, so please don't trust everything I say ;)

That being said, I think `cml_cont` is used a lot. If you look at the
`compiler/codeGen` directory (that's what turns STG to cmm), you'll
see that `MkGraph.mkCallReturnsTo` is called a few times. That's the
function that will construct a `CmmCall` with the continuation block.

When dumping cmm, you'll often see all those `returns to` notes. For
instance, compiling:

```
foo :: Int -> Int
foo x =
  case x of
42 -> 11
_  -> 00
```

results in:

```
   [...]
   c2cN: // global
   I64[Sp - 8] = c2cI;
   R1 = R2;
   Sp = Sp - 8;
   if (R1 & 7 != 0) goto c2cI; else goto c2cJ;

   // Evaluate the parameter.
   c2cJ: // global
   call (I64[R1])(R1) returns to c2cI, args: 8, res: 8, upd: 8;
   // ^^^
   // this specifies the continuation block
   // see also PprCmm.pprNode

   // Now check if it's 42.
   c2cI: // global
   if (I64[R1 + 7] == 42) goto c2cU; else goto c2cT;
   c2cU: // global
   [...]
```

As far as I understand it, this allows the code above to jump to the
`x` closure (to evalutae it), and have the closure jump back to the
continuation block (note that it's address is stored before we jump to
closure). AFAICS this particular code is created by
`StgCmmExpr.emitEnter`.

Hope this helps!

- Michal
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Re: Is "cml_cont" of CmmCall used in practice?

2018-03-18 Thread Cheng Shao
Hi Ömer,

See e.g. `lowerSafeForeignCall` and `blockCode`
> which set the field with `Just`. The former seems to be related with
> foreign
> calls so perhaps try compiling a FFI package.


I tried compiling a module with a `foreign import ccall safe` declaration,
yet the output raw Cmm still doesn't use `cml_cont`.

`CmmLayoutStack` uses that field
> for code generation (I don't understand the details yet).
>

Thanks for pointing that out, I'll check its implementation.

I also found another evidence of the field not used by codegens: in `PprC`
which is used by unregisterised builds, only the `cml_target` field is
used. So for now I assume that the assertion of `cml_cont = Nothing` holds
for final Cmm output, and backend writers need not be concerned with it.

Regards,
Shao Cheng
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs


Re: Is "cml_cont" of CmmCall used in practice?

2018-03-18 Thread Ömer Sinan Ağacan
Hi Shao,

Perhaps not in the Cmm output generated for your programs, but it's definitely
used in the code generator. See e.g. `lowerSafeForeignCall` and `blockCode`
which set the field with `Just`. The former seems to be related with foreign
calls so perhaps try compiling a FFI package. `CmmLayoutStack` uses that field
for code generation (I don't understand the details yet).

Ömer

2018-03-18 8:38 GMT+03:00 Shao, Cheng :
> Hi all,
>
> Is the "cml_cont" field of the CmmCall variant is really used in practice? I
> traversed the output of raw Cmm produced by ghc compiling the whole base
> package, but the value of cml_cont is always Nothing.
>
> Regards,
> Shao Cheng
>
> ___
> ghc-devs mailing list
> ghc-devs@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs
>
___
ghc-devs mailing list
ghc-devs@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs