32
OCL OMP clang reinven&ng the compiler C Obj-C 1 C++ Alp Toker h3p://www.nuan&.com

clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

  • Upload
    others

  • View
    41

  • Download
    1

Embed Size (px)

Citation preview

Page 1: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

OCL OMP

clangreinven&ng  the  compiler

C Obj-C

���1

C++

Alp  Toker h3p://www.nuan&.com

Page 2: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Overview

• What can we achieve going beyond compilation?

• Why are we compelled to invent a better wheel?

• How can we make everyday life better for coders?

• Could the compiler itself become an instrument for wider social change?

���2

Page 3: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Clang  in  a  Nutshell

LLVM backend

clang frontend

lld linker

clang driver

clang-cl driver

���3

Page 4: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

CodeGen to LLVM IR

Analysis ARCMigrate AST ASTMatchers Basic CodeGen Driver Edit Format Frontend FrontendTool Headers Index Lex Parse Rewrite Sema Serialization StaticAnalyzer Tooling

Sema semantic analysis

Parse semantic analysis

Lex tokenization and preprocessing

AST syntax tree

Analyzer static analysis

���4

clang  frontend  “lowering”

Page 5: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Plugins & frontend

passesRewrite

& other utilities

Tooling build-your-own

CIndex indexing engine

libclang stable C API

���5

Page 6: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Why  invent  a  be;er  wheel?

���6

Page 7: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

MSVC  support  is  coming.  But  why  are  we  even  doing  this?

���7

Page 8: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

���8

Page 9: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

���9

ISO  CObjec&ve  C

ISO  C++

a  kind  of  geekyRose;a  stone

MicrosoE  Visual  C++

OpenCL

OpenMP

Page 10: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

3.5:  MSVC  CompaJbility

• More significant than just Windows support

• Unusual parsing

• Name mangling

• Built-in types

• Delayed template parsing���10

Page 11: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

clang-­‐cl.exe

• A cl.exe drop-in replacement driver

• Visual Studio integration

���11

Page 12: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

���12

Page 13: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

���13

Page 14: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

How?

���14

Page 15: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

The  Clang  Parser

• Hand-written recursive-descent parser.

• A single unified parser for C/C++/ObjC

• (Mostly) decoupled from the AST representation

���15

Page 16: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Clang  SemanJc  Analysis

• Sema: The brains of the operation.

• Builds the AST and computes types, linkage etc.

• Some problems here too.

���16

Page 17: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

What  next?

���17

Page 18: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

The  road  to  Faster  CompilaJon

• In-process execution currently under investigation

• Multi-TU compilation supporting modules

• Cached resources across invocations

• Use MCJIT for constexpr compile-time evaluation?

���18

Page 19: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Spot  the  problem  in  this  code…

���19

bool ProcessingFailed = for (unsigned std::string // FIXME: chdir is thread hostile; on the other hand, creating the same // behavior as chdir is complex: chdir resolves the path once, thus // guaranteeing that all subsequent relative path operations work // on the same path the original chdir resulted in. This makes a difference // for example on network filesystems, where symlinks might be switched // during runtime of the tool. Fixing this depends on having a file system // abstraction that allows openat() style interactions. if (chdir llvm:: std::vector for (unsigned CommandLine = assert(!CommandLine. CommandLine[ // FIXME: We need a callback mechanism for the tool writer to output a // customized message for each file. DEBUG({ llvm:: });

Page 20: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Time  for  Compiler  Accessibility?• Vision and motion impaired users code too.

• Hierarchical documents lend themselves to universal access:

• The AST is a natural representation here to get started.

• Code completion machinery can help select inputs and refactoring will enable edits out of scope.

• Diagnostics can be annotated for voice output.

• We have all the technology today, yet no a11y story to speak of.���20

Page 21: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Clang  &  the  Linux  Kernel

• clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode.

• Integrated ASM parser support imminent for all .S files

• Users & developers joining the LLVM community to fulfil their needs.

���21

LLVMLinux 11:00 AM

Page 22: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

The  Clang  AST• Abstract Syntax Tree represented as a C++ class hierarchy

• Uses LLVM’s casting system, not RTTI

• Informal representation, some problems here:

• Objective-C duplication. Function/Method, Interface/Record/Class…

• Some semantic analysis still “performed” by AST

• Type system omits linkage & other details, time to address this?���22

Page 23: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

���23

LLVM  and  Clang  are  defending  your  SoEware  Freedom.  Here’s  how…

Page 24: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

���24

!

(That’s called defending your Software Freedom.)

Vendors are switching:

From 100% proprietary toolchains to 90%-100% Free Software compiler stacks built around clang.

(Freedom #1)

the freedom to innovate

Page 25: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

���25

(Freedom #2)

Page 26: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

LLVM  Community  2.0

• Do we approach controversial issues effectively?

• What makes a patch acceptable?

• Do we welcome new contributors or is there an initiation by fire?

• How should we handle non-code contributions?���26

Page 27: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

1.5m LoC30 active developers 500 commits per month

Clang  Zeitgeist  2014

���27

Page 28: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Some  introspecJve  quesJons…

• Where does our infrastructure come from?

• Is there a framework to deal with emergencies and existential threats to our project?

• Do we have transparent oversight?

• How about…���28

Page 29: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

���29

LLVM  Founda&oncompilers  for  everyone

[  proposal  ]  [  draft  ]  [  please-­‐review  ]

Page 30: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

Planet Clang http://planet.clang.org

LLVM Weekly http://llvmweekly.org

���30

Page 31: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

OpJmizer  pragmas  &  a;ributes

• A desire to offer hands-on control over the LLVM code generation and optimizer.

• Vectorization attributes

• optnone — or more granularity?

���31

Auto-vectorization 11:00 AM

Page 32: clang - LLVM · 2019-10-30 · Clang&theLinuxKernel • clang -m16: Code generation to support the x86 boot loader appropriate for a CPU running in 16-bit mode." • Integrated ASM

���32

Alp  Tokeralp@nuan&.com

TOGETHER, WE CAN REINVENT THE COMPILER.