{"id":783,"date":"2014-02-28T14:30:00","date_gmt":"2014-02-28T14:30:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2014\/02\/28\/avx2-support-in-visual-studio-c-compiler\/"},"modified":"2021-10-04T17:36:07","modified_gmt":"2021-10-04T17:36:07","slug":"avx2-support-in-visual-studio-c-compiler","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/avx2-support-in-visual-studio-c-compiler\/","title":{"rendered":"AVX2 Support in Visual Studio C++ Compiler"},"content":{"rendered":"<p>AVX2 is yet another extension to the venerable x86 line of processors,&nbsp;doubling the width of its SIMD vector registers to 256 bits, and adding <a href=\"http:\/\/software.intel.com\/sites\/products\/documentation\/doclib\/iss\/2013\/compiler\/cpp-lin\/GUID-9E84F9C5-1711-4F59-8742-8F9DF283A472.htm\">dozens of new instructions<\/a>.&nbsp; AVX2 shipped with Intel&#8217;s latest processor micro-architecture, codenamed &#8220;<a href=\"http:\/\/www.realworldtech.com\/haswell-cpu\/\">Haswell<\/a>&#8220;.&nbsp; (Its&nbsp;official name is &#8220;4th generation Intel&reg; Core&trade; processor family&#8221;).&nbsp; As well as&nbsp;AVX2, Haswell supports other features to&nbsp;help make&nbsp;your code run faster:&nbsp; FMA (Fused Multiply Add) and&nbsp;BMI (Bit Manipulation Instructions), in particular.&nbsp; Haswell chips appear in many of the latest PCs,&nbsp;laptops and tablets&nbsp;(including our own&nbsp;Surface Pro 2).<\/p>\n<p>This extra silicon opens a new corner of the playing field&nbsp;for&nbsp;compiler-writers &#8211; to take your C++ source&nbsp;and generate these new instructions,&nbsp;making your code run faster than before.&nbsp; And so we are releasing our initial support for AVX2 with the CTP2 of Visual Studio Update.<\/p>\n<p>How do you tell the VC++ compiler to generate&nbsp;AVX2 instruction?&nbsp; From the command line, include the \/arch:AVX2 switch.&nbsp; If you work within&nbsp;Visual Studio, the screenshot below shows how to set this option.&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/10\/8400.AVX2screenShot.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/10\/8400.AVX2screenShot.png\" alt=\"Image 8400 AVX2screenShot\" width=\"601\" height=\"427\" class=\"alignnone size-full wp-image-29140\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/10\/8400.AVX2screenShot.png 601w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/10\/8400.AVX2screenShot-300x213.png 300w\" sizes=\"(max-width: 601px) 100vw, 601px\" \/><\/a><\/p>\n<p>Yes, we will&nbsp;add an option to enable AVX2&nbsp;in the drop-down menu at: <span style=\"color: #3366ff\">Project Property Pages | Configuration Properties | C\/C++ | Code Generation | Enable Enhanced Instruction Set<\/span>.&nbsp; Likewise, we will add brief help to the command line usage for <span style=\"color: #3366ff\">cl&nbsp;\/? <\/span><\/p>\n<p>Further points worth calling out on AVX2 support:<\/p>\n<p>The compiler will generate code that includes AVX2 and FMA instructions.&nbsp; The resulting binaries will only run on PCs that support these instructions.&nbsp; If you run the binaries on an older PC, the program will crash, and&nbsp;display a popup like this:<\/p>\n<p style=\"padding-left: 30px\"><a href=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/10\/7713.AVX2onNonHaswell.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/10\/7713.AVX2onNonHaswell.png\" alt=\"Image 7713 AVX2onNonHaswell\" width=\"364\" height=\"184\" class=\"alignnone size-full wp-image-29138\" srcset=\"https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/10\/7713.AVX2onNonHaswell.png 364w, https:\/\/devblogs.microsoft.com\/cppblog\/wp-content\/uploads\/sites\/9\/2021\/10\/7713.AVX2onNonHaswell-300x152.png 300w\" sizes=\"(max-width: 364px) 100vw, 364px\" \/><\/a><\/p>\n<p>If you specify <span style=\"color: #3366ff\">\/arch:AVX2<\/span>, then it also enables <span style=\"color: #3366ff\">\/arch:AVX<\/span> &#8211; we try to keep those <span style=\"color: #3366ff\">\/arch<\/span> switches &#8216;monotonic&#8217;:&nbsp; the capabilities of each switch in the sequence <span style=\"color: #3366ff\">{IA32, SSE, SSE2, AVX, AVX2}<\/span> subsumes its predecessor (not sure I&#8217;ve explained this well &#8211; is it clear?)<\/p>\n<p>The AVX2 support in this CTP is just a start.&nbsp; We have more work to do!&nbsp; This really should not come as a surprise &#8211; in a sense,&nbsp;compiler optimizations are never &#8220;finished&#8221;.&nbsp; Compiler engineers&nbsp;have been improving optimizations since they were started&nbsp;in the mid-50s by John Backus in the&nbsp;FORTRAN I project.&nbsp; But it&#8217;s worth emphasizing, if only to fend off a little flurry of advice saying we could improve&nbsp;parts of our AVX2 codegen \ud83d\ude42<\/p>\n<p>If you want to write code that checks&nbsp;for whether the machine you are running is &#8220;Haswell-capable&#8221;, then you need to check 5&nbsp;configuration bits, via the CPUID instruction. <a href=\"http:\/\/software.intel.com\/en-us\/node\/405250?language=es&amp;wapkw=avx2+cpuid\">This Intel page <\/a>explains the details.&nbsp;<\/p>\n<p>As always, we are interested in your feedback. Post your comments below. Thanks!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>AVX2 is yet another extension to the venerable x86 line of processors,&nbsp;doubling the width of its SIMD vector registers to 256 bits, and adding dozens of new instructions.&nbsp; AVX2 shipped with Intel&#8217;s latest processor micro-architecture, codenamed &#8220;Haswell&#8220;.&nbsp; (Its&nbsp;official name is &#8220;4th generation Intel&reg; Core&trade; processor family&#8221;).&nbsp; As well as&nbsp;AVX2, Haswell supports other features to&nbsp;help make&nbsp;your [&hellip;]<\/p>\n","protected":false},"author":271,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[65],"class_list":["post-783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-compiler"],"acf":[],"blog_post_summary":"<p>AVX2 is yet another extension to the venerable x86 line of processors,&nbsp;doubling the width of its SIMD vector registers to 256 bits, and adding dozens of new instructions.&nbsp; AVX2 shipped with Intel&#8217;s latest processor micro-architecture, codenamed &#8220;Haswell&#8220;.&nbsp; (Its&nbsp;official name is &#8220;4th generation Intel&reg; Core&trade; processor family&#8221;).&nbsp; As well as&nbsp;AVX2, Haswell supports other features to&nbsp;help make&nbsp;your [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/271"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}