<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<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);">
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>
</body>
</html>