{"id":1005,"date":"2018-04-08T18:18:24","date_gmt":"2018-04-08T18:18:24","guid":{"rendered":"http:\/\/cppdepend.com\/blog\/?p=1005"},"modified":"2023-05-31T15:22:50","modified_gmt":"2023-05-31T15:22:50","slug":"chris-lattner-revolutionizing-cpp-world","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/chris-lattner-revolutionizing-cpp-world\/","title":{"rendered":"Chris Lattner: Revolutionizing the C++ World"},"content":{"rendered":"<p>Maybe almost all C++ developers know the LLVM infrastructure and the Clang compiler. But how many know that Chris Lattner is their creator when he\u00a0was only 25 years old. How it&#8217;s possible? I remember when I was 25 years I spend my time to understand the C++ basics \ud83d\ude42<\/p>\n<p><strong>The story begins with a thesis<\/strong><\/p>\n<p>In late 2000, Lattner joined the\u00a0University of Illinois at Urbana-Champaign\u00a0as a research assistant and M.Sc. student. While working with\u00a0Vikram Adve, he designed and began implementing LLVM, <strong>an innovative infrastructure for\u00a0optimizing compilers<\/strong>, which was the subject of his 2002 M.Sc. thesis. He completed his Ph.D. in 2005, researching new techniques for optimizing pointer-intensive programs and adding them to LLVM.<!--more--><\/p>\n<p>Here&#8217;s from\u00a0his thesis abstract the motivation behind the LLVM design:<\/p>\n<p><em>Modern programming languages and software engineering principles are causing increasing problems for compiler systems. Traditional approaches, which use a simple compile-link-execute model, are unable to provide adequate application performance under the demands of the new conditions. Traditional approaches to interprocedural and profile-driven compilation can provide the application performance needed, but require infeasible amounts of compilation time to build the application.<\/em><\/p>\n<p><em>This thesis presents LLVM, a design and implementation of a compiler infrastructure which supports a unique\u00a0multi-stage\u00a0optimization system. This system is designed to support extensive interprocedural and profile-driven <\/em>optimizations,<em> while being efficient enough for use in commercial compiler systems.<\/em><\/p>\n<p><em>The LLVM virtual instruction set is the glue that holds the system together. It is a low-level representation, but with\u00a0high-level type information. This provides the benefits of a low-level representation (compact representation, <\/em>wide<em> variety of available transformations, etc.) as well as providing high-level information to support aggressive interprocedural optimizations at <\/em>link<em>&#8211; and post-link time. In particular, this system is designed to support optimization in the field, both at run-time and during <\/em>otherwise<em> unused idle time on the machine.<\/em><\/p>\n<p>To resume the idea behind LLVM is to use the LLVM Intermediate Representation (IR), it\u2019s like the bytecode for java or IL for .net.<\/p>\n<p>LLVM IR is designed to host mid-level analyses and transformations that you find in the optimizer section of a compiler. It was designed with many specific goals in mind, including supporting lightweight runtime optimizations, cross-function\/interprocedural optimizations, whole program analysis, and aggressive restructuring transformations, etc. The most important aspect of it, though, is that it is itself defined as a first class language with well-defined semantics.<\/p>\n<p>Let&#8217;s consider a relatively straightforward function that takes three integer parameters and returns an arithmetic combination of them.<\/p>\n<div class=\"doc_code\">\n<pre>int mul_add(int x, int y, int z) {\r\n  return x * y + z;\r\n}\r\n<\/pre>\n<\/div>\n<p>The LLVM IR for this function will look like:<\/p>\n<div class=\"doc_code\">\n<pre>define i32 @mul_add(i32 %x, i32 %y, i32 %z) {\r\nentry:\r\n  %tmp = mul i32 %x, %y\r\n  %tmp2 = add i32 %tmp, %z\r\n  ret i32 %tmp2\r\n}\r\n<\/pre>\n<\/div>\n<p>Back to the goal of this thesis which focuses on the optimization. To explain this compiler phase I can\u2019t say better than Chris Lattner the father of LLVM in this\u00a0<a href=\"http:\/\/www.drdobbs.com\/architecture-and-design\/the-design-of-llvm\/240001128?pgno=1\">post<\/a>:<\/p>\n<p><em>\u201cTo give some intuition for how optimizations work, it is useful to walk through some examples. There are lots of different kinds of compiler optimizations, so it is hard to provide a recipe for how to solve an arbitrary problem. That said, most optimizations follow a simple three-part structure:<\/em><\/p>\n<ul>\n<li>Look for a pattern to be transformed.<\/li>\n<li>Verify that the transformation is safe\/correct for the matched instance.<\/li>\n<li>Do the transformation, updating the code.<\/li>\n<\/ul>\n<p><em>The optimizer reads LLVM IR in, chews on it a bit, then emits LLVM IR, which hopefully will execute faster. In LLVM (as in many other compilers) the optimizer is organized as a pipeline of distinct optimization passes each of which is run on the input and has a chance to do something. Common examples of passes are the inliner (which substitutes the body of a function into call sites), expression reassociation, loop invariant code motion, etc. Depending on the optimization level, different passes are run: for example at -O0 (no optimization) the Clang compiler runs no passes, at -O3 it runs a series of 67 passes in its optimizer (as of LLVM 2.8).<br \/>\n<\/em><\/p>\n<p><strong>LLVM and Clang as infrastructure to build tools:<\/strong><\/p>\n<p>A major design concept for Clang is its use of a library-based architecture. In this design, various parts of the front-end can be cleanly divided into separate libraries which can then be mixed up for different needs and uses. In addition, the library-based approach encourages good interfaces and makes it easier for new developers to get involved (because they only need to understand small pieces of the big picture).<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.javadepend.com\/Blog\/wp-content\/uploads\/llvm1.png\" alt=\"llvm1\" \/><\/p>\n<p>The design of LLVM\/Clang makes it an infrastructure to build tools. Thanks to its\u00a0library-based architecture which makes the reuse and integration of\u00a0new features more flexible\u00a0and easier to integrate into other projects.<\/p>\n<p>Many tools are based on LLVM and Clang, we can enumerate clang-tidy, clang-query,\u00a0CppDepend and more.<\/p>\n<p><strong>Clang always\u00a0 up to date with the new standards<\/strong><\/p>\n<div id=\"content\">\n<p>Clang is always compliant with any new standard. furthermore, it always provides an experimental support for the coming new features. The current version has\u00a0experimental\u00a0support for some proposed features of the C++ standard following C++17, provisionally named C++2a. Note that support for these features may change or be removed without notice, as the draft C++2a standard evolves.<\/p>\n<p>You can use Clang in C++2a mode with the\u00a0<code>-std=c++2a<\/code>\u00a0option.<\/p>\n<table border=\"1\" width=\"689\" cellspacing=\"0\">\n<tbody>\n<tr>\n<th>Language Feature<\/th>\n<th>C++2a Proposal<\/th>\n<th>Available in Clang?<\/th>\n<\/tr>\n<tr>\n<td>Default member initializers for bit-fields<\/td>\n<td><a href=\"http:\/\/wg21.link\/p0683r1\">P0683R1<\/a><\/td>\n<td class=\"svn\" align=\"center\">Clang 6<\/td>\n<\/tr>\n<tr>\n<td><tt>const&amp;<\/tt>-qualified pointers to members<\/td>\n<td><a href=\"http:\/\/wg21.link\/p0704r1\">P0704R1<\/a><\/td>\n<td class=\"svn\" align=\"center\">Clang 6<\/td>\n<\/tr>\n<tr>\n<td>Allow\u00a0<i>lambda-capture<\/i>\u00a0<tt>[=, this]<\/tt><\/td>\n<td><a href=\"http:\/\/wg21.link\/p0409r2\">P0409R2<\/a><\/td>\n<td class=\"svn\" align=\"center\">Clang 6<\/td>\n<\/tr>\n<tr>\n<td><tt>__VA_OPT__<\/tt>\u00a0for preprocessor comma elision<\/td>\n<td><a href=\"http:\/\/wg21.link\/p0306r4\">P0306R4<\/a><\/td>\n<td class=\"svn\" align=\"center\">Clang 6<\/td>\n<\/tr>\n<tr>\n<td>Designated initializers<\/td>\n<td><a href=\"http:\/\/wg21.link\/p0329r4\">P0329R4<\/a><\/td>\n<td class=\"partial\" align=\"center\">Partial (extension)<\/td>\n<\/tr>\n<tr>\n<td>Initializer list constructors in class template argument deduction<\/td>\n<td><a href=\"http:\/\/wg21.link\/p0702r1\">P0702R1<\/a><\/td>\n<td class=\"svn\" align=\"center\">Clang 6<\/td>\n<\/tr>\n<tr>\n<td>Access checking on specializations<\/td>\n<td><a href=\"http:\/\/wg21.link\/p0692r1\">P0692R1<\/a><\/td>\n<td class=\"partial\" align=\"center\">Partial<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Clang is now adopted by many big corporate\u00a0companies including Google, Microsoft and of course Apple\u00a0that uses it from the beginning.<\/p>\n<p>The LLVM infrastructure could be used to implement the compilers of many programming languages and\u00a0interpreters. For example,\u00a0the PostgreSQL uses now the LLVM JIT to optimize the queries execution as specified <a href=\"http:\/\/www.pgcon.org\/2017\/schedule\/events\/1092.en.html\">here<\/a>:<\/p>\n<p><em>Currently, PostgreSQL executes SQL queries using the interpreter, which is quite slow. However, significant speedup can be achieved by compiling query &#8220;on-the-fly&#8221; with LLVM JIT: this way it&#8217;s possible for given SQL query to generate more <\/em>effective<em> code, which is specialized using the information known at <\/em>run time<em>, and then additionally optimize it with LLVM. This approach is especially important for complex queries, where performance is CPU-bound.<\/em><\/p>\n<p>Thanks to all the LLVM\/Clang developers and special thanks to Chris Lattner who initiate the revolution and give us a powerful infrastructure to develop meaningful tools.<\/p>\n<p>Finally, a message to the young developers, be the next Chris Lattner and initiate a revolution in\u00a0the programming world \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Maybe almost all C++ developers know the LLVM infrastructure and the Clang compiler. But how many know that Chris Lattner is their creator when he\u00a0was only 25 years old. How it&#8217;s possible? I remember when I was 25 years I spend my time to understand the C++ basics \ud83d\ude42 The story begins with a thesis &hellip; <a href=\"https:\/\/cppdepend.com\/blog\/chris-lattner-revolutionizing-cpp-world\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Chris Lattner: Revolutionizing the C++ World&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[64,13],"class_list":["post-1005","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-chris-lattner","tag-cpp"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=1005"}],"version-history":[{"count":39,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1005\/revisions"}],"predecessor-version":[{"id":1467,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/1005\/revisions\/1467"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=1005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=1005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=1005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}