What Is LLVM? and a Status Update
Total Page:16
File Type:pdf, Size:1020Kb
What is LLVM? And a Status Update. Approved for public release Hal Finkel Leadership Computing Facility Argonne National Laboratory Clang, LLVM, etc. ✔ LLVM is a liberally-licensed(*) infrastructure for creating compilers, other toolchain components, and JIT compilation engines. ✔ Clang is a modern C++ frontend for LLVM ✔ LLVM and Clang will play significant roles in exascale computing systems! (*) Now under the Apache 2 license with the LLVM Exception LLVM/Clang is both a research platform and a production-quality compiler. 2 A role in exascale? Current/Future HPC vendors are already involved (plus many others)... Apple + Google Intel (Many millions invested annually) + many others (Qualcomm, Sony, Microsoft, Facebook, Ericcson, etc.) ARM LLVM IBM Cray NVIDIA (and PGI) Academia, Labs, etc. AMD 3 What is LLVM: LLVM is a multi-architecture infrastructure for constructing compilers and other toolchain components. LLVM is not a “low-level virtual machine”! LLVM IR Architecture-independent simplification Architecture-aware optimization (e.g. vectorization) Assembly printing, binary generation, or JIT execution Backends (Type legalization, instruction selection, register allocation, etc.) 4 What is Clang: LLVM IR Clang is a C++ frontend for LLVM... Code generation Parsing and C++ Source semantic analysis (C++14, C11, etc.) Static analysis ● For basic compilation, Clang works just like gcc – using clang instead of gcc, or clang++ instead of g++, in your makefile will likely “just work.” ● Clang has a scalable LTO, check out: https://clang.llvm.org/docs/ThinLTO.html 5 The core LLVM compiler-infrastructure components are one of the subprojects in the LLVM project. These components are also referred to as “LLVM.” 6 What About Flang? ● Started as a collaboration between DOE and NVIDIA/PGI. Now also involves ARM and other vendors. ● Public discussions on making Flang (f18+runtimes) part of LLVM going well. ● Two development paths: f18 – A new Flang based frontend on PGI’s written in existing modern C++. frontend (in C). Fortran Parsing, Production runtime semantic ready including library and analysis, etc. OpenMP vectorized under active support. math- function development. library. LLVM Project 7 What About MLIR? ● MLIR is now part of the LLVM project. ● MLIR is a "multi-level IR", originaly developed as part of the TensorFlow project. ● It is a kind of framework for producing particular IRs (along with a nice way to specify peephole optimizations for them, translations between them, etc.) ● MLIR is being used for Flang 8 Upcoming LLVM Status... ● Relicensing well underway. ● Moved to github (might also move issue tracking, etc. soon). ● Version 9.0.1 has been released. EuroLLVM 9 What’s Working Well ● Involvement: ● Features: LLVM has become well known for an important set of features: ● A well-defined IR allows use by a lot of different languages (C, C++, Fortran, Julia, Rust, Python (e.g., via Numba), Swift, ML frameworks (e.g., TensorFlow/XLA, PyTorch/Glow), and many others. ● A backend infrastructure allowing the efficient creation of backends for new hardware. ● A state-of-the-art C++ frontend, CUDA support, scalabale LTO, sanitizers and other debugging capabilities, and more. ● High code-quality standards. 10 MPI-specifc warning messages These are not really MPI specific, but uses the “type safety” attributes inspired by this use case: int MPI_Send(void *buf, int count, MPI_Datatype datatype) __attribute__(( pointer_with_type_tag(mpi,1,3) )); … #define MPI_DATATYPE_NULL ((MPI_Datatype) 0xa0000000) #define MPI_FLOAT ((MPI_Datatype) 0xa0000001) … static const MPI_Datatype mpich_mpi_datatype_null __attribute__(( type_tag_for_datatype(mpi,void,must_be_null) )) = 0xa0000000; static const MPI_Datatype mpich_mpi_float __attribute__(( type_tag_for_datatype(mpi,float) )) = 0xa0000001; See Clang's test/Sema/warn-type-safety-mpi-hdf5.c, test/Sema/warn-type-safety.c and test/Sema/warn-type-safety.cpp for more examples, 11 and: http://clang.llvm.org/docs/AttributeReference.html#type-safety-checking Sanitizers The sanitizers (some now also supported by GCC) – Instrumentation-based debugging ● Checks get compiled in (and optimized along with the rest of the code) – Execution speed an order of magnitude or more faster than Valgrind ● You need to choose which checks to run at compile time: ● Address sanitizer: -fsanitize=address – Checks for out-of-bounds memory access, use after free, etc.: http://clang.llvm.org/docs/AddressSanitizer.html ● Leak sanitizer: Checks for memory leaks; really part of the address sanitizer, but can be enabled in a mode just to detect leaks with -fsanitize=leak: http://clang.llvm.org/docs/LeakSanitizer.html ● Memory sanitizer: -fsanitize=memory – Checks for use of uninitialized memory: http://clang.llvm.org/docs/MemorySanitizer.html ● Thread sanitizer: -fsanitize=thread – Checks for race conditions: http://clang.llvm.org/docs/ThreadSanitizer.html ● Undefined-behavior sanitizer: -fsanitize=undefined – Checks for the execution of undefined behavior: http://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html ● Efficiency sanitizer [Recent development]: -fsanitize=efficiency-cache-frag, -fsanitize=efficiency-working- set (-fsanitize=efficiency-all to get both) And there's more, check out http://clang.llvm.org/docs/ and Clang's include/clang/Basic/Sanitizers.def for more information. 12 Clang Can Compile CUDA! ● CUDA is the language used to compile code for NVIDIA GPUs. ● Support now also developed by AMD as part of their HIP project. $ clang++ axpy.cu -o axpy --cuda-gpu-arch=<GPU arch> For example: --cuda-gpu-arch=sm_35 When compiling, you may also need to pass --cuda-path=/path/to/cuda if you didn’t install the CUDA SDK into /usr/local/cuda (or a few other “standard” locations). For more information, see: http://llvm.org/docs/CompileCudaWithLLVM.html Clang's CUDA aims to provide better support for modern C++ than NVIDIA's nvcc. 13 Many Derived Vendor Products Upstream LLVM Code Base Add optimizations Add optimizations Add optimizations and other features. and other features. and other features. Package and Ship Package and Ship Package and Ship Product Product Product Vendor C Vendor A Vendor B 14 On Interacting with Open-Source LLVM When compilers are open source: ● DOE Staff, collaborators, contractors, etc. can directly contribute features and bug fixes. ● And we can review code to keep quality high (and keep the open source community healthy). ● We can enhance user productivity by delivering new features and bug fixes to our users quickly… ● This critically depends on testing! CI/testing system with large Facilities Vendor(s) amount of DOE + applications and libraries Happy users! open-source with correctness tests! LLVM DOE/ECP’s CI effort is important here! Staff, collaborators, contractors, etc. Bug fixes (and simple features) deployed to users in days (not weeks or months)! 15 Flang Approved for public release Alexis Perry–Holby (Los Alamos National Laboratory) Patrick McCormick (Los Alamos National Laboratory) Douglas Miles(NVIDIA) Stephen Scalpone (NVIDIA) Hal Finkel (Argonne National Laboratory) David Bernholdt (Oak Ridge National Laboratory) Brian Friesen (Lawrence Berkeley National Laboratory) Feb. 6th, 2020 Flang – Current Status • Working on upstreaming Flang (F18) codebase to LLVM’s main monorepo – Crucial to the long-term success of the project – Identified items are worked on collaboratively by the community • Technical work scope details: – Working on resolution of type-bound generics and operators – Completed support of forward references to derived types – Implemented logical expression lowering – Began character expression lowering – Created an expression lowering test framework – Continued work on DO loop semantic checks, especially where zeroes are not allowed – Continued work on FIR definition 2 Flang – Path to Merging into the LLVM Monorepo • CMake changes to support in-tree building – PR coming soon • Style changes that take us closer to LLVM – Clang format changes to bring Flang more in line with LLVM (PR #945) – Rationalization of public/private headers (merged PR #943) – Renaming of files from .cc to .cpp (merge PR #958) • Making more general use of LLVM APIs and data structures – Discussion of detailed list of changes is ongoing on flang-dev mailing list • Port testing to use LLVM tools (lit and FileCheck) – PR #941 ports the test suite to lit – Separate discussions of various custom scripts are ongoing The LLVM Compiler Infrastructure • Build compiler support – Need plan for improving public buildbot coverage and moving in a direction to allow ECP-centric coverage to be addressed 3 Flang – Growing Community The LLVM Compiler Infrastructure 4 Flang – Project Timeline • Schedule is currently driven by phases of the compiler (parsing, semantic analysis, lowering, etc.) • Schedule priority is on getting infrastructure complete for a functionally correct, sequential compiler – it does not necessarily reflect the steps required for full LLVM adoption. – Additional efforts working on foundational pieces (shared with Clang) for OpenMP support (i.e. OpenMP IR Builder) • Fortran-centric optimizations, including runtime library, will follow in a series of releases timed with an early release within ECP and then a drop into LLVM’s 6-month release cycle Note: Current community design discussions are considering two MLIR dialects here. One for Fortran-centric constructs and a second for OpenMP. AMD Semantic Parse Lower to FIR Analysis FIR Analysis & LLVM (Fortran 2018) (Fortran 2018) Lower to LLVM