<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Sorry, what does "PC" mean here?</div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Philip Reames <listmail@philipreames.com><br>
<b>Sent:</b> Thursday, November 8, 2018 7:27 AM<br>
<b>To:</b> Jordan Rudd; llvm-dev@lists.llvm.org<br>
<b>Subject:</b> Re: [llvm-dev] Passing stack pointer to statepoint-gc</font>
<div> </div>
</div>
<div style="background-color:#FFFFFF">
<p>Jordon,</p>
<p><br>
</p>
<p>First of all, StackMaps are keyed by PC, not by SP.  Each entry corresponds to given location within the generated code. 
<br>
</p>
<p><br>
</p>
<p>This isn't something you want to do in generated code.  Your runtime should be responsible for this.  If you support stack walking or back traces for any purpose, you should already have the code to identify the PC for a suspended frame on the stack.</p>
<p><br>
</p>
<p>If you don't, the easiest approach will be to use a assembly stub between your generated code and your C/C++ runtime code which grabs the PC from the stack, and moves it into an argument register.</p>
<p><br>
</p>
<p>Philip</p>
<p><br>
</p>
<div class="x_moz-cite-prefix">On 11/7/18 5:22 PM, Jordan Rudd via llvm-dev wrote:<br>
</div>
<blockquote type="cite"><style type="text/css" style="display:none">
<!--
p
        {margin-top:0;
        margin-bottom:0}
-->
</style>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Hi,</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I'm trying to use the statepoint-example strategy to build a simple garbage collector. After using the PlaceSafepoints and RewriteStatepointsForGC passes, I can successfully generate a stack map in my binary and call into my runtime's gc poll.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
As far as I can tell, I need the stack pointer at the point of the poll to find the correct frame in the stack map. I'm having trouble doing that.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
One library I looked at online (statepoint-utils) simply defines a helper function written in assembly that pushes the RSP register to RDI before calling into the runtime, so that the runtime gc polling function can read it as a parameter.</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
I was trying to do the same thing except in llvm, so from my gc.safepoint_poll definition, I tried adding these lines:</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span style="font-family:Consolas,Courier,monospace; font-size:10pt">declare void @runtime_safepoint_poll(i64)</span><span><br>
</span>
<div><span style="font-family:Consolas,Courier,monospace; font-size:10pt">declare i64 @llvm.read_register.i64(metadata) #1</span><br>
</div>
<div><span style="font-family:Consolas,Courier,monospace; font-size:10pt">!0 = !{!"rsp\00"}</span><br>
</div>
<div><br>
</div>
<div><span style="font-family:Consolas,Courier,monospace; font-size:10pt">; Read the stack pointer and pass it to our polling function (assumes x64)</span><br>
</div>
<div><span style="font-family:Consolas,Courier,monospace; font-size:10pt">define void @gc.safepoint_poll() {</span><br>
</div>
<div><span style="font-family:Consolas,Courier,monospace; font-size:10pt">  %stackpointer = call i64 @llvm.read_register.i64(metadata !0)</span><br>
</div>
<div><span style="font-family:Consolas,Courier,monospace; font-size:10pt">  call void @runtime_safepoint_poll(i64 %stackpointer)</span><br>
</div>
<div><span style="font-family:Consolas,Courier,monospace; font-size:10pt">  ret void</span><br>
</div>
<div><span style="font-family:Consolas,Courier,monospace; font-size:10pt">}</span><br>
</div>
<div><br>
</div>
<span style="font-family:Consolas,Courier,monospace; font-size:10pt">attributes #1 = { "gc-leaf-function" nounwind readnone }</span><br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><br>
</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>Note that the target triple is x86_64-pc-windows-msvc.</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><br>
</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span>When I test this, the values that come from reading RSP are never in the address space of the module. Like, not even close.</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><br>
</span></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
As far as I can tell, this <i>should </i>work, and I'm a little stymied as to why I'm getting back garbage. Do I misunderstand the read_register call here?</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Thanks,</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
Jordan</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<span><br>
</span></div>
<br>
<fieldset class="x_mimeAttachmentHeader"></fieldset>
<pre class="x_moz-quote-pre">_______________________________________________
LLVM Developers mailing list
<a class="x_moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="x_moz-txt-link-freetext" href="https://nam01.safelinks.protection.outlook.com/?url=http%3A%2F%2Flists.llvm.org%2Fcgi-bin%2Fmailman%2Flistinfo%2Fllvm-dev&data=02%7C01%7C%7C75fc1886795644fde63508d6458ea8d1%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636772876347540128&sdata=rqOd0XZEm7VTKcxviSbjybgpSKgDL3lCU%2Fzq32fi4O0%3D&reserved=0" originalsrc="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" shash="gQrf42n4fyHS1HGTcjKu1JyEDKCnIZ4docPkDHlGxEoUFUINpRX62MQi5SOmVJSVRHfz5ptbrcH4cJGe780zs20cnbCEguiIZq7/MImjE34IcwDmXLFCUJCKdWOQzhWn4EFw/J5TL11vE598y5cF+pksUhNaP0CClqe8ovrHidE=">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
</blockquote>
</div>
</body>
</html>