Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright OpenEmbedded Contributors |
| 3 | # |
| 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
| 6 | |
| 7 | # Right now this is focused on arm-specific tune features. |
| 8 | # We get away with this for now as one can only use x86-64 as the build host |
| 9 | # (not arm). |
| 10 | # Note that TUNE_FEATURES is _always_ refering to the target, so we really |
| 11 | # don't want to use this for the host/build. |
| 12 | def llvm_features_from_tune(d): |
| 13 | f = [] |
| 14 | feat = d.getVar('TUNE_FEATURES') |
| 15 | if not feat: |
| 16 | return [] |
| 17 | feat = frozenset(feat.split()) |
| 18 | |
| 19 | mach_overrides = d.getVar('MACHINEOVERRIDES') |
| 20 | mach_overrides = frozenset(mach_overrides.split(':')) |
| 21 | |
| 22 | if 'vfpv4' in feat: |
| 23 | f.append("+vfp4") |
| 24 | if 'vfpv3' in feat: |
| 25 | f.append("+vfp3") |
| 26 | if 'vfpv3d16' in feat: |
| 27 | f.append("+d16") |
| 28 | |
| 29 | if 'vfpv2' in feat or 'vfp' in feat: |
| 30 | f.append("+vfp2") |
| 31 | |
| 32 | if 'neon' in feat: |
| 33 | f.append("+neon") |
| 34 | |
| 35 | if 'mips32' in feat: |
| 36 | f.append("+mips32") |
| 37 | |
| 38 | if 'mips32r2' in feat: |
| 39 | f.append("+mips32r2") |
| 40 | |
| 41 | if target_is_armv7(d): |
| 42 | f.append('+v7') |
| 43 | |
| 44 | if ('armv6' in mach_overrides) or ('armv6' in feat): |
| 45 | f.append("+v6") |
| 46 | if 'armv5te' in feat: |
| 47 | f.append("+strict-align") |
| 48 | f.append("+v5te") |
| 49 | elif 'armv5' in feat: |
| 50 | f.append("+strict-align") |
| 51 | f.append("+v5") |
| 52 | |
| 53 | if ('armv4' in mach_overrides) or ('armv4' in feat): |
| 54 | f.append("+strict-align") |
| 55 | |
| 56 | if 'dsp' in feat: |
| 57 | f.append("+dsp") |
| 58 | |
| 59 | if 'thumb' in feat: |
| 60 | if d.getVar('ARM_THUMB_OPT') == "thumb": |
| 61 | if target_is_armv7(d): |
| 62 | f.append('+thumb2') |
| 63 | f.append("+thumb-mode") |
| 64 | |
| 65 | if 'cortexa5' in feat: |
| 66 | f.append("+a5") |
| 67 | if 'cortexa7' in feat: |
| 68 | f.append("+a7") |
| 69 | if 'cortexa9' in feat: |
| 70 | f.append("+a9") |
| 71 | if 'cortexa15' in feat: |
| 72 | f.append("+a15") |
| 73 | if 'cortexa17' in feat: |
| 74 | f.append("+a17") |
| 75 | if ('riscv64' in feat) or ('riscv32' in feat): |
| 76 | f.append("+a,+c,+d,+f,+m") |
| 77 | return f |
| 78 | llvm_features_from_tune[vardepvalue] = "${@llvm_features_from_tune(d)}" |
| 79 | |
| 80 | # TARGET_CC_ARCH changes from build/cross/target so it'll do the right thing |
| 81 | # this should go away when https://github.com/rust-lang/rust/pull/31709 is |
| 82 | # stable (1.9.0?) |
| 83 | def llvm_features_from_cc_arch(d): |
| 84 | f = [] |
| 85 | feat = d.getVar('TARGET_CC_ARCH') |
| 86 | if not feat: |
| 87 | return [] |
| 88 | feat = frozenset(feat.split()) |
| 89 | |
| 90 | if '-mmmx' in feat: |
| 91 | f.append("+mmx") |
| 92 | if '-msse' in feat: |
| 93 | f.append("+sse") |
| 94 | if '-msse2' in feat: |
| 95 | f.append("+sse2") |
| 96 | if '-msse3' in feat: |
| 97 | f.append("+sse3") |
| 98 | if '-mssse3' in feat: |
| 99 | f.append("+ssse3") |
| 100 | if '-msse4.1' in feat: |
| 101 | f.append("+sse4.1") |
| 102 | if '-msse4.2' in feat: |
| 103 | f.append("+sse4.2") |
| 104 | if '-msse4a' in feat: |
| 105 | f.append("+sse4a") |
| 106 | if '-mavx' in feat: |
| 107 | f.append("+avx") |
| 108 | if '-mavx2' in feat: |
| 109 | f.append("+avx2") |
| 110 | |
| 111 | return f |
| 112 | |
| 113 | def llvm_features_from_target_fpu(d): |
| 114 | # TARGET_FPU can be hard or soft. +soft-float tell llvm to use soft float |
| 115 | # ABI. There is no option for hard. |
| 116 | |
| 117 | fpu = d.getVar('TARGET_FPU', True) |
| 118 | return ["+soft-float"] if fpu == "soft" else [] |
| 119 | |
| 120 | def llvm_features(d): |
| 121 | return ','.join(llvm_features_from_tune(d) + |
| 122 | llvm_features_from_cc_arch(d) + |
| 123 | llvm_features_from_target_fpu(d)) |
| 124 | |
| 125 | llvm_features[vardepvalue] = "${@llvm_features(d)}" |
| 126 | |
| 127 | ## arm-unknown-linux-gnueabihf |
| 128 | DATA_LAYOUT[arm-eabi] = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" |
| 129 | TARGET_ENDIAN[arm-eabi] = "little" |
| 130 | TARGET_POINTER_WIDTH[arm-eabi] = "32" |
| 131 | TARGET_C_INT_WIDTH[arm-eabi] = "32" |
| 132 | MAX_ATOMIC_WIDTH[arm-eabi] = "64" |
| 133 | FEATURES[arm-eabi] = "+v6,+vfp2" |
| 134 | |
| 135 | ## armv7-unknown-linux-gnueabihf |
| 136 | DATA_LAYOUT[armv7-eabi] = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" |
| 137 | TARGET_ENDIAN[armv7-eabi] = "little" |
| 138 | TARGET_POINTER_WIDTH[armv7-eabi] = "32" |
| 139 | TARGET_C_INT_WIDTH[armv7-eabi] = "32" |
| 140 | MAX_ATOMIC_WIDTH[armv7-eabi] = "64" |
| 141 | FEATURES[armv7-eabi] = "+v7,+vfp2,+thumb2" |
| 142 | |
| 143 | ## aarch64-unknown-linux-{gnu, musl} |
| 144 | DATA_LAYOUT[aarch64] = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" |
| 145 | TARGET_ENDIAN[aarch64] = "little" |
| 146 | TARGET_POINTER_WIDTH[aarch64] = "64" |
| 147 | TARGET_C_INT_WIDTH[aarch64] = "32" |
| 148 | MAX_ATOMIC_WIDTH[aarch64] = "128" |
| 149 | |
| 150 | ## x86_64-unknown-linux-{gnu, musl} |
| 151 | DATA_LAYOUT[x86_64] = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" |
| 152 | TARGET_ENDIAN[x86_64] = "little" |
| 153 | TARGET_POINTER_WIDTH[x86_64] = "64" |
| 154 | TARGET_C_INT_WIDTH[x86_64] = "32" |
| 155 | MAX_ATOMIC_WIDTH[x86_64] = "64" |
| 156 | |
| 157 | ## x86_64-unknown-linux-gnux32 |
| 158 | DATA_LAYOUT[x86_64-x32] = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" |
| 159 | TARGET_ENDIAN[x86_64-x32] = "little" |
| 160 | TARGET_POINTER_WIDTH[x86_64-x32] = "32" |
| 161 | TARGET_C_INT_WIDTH[x86_64-x32] = "32" |
| 162 | MAX_ATOMIC_WIDTH[x86_64-x32] = "64" |
| 163 | |
| 164 | ## i686-unknown-linux-{gnu, musl} |
| 165 | DATA_LAYOUT[i686] = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128" |
| 166 | TARGET_ENDIAN[i686] = "little" |
| 167 | TARGET_POINTER_WIDTH[i686] = "32" |
| 168 | TARGET_C_INT_WIDTH[i686] = "32" |
| 169 | MAX_ATOMIC_WIDTH[i686] = "64" |
| 170 | |
| 171 | ## XXX: a bit of a hack so qemux86 builds, clone of i686-unknown-linux-{gnu, musl} above |
| 172 | DATA_LAYOUT[i586] = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128" |
| 173 | TARGET_ENDIAN[i586] = "little" |
| 174 | TARGET_POINTER_WIDTH[i586] = "32" |
| 175 | TARGET_C_INT_WIDTH[i586] = "32" |
| 176 | MAX_ATOMIC_WIDTH[i586] = "64" |
| 177 | |
| 178 | ## mips-unknown-linux-{gnu, musl} |
| 179 | DATA_LAYOUT[mips] = "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64" |
| 180 | TARGET_ENDIAN[mips] = "big" |
| 181 | TARGET_POINTER_WIDTH[mips] = "32" |
| 182 | TARGET_C_INT_WIDTH[mips] = "32" |
| 183 | MAX_ATOMIC_WIDTH[mips] = "32" |
| 184 | |
| 185 | ## mipsel-unknown-linux-{gnu, musl} |
| 186 | DATA_LAYOUT[mipsel] = "e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64" |
| 187 | TARGET_ENDIAN[mipsel] = "little" |
| 188 | TARGET_POINTER_WIDTH[mipsel] = "32" |
| 189 | TARGET_C_INT_WIDTH[mipsel] = "32" |
| 190 | MAX_ATOMIC_WIDTH[mipsel] = "32" |
| 191 | |
| 192 | ## mips64-unknown-linux-{gnu, musl} |
| 193 | DATA_LAYOUT[mips64] = "E-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128" |
| 194 | TARGET_ENDIAN[mips64] = "big" |
| 195 | TARGET_POINTER_WIDTH[mips64] = "64" |
| 196 | TARGET_C_INT_WIDTH[mips64] = "64" |
| 197 | MAX_ATOMIC_WIDTH[mips64] = "64" |
| 198 | |
| 199 | ## mips64-n32-unknown-linux-{gnu, musl} |
| 200 | DATA_LAYOUT[mips64-n32] = "E-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32:64-S128" |
| 201 | TARGET_ENDIAN[mips64-n32] = "big" |
| 202 | TARGET_POINTER_WIDTH[mips64-n32] = "32" |
| 203 | TARGET_C_INT_WIDTH[mips64-n32] = "32" |
| 204 | MAX_ATOMIC_WIDTH[mips64-n32] = "64" |
| 205 | |
| 206 | ## mips64el-unknown-linux-{gnu, musl} |
| 207 | DATA_LAYOUT[mips64el] = "e-m:e-i8:8:32-i16:16:32-i64:64-n32:64-S128" |
| 208 | TARGET_ENDIAN[mips64el] = "little" |
| 209 | TARGET_POINTER_WIDTH[mips64el] = "64" |
| 210 | TARGET_C_INT_WIDTH[mips64el] = "64" |
| 211 | MAX_ATOMIC_WIDTH[mips64el] = "64" |
| 212 | |
| 213 | ## powerpc-unknown-linux-{gnu, musl} |
| 214 | DATA_LAYOUT[powerpc] = "E-m:e-p:32:32-i64:64-n32" |
| 215 | TARGET_ENDIAN[powerpc] = "big" |
| 216 | TARGET_POINTER_WIDTH[powerpc] = "32" |
| 217 | TARGET_C_INT_WIDTH[powerpc] = "32" |
| 218 | MAX_ATOMIC_WIDTH[powerpc] = "32" |
| 219 | |
| 220 | ## powerpc64-unknown-linux-{gnu, musl} |
| 221 | DATA_LAYOUT[powerpc64] = "E-m:e-i64:64-n32:64-S128-v256:256:256-v512:512:512" |
| 222 | TARGET_ENDIAN[powerpc64] = "big" |
| 223 | TARGET_POINTER_WIDTH[powerpc64] = "64" |
| 224 | TARGET_C_INT_WIDTH[powerpc64] = "64" |
| 225 | MAX_ATOMIC_WIDTH[powerpc64] = "64" |
| 226 | |
| 227 | ## powerpc64le-unknown-linux-{gnu, musl} |
| 228 | DATA_LAYOUT[powerpc64le] = "e-m:e-i64:64-n32:64-v256:256:256-v512:512:512" |
| 229 | TARGET_ENDIAN[powerpc64le] = "little" |
| 230 | TARGET_POINTER_WIDTH[powerpc64le] = "64" |
| 231 | TARGET_C_INT_WIDTH[powerpc64le] = "64" |
| 232 | MAX_ATOMIC_WIDTH[powerpc64le] = "64" |
| 233 | |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame^] | 234 | ## riscv32gc-unknown-linux-{gnu, musl} |
| 235 | DATA_LAYOUT[riscv32gc] = "e-m:e-p:32:32-i64:64-n32-S128" |
| 236 | TARGET_ENDIAN[riscv32gc] = "little" |
| 237 | TARGET_POINTER_WIDTH[riscv32gc] = "32" |
| 238 | TARGET_C_INT_WIDTH[riscv32gc] = "32" |
| 239 | MAX_ATOMIC_WIDTH[riscv32gc] = "32" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 240 | |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame^] | 241 | ## riscv64gc-unknown-linux-{gnu, musl} |
| 242 | DATA_LAYOUT[riscv64gc] = "e-m:e-p:64:64-i64:64-i128:128-n64-S128" |
| 243 | TARGET_ENDIAN[riscv64gc] = "little" |
| 244 | TARGET_POINTER_WIDTH[riscv64gc] = "64" |
| 245 | TARGET_C_INT_WIDTH[riscv64gc] = "64" |
| 246 | MAX_ATOMIC_WIDTH[riscv64gc] = "64" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 247 | |
| 248 | # Convert a normal arch (HOST_ARCH, TARGET_ARCH, BUILD_ARCH, etc) to something |
| 249 | # rust's internals won't choke on. |
| 250 | def arch_to_rust_target_arch(arch): |
| 251 | if arch == "i586" or arch == "i686": |
| 252 | return "x86" |
| 253 | elif arch == "mipsel": |
| 254 | return "mips" |
| 255 | elif arch == "mip64sel": |
| 256 | return "mips64" |
| 257 | elif arch == "armv7": |
| 258 | return "arm" |
| 259 | elif arch == "powerpc64le": |
| 260 | return "powerpc64" |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame^] | 261 | elif arch == "riscv32gc": |
| 262 | return "riscv32" |
| 263 | elif arch == "riscv64gc": |
| 264 | return "riscv64" |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 265 | else: |
| 266 | return arch |
| 267 | |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame^] | 268 | # Convert a rust target string to a llvm-compatible triplet |
| 269 | def rust_sys_to_llvm_target(sys): |
| 270 | if sys.startswith('riscv32gc-'): |
| 271 | return sys.replace('riscv32gc-', 'riscv32-', 1) |
| 272 | if sys.startswith('riscv64gc-'): |
| 273 | return sys.replace('riscv64gc-', 'riscv64-', 1) |
| 274 | return sys |
| 275 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 276 | # generates our target CPU value |
| 277 | def llvm_cpu(d): |
| 278 | cpu = d.getVar('PACKAGE_ARCH') |
| 279 | target = d.getVar('TRANSLATED_TARGET_ARCH') |
| 280 | |
| 281 | trans = {} |
| 282 | trans['corei7-64'] = "corei7" |
| 283 | trans['core2-32'] = "core2" |
| 284 | trans['x86-64'] = "x86-64" |
| 285 | trans['i686'] = "i686" |
| 286 | trans['i586'] = "i586" |
| 287 | trans['mips64'] = "mips64" |
| 288 | trans['mips64el'] = "mips64" |
| 289 | trans['riscv64'] = "generic-rv64" |
| 290 | trans['riscv32'] = "generic-rv32" |
| 291 | |
| 292 | if target in ["mips", "mipsel", "powerpc"]: |
| 293 | feat = frozenset(d.getVar('TUNE_FEATURES').split()) |
| 294 | if "mips32r2" in feat: |
| 295 | trans['mipsel'] = "mips32r2" |
| 296 | trans['mips'] = "mips32r2" |
| 297 | elif "mips32" in feat: |
| 298 | trans['mipsel'] = "mips32" |
| 299 | trans['mips'] = "mips32" |
| 300 | elif "ppc7400" in feat: |
| 301 | trans['powerpc'] = "7400" |
| 302 | |
| 303 | try: |
| 304 | return trans[cpu] |
| 305 | except: |
| 306 | return trans.get(target, "generic") |
| 307 | |
| 308 | llvm_cpu[vardepvalue] = "${@llvm_cpu(d)}" |
| 309 | |
| 310 | def rust_gen_target(d, thing, wd, arch): |
| 311 | import json |
| 312 | |
| 313 | build_sys = d.getVar('BUILD_SYS') |
| 314 | target_sys = d.getVar('TARGET_SYS') |
| 315 | |
| 316 | sys = d.getVar('{}_SYS'.format(thing)) |
| 317 | prefix = d.getVar('{}_PREFIX'.format(thing)) |
| 318 | rustsys = d.getVar('RUST_{}_SYS'.format(thing)) |
| 319 | |
| 320 | abi = None |
| 321 | cpu = "generic" |
| 322 | features = "" |
| 323 | |
| 324 | # Need to apply the target tuning consitently, only if the triplet applies to the target |
| 325 | # and not in the native case |
| 326 | if sys == target_sys and sys != build_sys: |
| 327 | abi = d.getVar('ABIEXTENSION') |
| 328 | cpu = llvm_cpu(d) |
| 329 | if bb.data.inherits_class('native', d): |
| 330 | features = ','.join(llvm_features_from_cc_arch(d)) |
| 331 | else: |
| 332 | features = llvm_features(d) or "" |
| 333 | # arm and armv7 have different targets in llvm |
| 334 | if arch == "arm" and target_is_armv7(d): |
| 335 | arch = 'armv7' |
| 336 | |
| 337 | rust_arch = oe.rust.arch_to_rust_arch(arch) |
| 338 | |
| 339 | if abi: |
| 340 | arch_abi = "{}-{}".format(rust_arch, abi) |
| 341 | else: |
| 342 | arch_abi = rust_arch |
| 343 | |
| 344 | features = features or d.getVarFlag('FEATURES', arch_abi) or "" |
| 345 | features = features.strip() |
| 346 | |
| 347 | # build tspec |
| 348 | tspec = {} |
Patrick Williams | 2390b1b | 2022-11-03 13:47:49 -0500 | [diff] [blame^] | 349 | tspec['llvm-target'] = rust_sys_to_llvm_target(rustsys) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 350 | tspec['data-layout'] = d.getVarFlag('DATA_LAYOUT', arch_abi) |
| 351 | if tspec['data-layout'] is None: |
| 352 | bb.fatal("No rust target defined for %s" % arch_abi) |
| 353 | tspec['max-atomic-width'] = int(d.getVarFlag('MAX_ATOMIC_WIDTH', arch_abi)) |
| 354 | tspec['target-pointer-width'] = d.getVarFlag('TARGET_POINTER_WIDTH', arch_abi) |
| 355 | tspec['target-c-int-width'] = d.getVarFlag('TARGET_C_INT_WIDTH', arch_abi) |
| 356 | tspec['target-endian'] = d.getVarFlag('TARGET_ENDIAN', arch_abi) |
| 357 | tspec['arch'] = arch_to_rust_target_arch(rust_arch) |
| 358 | tspec['os'] = "linux" |
| 359 | if "musl" in tspec['llvm-target']: |
| 360 | tspec['env'] = "musl" |
| 361 | else: |
| 362 | tspec['env'] = "gnu" |
| 363 | if "riscv64" in tspec['llvm-target']: |
| 364 | tspec['llvm-abiname'] = "lp64d" |
| 365 | if "riscv32" in tspec['llvm-target']: |
| 366 | tspec['llvm-abiname'] = "ilp32d" |
| 367 | tspec['vendor'] = "unknown" |
| 368 | tspec['target-family'] = "unix" |
| 369 | tspec['linker'] = "{}{}gcc".format(d.getVar('CCACHE'), prefix) |
| 370 | tspec['cpu'] = cpu |
| 371 | if features != "": |
| 372 | tspec['features'] = features |
| 373 | tspec['dynamic-linking'] = True |
| 374 | tspec['executables'] = True |
| 375 | tspec['linker-is-gnu'] = True |
| 376 | tspec['linker-flavor'] = "gcc" |
| 377 | tspec['has-rpath'] = True |
| 378 | tspec['position-independent-executables'] = True |
| 379 | tspec['panic-strategy'] = d.getVar("RUST_PANIC_STRATEGY") |
| 380 | |
| 381 | # write out the target spec json file |
| 382 | with open(wd + rustsys + '.json', 'w') as f: |
| 383 | json.dump(tspec, f, indent=4) |
| 384 | |
| 385 | # These are accounted for in tmpdir path names so don't need to be in the task sig |
| 386 | rust_gen_target[vardepsexclude] += "ABIEXTENSION llvm_cpu" |
| 387 | |
| 388 | do_rust_gen_targets[vardeps] += "DATA_LAYOUT TARGET_ENDIAN TARGET_POINTER_WIDTH TARGET_C_INT_WIDTH MAX_ATOMIC_WIDTH FEATURES" |
| 389 | |
| 390 | RUST_TARGETS_DIR = "${WORKDIR}/rust-targets/" |
| 391 | export RUST_TARGET_PATH = "${RUST_TARGETS_DIR}" |
| 392 | |
| 393 | python do_rust_gen_targets () { |
| 394 | wd = d.getVar('RUST_TARGETS_DIR') |
| 395 | # Order of BUILD, HOST, TARGET is important in case the files overwrite, most specific last |
| 396 | rust_gen_target(d, 'BUILD', wd, d.getVar('BUILD_ARCH')) |
| 397 | rust_gen_target(d, 'HOST', wd, d.getVar('HOST_ARCH')) |
| 398 | rust_gen_target(d, 'TARGET', wd, d.getVar('TARGET_ARCH')) |
| 399 | } |
| 400 | |
| 401 | addtask rust_gen_targets after do_patch before do_compile |
| 402 | do_rust_gen_targets[dirs] += "${RUST_TARGETS_DIR}" |
| 403 | |