LLVM 24.0.0git
AArch64SMEAttributes.cpp
Go to the documentation of this file.
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
10#include "AArch64ISelLowering.h"
12#include "llvm/IR/InstrTypes.h"
14#include <cassert>
15
16using namespace llvm;
17
18void SMEAttrs::validate() const {
19 // Streaming Mode Attrs
21 "SM_Enabled and SM_Compatible are mutually exclusive");
22
23 // ZA Attrs
24 assert(!(isNewZA() && (Bitmask & SME_ABI_Routine)) &&
25 "ZA_New and SME_ABI_Routine are mutually exclusive");
26
27 assert(
28 (isNewZA() + isInZA() + isOutZA() + isInOutZA() + isPreservesZA()) <= 1 &&
29 "Attributes 'aarch64_new_za', 'aarch64_in_za', 'aarch64_out_za', "
30 "'aarch64_inout_za' and 'aarch64_preserves_za' are mutually exclusive");
31
32 // ZT0 Attrs
33 assert(
35 1 &&
36 "Attributes 'aarch64_new_zt0', 'aarch64_in_zt0', 'aarch64_out_zt0', "
37 "'aarch64_inout_zt0' and 'aarch64_preserves_zt0' are mutually exclusive");
38
40 "Function cannot have a shared-ZA interface and an agnostic-ZA "
41 "interface");
42}
43
44SMEAttrs::SMEAttrs(const AttributeList &Attrs) {
45 // Note: 'aarch64_zt0_undef' was previously used (and subsequently removed).
46 // To avoid introducing any compatibility issues don't reuse
47 // 'aarch64_zt0_undef' for another purpose.
48 Bitmask = 0;
49 for (Attribute Attr : Attrs.getFnAttrs()) {
50 if (!Attr.isStringAttribute())
51 continue;
52
53 StringRef Kind = Attr.getKindAsString();
54 if (!Kind.consume_front("aarch64_"))
55 continue;
56
57 Bitmask |= StringSwitch<unsigned>(Kind)
58 .Case("pstate_sm_enabled", SM_Enabled)
59 .Case("pstate_sm_compatible", SM_Compatible)
60 .Case("pstate_sm_body", SM_Body)
61 .Case("za_state_agnostic", ZA_State_Agnostic)
70 .Case("preserves_zt0", encodeZT0State(StateValue::Preserved))
73 }
74}
75
76void SMEAttrs::addKnownFunctionAttrs(StringRef FuncName,
77 const RTLIB::RuntimeLibcallsInfo &RTLCI) {
78 RTLIB::LibcallImpl Impl = RTLCI.getSupportedLibcallImpl(FuncName);
79 if (Impl == RTLIB::Unsupported)
80 return;
81 unsigned KnownAttrs = SMEAttrs::Normal;
82 RTLIB::Libcall LC = RTLIB::RuntimeLibcallsInfo::getLibcallFromImpl(Impl);
83 switch (LC) {
84 case RTLIB::SMEABI_SME_STATE:
85 case RTLIB::SMEABI_TPIDR2_SAVE:
86 case RTLIB::SMEABI_GET_CURRENT_VG:
87 case RTLIB::SMEABI_SME_STATE_SIZE:
88 case RTLIB::SMEABI_SME_SAVE:
89 case RTLIB::SMEABI_SME_RESTORE:
91 break;
92 case RTLIB::SMEABI_ZA_DISABLE:
93 case RTLIB::SMEABI_TPIDR2_RESTORE:
96 break;
97 case RTLIB::SC_MEMCPY:
98 case RTLIB::SC_MEMMOVE:
99 case RTLIB::SC_MEMSET:
100 case RTLIB::SC_MEMCHR:
101 KnownAttrs |= SMEAttrs::SM_Compatible;
102 break;
103 default:
104 break;
105 }
106 set(KnownAttrs);
107}
108
110 if (callee().hasStreamingCompatibleInterface())
111 return false;
112
113 // Both non-streaming
114 if (caller().hasNonStreamingInterfaceAndBody() &&
115 callee().hasNonStreamingInterface())
116 return false;
117
118 // Both streaming
119 if (caller().hasStreamingInterfaceOrBody() &&
120 callee().hasStreamingInterface())
121 return false;
122
123 return true;
124}
125
127 const RTLIB::RuntimeLibcallsInfo *RTLCI)
128 : CallerFn(*CB.getFunction()), CalledFn(SMEAttrs::Normal),
129 Callsite(CB.getAttributes()), IsIndirect(CB.isIndirectCall()) {
130 if (auto *CalledFunction = CB.getCalledFunction())
131 CalledFn = SMEAttrs(*CalledFunction, RTLCI);
132
133 // FIXME: We probably should not allow SME attributes on direct calls but
134 // clang duplicates streaming mode attributes at each callsite.
135 assert((IsIndirect || ((Callsite | CalledFn) == CalledFn)) &&
136 "SME attributes at callsite do not match declaration");
137
138 // An `invoke` of an agnostic ZA function may not return normally (it may
139 // resume in an exception block). In this case, it acts like a private ZA
140 // callee and may require a ZA save to be set up before it is called.
141 if (isa<InvokeInst>(CB))
142 CalledFn.set(SMEAttrs::ZA_State_Agnostic, /*Enable=*/false);
143}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static Function * getFunction(FunctionType *Ty, const Twine &Name, Module *M)
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
SMEAttrs is a utility class to parse the SME ACLE attributes on functions.
bool isPreservesZT0() const
bool hasStreamingInterface() const
static unsigned encodeZAState(StateValue S)
SMEAttrs()=default
bool hasStreamingCompatibleInterface() const
bool hasAgnosticZAInterface() const
bool isPreservesZA() const
void set(unsigned M, bool Enable=true)
bool hasSharedZAInterface() const
static unsigned encodeZT0State(StateValue S)
SMECallAttrs(SMEAttrs Caller, SMEAttrs Callee, SMEAttrs Callsite=SMEAttrs::Normal)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
This is an optimization pass for GlobalISel generic memory operations.
static bool isIndirectCall(const MachineInstr &MI)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
A simple container for information about the supported runtime calls.
RTLIB::LibcallImpl getSupportedLibcallImpl(StringRef FuncName) const
Check if this is valid libcall for the current module, otherwise RTLIB::Unsupported.
static RTLIB::Libcall getLibcallFromImpl(RTLIB::LibcallImpl Impl)
Return the libcall provided by Impl.