LLVMLinux for the Real World
Bryce Adelstein-Lelbach, LSU Undergraduate
llvm.linuxfoundation.org
Overview
• I ask you some questions
• Introduction to LLVM and Clang
• History of the LLVMLinux project
• Applications of LLVMLinux
– Diagnostics
– Source Transformation
– Cross Compilation
• You ask me some questions
llvm.linuxfoundation.org 2
Have you ever used a search-and-replace tool to update deprecated or incorrect code?
How big was the codebase?
llvm.linuxfoundation.org 3
Have you ever manually performed a tedious update of deprecated or incorrect code?
How big was the codebase?
llvm.linuxfoundation.org 4
Raise your hand if you’ve used a cross compiler
llvm.linuxfoundation.org 5
LLVM and Clang
• LLVM
– A modular and extensible compiler framework
– Uses a common intermediate representation known as LLVM IR
• Clang
– An LLVM frontend for C, C++ and other C-like languages
– Highly standard compliant
– GCC compatible
llvm.linuxfoundation.org 6
Open Source (BSD style license)
Library Architecture
• Both LLVM and Clang are designed with a library architecture – Instead of traditional, monolithic drivers, LLVM
and Clang provide a series of libraries which can be used to implement compiler tools
• This design makes LLVM and Clang modular and extensible
• Facilitates the development of domain-specific solutions to toolchain problems
llvm.linuxfoundation.org 7
Beginning
• October, 2010 – First working Clang-compiled Linux kernel
• December 2010 – Most of the major initial bugs are resolved
• April, 2011 – Work from initial collaborators is unified on Github
llvm.linuxfoundation.org 8
Linux Foundation
• May, 2011 – Linux Foundation begins supporting development
• May, 2012 – All existing work is consolidated under the LLVMLinux project (hosted by LF)
llvm.linuxfoundation.org 9
Diagnostics
• Superb diagnostics
– Column numbers for all diagnostics
– Caret/Point diagnostics
• Range highlighting
– Intelligent typedef preservation and unwrapping
– Macro expansion
– Wide range of CFG-based warnings
• Example: –Wtautological-compare warns about checking if an unsigned integer is less than 0
llvm.linuxfoundation.org 10
Diagnostics
llvm.linuxfoundation.org 11
Suggested fix
Carrot and range
Source line Flag
Source location
Diagnostic type
Static Analyzer
• The Clang Static Analyzer is a tool built with the Clang libraries that detects bugs – Supports plugins
• A static analyzer uses techniques similar to those used to generate compiler warnings – Static analyzers perform far more exhaustive
analysis
– Static analyzers can detect bugs that would typically only manifest at runtime
llvm.linuxfoundation.org 12
Static Analyzer
llvm.linuxfoundation.org 13
Fixits
• Clang’s diagnostic framework can suggest solutions (fixits) to common syntactical mistakes and even simple semantic errors
– Fixits can fix the source files itself
– Can copy the source files and apply the fixes to the copy
• Fixits are the first iteration of a still evolving framework for automatic refactoring
llvm.linuxfoundation.org 14
Refactoring
• Clang developers are currently developing infrastructure for automatic source refactoring
• Automatic refactoring with a compiler framework is more powerful than refactoring with regex-based tools – Most refactoring tools only refactor syntax, Clang-
based tools can refactor semantics and syntax
• The framework could potentially be utilized to replace tools like coccinelle with something more robust
llvm.linuxfoundation.org 15
Universal Driver
• Clang has, from the start, been capable of generating code for multiple platforms
• The universal driver model is a new initiative which Clang is pursuing to make cross compiling easier – The basic idea is to have one driver application for all
targets
– The user would specify a target via a command line option and the driver would handle the rest
• This technique would simplify the deployment of Linux to embedded platforms such as smartphones and tablets
llvm.linuxfoundation.org 16
Acknowledgments
• Linux Foundation – Mike Woster – Jan-Simon Möller – Behan Webster
• Early Colloborators – PaX Team – Alp Toker – Török Edwin – Sedat Dilek
• Mark Charlebois • Various LLVM/Clang developers • Various Linux kernel developers
llvm.linuxfoundation.org 17
llvm.linuxfoundation.org 18