<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/104393>104393</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [stack guard] Stack guard overhead on Windows platform
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          wenqinI
      </td>
    </tr>
</table>

<pre>
    ## Background
Recently I found the stack guard will bring obviously bigger overhead on Windows than Linux or MacOS, for Speedometer3 (a popular WEB/browser benchmark), it shows about **6-7%** performance improvement with `\GS-` flag on Windows, but just **~1%** improvement with `-fno-stack-protector` on Linux or MacOS.

Under such a background, I did some more research on this, it shows that compared with Linux platform there is more operations stack guard do on Windows platform.

In summary, Windows did 3 more operations:

1. Instead of inlining stack check into caller, Windows platform implement stack check by calling a function.
2. More instructions for checking higher 16 bits is 0 or not in security cookie.
3. More instructions for XOR the rsp register for cookie.

## POC
At current stage, I though the operation 1 and 2 mentioned above may not really help on improving security, so I tried to remove them by  customizing LLVM, it shows **~3%** performance improvement for Speedometer3.

Here is dsm code for a function inside chromium, with `/GS-` flag or custimozed LLVM.

![image](https://github.com/user-attachments/assets/5f1f5001-5a5d-4aff-a8e4-29cddbac149a)


I also filed an [issue](https://issues.chromium.org/issues/352371722) in chromium issue tracker, please check more details in it if you're interested in.

## What could we do
If this approach makes sense, I will clean up the code for LLVM on my local and plan to submit a PR which could ship this feature behind a flag (like `/GS-Unixalike`), WDYT?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVU1z2zgS_TXQpUsqEhQt6aCDP1a7rkoqqXiz3jmCQJNAhA8OPqwoh_ntUwBpW3Z5Zi6yDKFfv379Gs1CUINF3JP2hrR3C5aidH5_Qvu7sveLzonzntCG0AZuGD8O3iUrSHVHqutvyNFGfYZ76PMpRIkQIuNHGBLzAk5Ka-i8s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