| 1 | //===-- AArch64SMEAttributes.cpp - Helper for interpreting SME attributes -===// |
| 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 "AArch64SMEAttributes.h" |
| 10 | #include "AArch64ISelLowering.h" |
| 11 | #include "llvm/IR/InstrTypes.h" |
| 12 | #include "llvm/IR/RuntimeLibcalls.h" |
| 13 | #include <cassert> |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | void SMEAttrs::validate() const { |
| 18 | // Streaming Mode Attrs |
| 19 | assert(!(hasStreamingInterface() && hasStreamingCompatibleInterface()) && |
| 20 | "SM_Enabled and SM_Compatible are mutually exclusive" ); |
| 21 | |
| 22 | // ZA Attrs |
| 23 | assert(!(isNewZA() && (Bitmask & SME_ABI_Routine)) && |
| 24 | "ZA_New and SME_ABI_Routine are mutually exclusive" ); |
| 25 | |
| 26 | assert( |
| 27 | (isNewZA() + isInZA() + isOutZA() + isInOutZA() + isPreservesZA()) <= 1 && |
| 28 | "Attributes 'aarch64_new_za', 'aarch64_in_za', 'aarch64_out_za', " |
| 29 | "'aarch64_inout_za' and 'aarch64_preserves_za' are mutually exclusive" ); |
| 30 | |
| 31 | // ZT0 Attrs |
| 32 | assert( |
| 33 | (isNewZT0() + isInZT0() + isOutZT0() + isInOutZT0() + isPreservesZT0()) <= |
| 34 | 1 && |
| 35 | "Attributes 'aarch64_new_zt0', 'aarch64_in_zt0', 'aarch64_out_zt0', " |
| 36 | "'aarch64_inout_zt0' and 'aarch64_preserves_zt0' are mutually exclusive" ); |
| 37 | |
| 38 | assert(!(hasAgnosticZAInterface() && hasSharedZAInterface()) && |
| 39 | "Function cannot have a shared-ZA interface and an agnostic-ZA " |
| 40 | "interface" ); |
| 41 | } |
| 42 | |
| 43 | SMEAttrs::SMEAttrs(const AttributeList &Attrs) { |
| 44 | // Note: 'aarch64_zt0_undef' was previously used (and subsequently removed). |
| 45 | // To avoid introducing any compatibility issues don't reuse |
| 46 | // 'aarch64_zt0_undef' for another purpose. |
| 47 | Bitmask = 0; |
| 48 | if (Attrs.hasFnAttr(Kind: "aarch64_pstate_sm_enabled" )) |
| 49 | Bitmask |= SM_Enabled; |
| 50 | if (Attrs.hasFnAttr(Kind: "aarch64_pstate_sm_compatible" )) |
| 51 | Bitmask |= SM_Compatible; |
| 52 | if (Attrs.hasFnAttr(Kind: "aarch64_pstate_sm_body" )) |
| 53 | Bitmask |= SM_Body; |
| 54 | if (Attrs.hasFnAttr(Kind: "aarch64_za_state_agnostic" )) |
| 55 | Bitmask |= ZA_State_Agnostic; |
| 56 | if (Attrs.hasFnAttr(Kind: "aarch64_in_za" )) |
| 57 | Bitmask |= encodeZAState(S: StateValue::In); |
| 58 | if (Attrs.hasFnAttr(Kind: "aarch64_out_za" )) |
| 59 | Bitmask |= encodeZAState(S: StateValue::Out); |
| 60 | if (Attrs.hasFnAttr(Kind: "aarch64_inout_za" )) |
| 61 | Bitmask |= encodeZAState(S: StateValue::InOut); |
| 62 | if (Attrs.hasFnAttr(Kind: "aarch64_preserves_za" )) |
| 63 | Bitmask |= encodeZAState(S: StateValue::Preserved); |
| 64 | if (Attrs.hasFnAttr(Kind: "aarch64_new_za" )) |
| 65 | Bitmask |= encodeZAState(S: StateValue::New); |
| 66 | if (Attrs.hasFnAttr(Kind: "aarch64_in_zt0" )) |
| 67 | Bitmask |= encodeZT0State(S: StateValue::In); |
| 68 | if (Attrs.hasFnAttr(Kind: "aarch64_out_zt0" )) |
| 69 | Bitmask |= encodeZT0State(S: StateValue::Out); |
| 70 | if (Attrs.hasFnAttr(Kind: "aarch64_inout_zt0" )) |
| 71 | Bitmask |= encodeZT0State(S: StateValue::InOut); |
| 72 | if (Attrs.hasFnAttr(Kind: "aarch64_preserves_zt0" )) |
| 73 | Bitmask |= encodeZT0State(S: StateValue::Preserved); |
| 74 | if (Attrs.hasFnAttr(Kind: "aarch64_new_zt0" )) |
| 75 | Bitmask |= encodeZT0State(S: StateValue::New); |
| 76 | } |
| 77 | |
| 78 | void SMEAttrs::addKnownFunctionAttrs(StringRef FuncName, |
| 79 | const RTLIB::RuntimeLibcallsInfo &RTLCI) { |
| 80 | RTLIB::LibcallImpl Impl = RTLCI.getSupportedLibcallImpl(FuncName); |
| 81 | if (Impl == RTLIB::Unsupported) |
| 82 | return; |
| 83 | unsigned KnownAttrs = SMEAttrs::Normal; |
| 84 | RTLIB::Libcall LC = RTLIB::RuntimeLibcallsInfo::getLibcallFromImpl(Impl); |
| 85 | switch (LC) { |
| 86 | case RTLIB::SMEABI_SME_STATE: |
| 87 | case RTLIB::SMEABI_TPIDR2_SAVE: |
| 88 | case RTLIB::SMEABI_GET_CURRENT_VG: |
| 89 | case RTLIB::SMEABI_SME_STATE_SIZE: |
| 90 | case RTLIB::SMEABI_SME_SAVE: |
| 91 | case RTLIB::SMEABI_SME_RESTORE: |
| 92 | KnownAttrs |= SMEAttrs::SM_Compatible | SMEAttrs::SME_ABI_Routine; |
| 93 | break; |
| 94 | case RTLIB::SMEABI_ZA_DISABLE: |
| 95 | case RTLIB::SMEABI_TPIDR2_RESTORE: |
| 96 | KnownAttrs |= SMEAttrs::SM_Compatible | encodeZAState(S: StateValue::In) | |
| 97 | SMEAttrs::SME_ABI_Routine; |
| 98 | break; |
| 99 | case RTLIB::SC_MEMCPY: |
| 100 | case RTLIB::SC_MEMMOVE: |
| 101 | case RTLIB::SC_MEMSET: |
| 102 | case RTLIB::SC_MEMCHR: |
| 103 | KnownAttrs |= SMEAttrs::SM_Compatible; |
| 104 | break; |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | set(M: KnownAttrs); |
| 109 | } |
| 110 | |
| 111 | bool SMECallAttrs::requiresSMChange() const { |
| 112 | if (callee().hasStreamingCompatibleInterface()) |
| 113 | return false; |
| 114 | |
| 115 | // Both non-streaming |
| 116 | if (caller().hasNonStreamingInterfaceAndBody() && |
| 117 | callee().hasNonStreamingInterface()) |
| 118 | return false; |
| 119 | |
| 120 | // Both streaming |
| 121 | if (caller().hasStreamingInterfaceOrBody() && |
| 122 | callee().hasStreamingInterface()) |
| 123 | return false; |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | SMECallAttrs::SMECallAttrs(const CallBase &CB, |
| 129 | const RTLIB::RuntimeLibcallsInfo *RTLCI) |
| 130 | : CallerFn(*CB.getFunction()), CalledFn(SMEAttrs::Normal), |
| 131 | Callsite(CB.getAttributes()), IsIndirect(CB.isIndirectCall()) { |
| 132 | if (auto *CalledFunction = CB.getCalledFunction()) |
| 133 | CalledFn = SMEAttrs(*CalledFunction, RTLCI); |
| 134 | |
| 135 | // FIXME: We probably should not allow SME attributes on direct calls but |
| 136 | // clang duplicates streaming mode attributes at each callsite. |
| 137 | assert((IsIndirect || ((Callsite | CalledFn) == CalledFn)) && |
| 138 | "SME attributes at callsite do not match declaration" ); |
| 139 | |
| 140 | // An `invoke` of an agnostic ZA function may not return normally (it may |
| 141 | // resume in an exception block). In this case, it acts like a private ZA |
| 142 | // callee and may require a ZA save to be set up before it is called. |
| 143 | if (isa<InvokeInst>(Val: CB)) |
| 144 | CalledFn.set(M: SMEAttrs::ZA_State_Agnostic, /*Enable=*/false); |
| 145 | } |
| 146 | |