_llvm_arm_external Snapdragon LLVM ARM 40 Compiler User Guide

Snapdragon_LLVM_ARM_40_Compiler_User_Guide

Snapdragon_LLVM_ARM_40_Compiler_User_Guide

Snapdragon_LLVM_ARM_40_Compiler_User_Guide

Snapdragon_LLVM_ARM_40_Compiler_User_Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 161

Download_llvm_arm_external Snapdragon LLVM ARM 40 Compiler User Guide
Open PDF In BrowserView PDF
Qualcomm Technologies, Inc.

Qualcomm® Snapdragon™ LLVM ARM
Compiler for Android
User Guide
80-VB419-90 M
September 1, 2017

Qualcomm Snapdragon is a product of Qualcomm Technologies, Inc. Other Qualcomm products referenced herein are products of Qualcomm
Technologies, Inc. or its subsidiaries.
Qualcomm and Snapdragon are trademarks of Qualcomm Incorporated, registered in the United States and other countries. Krait is a
trademark of Qualcomm Incorporated. Other product and brand names may be trademarks or registered trademarks of their respective
owners.
This technical data may be subject to U.S. and international export, re-export, or transfer (“export”) laws. Diversion contrary to U.S. and
international law is strictly prohibited.
This document contains material provided to Qualcomm Technologies, Inc. under licenses reproduced in Appendix B that are provided to you
for attribution purposes only. Your license to this document is from Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc.
5775 Morehouse Drive
San Diego, CA 92121
U.S.A.
© 2013-2017 Qualcomm Technologies, Inc. All rights reserved.

Contents
1 Introduction........................................................................................... 8
1.1 Conventions.................................................................................................................. 8
1.2 Technical assistance...................................................................................................... 9

2 Functional Overview .......................................................................... 10
2.1
2.2
2.3
2.4
2.5
2.6

Features....................................................................................................................... 10
Languages................................................................................................................... 10
GCC compatibility...................................................................................................... 11
Processor versions ...................................................................................................... 11
LLVM versions........................................................................................................... 11
C language and compiler references........................................................................... 11

3 Getting Started.................................................................................... 12
3.1 Create source file........................................................................................................ 12
3.2 Compile program........................................................................................................ 12
3.3 Execute program......................................................................................................... 13

4 Using the Compilers........................................................................... 14
4.1 Starting the compilers................................................................................................. 14
4.2 Input and output files.................................................................................................. 15
4.3 Compiler options ........................................................................................................ 16
4.3.1 Display .............................................................................................................. 21
4.3.2 Compilation....................................................................................................... 21
4.3.3 C dialect ............................................................................................................ 22
4.3.4 C++ dialect........................................................................................................ 23
4.3.5 Warning and error messages ............................................................................. 24
4.3.6 Debugging......................................................................................................... 32
4.3.7 Diagnostic format.............................................................................................. 34
4.3.8 Individual warning groups ................................................................................ 36
4.3.9 Compiler crash diagnostics ............................................................................... 38
4.3.10 Linker.............................................................................................................. 38
4.3.11 Preprocessor .................................................................................................... 39
4.3.12 Assembling ..................................................................................................... 43
4.3.13 Linking............................................................................................................ 43
4.3.14 Directory search .............................................................................................. 45

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

2

Qualcomm Snapdragon LLVM ARM Compiler for Android

4.4

4.5
4.6
4.7
4.8

Contents

4.3.15 Processor version ............................................................................................ 45
4.3.16 Code generation ............................................................................................. 48
4.3.17 Vectorization ................................................................................................... 59
4.3.18 Parallelization ................................................................................................. 60
4.3.19 Optimization ................................................................................................... 60
4.3.20 Specific optimizations..................................................................................... 62
4.3.21 Math optimization........................................................................................... 63
4.3.22 Link-time optimization ................................................................................... 64
4.3.23 Profile-guided optimization ............................................................................ 65
4.3.24 Optimization reports ....................................................................................... 66
4.3.25 Compiler security............................................................................................ 66
4.3.26 LLVM 4.0-specific compiler flags.................................................................. 68
Warning and error messages....................................................................................... 70
4.4.1 Controlling how diagnostics are displayed ....................................................... 70
4.4.2 Diagnostic mappings......................................................................................... 70
4.4.3 Diagnostic categories ........................................................................................ 71
4.4.4 Controlling diagnostics with compiler options ................................................. 71
4.4.5 Controlling diagnostics with pragmas .............................................................. 72
4.4.6 Controlling diagnostics in system headers........................................................ 73
4.4.7 Enabling all warnings ....................................................................................... 73
Using GCC cross compile environments ................................................................... 74
Using LLVM with GNU Assembler........................................................................... 75
Built-in functions........................................................................................................ 76
Compilation phases .................................................................................................... 76

5 Code Optimization.............................................................................. 78
5.1
5.2
5.3
5.4
5.5
5.6
5.7

5.8

80-VB419-90 M

Optimizing for performance ....................................................................................... 78
Optimizing for code size ............................................................................................ 78
Automatic vectorization ............................................................................................. 79
Automatic parallelization ........................................................................................... 79
5.4.1 Auto-parallelization using SYMPHONY library.............................................. 80
Merging functions ...................................................................................................... 82
Link-time optimization............................................................................................... 83
Profile-guided optimization........................................................................................ 84
5.7.1 Instrumentation-based PGO.............................................................................. 84
5.7.2 Instrumentation-based profile gen with Android apps...................................... 86
5.7.3 Sampling-based PGO........................................................................................ 87
5.7.4 Sampling-based PGO on Snapdragon MDP ..................................................... 88
5.7.5 Profile resiliency ............................................................................................... 89
5.7.6 PGO tips............................................................................................................ 89
Loop optimization pragmas........................................................................................ 90
5.8.1 Pragma syntax................................................................................................... 90
5.8.2 Compile options ................................................................................................ 91

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

3

Qualcomm Snapdragon LLVM ARM Compiler for Android

Contents

5.8.3 Vectorization pragmas....................................................................................... 92
5.8.4 Reporting........................................................................................................... 93
5.8.5 Examples........................................................................................................... 94
5.9 Optimization reports................................................................................................... 96
5.9.1 Example output ................................................................................................. 96
5.9.2 Optimization report message details................................................................. 97

6 Compiler Security Tools .................................................................. 104
6.1
6.2
6.3
6.4
6.5

Sanitizer support....................................................................................................... 104
Sanitizer special case lists ........................................................................................ 104
Sanitizer usage on Android ...................................................................................... 106
Sanitizer usage on Linux .......................................................................................... 107
Address Sanitizer...................................................................................................... 108
6.5.1 Usage............................................................................................................... 108
6.5.2 Symbolizing the reports .................................................................................. 109
6.5.3 Additional checks............................................................................................ 110
6.5.4 Issue suppression ............................................................................................ 110
6.5.5 Suppressing memory leaks ............................................................................. 112
6.5.6 Limitations ...................................................................................................... 112
6.5.7 Options............................................................................................................ 112
6.5.8 Notes ............................................................................................................... 113
6.6 Data Flow Sanitizer .................................................................................................. 114
6.6.1 Usage............................................................................................................... 114
6.6.2 ABI list............................................................................................................ 114
6.6.3 Example .......................................................................................................... 116
6.7 Leak Sanitizer........................................................................................................... 117
6.7.1 Usage............................................................................................................... 117
6.8 Memory Sanitizer ..................................................................................................... 118
6.8.1 Usage............................................................................................................... 118
6.8.2 Report symbolization ...................................................................................... 119
6.8.3 Origin tracking ................................................................................................ 120
6.8.4 Use-after-destruction detection ....................................................................... 121
6.8.5 Handling external code ................................................................................... 121
6.8.6 Limitations ...................................................................................................... 121
6.9 Thread Sanitizer........................................................................................................ 122
6.9.1 Usage............................................................................................................... 122
6.9.2 Limitations ...................................................................................................... 124
6.10 Undefined Behavior Sanitizer ................................................................................ 124
6.10.1 Usage............................................................................................................. 125
6.10.2 Available checks ........................................................................................... 126
6.10.3 Stack traces and report symbolization .......................................................... 127
6.10.4 Issue suppression .......................................................................................... 127
6.10.5 Notes ............................................................................................................. 128

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

4

Qualcomm Snapdragon LLVM ARM Compiler for Android

Contents

6.11 LLVM Symbolizer.................................................................................................. 128
6.11.1 Usage............................................................................................................. 129
6.11.2 Options .......................................................................................................... 130
6.12 Control flow integrity ............................................................................................. 130
6.12.1 Configuration ................................................................................................ 131
6.12.2 Usage............................................................................................................. 131
6.12.3 Options.......................................................................................................... 132
6.12.4 Handler functions.......................................................................................... 132
6.12.5 Notes ............................................................................................................. 133
6.13 Static program analysis........................................................................................... 133
6.13.1 Static analyzer............................................................................................... 133
6.13.2 Postprocessor ................................................................................................ 139
6.13.3 Scan-build ..................................................................................................... 140

7 Porting Code from GCC ................................................................... 141
7.1
7.2
7.3
7.4
7.5
7.6
7.7

Command options..................................................................................................... 141
Errors and warnings.................................................................................................. 141
Function declarations................................................................................................ 141
Casting to incompatible types .................................................................................. 142
aligned attribute ........................................................................................................ 143
Reserved registers..................................................................................................... 143
Inline versus extern inline ........................................................................................ 144

8 Coding Practices .............................................................................. 145
8.1
8.2
8.3
8.4

Use int types for loop counters................................................................................. 145
Mark function arguments as restrict (if possible)..................................................... 145
Do not pass or return structs by value ...................................................................... 146
Avoid using inline assembly..................................................................................... 147

9 Language Compatibility................................................................... 148
9.1 C compatibility ......................................................................................................... 148
9.1.1 Differences between various standard modes................................................. 148
9.1.2 GCC extensions not implemented yet............................................................. 149
9.1.3 Intentionally unsupported GCC extensions .................................................... 150
9.1.4 Lvalue casts..................................................................................................... 150
9.1.5 Jumps to within __block variable scope ......................................................... 150
9.1.6 Non-initialization of __block variables .......................................................... 151
9.1.7 Inline assembly ............................................................................................... 151
9.2 C++ compatibility..................................................................................................... 152
9.2.1 Deleted special member functions .................................................................. 152
9.2.2 Variable-length arrays ..................................................................................... 152
9.2.3 Unqualified lookup in templates..................................................................... 153

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

5

Qualcomm Snapdragon LLVM ARM Compiler for Android

9.2.4
9.2.5
9.2.6
9.2.7
9.2.8

Contents

Unqualified lookup into dependent bases of class templates.......................... 156
Incomplete types in templates......................................................................... 157
Templates with no valid instantiations............................................................ 158
Default initialization of const variable of a class type.................................... 159
Parameter name lookup................................................................................... 159

A References........................................................................................ 160
A.1 Related documents................................................................................................... 160

B Acknowledgements ......................................................................... 161

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

6

Qualcomm Snapdragon LLVM ARM Compiler for Android

Tables

Tables
Table 4-1
Table 4-2
Table 4-3
Table 4-4
Table 5-1
Table 5-2
Table 5-3
Table 5-4
Table 5-5
Table 5-6
Table 5-7
Table 6-1
Table 6-2
Table 6-3
Table 6-4
Table 6-5

80-VB419-90 M

Compiler input files ............................................................................. 15
Compiler output files ........................................................................... 16
LLVM 4.0 release – new warnings ...................................................... 31
LLVM 4.0 release – new compiler flags.............................................. 68
Options to use for optimizing code performance................................. 78
Options to use for optimizing code size............................................... 78
SYMPHONY library versions for supported development platforms. 81
Supported loop pragmas ...................................................................... 90
Loop pragma options that enable auto-vectorization........................... 91
Loop pragma option combinations ...................................................... 91
Loop optimization reporting ................................................................ 93
Sanitizer support ................................................................................ 104
Options supported in ASan ................................................................ 113
UBSan checks and option values ....................................................... 126
Options supported in the Symbolizer................................................. 130
CFI options supported in the static compiler ..................................... 132

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

7

1 Introduction

This document is a reference for C/C++ programmers. It describes C and C++ compilers
for the ARM processor architecture. The compilers are based on the LLVM compiler
framework, and are collectively referred to as the LLVM compilers.

1.1

NOTE

The LLVM compilers are commonly referred to as Clang.

NOTE

We strongly recommend trying to use the various LLVM code optimizations
to improve the performance of your program. Using only the default
optimization settings is likely to result in suboptimal performance.

Conventions
Courier font is used for computer text:
int main()
{
printf(“Hello world\n”);
return(0);
}

The following notation is used to define the syntax of functions and commands:

80-VB419-90 M

■

Square brackets enclose optional items (e.g., help [command]).

■

Bold is used to indicate literal symbols (e.g., the brackets in array[index]).

■

The vertical bar character | is used to indicate a choice of items.

■

Parentheses are used to enclose a choice of items (e.g., (on|off)).

■

An ellipsis, ..., follows items that can appear more than once.

■

Italics are used for terms that represent categories of symbols.

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

8

Qualcomm Snapdragon LLVM ARM Compiler for Android

Introduction

Examples:
#define name(parameter1[, parameter2...]) definition
logging (on|off)

Where:
■

#define is a preprocessor directive, and logging is an interactive compiler

command.
■

name represents the name of a defined symbol.

■

parameter1 and parameter2 are macro parameters. The second parameter is
optional because it is enclosed in square brackets. The ellipsis indicates that the
macro accepts more than parameters.

■

on and off are bold to show that they are literal symbols. The vertical bar
between them shows that they are alternative parameters of the logging

command.

1.2

Technical assistance
For assistance or clarification on information in this document, submit a case to
Qualcomm Technologies, Inc. (QTI) at https://createpoint.qti.qualcomm.com/.
If you do not have access to the CDMATech Support website, register for access or send
email to support.cdmatech@qti.qualcomm.com.

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

9

2 Functional Overview

The C and C++ compilers are based on the LLVM compiler framework and are
collectively referred to as the LLVM compilers.

2.1

Features
The LLVM compilers offer the following features:
■

ISO C conformance
Supports the International Standards Organization (ISO) C language standard

■

Compatibility
Supports LLVM extensions and most GCC extensions to simplify porting

■

System library
Supports standard libraries as provided in the Android NDK

■

Processor-specific libraries
Provides library routines that are optimized for the Qualcomm ARM architecture

■

Intrinsics
Provides a mechanism for emitting LLVM assembly instructions in C source code

2.2

Languages
The LLVM compilers support C, C++, and many dialects of those languages:
■

C language: K&R C, ANSI C89, ISO C90, ISO C94 (C89+AMD1),
ISO C99 (+TC1, TC2, TC3)

■

C++ language: C++98, C++11

In addition to these base languages and their dialects, the LLVM compilers support a
broad variety of language extensions. These extensions are provided for compatibility
with the GCC, Microsoft, and other popular compilers, as well as to improve functionality
through the addition of extensions unique to the LLVM compilers.
All language extensions are explicitly recognized as such by the LLVM compilers, and
marked with extension diagnostics which can be mapped to warnings, errors, or simply
ignored.

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

10

Qualcomm Snapdragon LLVM ARM Compiler for Android

2.3

Functional Overview

GCC compatibility
The LLVM compiler driver and language features are intentionally designed to be as
compatible with the GNU GCC compiler as reasonably possible, easing migration from
GCC to LLVM. In most cases, code just works.

2.4

Processor versions
The LLVM compilers can generate code for all versions of the ARM processor
architecture that are supported by the standard LLVM compiler.
ARMv8 supports two instruction set architectures (ISA):
■

AArch64 – the new 64-bit ISA, which supports a larger virtual and physical
address space. All general purpose registers (and many of the system registers) are
64 bits. All instructions are encoded in 32 bits.

■

AArch32 – a 32-bit ISA which incorporates the ARMv7 ISA for both ARM and
Thumb modes, and also includes many aspects of AArch64 (including support for
cryptography and enhanced floating point).

For more information, see the ARMv8-A Reference Manual.

2.5

LLVM versions
The LLVM compilers are based on LLVM 4.0+, as defined at llvm.org.

2.6

C language and compiler references
This document does not describe the following:
■

C or C++ languages

■

Detailed descriptions of the code optimizations performed by LLVM

For suggested references, see Section A.1.

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

11

3 Getting Started

This chapter shows how to build and execute a simple C program using the LLVM
compiler.
The program is built in the Linux environment, and executed directly on ARMv7 or
ARMv8 hardware running Linux.

3.1

NOTE

The Android NDK is assumed to be already installed on your computer. This
includes the tools required for assembling and linking a compiled program.

NOTE

The commands shown in this chapter are for illustration only. For detailed
information on building programs, see Chapter 4.

Create source file
Create the following C source file:
#include 
int main()
{
printf(“Hello world\n”);
return(0);
}

Save the file as hello.c.

3.2

Compile program
Compile the program with the following command:
clang hello.c -o hello

This translates the C source file hello.c into the executable file hello.

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

12

Qualcomm Snapdragon LLVM ARM Compiler for Android

3.3

Getting Started

Execute program
To execute the program, use the following command:
hello

The program outputs its message to the terminal:
Hello world

You have now compiled and executed a C program using the LLVM compiler. For more
information on using the compiler see the following chapter.

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

13

4 Using the Compilers

The LLVM compilers translate C and C++ programs into LLVM processor code.
C and C++ programs are stored in source files, which are text files created with a text
editor. LLVM processor code is stored in object files, which are executable binary files.

4.1

Starting the compilers
To start the C compiler from the command line, enter:
clang [options...] input_files...

To start the C++ compiler from the command line, enter:
clang++ [options...] input_files...

The compilers accept one or more input files on the command line. Input files can be
C/C++ source files or object files. For example:
clang hello.c mylib.c

Command switches are used to control various compiler options (Section 4.3). A switch
consists of a dash character (-) followed by a switch name and optional parameter.
Switches are case-sensitive and must be separated by at least one space. For example:
clang hello.c -o hello

To list the available command options, use the --help option:
clang --help
clang++ --help

This option causes the compiler to display the command line syntax, followed by a list of
the available command options.
NOTE

80-VB419-90 M

clang is the name of the front-end driver for the LLVM compiler framework.

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

14

Qualcomm Snapdragon LLVM ARM Compiler for Android

4.2

Using the Compilers

Input and output files
The LLVM compilers preprocess and compile one or more source files into object files.
The compilers then invoke the linker to combine the object files into an executable file.
Table 4-1 lists the input file types and the tool that processes files of each type. The
compilers use the file name extension to determine how to process the file.
Table 4-1

Compiler input files

Extension

Description

.c

C source file

.i

C preprocessed file

.h

C header file

.cc
.cp

C++ source file

Tool
C compiler

C++ compiler

.cxx
.cpp
.CPP
.c++
.C
.ii

C++ preprocessed file

.h

C++ header file

.hh
.H
.bc
.ll

LLVM intermediate representation (IR) file

C/C++ compiler

.s
.S

Assembly source file

Assembler

Binary object file

Linker

other

All file name extensions are case-sensitive literal strings. Input files with unrecognized
extensions are treated as object files. For more information on LLVM IR files, see
llvm.org.

80-VB419-90 M

MAY CONTAIN U.S. AND INTERNATIONAL EXPORT CONTROLLED INFORMATION

15

Qualcomm Snapdragon LLVM ARM Compiler for Android

Using the Compilers

Table 4-2 lists the output file types and the tools used to generate each file type. Compiler
options (Section 4.3) are used to specify the output file type.
Table 4-2

Compiler output files
File type

4.3

Default
file name

Input files

Executable file

a.out

The specified source files are compiled and
linked to a single executable file.

Object file

file.o

Each specified source file is compiled to a
separate object file (where file is the source
file name).

Assembly source file

file.s

Each specified source file is compiled to a
separate assembly source file (where file is
the source file name).

Preprocessed C/C++ source
file

stdout

The preprocessor output is written to the
standard output.

Compiler options
The LLVM compilers can be controlled by command-line options (Section 4.1). Many of
the GCC options are supported, along with options that are LLVM-specific.
Many of the -f, -m, and -W options can be written in two ways: -f

Source Exif Data:
File Type                       : PDF
File Type Extension             : pdf
MIME Type                       : application/pdf
PDF Version                     : 1.5
Linearized                      : Yes
Create Date                     : 2017:09:01 06:38:55Z
Creator                         : FrameMaker 11.0.2
Modify Date                     : 2017:09:01 06:38:55Z
XMP Toolkit                     : Adobe XMP Core 5.4-c005 78.147326, 2012/08/23-13:03:03
Creator Tool                    : FrameMaker 11.0.2
Format                          : application/pdf
Title                           : _llvm_arm_external.book
Producer                        : Acrobat Distiller 11.0 (Windows)
Document ID                     : uuid:712fb608-a53b-496d-adb9-135c48943ecc
Instance ID                     : uuid:b2ddb1e3-c02b-4777-8a10-6df4a61df32b
Page Mode                       : UseOutlines
Page Count                      : 161
EXIF Metadata provided by EXIF.tools

Navigation menu