LLVM and Clang: Advancing Compiler Technology

Similar documents
LLVM An Introduction. Linux Collaboration Summit, April 7, 2011 David Kipping, Qualcomm Incorporated

LLVM and beyond!

What s New in the LLVM Compiler. Chris Lattner LLVM Chief Architect

McSema: Static Translation of X86 Instructions to LLVM

LLVM + Gallium3D: Mixing a Compiler With a Graphics Framework. Stéphane Marchesin

LLVM, Clang and Embedded Linux Systems. Bruno Cardoso Lopes University of Campinas

LDC: The LLVM-based D Compiler

Tuesday, 23 March OpenGL Shading Language

GLSL. OpenGL Shading Language. OpenGL 1.5 Logical Diagram. OpenGL 2.0 Logical Diagram

15-411: LLVM. Jan Hoffmann. Substantial portions courtesy of Deby Katz

Connecting the EDG front-end to LLVM. Renato Golin, Evzen Muller, Jim MacArthur, Al Grant ARM Ltd.

Porting Fabric Engine to NVIDIA Unified Memory: A Case Study. Peter Zion Chief Architect Fabric Engine Inc.

Introduction. CS 2210 Compiler Design Wonsun Ahn

Compilers and Code Optimization EDOARDO FUSELLA

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

LCA14-412: GPGPU on ARM SoC. Thu 6 March, 2.00pm, T.Gall, G.Pitney

LLVM MC in Practice. Jim Grosbach and Owen Anderson

Martin Kruliš, v

BEAMJIT, a Maze of Twisty Little Traces

Topic 9: Type Checking

Topic 9: Type Checking

trisycl Open Source C++17 & OpenMP-based OpenCL SYCL prototype Ronan Keryell 05/12/2015 IWOCL 2015 SYCL Tutorial Khronos OpenCL SYCL committee

Traditional Smalltalk Playing Well With Others Performance Etoile. Pragmatic Smalltalk. David Chisnall. August 25, 2011

Tutorial: Building a backend in 24 hours. Anton Korobeynikov

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

KOTLIN/NATIVE + CLANG, TRAVEL NOTES NIKOLAY IGOTTI, JETBRAINS

Object Code Emission & llvm-mc

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Vertex & Fragment shaders

ECE 5775 (Fall 17) High-Level Digital Design Automation. Static Single Assignment

Copyright Khronos Group Page 1. Vulkan Overview. June 2015

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

Chapter 11 Introduction to Programming in C

Compilation I. Hwansoo Han

Compilers and Code Optimization EDOARDO FUSELLA

OpenACC Course. Office Hour #2 Q&A

A Plan 9 C Compiler for RISC-V

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

Computer Components. Software{ User Programs. Operating System. Hardware

Introduction to LLVM. UG3 Compiling Techniques Autumn 2018

Design & Implementation Overview

Intermediate Code Generation

Four Components of a Computer System

WIND RIVER DIAB COMPILER

Lecture 3 Overview of the LLVM Compiler

CHAPTER 2: SYSTEM STRUCTURES. By I-Chen Lin Textbook: Operating System Concepts 9th Ed.

Chapter 2. Operating-System Structures

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

Chapter 11 Introduction to Programming in C

Accelerating Ruby with LLVM

Virtual Machines and Dynamic Translation: Implementing ISAs in Software

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edit9on

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

DDMD AND AUTOMATED CONVERSION FROM C++ TO D

Lecture 2: C Programm

LLVM and IR Construction

Working with Metal Overview

Dynamic Cuda with F# HPC GPU & F# Meetup. March 19. San Jose, California

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

Instruction Set Architecture

reinven&ng the compiler

12.2 Programmable Graphics Hardware

Introducing LLDB for Linux on Arm and AArch64. Omair Javaid

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

CMPT 379 Compilers. Anoop Sarkar.

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Come and join us at WebLyceum

C - Basics, Bitwise Operator. Zhaoguo Wang

Compiler Theory Introduction and Course Outline Sandro Spina Department of Computer Science

Chapter 2: Operating-System Structures

Going to cover; - Why we have SPIR-V - Brief history of SPIR-V - Some of the core required features we wanted - How OpenCL will use SPIR-V - How

BEAMJIT: An LLVM based just-in-time compiler for Erlang. Frej Drejhammar

Software Engineering /48

Porting OpenVMS to x Update

Chapter 11 Introduction to Programming in C

Programmable Graphics Hardware

Computer Components. Software{ User Programs. Operating System. Hardware

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

VA Smalltalk 9: Exploring the next-gen LLVM-based virtual machine

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edition

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Overview of C++ Support in TI Compiler Tools July 2008

Practical Malware Analysis

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

Instruction Set Principles and Examples. Appendix B

Crafting a Compiler with C (II) Compiler V. S. Interpreter

INTRODUCTION TO LLVM Bo Wang SA 2016 Fall

Libabigail & ABI compatibility

Chapter 11 Introduction to Programming in C

SIGGRAPH Briefing August 2014

CSc 520 Principles of Programming Languages

Kampala August, Agner Fog

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

COMP-520 GoLite Tutorial

CLICK TO EDIT MASTER TITLE STYLE. Click to edit Master text styles. Second level Third level Fourth level Fifth level

Transcription:

LLVM and Clang: Advancing Compiler Technology FOSDEM 11 - Feb 5, 2011

What is the LLVM Umbrella Project? Language independent optimizer and code generator Many optimizations, many targets, generates great code Clang C/C++/Objective-C front-end Designed for speed, reusability, compatibility with GCC quirks Debuggers, binutils, standard libraries Providing pieces of a low-level toolchain, with many advantages Applications of LLVM OpenGL, OpenCL, Python, Ruby, etc, even RealBasic and Cray Fortran LLVM/Clang are Open Source with a BSD-like License!

Why new compilers? Existing open source C compilers have stagnated! Based on decades old code generation technology Aging code bases: difficult to learn, hard to change substantially Not modular, can t be reused in many other applications Keep getting slower with every release What I want: A set of production-grade reusable libraries... which implement the best known techniques... which focus on compile time... and performance of the generated code Ideally support many different languages and applications!

LLVM Vision and Approach Primary mission: build a set of modular compiler components: Reduces the time & cost to construct a particular compiler A new compiler = glue code plus any components not yet available Components are shared across different compilers Improvements made for one compiler benefits the others Allows choice of the right component for the job Don t force one true register allocator, scheduler, or optimization order Secondary mission: Build compilers that use these components... for example, an amazing C compiler X86 ARM PPC clang GCC LTO... DWARF Target Code gen JIT Optzn linker IPO... BC IO ELF System Core Support xforms analysis GC...

LLVM Code Generator Highlights Approachable C++ code base, modern design, easy to learn Strong and friendly community, good documentation Language and target independent code representation Very easy to generate from existing language front-ends Text form allows you to write your front-end in perl if you desire Modern code generator: Supports both JIT and static code generation Much easier to retarget to new chips than GCC Many popular targets supported: X86, ARM, PowerPC, SPARC, Alpha, MIPS, Blackfin, CellSPU, MBlaze, MSP430, XCore, etc. docs/

Example Application: LLVM + OpenGL

Colorspace Conversion Code to convert from one color format to another: e.g. BGRA 444R to RGBA 8888 Hundreds of combinations, importance depends on input for each pixel { switch (infmt) { case RGBA5551: R = (*in >> 11) & C G = (*in >> 6) & C B = (*in >> 1) & C... } switch (outfmt) { case RGB888: *outptr = R << 16 G << 8... } } Run-time specialize for each pixel { R = (*in >> 11) & C; G = (*in >> 6) & C; B = (*in >> 1) & C; *outptr = R << 16 G << 8... } Compiler optimizes shifts and masking Speedup depends on src/dest format: 5.4x speedup on average, 19.3x max speedup: (13.3MB/s to 257.7MB/s)

OpenGL Pixel/Vertex Shaders Small program run on each vertex/pixel, provided at run-time: Written in one of a few high-level graphics languages (e.g. GLSL) Executed millions of times, extremely performance sensitive Ideally, these are executed on the graphics card: What if hardware doesn t support some feature? (e.g. laptop gfx) Interpret or JIT on main CPU void main() { vec3 ecposition = vec3(gl_modelviewmatrix * gl_vertex); vec3 tnorm = normalize(gl_normalmatrix * gl_normal); vec3 lightvec = normalize(lightposition - ecposition); vec3 reflectvec = reflect(-lightvec, tnorm); vec3 viewvec = normalize(-ecposition); float diffuse = max(dot(lightvec, tnorm), 0.0); float spec = 0.0; if (diffuse > 0.0) { spec = max(dot(reflectvec, viewvec), 0.0); spec = pow(spec, 16.0); } LightIntensity = DiffuseContribution * diffuse + SpecularContribution * spec; MCposition = gl_vertex.xy; gl_position = ftransform(); } OpenGL Pixel/Vertex Shader (GLSL)

OpenGL Implementation Before LLVM Custom JIT for X86-32 and PPC-32: Very simple codegen: pasted chunks of AltiVec or SSE code Little optimization across operations (e.g. scheduling) Very fragile, hard to understand and change (hex opcodes) Interpreter: JIT didn t support all OpenGL features: fallback to interpreter Interpreter was very slow, 100x or worse than JIT GLSL Text OpenGL Shader Parser GPU Custom JIT Interpreter

OpenGL JIT Built with LLVM Components GLSL Text OpenGL Shader Parser OpenGL to LLVM LLVM Optimizer LLVM JIT At runtime, build LLVM IR for program, optimize, JIT: Result supports any target LLVM supports Generated code is as good as an optimizing static compiler OpenGL benefits from LLVM optimizer/codegen improvements How does the OpenGL to LLVM stage work?

Detour: Structure of an Interpreter Simple opcode-based dispatch loop: while (...) {... switch (cur_opcode) { case dotproduct: result = opengl_dot(lhs, rhs); break; case texturelookup: result = opengl_texlookup(lhs, rhs); break; case... One function per operation, written in C: double opengl_dot(vec3 LHS, vec3 RHS) { #ifdef ALTIVEC... altivec intrinsics... #elif SSE... sse intrinsics... #else... generic c code... #endif } Key Advantage of an Interpreter: Easy to understand and debug, easy to write each operation (each operation is just C code) In a high-level language like GLSL, ops can be hundreds of LOC

OpenGL to LLVM Implementation C Code Opcode Functions LLVM C Compiler Bytecode DISK OpenGL Build Time Bytecode Ship GLSL OpenGL Shader Parser OpenGL to LLVM LLVM Optimizer LLVM JIT At OpenGL build time, compile each opcode to LLVM bytecode: Same code used by the interpreter: easy to understand/change/optimize

OpenGL to LLVM: At runtime 1. Translate OpenGL AST into LLVM call instructions: one per operation 2. Use the LLVM inliner to inline opcodes from precompiled bytecode 3. Optimize/codegen as before GLSL OpenGL Shader Parser OpenGL to LLVM LLVM Optimizer LLVM JIT... vec3 viewvec = normalize(-ecposition); float diffuse = max(dot(lightvec, tnorm), 0.0);... PPC X86 OpenGL to LLVM Optimize, Codegen... %tmp1 = call opengl_negate(ecposition) %viewvec = call opengl_normalize(%tmp1); %tmp2 = call opengl_dot(%lightvec, %tnorm) %diffuse = call opengl_max(%tmp2, 0.0);... LLVM Inliner... %tmp1 = sub <4 x float> <0,0,0,0>, %ecposition %tmp3 = shuffle <4 x float> %tmp1,...; %tmp4 = mul <4 x float> %tmp3, %tmp3...

Benefits of this Approach Each opcode is written/debugged for a simple interpreter as standard C code Retains all advantages of an interpreter: debug-ability, understandability, etc Easy to make algorithmic changes to opcodes Great performance!

Lots of Other Applications OpenCL: a GPGPU language, with most vendors using LLVM Dynamic Languages: Unladen Swallow, Rubinious, MacRuby llvm-gcc 4.2 & DragonEgg Cray Cascade Fortran Compiler vmkit: Java and.net VMs Haskell, Mono, LDC, Pure, Roadsend PHP, RealBasic IOQuake3 for real-time raytracing of Quake! Users.html

Clang Compiler

Clang Goals Unified parser for C-based languages Language conformance (C, Objective-C, C++) Useful error and warning messages Library based architecture with finely crafted API s Useable and extensible by mere mortals Reentrant, composable, replaceable Multi-purpose Indexing, static analysis, code generation Source to source tools, refactoring http://clang.llvm.org/

Clang Goals #2 High performance! Low memory footprint, fast compiles Support lazy evaluation, caching, multithreading get the compiler out of the way during development Highly Compatible with GCC Supports almost all the arcane, but useful, GCC extensions GCC Inline ASM and CPU built-ins / intrinsics supported Aim for drop-in replacement where reasonable http://clang.llvm.org/

Clang Compiler Status C, Objective-C, and C++ support are production quality Clang has successfully compiled millions of lines of C/C++/Objective-C code Can bootstrap itself, build Boost, Mozilla, and many other compiler busters Builds a working FreeBSD base system Interesting tools starting to be built on it Common stumbling blocks migrating from GCC to Clang: C89 vs C99 inlining differences Bugs in G++ s template implementation http://clang.llvm.org/compatibility.html Work is progressing on MSVC compatibility and C++ 0x support Shockingly fast and memory efficient, much better user experience! http://clang.llvm.org/

Compile Time Comparison: Front-end PostgreSQL: a medium sized C project: 619 C Files in 665K LOC, excluding headers 50s 48.9s 40s 30s 23.93s 2.3x faster! 21.6s 20s 6.91s 10s 25.07s 14.70s GCC 4.2 Preprocessor clang Parsing, Type Checking http://clang.llvm.org/performance.html http://clang.llvm.org/

SPEC INT 2000 Optimizer Compile Times 200s 160s 120s 80s 133s GCC 4.2 LLVM 112s 164s 126s 30% Faster at -O2! 187s 131s 42% Faster at -O3! Lower is Better 144s Faster than GCC at -O2! 40s 18% Faster at -O1! 0s -O1 -O2 -O3 -O4: LTO 0s http://clang.llvm.org/

100% 95% SPEC INT 2000 Execution Time Relative to GCC -O2: Lower is Better 100.0% 95.1% 96.4% GCC 4.2 LLVM 90% 5% Faster at -O2! 92.5% 4% Faster at -O3! 85% 80% 20% Faster than -O2! 80.3% 75% -O2 -O3 -O4 (LTO) http://clang.llvm.org/

User Experience: Diagnostics $ clang implicit-def.c -std=c89 implicit-def.c:6:10: warning: implicit declaration of function 'X' return X(); ^ struct A { int X; } somea; int func(int); 5: 6: int test1(int intarg) { intarg += *(somea.x); return intarg + func(intarg? ((somea.x + 40) + somea) / 42 + somea.x : somea.x)); } % gcc-4.2 t.c t.c: In function 'test1': t.c:5: error: invalid type argument of 'unary *' t.c:6: error: invalid operands to binary + http://clang.llvm.org/

User Experience: Expressive Diagnostics Other Features: std::string instead of std::basic_string<char, std::char_traits<char>, std::allocator<char> > #pragma control over diagnostics Doesn t pretty print expressions back out at you % clang test.c t.c:5:13: error: indirection requires pointer operand ('int' invalid) intarg += *(somea.x); ^~~~~~~~~~ t.c:6:49: error: invalid operands to binary expression ('int' and 'struct A') return intarg + func(intarg? ((somea.x+40) + somea) / 42 + somea.x : somea.x)); ~~~~~~~~~~~~ ^ ~~~~~ % gcc-4.2 t.c t.c: In function 'test1': t.c:5: error: invalid type argument of 'unary *' t.c:6: error: invalid operands to binary + http://clang.llvm.org/

Other Improvements $ g++-4.2 t.cpp t.cpp:12: error: no match for 'operator=' in 'str = vec' $ clang t.cpp t.cpp:12:7: error: incompatible type assigning 'vector<real>', expected 'std::string' (aka 'class std::basic_string<char>') str = vec; ^ ~~~ t.c:48:7: error: invalid operands to binary expression ('int' and 'struct A') X = MAX(X, *Ptr); ^~~~~~~~~~~~ t.c:43:24: note: instantiated from: #define MAX(A, B) ((A) > (B)? (A) : (B)) ~~~ ^ ~~~ http://clang.llvm.org/diagnostics.html http://clang.llvm.org/

Clang Static Analyzer Automatically finds and reports bugs in your code Uses deep analysis techniques to explore things that testing misses http://clang-analyzer.llvm.org/ http://clang.llvm.org/

Other Notable LLVM Projects MC: Machine Code slicing and dicing Assemblers, disassemblers, object file processing LLDB: Low Level Debugger Command-line debugger http://lldb.llvm.org/ Reuses Clang parser, LLVM JIT, MC disassemblers Great support for C++, and multithreaded apps libc++: C++ standard runtime library Full support for C++ 0x http://libcxx.llvm.org/ No compromises performance devmtg/2010-11/

LLVM and Clang Compiler infrastructure built with reusable components Bringing compiler techniques to new interesting problems LLVM: flexible optimizer and code generator Fast compiles, great generated code Supports many targets Reusable in nontraditional contexts Clang: C/ObjC/C++ front-end Multiple times faster than other compilers Great end-user features (e.g. warnings/errors) Platform for new source level tools Come join us at: http://llvm.org http://clang.llvm.org