<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title>Sergey &quot;Shnatsel&quot; Davidoff</title>
	<subtitle>Rust, security, and making computers go brrr</subtitle>
	<link href="https://shnatsel.github.io/atom.xml" rel="self" type="application/atom+xml"/>
	<link href="https://shnatsel.github.io"/>
	<generator uri="https://www.getzola.org/">Zola</generator>
	<updated>2026-06-20T00:00:00+00:00</updated>
	<id>https://shnatsel.github.io/atom.xml</id>
	<entry xml:lang="en">
		<title>The unreasonable effectiveness of LLMs for auditing Rust code</title>
        <author>
            <name>Sergey &quot;Shnatsel&quot; Davidoff</name>
        </author>
		<published>2026-06-20T00:00:00+00:00</published>
		<updated>2026-06-20T00:00:00+00:00</updated>
		<link href="https://shnatsel.github.io/the-unreasonable-effectiveness-of-llms-for-auditing-rust-code/"/>
		<link rel="alternate" href="https://shnatsel.github.io/the-unreasonable-effectiveness-of-llms-for-auditing-rust-code/" type="text/html"/>
		<id>https://shnatsel.github.io/the-unreasonable-effectiveness-of-llms-for-auditing-rust-code/</id>
        <summary type="html">&lt;p&gt;As a lead of the &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;rust-lang.org&#x2F;governance&#x2F;teams&#x2F;#team-wg-secure-code&quot;&gt;Rust Secure Code Working Group&lt;&#x2F;a&gt;, I got free access to GPT-5.5 via the &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;openai.com&#x2F;form&#x2F;codex-for-oss&#x2F;&quot;&gt;Codex for Open Source&lt;&#x2F;a&gt;. Since then I’ve found and reported dozens of issues of varying severity in widely used Rust crates.&lt;&#x2F;p&gt;</summary>
		<content type="html">&lt;p&gt;As a lead of the &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;rust-lang.org&#x2F;governance&#x2F;teams&#x2F;#team-wg-secure-code&quot;&gt;Rust Secure Code Working Group&lt;&#x2F;a&gt;, I got free access to GPT-5.5 via the &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;openai.com&#x2F;form&#x2F;codex-for-oss&#x2F;&quot;&gt;Codex for Open Source&lt;&#x2F;a&gt;. Since then I’ve found and reported dozens of issues of varying severity in widely used Rust crates.&lt;&#x2F;p&gt;
&lt;span id=&quot;continue-reading&quot;&gt;&lt;&#x2F;span&gt;
&lt;p&gt;Separately, the &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;rustfoundation.org&#x2F;security-initiative&#x2F;&quot;&gt;Rust Foundation security initiative&lt;&#x2F;a&gt; got access to &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;www.anthropic.com&#x2F;claude&#x2F;mythos&quot;&gt;Mythos&lt;&#x2F;a&gt; via &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;www.anthropic.com&#x2F;glasswing&quot;&gt;Project Glasswing&lt;&#x2F;a&gt;, and their report should also be coming soon. I’ve coordinated with them so that our audit targets would not overlap.&lt;&#x2F;p&gt;
&lt;p&gt;While I haven’t found any truly devastating vulnerabilities, I am very impressed with GPT-5.5 for auditing Rust source code, and I’ll absolutely be adding it to my toolkit alongside fuzzers.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; &lt;em&gt;All opinions expressed in this article are my own, not that of any organizations I am a part of.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;methodology&quot;&gt;Methodology&lt;a class=&quot;zola-anchor&quot; href=&quot;#methodology&quot; aria-label=&quot;Anchor link for: methodology&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;Since maintainers could already be dealing with a large amount of vulnerability reports, it is &lt;em&gt;imperative&lt;&#x2F;em&gt; that I do not submit any invalid vulnerability reports and waste maintainers’ already limited time.&lt;&#x2F;p&gt;
&lt;p&gt;So I’ve decided to look for unambiguously problematic class of vulnerability that’s easy to verify: memory safety bugs.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;wait-isn-t-rust-memory-safe&quot;&gt;Wait, isn’t Rust memory-safe?&lt;a class=&quot;zola-anchor&quot; href=&quot;#wait-isn-t-rust-memory-safe&quot; aria-label=&quot;Anchor link for: wait-isn-t-rust-memory-safe&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h3&gt;
&lt;p&gt;Yes, with an asterisk.&lt;&#x2F;p&gt;
&lt;p&gt;Most code you’d write in Rust is memory-safe, but at some point you have to talk to the operating system or a C library or implement things like intrusive data structures, all of which involves raw pointers.&lt;&#x2F;p&gt;
&lt;p&gt;Most languages implement these parts in C (e.g. CPython), provide unsafe interoperability with C, and have you write C for your own unsafe code, while Rust has its own unsafe subset where you can muck about with raw pointers.&lt;&#x2F;p&gt;
&lt;p&gt;This puts Rust’s memory safety properties on par with Python’s, ahead of Go &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;www.ralfj.de&#x2F;blog&#x2F;2025&#x2F;07&#x2F;24&#x2F;memory-safety.html&quot;&gt;which violates safety on data races&lt;&#x2F;a&gt;, and behind browser-sandboxed JavaScript (but you can match that by compiling Rust to WebAssembly).&lt;&#x2F;p&gt;
&lt;p&gt;As for the amount of safe vs unsafe code in the wild, &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;g0wu9b&#x2F;percentage_of_unsafe_code_per_crate_for&#x2F;&quot;&gt;my own scan from 2020&lt;&#x2F;a&gt; showed that 95% of the code on &lt;a rel=&quot;nofollow external&quot; href=&quot;http:&#x2F;&#x2F;crates.io&quot;&gt;crates.io&lt;&#x2F;a&gt; is memory safe. The authors of the 2020 paper “&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;pm.inf.ethz.ch&#x2F;publications&#x2F;AstrauskasMathejaMuellerPoliSummers20.pdf&quot;&gt;How do programmers use unsafe Rust?&lt;&#x2F;a&gt;” independently arrived to the 95% number, although they didn’t put it into the final paper because they weren’t confident in their methodology for it. My own scan is also rather crude, but two completely different measurements arriving to the same number is encouraging.&lt;&#x2F;p&gt;
&lt;p&gt;In practice memory safety vulnerability rate reduction compared to C++ is &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;blog.google&#x2F;security&#x2F;rust-in-android-move-fast-fix-things&#x2F;&quot;&gt;about 1000x&lt;&#x2F;a&gt;, which is more than you’d expect based on the above figures.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;preventing-false-positives&quot;&gt;Preventing false positives&lt;a class=&quot;zola-anchor&quot; href=&quot;#preventing-false-positives&quot; aria-label=&quot;Anchor link for: preventing-false-positives&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h3&gt;
&lt;p&gt;Rust has a tool called &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;miri&quot;&gt;miri&lt;&#x2F;a&gt; that runs Rust code in an interpreter and tells you precisely whether it committed any crimes against the language rules or not. Safe Rust cannot violate them by construction, but unsafe Rust can, and a validator that immediate tells you whether you messed up or not instead of having to parse dozens of pages of dense prose is indispensable.&lt;&#x2F;p&gt;
&lt;p&gt;It also &lt;strong&gt;completely eliminates false positives&lt;&#x2F;strong&gt; from LLM vulnerability findings.&lt;&#x2F;p&gt;
&lt;p&gt;If the LLM can construct a unit test that causes miri to fail, I can report that to the maintainers and be certain that it’s a bug. I don’t ever have to argue if it’s a real issue or not, either — the proof is right there. And if miri says the execution is completely fine, then the LLM false positive gets discarded before anyone even sees it.&lt;&#x2F;p&gt;
&lt;p&gt;To the best of my knowledge, &lt;strong&gt;no other language&lt;&#x2F;strong&gt; has a practical tool with this level of precision. &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;clang.llvm.org&#x2F;docs&#x2F;AddressSanitizer.html&quot;&gt;Sanitizers&lt;&#x2F;a&gt; are very nice, but can’t catch everything, so verifying against them does not prove absence of issues.&lt;&#x2F;p&gt;
&lt;p&gt;Sadly miri is not without limitations — execution with extra checks is slow, calling into C is not supported, and syscall support is limited. When miri is not applicable, you can fall back on the &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;beta&#x2F;unstable-book&#x2F;compiler-flags&#x2F;sanitizer.html&quot;&gt;sanitizers&lt;&#x2F;a&gt; and get &lt;em&gt;some&lt;&#x2F;em&gt; filtering.&lt;&#x2F;p&gt;
&lt;p&gt;I also had to switch miri to the newer &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;perso.crans.org&#x2F;vanille&#x2F;treebor&#x2F;&quot;&gt;Tree Borrows&lt;&#x2F;a&gt; aliasing model (as opposed to the older &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;plv.mpi-sws.org&#x2F;rustbelt&#x2F;stacked-borrows&#x2F;&quot;&gt;Stacked Borrows&lt;&#x2F;a&gt;) to avoid false positives, but fortunately that’s just one flag, -Zmiri-tree-borrows.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;harness&quot;&gt;Harness&lt;a class=&quot;zola-anchor&quot; href=&quot;#harness&quot; aria-label=&quot;Anchor link for: harness&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h3&gt;
&lt;p&gt;My setup was very basic: just Codex and a &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;Shnatsel&#x2F;e83219d7d6b73255373c2818ee438cda&quot;&gt;prompt&lt;&#x2F;a&gt;, with GPT-5.5 set to xhigh reasoning effort. It is important for the model to be able to write and run unit tests to try and trigger the issue under miri, so I consider something like Codex essential.&lt;&#x2F;p&gt;
&lt;p&gt;It would be interesting to try a more elaborate harness like &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;arm&#x2F;metis&quot;&gt;metis&lt;&#x2F;a&gt;, but even this basic setup was enough to discover interesting bugs.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;findings&quot;&gt;Findings&lt;a class=&quot;zola-anchor&quot; href=&quot;#findings&quot; aria-label=&quot;Anchor link for: findings&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;The most serious issue I’ve found is an &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tirr-c&#x2F;jxl-oxide&#x2F;security&#x2F;advisories&#x2F;GHSA-5pmv-rx8r-wmv5&quot;&gt;out-of-bounds write in&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tirr-c&#x2F;jxl-oxide&#x2F;security&#x2F;advisories&#x2F;GHSA-5pmv-rx8r-wmv5&quot;&gt;jxl-grid&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tirr-c&#x2F;jxl-oxide&#x2F;security&#x2F;advisories&#x2F;GHSA-5pmv-rx8r-wmv5&quot;&gt;crate&lt;&#x2F;a&gt;. It is a part of &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;tirr-c&#x2F;jxl-oxide&quot;&gt;jxl-oxide&lt;&#x2F;a&gt;, a JPEG XL decoder in Rust (not to be confused with &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;libjxl&#x2F;jxl-rs&quot;&gt;jxl-rs&lt;&#x2F;a&gt;, which Firefox and Chromium are adopting for JPEG XL decoding; that one came up clean in my audit).&lt;&#x2F;p&gt;
&lt;p&gt;I’ve already fuzzed this crate earlier, but the fuzzer didn’t catch this issue because it only happens on 32-bit platforms and requires very large image dimensions. I wasn’t running the fuzzer on 32-bit, and the fuzzer is limited to small image dimensions to avoid exhausting my computer’s RAM, so it never had a chance to trigger this condition at runtime.&lt;&#x2F;p&gt;
&lt;p&gt;The initial demonstrator showed this as a jxl-grid issue, but it was not clear if it’s a theoretical problem or if it can be triggered by decoding a crafted image. GPT-5.5 &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;gist.github.com&#x2F;Shnatsel&#x2F;2c4e4f75e5892988d1315aa7ede4e575&quot;&gt;helped analyze that too&lt;&#x2F;a&gt;, and it turned out to be reachable. This was very valuable information to correctly prioritize the bug.&lt;&#x2F;p&gt;
&lt;p&gt;This still isn’t that big a deal in practice because old 32-bit devices and very recent and computationally intensive image formats rarely meet, but it does showcase the capability of the tool quite well.&lt;&#x2F;p&gt;
&lt;p&gt;Here are some more samples of bugs GPT-5.5 has discovered, showing the breadth of the kinds issues it has found:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Amanieu&#x2F;intrusive-rs&#x2F;pull&#x2F;104&quot;&gt;Use-after-free&lt;&#x2F;a&gt;, &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Amanieu&#x2F;intrusive-rs&#x2F;pull&#x2F;105&quot;&gt;data races&lt;&#x2F;a&gt; and &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Amanieu&#x2F;intrusive-rs&#x2F;pull&#x2F;106&quot;&gt;panic safety issues&lt;&#x2F;a&gt; in intrusive-collections&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rkyv&#x2F;rkyv&#x2F;issues&#x2F;670&quot;&gt;Out-of-bounds reads&lt;&#x2F;a&gt; on deserializing crafted archives in rkyv&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;anza-xyz&#x2F;wincode&#x2F;issues&#x2F;306&quot;&gt;Exposing uninitialized memory in serialized data&lt;&#x2F;a&gt; in wincode&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;unicode-org&#x2F;icu4x&#x2F;pull&#x2F;8029&quot;&gt;Incorrect&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;unicode-org&#x2F;icu4x&#x2F;pull&#x2F;8029&quot;&gt;Send&lt;&#x2F;a&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;unicode-org&#x2F;icu4x&#x2F;pull&#x2F;8029&quot;&gt;&#x2F;&lt;&#x2F;a&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;unicode-org&#x2F;icu4x&#x2F;pull&#x2F;8029&quot;&gt;Sync&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;unicode-org&#x2F;icu4x&#x2F;pull&#x2F;8029&quot;&gt;impls&lt;&#x2F;a&gt; in yoke&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;unicode-org&#x2F;icu4x&#x2F;pull&#x2F;7940&quot;&gt;Construction of invalid enum values&lt;&#x2F;a&gt; in zerovec&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;djc&#x2F;hashlink&#x2F;issues&#x2F;42&quot;&gt;Data races for types with interior mutability&lt;&#x2F;a&gt; in hashlink&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;mozilla&#x2F;thin-vec&#x2F;issues&#x2F;86&quot;&gt;Soundness issue in Gecko FFI&lt;&#x2F;a&gt; in thin_vec (crossing languages!)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;jaredforth&#x2F;webp&#x2F;pull&#x2F;51&quot;&gt;Out-of-bounds reads&lt;&#x2F;a&gt; in webp and &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;imazen&#x2F;webpx&#x2F;commit&#x2F;373015705ec84460ddc8722550805520478a2d57&quot;&gt;another one&lt;&#x2F;a&gt; inwebpx (C library wrappers)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;zkat&#x2F;miette&#x2F;issues&#x2F;469&quot;&gt;Turning a&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;zkat&#x2F;miette&#x2F;issues&#x2F;469&quot;&gt;&amp;amp;&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;zkat&#x2F;miette&#x2F;issues&#x2F;469&quot;&gt;into a&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;zkat&#x2F;miette&#x2F;issues&#x2F;469&quot;&gt;&amp;amp;mut&lt;&#x2F;a&gt; in miette&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ruffle-rs&#x2F;nihav-vp6&#x2F;issues&#x2F;2&quot;&gt;Multiple&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ruffle-rs&#x2F;nihav-vp6&#x2F;issues&#x2F;2&quot;&gt;&amp;amp;mut&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ruffle-rs&#x2F;nihav-vp6&#x2F;issues&#x2F;2&quot;&gt;pointing to the same memory&lt;&#x2F;a&gt; in nihav-core&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;andylokandy&#x2F;arraydeque&#x2F;issues&#x2F;34&quot;&gt;Aliasing violation&lt;&#x2F;a&gt; in arraydeque&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rust-av&#x2F;v_frame&#x2F;pull&#x2F;74&quot;&gt;Incorrect alignment handling&lt;&#x2F;a&gt; in v_frame&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;An interesting type confusion issue that’s not yet public&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This demonstrates not just understanding of generic issues like out-of-bounds accesses, but also the ability to reason about Rust-specific concepts such as panic safety, aliasing, and the Send&#x2F;Sync traits that enforce thread safety.&lt;&#x2F;p&gt;
&lt;p&gt;I also got some results out of Claude before I got GPT-5.5 access:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;servo&#x2F;rust-smallvec&#x2F;pull&#x2F;407&quot;&gt;Use-after-free&lt;&#x2F;a&gt; in one function on a zero-capacity SmallVec (Opus 4.6)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;rustsec.org&#x2F;packages&#x2F;imageproc.html&quot;&gt;Multiple out-of-bounds reads&lt;&#x2F;a&gt; in imageproc (Opus 4.7)&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;I haven’t used Claude enough to be able to compare the models. I also cannot compare GPT-5.5 to Mythos, as interesting as that would be, because I deliberately picked different targets to avoid duplicate vulnerability reports putting extra load on maintainers.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;fixes&quot;&gt;Fixes&lt;a class=&quot;zola-anchor&quot; href=&quot;#fixes&quot; aria-label=&quot;Anchor link for: fixes&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;Once the issue is identified and explained, the model usually can also fix it autonomously. I avoid “fix the bugs you found” style prompts and instead discuss possible solutions with the model first, then have the model implement one of them.&lt;&#x2F;p&gt;
&lt;p&gt;Submitting a possible fix alongside the vulnerability report puts less pressure on the maintainers. If you look up my pull requests, the first commit usually adds proof-of-concept tests that cause miri to complain, and the subsequent commit fixes the issues and turns the proof-of-concept snippets into regression tests.&lt;&#x2F;p&gt;
&lt;p&gt;GPT-5.5 has also assisted me in locating the version where the bug was introduced, which is essential for security advisories.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;reflections&quot;&gt;Reflections&lt;a class=&quot;zola-anchor&quot; href=&quot;#reflections&quot; aria-label=&quot;Anchor link for: reflections&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;In my experience Rust code is a lot easier to audit than C code. In C, if I look at a line like data[a + b], I have to trace through the entire codebase and find all the possible values it can be set to just to validate this one line and make sure it doesn&#x27;t have out-of-bounds accesses.&lt;&#x2F;p&gt;
&lt;p&gt;Rust, even unsafe Rust, still relies on local reasoning: if I see unsafe { data.get_unchecked(a + b) }, then the function it&#x27;s in must either validate a and b to make sure their addition is in-bounds, or be itself unsafe to call. Either way, there is a &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;kobzol.github.io&#x2F;rust&#x2F;2026&#x2F;06&#x2F;15&#x2F;how-memory-safety-cves-differ-between-rust-and-c-cpp.html&quot;&gt;clear point where verification must happen&lt;&#x2F;a&gt; - and if it&#x27;s not there, it&#x27;s a bug.&lt;&#x2F;p&gt;
&lt;p&gt;I don’t have to chase the data flow through the entire JPEG XL decoder by hand, and neither does an LLM. This reduces the complexity of auditing the code from combinatorial (all possible combinations of call trees) to linear (each function in isolation).&lt;&#x2F;p&gt;
&lt;p&gt;In that light, it’s not terribly surprising that LLMs are so good at auditing Rust code. And it’s also not terribly surprising that I haven’t found any devastating vulnerabilities after all.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;limitations&quot;&gt;Limitations&lt;a class=&quot;zola-anchor&quot; href=&quot;#limitations&quot; aria-label=&quot;Anchor link for: limitations&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;Just like fuzzers before them, LLMs surface numerous bugs that weren’t economical to discover previously. But at the end of the day, no heuristic tool can prove the absence of vulnerabilities.&lt;&#x2F;p&gt;
&lt;p&gt;For example, a GPT-5.5 alone didn’t discover &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;mariofeter&#x2F;secureloop-findings-public&#x2F;tree&#x2F;master&#x2F;findings&#x2F;rkyv&quot;&gt;several bugs&lt;&#x2F;a&gt; that a combination of a simpler LLM with a fuzzer did.&lt;&#x2F;p&gt;
&lt;p&gt;But we don’t have to rely on heuristics. Rust without unsafe does guarantee the absence of memory safety bugs.&lt;&#x2F;p&gt;
&lt;p&gt;So I’ll &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;how-to-avoid-bounds-checks-in-rust-without-unsafe-f65e618b4c1e&quot;&gt;keep&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;safe-simd-in-rust-even-on-the-inside-c6f1ff381828&quot;&gt;shrinking&lt;&#x2F;a&gt; the unsafe surface where I can, and I&#x27;m glad to have these tools for when I can&#x27;t.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;This article was originally published &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;the-unreasonable-effectiveness-of-llms-for-auditing-rust-code-d4df8bf0afd3&quot;&gt;on my Medium&lt;&#x2F;a&gt;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Safe SIMD in Rust, even on the inside</title>
        <author>
            <name>Sergey &quot;Shnatsel&quot; Davidoff</name>
        </author>
		<published>2026-06-19T00:00:00+00:00</published>
		<updated>2026-06-19T00:00:00+00:00</updated>
		<link href="https://shnatsel.github.io/safe-simd-in-rust-even-on-the-inside/"/>
		<link rel="alternate" href="https://shnatsel.github.io/safe-simd-in-rust-even-on-the-inside/" type="text/html"/>
		<id>https://shnatsel.github.io/safe-simd-in-rust-even-on-the-inside/</id>
        <summary type="html">&lt;p&gt;&lt;em&gt;Rust’s&lt;&#x2F;em&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;the-state-of-simd-in-rust-in-2025-32c263e5f53d&quot;&gt;&lt;em&gt;SIMD abstractions&lt;&#x2F;em&gt;&lt;&#x2F;a&gt; &lt;em&gt;were not as safe as I’d like. Until now.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;</summary>
		<content type="html">&lt;p&gt;&lt;em&gt;Rust’s&lt;&#x2F;em&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;the-state-of-simd-in-rust-in-2025-32c263e5f53d&quot;&gt;&lt;em&gt;SIMD abstractions&lt;&#x2F;em&gt;&lt;&#x2F;a&gt; &lt;em&gt;were not as safe as I’d like. Until now.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;span id=&quot;continue-reading&quot;&gt;&lt;&#x2F;span&gt;
&lt;p&gt;It’s no secret that raw SIMD intrinsics are unpleasant to use.&lt;&#x2F;p&gt;
&lt;p&gt;You want to write &lt;code&gt;a + b&lt;&#x2F;code&gt;, not this monstrosity:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;unsafe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;    #&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;cfg&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;any&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;target_arch &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;x86&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; target_arch &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;x86_64&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; target_feature &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;avx2&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;))]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;    _mm256_add_ps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;    #&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;cfg&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;any&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;target_arch &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;x86&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; target_arch &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;x86_64&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; target_feature &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;sse&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; not&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;target_feature &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;avx2&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)))]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;    _mm_add_ps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;    #&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;cfg&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;all&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;target_arch &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;aarch64&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; target_feature &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;neon&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;))]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;    vaddq_f32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Look at it. It’s hideous. And the whole thing is wrapped in &lt;code&gt;unsafe&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;And that’s a simplified example. It still doesn’t handle:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Other common platforms: AVX-512, 32-bit ARM, WebAssembly&lt;&#x2F;li&gt;
&lt;li&gt;Platforms without SIMD or obscure platforms like RISC-V&lt;&#x2F;li&gt;
&lt;li&gt;Actually loading data like &lt;code&gt;&amp;amp;[f32]&lt;&#x2F;code&gt; into a form that each intrinsic accepts&lt;&#x2F;li&gt;
&lt;li&gt;Selecting the best implementation for the CPU it’s running on&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Luckily, Rust provides &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;the-state-of-simd-in-rust-in-2025-32c263e5f53d&quot;&gt;many SIMD abstractions&lt;&#x2F;a&gt; that handle all of that for you and let you simply write &lt;code&gt;a + b&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;There is just one wrinkle. Inside, they’re still full of &lt;code&gt;unsafe&lt;&#x2F;code&gt;. It wasn’t gone, just hidden. Vast quantities of it lurking just beneath the surface, getting &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;servo&#x2F;pathfinder&#x2F;issues&#x2F;588&quot;&gt;screwed&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;wingertge&#x2F;macerator&#x2F;issues&#x2F;31&quot;&gt;up&lt;&#x2F;a&gt; &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;sarah-quinones&#x2F;pulp&#x2F;issues&#x2F;28&quot;&gt;occasionally&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Or rather, they were. Until now.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;why-do-we-even-need-unsafe&quot;&gt;Why do we even need ‘unsafe’?&lt;a class=&quot;zola-anchor&quot; href=&quot;#why-do-we-even-need-unsafe&quot; aria-label=&quot;Anchor link for: why-do-we-even-need-unsafe&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;For the longest time you couldn’t get around wrapping the call to each intrinsic function such as &lt;code&gt;_mm256_add_ps&lt;&#x2F;code&gt; into &lt;code&gt;unsafe&lt;&#x2F;code&gt; because it is illegal to call one when it’s not available on the CPU you’re running on.&lt;&#x2F;p&gt;
&lt;p&gt;So you &lt;em&gt;had&lt;&#x2F;em&gt; to have some mechanism for tracking which instructions are needed for each intrinsic, and which instructions you have access to, and cross-referencing them to decide if it’s safe to call a given function.&lt;&#x2F;p&gt;
&lt;p&gt;It was either tedious if done by hand or complex if done by a code generator, always error-prone, and required &lt;code&gt;unsafe&lt;&#x2F;code&gt; around every intrinsic.&lt;&#x2F;p&gt;
&lt;p&gt;This changed in Rust 1.87 when the compiler started tracking the required instruction sets itself, so you could write this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;target_feature&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;enable &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;avx2&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add_avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;    _mm256_add_ps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt; &#x2F;&#x2F; this is an avx2 intrinsic&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Look ma, no &lt;code&gt;unsafe&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;…yet.&lt;&#x2F;p&gt;
&lt;p&gt;You still cannot write &lt;code&gt;a + b&lt;&#x2F;code&gt; with this. The best you can do is this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;unsafe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add_avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;) }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This only shifts the &lt;code&gt;unsafe&lt;&#x2F;code&gt; up a layer. You can call intrinsics inside functions annotated with the correct &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; now, but there still has to be &lt;code&gt;unsafe&lt;&#x2F;code&gt; somewhere in the chain.&lt;&#x2F;p&gt;
&lt;p&gt;The other problem is more fundamental. You cannot put &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; on the implementation of &lt;code&gt;+&lt;&#x2F;code&gt; for your type, because &lt;code&gt;+&lt;&#x2F;code&gt; must be available always. So no &lt;code&gt;a + b&lt;&#x2F;code&gt; for us using this mechanism.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;lemma-cpu-feature-tokens&quot;&gt;Lemma: CPU feature tokens&lt;a class=&quot;zola-anchor&quot; href=&quot;#lemma-cpu-feature-tokens&quot; aria-label=&quot;Anchor link for: lemma-cpu-feature-tokens&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;To understand how the final solution works, you first need to understand how CPU feature detection works.&lt;&#x2F;p&gt;
&lt;p&gt;Normally, checking for a CPU feature like AVX2 is done at runtime using &lt;code&gt;is_x86_feature_detected!(&quot;avx2&quot;)&lt;&#x2F;code&gt;. But we definitely don’t want to run this check every single time we add two numbers together — that would completely tank performance. We want to check it &lt;em&gt;once&lt;&#x2F;em&gt;, and then prove to the compiler that it’s safe to use AVX2 instructions from that point on.&lt;&#x2F;p&gt;
&lt;p&gt;Instead we can encode this proof into the type system using an &lt;em&gt;unforgeable token&lt;&#x2F;em&gt;: a zero-sized type with a private inner field. The &lt;em&gt;only&lt;&#x2F;em&gt; way to obtain this token is to call a function that performs the CPU feature check. If the check passes, the function hands you the token:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;pub struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; detect_avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span style=&quot;color: #04A5E5;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #04A5E5;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; is_x86_feature_detected!&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt;&amp;quot;avx2&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;        Some&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(()))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt; else&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;        None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And because it’s a zero-sized type, passing this token around has no runtime overhead. It exists purely as a compile-time proof.&lt;&#x2F;p&gt;
&lt;p&gt;The upshot is that as long as you have an instance of the &lt;code&gt;Avx2&lt;&#x2F;code&gt; struct, you can be sure that AVX2 instructions are available on the system.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-key-insight&quot;&gt;The key insight&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-key-insight&quot; aria-label=&quot;Anchor link for: the-key-insight&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;The compiler doesn’t know it, but this function is safe to call:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;target_feature&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;enable &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;avx2&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add_avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;    _mm256_add_ps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can &lt;em&gt;only&lt;&#x2F;em&gt; call this function if you have an &lt;code&gt;Avx2&lt;&#x2F;code&gt; token, which you can only get if AVX2 instructions are available on the system.&lt;&#x2F;p&gt;
&lt;p&gt;If we can explain to the compiler that this is valid (using &lt;code&gt;unsafe&lt;&#x2F;code&gt;), we can write that &lt;code&gt;unsafe&lt;&#x2F;code&gt; only once and reuse it everywhere.&lt;&#x2F;p&gt;
&lt;p&gt;What we need is a macro which is safe to invoke:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;with_avx2!&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add_avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;        _mm256_add_ps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;but expands into this behind the scenes:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add_avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;    &#x2F;&#x2F; SAFETY: Avx2 is available according to the token,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;    &#x2F;&#x2F; and we verified that the inner function is not an `unsafe fn`&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;    unsafe&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; inner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;) }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;    #&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;target_feature&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;enable &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;avx2&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; inner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;        _mm256_add_ps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now if you use an intrinsic that isn’t in AVX2, &lt;strong&gt;the compiler will reject it!&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We’ve just managed to provide a safe programming interface to SIMD intrinsics without any bespoke tracking of target features!&lt;&#x2F;p&gt;
&lt;p&gt;Even though there is one &lt;code&gt;unsafe&lt;&#x2F;code&gt; block still inside it, it’s encapsulated in a sound API, so you cannot misuse it to cause memory safety bugs. In that sense it is just like &lt;code&gt;println!&lt;&#x2F;code&gt;, safely abstracting unsafe code.&lt;&#x2F;p&gt;
&lt;p&gt;This way you only ever need to review and audit &lt;em&gt;this one macro,&lt;&#x2F;em&gt; not hundreds upon hundreds of bespoke &lt;code&gt;unsafe&lt;&#x2F;code&gt; blocks. And the only things we could &lt;em&gt;possibly&lt;&#x2F;em&gt; screw up in the implementation are:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Mapping the token to the wrong &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Allowing an &lt;code&gt;unsafe fn&lt;&#x2F;code&gt; to be called from a safe context&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;And both failure modes are quite easy to check for.&lt;&#x2F;p&gt;
&lt;p&gt;So now we can call &lt;code&gt;add_avx2(token, a, b)&lt;&#x2F;code&gt; without &lt;code&gt;unsafe&lt;&#x2F;code&gt;, but that still doesn’t get us to &lt;code&gt;a + b&lt;&#x2F;code&gt;. How do we solve &lt;em&gt;that?&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;generics-to-the-rescue&quot;&gt;Generics to the rescue&lt;a class=&quot;zola-anchor&quot; href=&quot;#generics-to-the-rescue&quot; aria-label=&quot;Anchor link for: generics-to-the-rescue&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;We cannot annotate the implementation of &lt;code&gt;a + b&lt;&#x2F;code&gt; with &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; because it must be safe to call from anywhere. And we cannot pass a token into the function because it accepts &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt; but not &lt;code&gt;token&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;But even if we could do that, it would make for a pretty ugly API. We want &lt;code&gt;a + b&lt;&#x2F;code&gt; to always work and automatically use the best SIMD instructions without the user ever fussing with tokens.&lt;&#x2F;p&gt;
&lt;p&gt;We can use generics to solve both problems at once: by defining an &lt;code&gt;f32x8&lt;&#x2F;code&gt; type that’s generic over the available instruction sets, we can implement addition on it that both smuggles a token inside it &lt;em&gt;and&lt;&#x2F;em&gt; creates a separate implementation for each SIMD instruction set!&lt;&#x2F;p&gt;
&lt;p&gt;This is what it looks like:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;pub trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Level&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;Clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Copy&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;pub struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Level&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;pub struct&lt;&#x2F;span&gt;&lt;span&gt; f32x8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #04A5E5;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Level&lt;&#x2F;span&gt;&lt;span style=&quot;color: #04A5E5;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;    &#x2F;&#x2F; For simplicity we&amp;#39;ll back this with an array in this example.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;    &#x2F;&#x2F; In production code we use native SIMD types for the level.&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;f32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #FE640B;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;    &#x2F;&#x2F; The smuggled token!&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; L&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;&#x2F;&#x2F;&#x2F; implementation of `a + b` for Avx2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;&quot;&gt;ops&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; f32x8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #04A5E5;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #04A5E5;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Output&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; rhs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;Output&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;        &#x2F;&#x2F; (type conversions abbreviated)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;        &#x2F;&#x2F; Use the Avx2 token to call our safe wrapper&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add_avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; rhs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt;        Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; store_m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then we can just as easily make it work for any other instruction set, or when SIMD is not available at all:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;derive&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;Clone&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Copy&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;pub struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; NoSimd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Level&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; NoSimd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;&#x2F;&#x2F;&#x2F; implementation of `a + b` when no SIMD is available&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;&quot;&gt;ops&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;Add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; f32x8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #04A5E5;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;NoSimd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #04A5E5;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Output&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; rhs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;Output&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;&quot;&gt;array&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;from_fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; rhs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt;        Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We’ve just solved safety &lt;em&gt;and&lt;&#x2F;em&gt; runtime instruction selection at once!&lt;&#x2F;p&gt;
&lt;p&gt;Add a convenience function that gives you the best &lt;code&gt;Level&lt;&#x2F;code&gt; available on the system, and you get pretty much the perfect API for SIMD!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;the-abi-would-like-a-word&quot;&gt;The ABI would like a word&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-abi-would-like-a-word&quot; aria-label=&quot;Anchor link for: the-abi-would-like-a-word&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;There is, unfortunately, a fundamental problem to writing &lt;code&gt;a + b&lt;&#x2F;code&gt; and having it lower to SIMD instructions: &lt;strong&gt;function call overhead.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Calling a function is not free but pretty cheap — just a handful of CPU instructions. But a &lt;em&gt;handful&lt;&#x2F;em&gt; of instructions is a lot more than &lt;em&gt;one&lt;&#x2F;em&gt; instruction we’ve just used for implementing addition!&lt;&#x2F;p&gt;
&lt;p&gt;So if there’s a function call in the way, addition performance will plummet. And performance is the entire point of using SIMD in the first place!&lt;&#x2F;p&gt;
&lt;p&gt;The compiler is usually pretty good at erasing this overhead via &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;matklad.github.io&#x2F;2021&#x2F;07&#x2F;09&#x2F;inline-in-rust.html&quot;&gt;inlining&lt;&#x2F;a&gt;. It basically copy-pastes the implementation of a function you called into the function calling it, so there’s no more function and no more overhead.&lt;&#x2F;p&gt;
&lt;p&gt;But &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; annotations throw a wrench in the works. The compiler cannot inline a function that has a &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; annotation into one that doesn’t, because the required features are not available in it!&lt;&#x2F;p&gt;
&lt;p&gt;And guess what cannot have a &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; annotation? Yeah.&lt;&#x2F;p&gt;
&lt;p&gt;So how &lt;em&gt;do&lt;&#x2F;em&gt; we make &lt;code&gt;a + b&lt;&#x2F;code&gt; work with SIMD?&lt;&#x2F;p&gt;
&lt;h2 id=&quot;inline-it-inline-it-with-fire&quot;&gt;Inline it, inline it with fire!&lt;a class=&quot;zola-anchor&quot; href=&quot;#inline-it-inline-it-with-fire&quot; aria-label=&quot;Anchor link for: inline-it-inline-it-with-fire&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;We cannot put &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; on the function that implements &lt;code&gt;a + b&lt;&#x2F;code&gt;, but we &lt;em&gt;can&lt;&#x2F;em&gt; put it on the function that calls &lt;code&gt;a + b&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;Then we can use &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;matklad.github.io&#x2F;2021&#x2F;07&#x2F;09&#x2F;inline-in-rust.html&quot;&gt;inlining&lt;&#x2F;a&gt; to get the implementation of &lt;code&gt;a + b&lt;&#x2F;code&gt; copied into the function calling it, and it ends up in a &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; context.&lt;&#x2F;p&gt;
&lt;p&gt;So the call chain looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #4C4F69; background-color: #EFF1F5;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;target_feature&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;enable &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;avx2&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; do_stuff&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;    &#x2F;&#x2F; TODO: some computation&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;    &#x2F;&#x2F; TODO: some more computation&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;&#x2F;&#x2F; which calls into...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;inline&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;always&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt; &#x2F;&#x2F; Function body will be copied into the caller&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; rhs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;Output&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;    &#x2F;&#x2F; Use the Avx2 token to call our safe wrapper&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;    add_avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D20F39;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; rhs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;    &#x2F;&#x2F; return statement abbreviated&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt;&#x2F;&#x2F; which calls into...&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;inline&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;font-style: italic;&quot;&gt; &#x2F;&#x2F; Function body will be copied into the caller if feasible&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;#&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;target_feature&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt;enable &lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #40A02B;&quot;&gt; &amp;quot;avx2&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #8839EF;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt; add_avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt;token&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #DF8E1D;font-style: italic;&quot;&gt; Avx2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #179299;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E64553;&quot;&gt; __m256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #1E66F5;font-style: italic;&quot;&gt;    _mm256_add_ps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;(&lt;&#x2F;span&gt;&lt;span&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #7C7F93;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This works.&lt;&#x2F;p&gt;
&lt;p&gt;You can add these annotations in the right places and abstract over the SIMD levels and you &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;fearless_simd&#x2F;latest&#x2F;fearless_simd&#x2F;trait.Simd.html#tymethod.vectorize&quot;&gt;don’t even need macros&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The problem is that whenever you call &lt;code&gt;a + b&lt;&#x2F;code&gt; on SIMD types, you have to do it from a function with either &lt;code&gt;#[inline(always)]&lt;&#x2F;code&gt; or &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; on it, otherwise the code still compiles but performance plummets.&lt;&#x2F;p&gt;
&lt;p&gt;Wanna see for yourself? Open &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;rust.godbolt.org&#x2F;z&#x2F;xo1fEfqnT&quot;&gt;this example&lt;&#x2F;a&gt;, remove &lt;code&gt;#[target_feature]&lt;&#x2F;code&gt; from it and watch the generated assembly turn into absolute horror show.&lt;&#x2F;p&gt;
&lt;p&gt;I’m not sure what can be done about this. The limitation seems quite fundamental for any approach that implements &lt;code&gt;a + b&lt;&#x2F;code&gt; with SIMD.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;github.com&#x2F;rust-lang&#x2F;rfcs&#x2F;pull&#x2F;3525&quot;&gt;Struct Target Features RFC&lt;&#x2F;a&gt; solves this for &lt;code&gt;add_avx2(token, a, b)&lt;&#x2F;code&gt; and &lt;code&gt;add::&amp;lt;S: Simd&amp;gt;(token, a, b)&lt;&#x2F;code&gt; but I don’t see a path to &lt;code&gt;a + b&lt;&#x2F;code&gt; just yet.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;where-does-that-leave-us&quot;&gt;Where does that leave us?&lt;a class=&quot;zola-anchor&quot; href=&quot;#where-does-that-leave-us&quot; aria-label=&quot;Anchor link for: where-does-that-leave-us&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;Despite the inlining wrinkle inherent to all SIMD code, we’ve managed to provide a remarkably pleasant SIMD abstraction at a staggeringly low, never-before-seen level of unsafe code.&lt;&#x2F;p&gt;
&lt;p&gt;You can find the production version of these ideas in &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;fearless_simd&quot;&gt;&lt;code&gt;fearless_simd&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; v0.5, &lt;strong&gt;available now&lt;&#x2F;strong&gt; in a package registry near you! And &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;linebender&#x2F;fearless_simd&#x2F;blob&#x2F;d2411665f0726cd6c09bd6fd98af4af18e3c1778&#x2F;fearless_simd&#x2F;examples&#x2F;sigmoid.rs&quot;&gt;here’s a small example&lt;&#x2F;a&gt; to see how it all fits together in production.&lt;&#x2F;p&gt;
&lt;p&gt;The macro used to implement it is also exposed publicly, so you can easily &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;linebender&#x2F;fearless_simd&#x2F;blob&#x2F;e76b820154dc09f41bca8302670424d8ca079219&#x2F;fearless_simd&#x2F;examples&#x2F;srgb.rs&quot;&gt;mix and match&lt;&#x2F;a&gt; high-level operations like &lt;code&gt;a + b&lt;&#x2F;code&gt; and platform-specific intrinsics to take full advantage of the hardware.&lt;&#x2F;p&gt;
&lt;p&gt;There is more than one &lt;code&gt;unsafe&lt;&#x2F;code&gt; block in &lt;code&gt;fearless_simd&lt;&#x2F;code&gt; because it also provides the functionality of &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;safe_unaligned_simd&quot;&gt;&lt;code&gt;safe_unaligned_simd&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; crate, but that too is done at a &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;okaneco&#x2F;safe_unaligned_simd&#x2F;issues&#x2F;51&quot;&gt;significantly lower&lt;&#x2F;a&gt; amount of unsafe code than the original.&lt;&#x2F;p&gt;
&lt;p&gt;For me the barrier to using high-level SIMD abstractions was always the sheer amount of &lt;code&gt;unsafe&lt;&#x2F;code&gt; they brought. It was scary and hard to justify.&lt;&#x2F;p&gt;
&lt;p&gt;But now SIMD in Rust can be truly fearless.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;a class=&quot;zola-anchor&quot; href=&quot;#acknowledgements&quot; aria-label=&quot;Anchor link for: acknowledgements&quot;&gt;&lt;i class=&quot;fas fa-link&quot;&gt;&lt;&#x2F;i&gt;&lt;&#x2F;a&gt; 
&lt;&#x2F;h2&gt;
&lt;p&gt;I am shocked that I am the first to put this into production (as far as I can tell), because I’m certainly not the first person to think of this.&lt;&#x2F;p&gt;
&lt;p&gt;CPU feature tokens are an old and common idea. The &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;pulp&quot;&gt;&lt;code&gt;pulp&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; crate has been using them for years, but they relied on handwritten unsafe wrappers around intrinsics, and &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;sarah-quinones&#x2F;pulp&#x2F;issues&#x2F;28&quot;&gt;occasionally got them wrong&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Generating multiple implementations using generics is also an old idea. It is part of the &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;linebender.org&#x2F;blog&#x2F;towards-fearless-simd&#x2F;&quot;&gt;original &lt;code&gt;fearless_simd&lt;&#x2F;code&gt; concept&lt;&#x2F;a&gt; from 8 years ago. The &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;simdeez&quot;&gt;&lt;code&gt;simdeez&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; crate, which predates it, also &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;simdeez&#x2F;1.0.8&#x2F;simdeez&#x2F;&quot;&gt;seems to use something similar&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The key insight of combining tokens with a single safe wrapper that delegates to &lt;code&gt;rustc&lt;&#x2F;code&gt; is not unique to me either. Just in the context of &lt;code&gt;fearless_simd&lt;&#x2F;code&gt; crate, Raph Levien has &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;linebender&#x2F;fearless_simd&#x2F;commit&#x2F;81b6ab4c8dd6f25064f539c49818c45c2f686815&quot;&gt;experimented with it&lt;&#x2F;a&gt;, and &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;DJMcNab&quot;&gt;Daniel McNab&lt;&#x2F;a&gt; created a &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;linebender&#x2F;fearless_simd&#x2F;pull&#x2F;108&quot;&gt;more elaborate implementation&lt;&#x2F;a&gt; than mine months ago.&lt;&#x2F;p&gt;
&lt;p&gt;Daniel’s approach allows fine-grained tracking of every individual CPU feature, as opposed to a handful of fixed CPU feature levels that &lt;code&gt;fearless_simd&lt;&#x2F;code&gt; uses. It is more expressive, but came at the cost of complexity, and his approach never got merged because no other maintainer stepped up to review it. I still hope it will be published as a standalone crate someday.&lt;&#x2F;p&gt;
&lt;p&gt;Thanks to Daniel and to &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;LaurenzV&quot;&gt;Laurenz Stampfl&lt;&#x2F;a&gt; for reviewing all my PRs to &lt;code&gt;fearless_simd&lt;&#x2F;code&gt;, they were big and the quick reviews are really appreciated!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;This article was originally published &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;safe-simd-in-rust-even-on-the-inside-c6f1ff381828&quot;&gt;on my Medium&lt;&#x2F;a&gt;.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>My old articles are on Medium</title>
        <author>
            <name>Sergey &quot;Shnatsel&quot; Davidoff</name>
        </author>
		<published>2026-06-18T00:00:00+00:00</published>
		<updated>2026-06-18T00:00:00+00:00</updated>
		<link href="https://shnatsel.github.io/my-old-articles-are-on-medium/"/>
		<link rel="alternate" href="https://shnatsel.github.io/my-old-articles-are-on-medium/" type="text/html"/>
		<id>https://shnatsel.github.io/my-old-articles-are-on-medium/</id>
		<content type="html">&lt;p&gt;Find them at &lt;a rel=&quot;nofollow external&quot; href=&quot;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;&quot;&gt;https:&#x2F;&#x2F;shnatsel.medium.com&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
</feed>
