{"id":4469,"date":"2018-08-27T20:59:58","date_gmt":"2018-08-28T01:59:58","guid":{"rendered":"http:\/\/neosmart.net\/blog\/?p=4469"},"modified":"2024-07-02T11:07:26","modified_gmt":"2024-07-02T16:07:26","slug":"could-not-find-avx-in-x86","status":"publish","type":"post","link":"https:\/\/neosmart.net\/blog\/could-not-find-avx-in-x86\/","title":{"rendered":"Rust Error: Could not find `avx` in `x86`"},"content":{"rendered":"<p>Let&#8217;s start this article with an error message right off the bat:<sup id=\"rf1-4469\"><a href=\"#fn1-4469\" title=\"I&rsquo;m primarily blogging this as I&rsquo;ve run into &ndash; and worked around &ndash; this error several times before running into it again and not being able to remember how I resolved this problem the last time around.\" rel=\"footnote\">1<\/a><\/sup><\/p>\n<pre>~&gt; cargo install ripgrep --features 'avx-accel simd-accel'\n    <span class=\"green\">Updating<\/span> registry `https:\/\/github.com\/rust-lang\/crates.io-index`\n  <span class=\"green\">Installing<\/span> ripgrep v0.9.0\n\u2026\n<span class=\"red\">error[E0432]<\/span>: unresolved import `simd::x86::avx`\n  --&gt; \/home\/mqudsi\/.cargo\/registry\/src\/github.com-1ecc6299db9ec823\/bytecount-0.3.2\/src\/lib.rs:49:16\n   |\n49 | use simd::x86::avx::{LowHigh128, u8x32};\n   |                <span class=\"red\">^^^ Could not find `avx` in `x86`<\/span>\n<span class=\"red\">error<\/span>: aborting due to previous error\nFor more information about this error, try `rustc --explain E0432`.\n<span class=\"red\">error<\/span>: Could not compile `bytecount`.\n<\/pre>\n<p><!--more--><\/p>\n<p>Does the error look familiar? It&#8217;s a common one. The rust compiler does not avail itself of processor-specific features and capabilities needed to perform advanced optimizations or to use vectorized instructions like the SSE{,2,3,4{,.1,.2}}<sup id=\"rf2-4469\"><a href=\"#fn2-4469\" title=\"fish&rsquo;s nested parameter expansion ftw!\" rel=\"footnote\">2<\/a><\/sup> or AVX intrinsics. But some cargo crates have features that take advantage of these features &#8212; and depending on your platform and architecture, compilation will fail with an error like the one above.<\/p>\n<p>While <code>bytecount<\/code>\u00a0is a common culprit, it&#8217;s by no means the only one, and you can encounter this error (or one like it) with pretty much any crate trying to use lower-level CPU instructions. Fortunately in this case if we were to try compiling\/installing <code>ripgrep<\/code>\u00a0normally without the <code>--features ...<\/code>\u00a0bit we would not encounter this error at all, but what&#8217;s the use of using the fastest code search utility on the planet on an advanced CPU you paid good money for if you&#8217;re not planning on milking every last drop of performance you can get out of it?<\/p>\n<p>The solution is to explicitly instruct the <code>rustc<\/code>\u00a0compiler to use all the capabilities of your CPU, including those not necessarily found on other machines in your platform (which will cause the binaries you build to no longer be guaranteed to run on another ABI-compatible machine).<sup id=\"rf3-4469\"><a href=\"#fn3-4469\" title=\"There is a proposal to add a crate-level profiling option to enable this automatically over at rust-lang\/cargo#2535.\" rel=\"footnote\">3<\/a><\/sup> If you set the <code>RUSTC_FLAGS<\/code>\u00a0environment variable prior to invoking <code>cargo install ...<\/code>\u00a0to instruct the compiler to use the native capabilities of the CPU, that&#8217;s exactly what we get:<\/p>\n<pre>~&gt; <strong>env RUSTFLAGS=\"-C target-cpu=native\"<\/strong> \\\n       cargo install ripgrep --features 'avx-accel simd-accel'\n    <span class=\"green\">Updating<\/span> registry `https:\/\/github.com\/rust-lang\/crates.io-index`\n  <span class=\"green\">Installing<\/span> ripgrep v0.9.0\n    <span class=\"green\">Finished<\/span> release [optimized + debuginfo] target(s) in 58.88s\n   <span class=\"green\">Replacing<\/span> \/home\/mqudsi\/.cargo\/bin\/rg\n<\/pre>\n<p>Note that you may see a message like the following:<\/p>\n<blockquote><p>&#8216;native&#8217; is not a recognized processor for this target (ignoring processor)<\/p><\/blockquote>\n<p>This is perfectly normal &#8212; some crates try to introspect the current value of <code>target-cpu<\/code>\u00a0to take differing build actions, and are unable to identify <code>native<\/code>\u00a0as a valid CPU target (which makes sense, since it&#8217;s really a\u00a0<em>meta<\/em> target and not a real one).<\/p>\n<hr class=\"footnotes\"><ol class=\"footnotes\"><li id=\"fn1-4469\"><p>I&#8217;m primarily blogging this as I&#8217;ve run into \u2013 and worked around \u2013 this error several times before running into it again and not being able to remember how I resolved this problem the last time around.&nbsp;<a href=\"#rf1-4469\" class=\"backlink\" title=\"Jump back to footnote 1 in the text.\">&#8617;<\/a><\/p><\/li><li id=\"fn2-4469\"><p>fish&#8217;s nested parameter expansion ftw!&nbsp;<a href=\"#rf2-4469\" class=\"backlink\" title=\"Jump back to footnote 2 in the text.\">&#8617;<\/a><\/p><\/li><li id=\"fn3-4469\"><p>There is a proposal to add a crate-level profiling option to enable this automatically over at <a href=\"https:\/\/github.com\/rust-lang\/cargo\/issues\/2535\" rel=\"nofollow\">rust-lang\/cargo#2535<\/a>.&nbsp;<a href=\"#rf3-4469\" class=\"backlink\" title=\"Jump back to footnote 3 in the text.\">&#8617;<\/a><\/p><\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s start this article with an error message right off the bat:1 ~&gt; cargo install ripgrep &#8211;features &#8216;avx-accel simd-accel&#8217; Updating registry `https:\/\/github.com\/rust-lang\/crates.io-index` Installing ripgrep v0.9.0 \u2026 error[E0432]: unresolved import `simd::x86::avx` &#8211;&gt; \/home\/mqudsi\/.cargo\/registry\/src\/github.com-1ecc6299db9ec823\/bytecount-0.3.2\/src\/lib.rs:49:16 | 49 | use simd::x86::avx::{LowHigh128, u8x32}; | ^^^ &hellip; <a href=\"https:\/\/neosmart.net\/blog\/could-not-find-avx-in-x86\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":505,"featured_media":4454,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[986,988,987,936],"class_list":["post-4469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","tag-avx","tag-cargo","tag-ripgrep","tag-rust"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/neosmart.net\/blog\/wp-content\/uploads\/rust-logo.png","jetpack_shortlink":"https:\/\/wp.me\/p4xDa-1a5","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/users\/505"}],"replies":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/comments?post=4469"}],"version-history":[{"count":7,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4469\/revisions"}],"predecessor-version":[{"id":5193,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/posts\/4469\/revisions\/5193"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media\/4454"}],"wp:attachment":[{"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/media?parent=4469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/categories?post=4469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/neosmart.net\/blog\/wp-json\/wp\/v2\/tags?post=4469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}