<div dir="ltr">My 2c:<div><br></div><div> * 'this' inside a lambda should probably refer to the captured 'this', so that the semantics match those of the source language. It would seem reasonable to provide an easy way to access the closure object itself, but I don't really have a preference for what name to give it.</div>
<div> * I'm pretty sure that reference captures should have type [const] T&, not T, within the lambda. Consider the case where the lambda is invoked (and is crashing) after the enclosing function has returned. It's *much* more useful to see that your reference capture is a reference (and that it's a reference to something that no longer exists) than to transparently look through it to the non-existent enclosing stack frame.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Apr 6, 2014 at 8:00 PM, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">(oops, add the list back...)<br>
<br>
Hmm - well there's a wrinkle.<br>
<br>
Seems GCC's approach allows GDB to print unqualified members of the<br>
captured 'this'. ie: in the below example, breaking in the body of the<br>
lambda and "print i" works as expected. I assume that this is<br>
happening by GDB completely ignoring object_pointer and doing<br>
unqualified name lookup with whatever variable "this" it happens to<br>
find in scope.<br>
<br>
That would explain why, if I remove the name of the "this" parameter*<br>
GDB stops being able to find anything - either the locally captured<br>
'x' or the lambda.<br>
<br>
So I guess we can't have it both ways - we can't expect GDB to treat a<br>
member named "this" the way it should treat the object_pointer<br>
(allowing unqualified names to find members of the captured object)<br>
while also expecting GDB to find the "this" member to begin with...<br>
<br>
Well, maybe, but that'd be somewhat heroic of GDB to juggle those<br>
competing attitudes/ideas about what the this pointer is.<br>
<br>
I still dislike that GCC's approach causes the captures to shadow<br>
parameters, which is inconsistent with the behavior of C++... so I'm<br>
not sure there's a "right" answer here, yet.<br>
<br>
* my previous observation was that removing this name did nothing -<br>
that wasn't the case, I'd just removed it from the wrong function...<br>
<div class="HOEnZb"><div class="h5"><br>
On Sun, Apr 6, 2014 at 6:18 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>> wrote:<br>
> On Sun, Apr 6, 2014 at 4:38 PM, Eric Christopher <<a href="mailto:echristo@gmail.com">echristo@gmail.com</a>> wrote:<br>
>> I was pretty sure all of this already worked outside of the captured this?<br>
><br>
> Seems to work well enough (my comments in (2) were mostly pedantry if<br>
> we wanted to get the type exactly as written, rather than as a<br>
> reference), though it's not quite the same as how GCC implements it.<br>
><br>
>> The captured this was the only problem. GCC deals with this by calling the<br>
>> captured this 'this' and the anonymous this as '__this' IIRC.<br>
><br>
> Looks like it might be the other way around. GCC's captured member is<br>
> "__this", their lambda's "this" is called "__closure", but then they<br>
> introduce local variables into the op() function to describe all of<br>
> the captures with locations like:<br>
><br>
> 91 68 06 (fbreg+68 (the same location as "__closure"), 06 (deref)) for<br>
> the first capture<br>
> <a href="tel:91%2068%2006%2023%2008" value="+19168062308">91 68 06 23 08</a> (as above, then plus_uconst 8)<br>
><br>
> I suspect this would get the wrong behavior if you happened to name<br>
> your parameters the same as your captured variables (can you do that?<br>
> seems you can - if you capture a global by reference (by qualifying<br>
> the name)) - with GCC's scheme the name of the captured variable<br>
> shadows the name of the parameter making the parameter impossible to<br>
> access. With Clang's scheme (just naming the member variables without<br>
> introducing the local variable DIEs) the unqualified name finds the<br>
> parameter and "this->name" finds the captured variable.<br>
><br>
> So it's not just a matter of renaming things - indeed, even with the<br>
> lambda's 'this' unnamed, GDB manages to make that happen (presumably<br>
> based on the subprogram's object_pointer attribute and the assumption<br>
> that that variable should be called "this" even if the name wasn't<br>
> provided).<br>
><br>
> If GDB were to be 'fixed' (perhaps there's a good reason it treats<br>
> unnamed object_pointers as though they were named "this", and thus<br>
> cannot be changed/fixed) then Clang should need to do nothing to be as<br>
> right as GCC and more right in some ways (shadowing).<br>
><br>
><br>
><br>
> & the pedantry I was referring to in (2) is just that if you describe<br>
> the variable as a T& (either as a member like Clang does, or as a<br>
> local variable as GCC does) then GDB prints it out like "= (int &)<br>
> @0x7fffffffd994: 3" whereas if we do the extra deref (probably only<br>
> worth doing if we're doing a bunch of work to do custom location<br>
> descriptions from the frontend anyway) you get "= 3" which is a bit<br>
> nicer.<br>
><br>
><br>
>> Could be wrong<br>
>> though, example?<br>
><br>
> struct foo { int i; int func(); };<br>
><br>
> int foo::func() {<br>
> int x = 3;<br>
> return [&]() { return x + i; }();<br>
> }<br>
><br>
> int main() {<br>
> foo f;<br>
> f.i = 42;<br>
> f.func();<br>
> }<br>
><br>
> GCC's lambda looks like this:<br>
><br>
> structure_type<br>
> name = "<lambda()>"<br>
><br>
> member<br>
> name = "__x"<br>
> type = int&<br>
> data_member_location = 0<br>
><br>
> member<br>
> name = "__this"<br>
> type = foo*<br>
> data_member_location = 8<br>
><br>
> subprogram<br>
> name = "~<lambda>"<br>
> ...<br>
><br>
> subprogram<br>
> name = "operator()"<br>
> frame_base = call_frame_cfa<br>
> object_pointer = the __closure parameter DIE<br>
> Unknown_2117 = true<br>
><br>
> formal_parameter<br>
> name = "__closure"<br>
> type = const lambda* const<br>
> artificial<br>
> location = fbreg(0x68)<br>
><br>
> variable<br>
> name = "x"<br>
> type = int&<br>
> artificial<br>
> location = fbreg(0x68) deref<br>
><br>
> variable<br>
> name = "this"<br>
> type = foo * const<br>
> artificial<br>
> location = fbreg(0x68) deref plus_uconst(8)<br>
><br>
><br>
> Clang's output looks like this:<br>
><br>
> class_type<br>
> member<br>
> name = "x"<br>
> type = int&<br>
> data_member_location = 0<br>
> accessibility = private<br>
><br>
> member<br>
> name = "this"<br>
> type = foo*<br>
> data_member_location = 8<br>
> accessibility = private<br>
><br>
> subprogram<br>
> name = "operator()"<br>
> accessibility = public<br>
> declaration<br>
><br>
> formal_parameter<br>
> type = const lambda*<br>
> artificial<br>
><br>
> ...<br>
><br>
> subprogram<br>
> specification = lambda::operator()<br>
> frame_base = DW_OP_reg6<br>
> object_pointer = the 'this' parameter DIE<br>
><br>
> formal_parameter<br>
> name = "this"<br>
> type = const lambda*<br>
> artificial<br>
> location = DW_OP_fbreg(0x78)<br>
><br>
><br>
> So, different implementations (GCC munges the names of the actual<br>
> member variables that do the capturing, but introduces local variables<br>
> (that just use location expressions to point to the member variables)<br>
> into the lambda's function call operator - clang just names the member<br>
> variables the actual name in the first place) but a similar result,<br>
> except for 'this'.<br>
><br>
> Still pretty sure that GDB should let you name the object_pointer<br>
> something other than "this" and respect that, finding the member<br>
> variable "this" instead. (with a modification to Clang not to name the<br>
> parameter "this" - either no name, or "__closure" like GCC, etc)<br>
><br>
>><br>
>> Figured I'd start the discussion here, while it's on my mind/I've<br>
>> built up some state.<br>
>><br>
>> The theory is that, at a bare minimum, we need to describe the<br>
>> location of the captured "this" variable (so the lambda's own "this"<br>
>> doesn't hide it).<br>
>><br>
>> 1) Counterpoint: Clang correctly describes the member variable as<br>
>> being named "this". Perhaps we could argue that the debugger, upon<br>
>> seeing a member variable called "this" should treat that as what the<br>
>> user means when they say "this". But that's probably not actually a<br>
>> reasonable thing to do - the lambda's "this" is in a closer scope (an<br>
>> implicit argument to the member function) and should probably override<br>
>> (though clearly the compiler has to treat the object pointer as<br>
>> special in some way... )<br>
>><br>
>> ooh, that gets me thinking: what if we just didn't provide a name for<br>
>> the object pointer? Since we want "this" to refer to the member<br>
>> variable, it seems like it should be possible to remove the name and<br>
>> then the debugger should fall back to finding the member variable<br>
>> 'this' instead.<br>
>><br>
>> Hacking up LLVM's IR to do this produced the expected debug info, but<br>
>> it did not produce the expected debugger behavior, "ptype this" still<br>
>> found the lambda's "this" rather than the capture. I'm inclined to<br>
>> file that as a gdb bug. Thoughts?<br>
>><br>
>> 2) If we wanted to be pedantically correct, we should actually create<br>
>> special variables to describe the location of all reference captures -<br>
>> currently with both Clang and GCC, the type of a reference capture is<br>
>> T&, but arguably we should have a variable of type "T" (that was the<br>
>> actual type in the user's code after all, right?) and we could do this<br>
>> by introducing a DW_TAG_variable for each captured variable that<br>
>> indirects through the lambda's this pointer to the member and<br>
>> indirects again through the pointer that lives there. But this is<br>
>> perhaps a bit more than is valuable.<br>
>><br>
>> But if we do end up needing to describe the location of captured<br>
>> "this" rather than using my suggestion above, that would probably mean<br>
>> introducing a sufficiently generic location expression system into<br>
>> Clang that we could address (2) too.<br>
>><br>
>> The main thing I think we need is the ability to do "indirect +<br>
>> offset" (currently for C++ non-trivial pass by value parameters we<br>
>> have "indirect", and if the captured this were always the first member<br>
>> of the lambda, that feature alone would be sufficient (I haven't<br>
>> tested this, but might) - just use the same location for captured<br>
>> "this" as for the lambda's "this", but add the indirect flag... -<br>
>> except we need an offset as well - and that might fall outside the<br>
>> sort of stuff we want to stuff in the DW_TAG_auto_variable metadata,<br>
>> and instead incline us towards generalizing the dbg.declare intrinsic<br>
>> to describe more complex location information?)<br>
>><br>
>> Any other thoughts/ideas?<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
</div></div></blockquote></div><br></div>