{"id":19921,"date":"2017-04-19T10:48:48","date_gmt":"2017-04-19T17:48:48","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=19921"},"modified":"2021-06-11T09:39:34","modified_gmt":"2021-06-11T16:39:34","slug":"labview-compiler-under-the-hood","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/labview-compiler-under-the-hood\/","title":{"rendered":"LabVIEW Compiler: Under the Hood"},"content":{"rendered":"<p>Compiler design is a complex topic, and is considered specialized knowledge even among professional software engineers. NI\u00a0<a href=\"https:\/\/digilent.com\/shop\/labview-home-bundle\/\">LabVIEW<\/a>\u00a0software is a multiparadigmatic graphical programming environment that incorporates a wide variety of concepts including data flow, object orientation, and event-driven programming. LabVIEW is also exceedingly cross-platform, targeting multiple OSs, chipsets, embedded devices, and\u00a0field-programmable gate arrays\u00a0(<a href=\"https:\/\/digilent.com\/shop\/fpga-programmable-logic\/\">FPGAs<\/a>).<\/p>\n<p>National Instruments <a href=\"http:\/\/www.ni.com\/newsletter\/51177\/en\/\">website<\/a> has a wealth of information about the LabVIEW compiler.\u00a0In this blog post, I have done my best to provide a simplified explanation.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone \" src=\"https:\/\/acsesheffield.files.wordpress.com\/2017\/02\/lv.png\" width=\"702\" height=\"145\" \/><\/p>\n<h3>1. LabVIEW Compile Process<\/h3>\n<p>First in the compilation of a VI is type propagation, which is responsible for resolving implied types for terminals that can adapt to type and detect syntax errors. After type propagation, the VI is converted from the editor model into the data flow intermediate representation <strong>(DFIR)<\/strong> graph used by the compiler.<\/p>\n<p>The compiler executes several transforms, such as dead code elimination, on the DFIR graph to decompose, optimize, and prepare it for code generation. The DFIR graph is then translated into a Low-Level Virtual Machine <strong>(LLVM)<\/strong> intermediate representation <strong>(IR)<\/strong>, and a series of passes is run over the IR to further optimize and lower it \u2013 eventually \u2013 to machine code.<\/p>\n<h3>2. DFIR Provides a High-Level Intermediate Representation<\/h3>\n<p>DFIR is a hierarchical, graph-based IR of block diagram code. Similar to G-code, DFIR is composed of various nodes with terminals that can be connected to other terminals. Some nodes, such as loops, contain diagrams, which may in turn contain other nodes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium\" src=\"http:\/\/www.ni.com\/cms\/images\/devzone\/pub\/Page_26_Fig_1_Q3_2010_INL.png\" width=\"750\" height=\"209\" \/>The picture above shows an initial DFIR of a simple VI. As the compile progresses, DFIR nodes may be moved, split apart, or injected, but the compiler will still preserve characteristics, such as parallelism, inherent in the developer\u2019s G code.<\/p>\n<h3>3. DFIR Decompositions and Optimizations<\/h3>\n<p>Once in DFIR, the VI runs through a series of decomposition transforms that reduce or normalize the DFIR graph. After the DFIR graph is thoroughly decomposed, the DFIR optimization passes begin. There are more than 30 decompositions and optimizations that can improve the performance of LabVIEW code.<\/p>\n<h3>4. DFIR Back-End Transforms<\/h3>\n<p>After the DFIR graph is decomposed and optimized, the back-end transforms execute. These transforms evaluate and annotate the DFIR graph in preparation for ultimately lowering the DFIR graph to a LLVM IR.<\/p>\n<p>The clumper is responsible for grouping nodes into clumps, which can run in parallel. The inplacer identifies when allocations can be reused and when a copy must be made. After the inplacer runs, the allocator reserves the memory that the VI needs to execute.<\/p>\n<p>Finally, the code generator is responsible for converting the DFIR graph into executable machine instructions for the target processor.<\/p>\n<h3>5. LLVM Provides a Low-Level Intermediate Representation<\/h3>\n<p>LLVM is a versatile, high-performance open source compiler framework. The LLVM is now widely used both in academia and industry due to its flexible, clean API and nonrestrictive licensing.<\/p>\n<p>LabVIEW uses LLVM to perform instruction combining, jump threading, scalar replacement of aggregates, conditional propagation, tail call elimination, loop invariant code motion, dead code elimination, and loop unrolling.<\/p>\n<h3>6. DFIR and LLVM Work in Tandem<\/h3>\n<p>While DFIR is a high-level IR that preserves parallelism and LLVM is a low-level IR with knowledge of target machine characteristics, the pair works in tandem to optimize the <a href=\"https:\/\/digilent.com\/shop\/labview-home-bundle\/\">LabVIEW<\/a> code developers write for the processor architecture on which the code executes.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-20046 aligncenter\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2017\/04\/labview-logo-2-600x519.jpg\" alt=\"\" width=\"447\" height=\"387\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2017\/04\/labview-logo-2-600x519.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2017\/04\/labview-logo-2-768x664.jpg 768w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2017\/04\/labview-logo-2-1024x885.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2017\/04\/labview-logo-2-110x96.jpg 110w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2017\/04\/labview-logo-2-800x691.jpg 800w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2017\/04\/labview-logo-2.jpg 1826w\" sizes=\"auto, (max-width: 447px) 100vw, 447px\" \/><\/p>\n<p>Thank you for reading my blog post, and for a more technical explanation about how LabVIEW works follow this\u00a0<a href=\"http:\/\/www.ni.com\/newsletter\/51473\/en\/\">link<\/a>. If you are interested in trying out LabVIEW for yourself, you can purchase a copy of\u00a0<a href=\"https:\/\/digilent.com\/shop\/labview-home-bundle\/\">LabVIEW 2014 Home Edition<\/a>\u00a0which includes everything you will need to run\u00a0<a href=\"https:\/\/www.labviewmakerhub.com\/doku.php?id=learn:tutorials:libraries:linx:3-0:beaglebone-black-setup\">LINX 3.0<\/a>.\u00a0Please comment below with any questions or comments you may have.<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-19921 jlk' data-task='like' data-post_id='19921' data-nonce='911161e922' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-19921 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-19921 jlk' data-task='unlike' data-post_id='19921' data-nonce='911161e922' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-19921 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-19921 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>Ever wonder how LabVIEW works under the hood? Austin reveals some of the process.<\/p>\n","protected":false},"author":42,"featured_media":20046,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4323,1563],"tags":[],"ppma_author":[4493],"class_list":["post-19921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2017\/04\/labview-logo-2.jpg","authors":[{"term_id":4493,"user_id":42,"is_guest":0,"slug":"astanton","display_name":"Austin Stanton","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/056fddaefc60a1f99c226ea5757a8c41?s=96&d=mm&r=g","author_category":"","user_url":"","last_name":"Stanton","last_name_2":"","first_name":"Austin","first_name_2":"","job_title":"","description":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/19921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=19921"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/19921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/20046"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=19921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=19921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=19921"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=19921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}