MLIR  22.0.0git
ArmRunnerUtils.cpp
Go to the documentation of this file.
1 //===- ArmRunnerUtils.cpp - Utilities for configuring architecture properties //
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/MathExtras.h"
10 #include <iostream>
11 #include <stdint.h>
12 #include <string_view>
13 
14 #if (defined(_WIN32) || defined(__CYGWIN__))
15 #define MLIR_ARMRUNNERUTILS_EXPORTED __declspec(dllexport)
16 #else
17 #define MLIR_ARMRUNNERUTILS_EXPORTED __attribute__((visibility("default")))
18 #endif
19 
20 #ifdef __linux__
21 #include <sys/prctl.h>
22 #endif
23 
24 extern "C" {
25 
26 // Defines for prctl() calls. These may not necessarily exist in the host
27 // <sys/prctl.h>, but will still be useable under emulation.
28 //
29 // https://www.kernel.org/doc/html/v5.3/arm64/sve.html#prctl-extensions
30 #ifndef PR_SVE_SET_VL
31 #define PR_SVE_SET_VL 50
32 #endif
33 // https://docs.kernel.org/arch/arm64/sme.html#prctl-extensions
34 #ifndef PR_SME_SET_VL
35 #define PR_SME_SET_VL 63
36 #endif
37 // Note: This mask is the same as both PR_SME_VL_LEN_MASK and
38 // PR_SVE_VL_LEN_MASK.
39 #define PR_VL_LEN_MASK 0xffff
40 
41 /// Sets the vector length (streaming or not, as indicated by `option`) to
42 /// `bits`.
43 ///
44 /// Caveat emptor: If a function has allocated stack slots for SVE registers
45 /// (e.g. slots for callee-saved SVE registers or spill slots) changing
46 /// the vector length is tricky and error prone - it may cause incorrect stack
47 /// deallocation or incorrect access to stack slots.
48 ///
49 /// The recommended strategy is to call `setArmVectorLength` only from functions
50 /// that do not access SVE registers, either by themselves or by inlining other
51 /// functions.
52 static void setArmVectorLength(std::string_view helper_name, int option,
53  uint32_t bits) {
54 #if defined(__linux__) && defined(__aarch64__)
55  if (bits < 128 || bits > 2048 || !llvm::isPowerOf2_32(bits)) {
56  std::cerr << "[error] Attempted to set an invalid vector length (" << bits
57  << "-bit)" << std::endl;
58  abort();
59  }
60  uint32_t vl = bits / 8;
61  if (auto ret = prctl(option, vl & PR_VL_LEN_MASK); ret < 0) {
62  std::cerr << "[error] prctl failed (" << ret << ")" << std::endl;
63  abort();
64  }
65 #else
66  std::cerr << "[error] " << helper_name << " is unsupported" << std::endl;
67  abort();
68 #endif
69 }
70 
71 /// Sets the SVE vector length (in bits) to `bits`.
73  setArmVectorLength(__func__, PR_SVE_SET_VL, bits);
74 }
75 
76 /// Sets the SME streaming vector length (in bits) to `bits`.
78  setArmVectorLength(__func__, PR_SME_SET_VL, bits);
79 }
80 }
#define MLIR_ARMRUNNERUTILS_EXPORTED
#define PR_SME_SET_VL
#define PR_SVE_SET_VL
void MLIR_ARMRUNNERUTILS_EXPORTED setArmSVLBits(uint32_t bits)
Sets the SME streaming vector length (in bits) to bits.
void MLIR_ARMRUNNERUTILS_EXPORTED setArmVLBits(uint32_t bits)
Sets the SVE vector length (in bits) to bits.
#define PR_VL_LEN_MASK
static void setArmVectorLength(std::string_view helper_name, int option, uint32_t bits)
Sets the vector length (streaming or not, as indicated by option) to bits.