|
| 1 | +## Getting Started |
| 2 | + |
| 3 | +This document describes how to get started with the already installed Arm |
| 4 | +Toolchain for Linux. Here you will find out how to use it to compile a source |
| 5 | +code into an executable binary. |
| 6 | + |
| 7 | +### Looking at the toolchain |
| 8 | + |
| 9 | +The default installation site of Arm Toolchain For Linux is the |
| 10 | +`/opt/arm/arm-toolchain-for-linux` directory. It contains a complete set of LLVM |
| 11 | +tooling, header files, compiler libraries and runtime libraries (including the |
| 12 | +OpenMP runtime). The main tools are as follows: |
| 13 | + |
| 14 | +* `armclang` - the C compiler |
| 15 | +* `armclang++` - the C++ compiler |
| 16 | +* `armflang` - the Fortran compiler |
| 17 | + |
| 18 | +Note that the LLVM equivalents of these commands (`clang`, `clang++`, `flang`) |
| 19 | +are also available and functionally identical. |
| 20 | + |
| 21 | +### Configure and load environment modules |
| 22 | + |
| 23 | +After installation, you should be able to invoke any of those commands with |
| 24 | +their absolute paths, e.g.: |
| 25 | + |
| 26 | +``` |
| 27 | +$ /opt/arm/arm-toolchain-for-linux/bin/armclang -print-resource-dir |
| 28 | +/opt/arm/arm-toolchain-for-linux/lib/clang/<version> |
| 29 | +``` |
| 30 | + |
| 31 | +Although fully operable, this is not the most convenient way of using the |
| 32 | +toolchain. Therefore, we recommend using the environment modules. These can be |
| 33 | +installed on most of the existing Linux distributions, as presented below. |
| 34 | + |
| 35 | +#### Ubuntu systems |
| 36 | + |
| 37 | +``` |
| 38 | +$ sudo apt install environment-modules |
| 39 | +``` |
| 40 | + |
| 41 | +#### Red Hat Enterprise Linux and Amazon Linux systems |
| 42 | + |
| 43 | +``` |
| 44 | +$ sudo dnf install environment-modules |
| 45 | +``` |
| 46 | + |
| 47 | +#### SUSE Linux Enterprise Server systems |
| 48 | + |
| 49 | +``` |
| 50 | +$ sudo zypper install environment-modules |
| 51 | +``` |
| 52 | + |
| 53 | +After installing environment modules, you need to execute a profile script which |
| 54 | +matches with your default shell: |
| 55 | + |
| 56 | +#### BASH |
| 57 | + |
| 58 | +``` |
| 59 | +$ source /etc/profile.d/modules.sh |
| 60 | +``` |
| 61 | + |
| 62 | +#### csh or tcsh |
| 63 | + |
| 64 | +``` |
| 65 | +$ source /etc/profile.d/modules.csh |
| 66 | +``` |
| 67 | + |
| 68 | +Use the `module use` command to update your `MODULEPATH` environment variable to |
| 69 | +include the path to the Arm Toolchain For Linux module files directory: |
| 70 | + |
| 71 | +``` |
| 72 | +$ module use /opt/arm/modulefiles |
| 73 | +``` |
| 74 | + |
| 75 | +Alternatively, you can set or amend your `MODULEPATH` environment variable |
| 76 | +manually. Use the `module avail` command to examine the list of available modules. |
| 77 | + |
| 78 | +To load the module for Arm Toolchain for Linux, type: |
| 79 | + |
| 80 | +``` |
| 81 | +$ module load atfl |
| 82 | +``` |
| 83 | + |
| 84 | +This should load the default version of Arm Toolchain for Linux. Alternatively, |
| 85 | +if multiple versions are available, you can load the desired version by |
| 86 | +specifying `atfl/<version>`. |
| 87 | + |
| 88 | +From now on, the toolchain commands should be accessible from the command line: |
| 89 | + |
| 90 | +``` |
| 91 | +$ which armclang |
| 92 | +/opt/arm/arm-toolchain-for-linux/bin/armclang |
| 93 | +
|
| 94 | +$ which armclang++ |
| 95 | +/opt/arm/arm-toolchain-for-linux/bin/armclang++ |
| 96 | +
|
| 97 | +$ which armflang |
| 98 | +/opt/arm/arm-toolchain-for-linux/bin/armflang |
| 99 | +``` |
| 100 | + |
| 101 | +### Using the compiler |
| 102 | + |
| 103 | +#### Example 1: Compile and run example C program |
| 104 | + |
| 105 | +Consider a simple program stored in a `.c` file, for example: `hello.c`: |
| 106 | + |
| 107 | +``` |
| 108 | +#include <stdio.h> |
| 109 | +
|
| 110 | +int main() |
| 111 | +{ |
| 112 | + printf("Hello, World!"); |
| 113 | + return 0; |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +To generate an executable binary, compile your example C program with the |
| 118 | +`armclang` compiler. Specify the input file, `hello.c`, and the binary name |
| 119 | +(using `-o`), `hello`: |
| 120 | + |
| 121 | +``` |
| 122 | +$ armclang -o hello hello.c |
| 123 | +``` |
| 124 | + |
| 125 | +Run the generated binary `hello`: |
| 126 | + |
| 127 | +``` |
| 128 | +$ ./hello |
| 129 | +Hello, World! |
| 130 | +``` |
| 131 | + |
| 132 | +#### Example 2: Compile and run example Fortran program |
| 133 | + |
| 134 | +Consider a simple program stored in a `.f90` file, for example: `hello.f90`: |
| 135 | + |
| 136 | +``` |
| 137 | +program hello |
| 138 | + print *, 'hello world' |
| 139 | +end program |
| 140 | +``` |
| 141 | + |
| 142 | +To generate an executable binary, compile your example Fortran program with the |
| 143 | +`armflang` compiler. Specify the input file, `hello.f90`, and the binary name |
| 144 | +(using `-o`), `hello`: |
| 145 | + |
| 146 | +``` |
| 147 | +$ armflang -o hello hello.f90 |
| 148 | +``` |
| 149 | + |
| 150 | +Run the generated binary `hello`: |
| 151 | + |
| 152 | +``` |
| 153 | +$ ./hello |
| 154 | + hello world |
| 155 | +``` |
| 156 | + |
| 157 | +### Compile and link C/C++ programs |
| 158 | + |
| 159 | +To generate an executable binary, compile your source file (for example, |
| 160 | +`source.c`) with the `armclang` command: |
| 161 | + |
| 162 | +``` |
| 163 | +$ armclang source.c |
| 164 | +``` |
| 165 | + |
| 166 | +A binary with the filename `a.out` is the output. |
| 167 | + |
| 168 | +Optionally, use the `-o` option to set the binary filename (for example, `binary`): |
| 169 | + |
| 170 | +``` |
| 171 | +$ armclang -o binary source.c |
| 172 | +``` |
| 173 | + |
| 174 | +You can specify multiple source files on a single line. Each source file is |
| 175 | +compiled individually and then linked into a single executable binary. For |
| 176 | +example, to compile the source files `source1.c` and `source2.c`, use: |
| 177 | + |
| 178 | +``` |
| 179 | +$ armclang -o binary source1.c source2.c |
| 180 | +``` |
| 181 | + |
| 182 | +To compile each of your source files individually into an object (`.o`) file, |
| 183 | +specify the compile-only option, `-c`, and then pass the resulting object files |
| 184 | +into another invocation of `armclang` to link them into an executable binary: |
| 185 | + |
| 186 | +``` |
| 187 | +$ armclang -c source1.c |
| 188 | +
|
| 189 | +$ armclang -c source2.c |
| 190 | +
|
| 191 | +$ armclang -o binary source1.o source2.o |
| 192 | +``` |
| 193 | + |
| 194 | +#### Note |
| 195 | + |
| 196 | +For the C/C++ compiler's command line arguments reference visit this page: |
| 197 | +[Clang command line argument reference](https://releases.llvm.org/20.1.0/tools/clang/docs/ClangCommandLineReference.html). |
| 198 | + |
| 199 | +### Compile and link Fortran programs |
| 200 | + |
| 201 | +To generate an executable binary, compile your source file (for example, |
| 202 | +`source.f90`) with the `armflang` command: |
| 203 | + |
| 204 | +``` |
| 205 | +$ armflang source.f90 |
| 206 | +``` |
| 207 | + |
| 208 | +A binary with the filename `a.out` is the output. |
| 209 | + |
| 210 | +You can specify multiple source files on a single line. Each source file is |
| 211 | +compiled individually and then linked into a single executable binary. For |
| 212 | +example, to compile the source files `source1.f90` and `source2.f90`, use: |
| 213 | + |
| 214 | +``` |
| 215 | +$ armflang -o binary source1.f90 source2.f90 |
| 216 | +``` |
| 217 | + |
| 218 | +To compile each of your source files individually into an object (`.o`) file, |
| 219 | +specify the compile-only option, `-c`, and then pass the resulting object files |
| 220 | +into another invocation of `armflang` to link them into an executable binary: |
| 221 | + |
| 222 | +``` |
| 223 | +$ armflang -c source1.f90 |
| 224 | +
|
| 225 | +$ armflang -c source2.f90 |
| 226 | +
|
| 227 | +$ armflang -o binary source1.o source2.o |
| 228 | +``` |
| 229 | + |
| 230 | +#### Note |
| 231 | + |
| 232 | +For the Fortran compiler's command line arguments reference visit this page: |
| 233 | +[Flang command line argument reference](https://flang.llvm.org/docs/FlangCommandLineReference.html). |
| 234 | + |
| 235 | +### Increase the optimization level |
| 236 | + |
| 237 | +To control the optimization level, specify the `-O<level>` option on your |
| 238 | +compile line, and replace `<level>` with one of `0`, `1`, `2` or `3`. The `-O0` |
| 239 | +option is the lowest, and the default, optimization level. `-O3` is the highest |
| 240 | +optimization level. Arm compilers performs auto-vectorization at level `-O2` and |
| 241 | +above. |
| 242 | + |
| 243 | +For example, to compile the `source.c` file into a binary called `binary`, and |
| 244 | +use the `-O3` optimization level, use: |
| 245 | + |
| 246 | +``` |
| 247 | +$ armclang -O3 -o binary source.c |
| 248 | +``` |
| 249 | + |
| 250 | +To compile the `source.f90` file into a binary called binary, and use the `-O3` |
| 251 | +optimization level, use: |
| 252 | + |
| 253 | +``` |
| 254 | +$ armflang -O3 -o binary source.f90 |
| 255 | +``` |
| 256 | + |
| 257 | +#### Note |
| 258 | + |
| 259 | +Similarly to other compilers, the Arm Toolchain for Linux C and C++ compilers |
| 260 | +can also be supplied with the `-Ofast option`. This will however result in |
| 261 | +displaying the following deprecation warning: |
| 262 | + |
| 263 | +``` |
| 264 | +warning: argument '-Ofast' is deprecated; use '-O3 -ffast-math' for the same behavior, or '-O3' to enable only conforming optimizations [-Wdeprecated-ofast] |
| 265 | +``` |
| 266 | + |
| 267 | +As the warning message states, the effect of applying the `-Ofast` option when |
| 268 | +compiling the C/C++ programs can be achieved by using the `-O3 -ffast-math` |
| 269 | +options instead. |
| 270 | + |
| 271 | +In case of Fortran, the use of the `-Ofast` option triggers the following |
| 272 | +deprecation warning: |
| 273 | + |
| 274 | +``` |
| 275 | +warning: argument '-Ofast' is deprecated; use '-O3 -ffast-math -fstack-arrays' for the same behavior, or '-O3 -fstack-arrays' to enable only conforming optimizations [-Wdeprecated-ofast] |
| 276 | +``` |
| 277 | + |
| 278 | +As the warning message states, the effect of applying the `-Ofast` option when |
| 279 | +compiling Fortram programs can be achieved by using the |
| 280 | +`-O3 -ffast-math -fstack-arrays` options instead. |
| 281 | + |
| 282 | +### Compile and optimize using CPU auto-detection |
| 283 | + |
| 284 | +If you tell the compiler what target CPU your application will run on, it can |
| 285 | +make target-specific optimization decisions. Target-specific optimization |
| 286 | +decisions help ensure your application runs as efficiently as possible. To tell |
| 287 | +the compiler to make target-specific compilation decisions, use the |
| 288 | +`-mcpu=<target>` option and replace `<target>` with your target processor (from |
| 289 | +a supported list of targets). |
| 290 | + |
| 291 | +In addition, the `-mcpu` option also supports the `native` argument. |
| 292 | +`-mcpu=native` enables the compiler to auto-detect the architecture and |
| 293 | +processor type of the CPU that you are running the compiler on. |
| 294 | + |
| 295 | +For example, to auto-detect the target CPU and optimize your C application for |
| 296 | +this target, use: |
| 297 | + |
| 298 | +``` |
| 299 | +$ armclang -O3 -mcpu=native -o binary source.c |
| 300 | +``` |
| 301 | + |
| 302 | +To auto-detect the target CPU and optimize your Fortran application for this |
| 303 | +target, use: |
| 304 | + |
| 305 | +``` |
| 306 | +$ armflang -O3 -mcpu=native -o binary source.f90 |
| 307 | +``` |
| 308 | + |
| 309 | +The `-mcpu` option supports a range of Armv8-A-based Systems-on-Chips (SoCs). |
| 310 | +When `-mcpu` is not specified, by default, `-mcpu=generic` is set, which |
| 311 | +generates portable output suitable for any Armv8-A-based target. |
| 312 | + |
| 313 | +#### Note |
| 314 | + |
| 315 | +* The optimizations that are performed from setting the `-mcpu` option (also |
| 316 | + known as hardware, or CPU, tuning) are independent of the optimizations that |
| 317 | + are performed from setting the `-O<level>` option. |
| 318 | + |
| 319 | +* If you run the compiler on one target, but will run the application you are |
| 320 | + compiling on a different target, do not use `-mcpu=native`. Instead, use |
| 321 | + `-mcpu=<target>` where `<target>` is the target processor that you will run |
| 322 | + the application on. |
0 commit comments