81 lines · cpp
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-exception6//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#else17#define MLIR_ARMRUNNERUTILS_EXPORTED __attribute__((visibility("default")))18#endif19 20#ifdef __linux__21#include <sys/prctl.h>22#endif23 24extern "C" {25 26// Defines for prctl() calls. These may not necessarily exist in the host27// <sys/prctl.h>, but will still be useable under emulation.28//29// https://www.kernel.org/doc/html/v5.3/arm64/sve.html#prctl-extensions30#ifndef PR_SVE_SET_VL31#define PR_SVE_SET_VL 5032#endif33// https://docs.kernel.org/arch/arm64/sme.html#prctl-extensions34#ifndef PR_SME_SET_VL35#define PR_SME_SET_VL 6336#endif37// Note: This mask is the same as both PR_SME_VL_LEN_MASK and38// PR_SVE_VL_LEN_MASK.39#define PR_VL_LEN_MASK 0xffff40 41/// Sets the vector length (streaming or not, as indicated by `option`) to42/// `bits`.43///44/// Caveat emptor: If a function has allocated stack slots for SVE registers45/// (e.g. slots for callee-saved SVE registers or spill slots) changing46/// the vector length is tricky and error prone - it may cause incorrect stack47/// deallocation or incorrect access to stack slots.48///49/// The recommended strategy is to call `setArmVectorLength` only from functions50/// that do not access SVE registers, either by themselves or by inlining other51/// functions.52static 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 (" << bits57 << "-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#else66 std::cerr << "[error] " << helper_name << " is unsupported" << std::endl;67 abort();68#endif69}70 71/// Sets the SVE vector length (in bits) to `bits`.72void MLIR_ARMRUNNERUTILS_EXPORTED setArmVLBits(uint32_t bits) {73 setArmVectorLength(__func__, PR_SVE_SET_VL, bits);74}75 76/// Sets the SME streaming vector length (in bits) to `bits`.77void MLIR_ARMRUNNERUTILS_EXPORTED setArmSVLBits(uint32_t bits) {78 setArmVectorLength(__func__, PR_SME_SET_VL, bits);79}80}81