LLVM 18.0.0git
SMEABIPass.cpp
Go to the documentation of this file.
1//===--------- SMEABI - SME ABI-------------------------------------------===//
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// This pass implements parts of the the SME ABI, such as:
10// * Using the lazy-save mechanism before enabling the use of ZA.
11// * Setting up the lazy-save mechanism around invokes.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AArch64.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/IR/Constants.h"
20#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/IntrinsicsAArch64.h"
24#include "llvm/IR/LLVMContext.h"
26#include "llvm/Support/Debug.h"
28
29using namespace llvm;
30
31#define DEBUG_TYPE "aarch64-sme-abi"
32
33namespace {
34struct SMEABI : public FunctionPass {
35 static char ID; // Pass identification, replacement for typeid
36 SMEABI() : FunctionPass(ID) {
38 }
39
40 bool runOnFunction(Function &F) override;
41
42private:
43 bool updateNewZAFunctions(Module *M, Function *F, IRBuilder<> &Builder);
44};
45} // end anonymous namespace
46
47char SMEABI::ID = 0;
48static const char *name = "SME ABI Pass";
49INITIALIZE_PASS_BEGIN(SMEABI, DEBUG_TYPE, name, false, false)
51
52FunctionPass *llvm::createSMEABIPass() { return new SMEABI(); }
53
54//===----------------------------------------------------------------------===//
55// Utility functions
56//===----------------------------------------------------------------------===//
57
58// Utility function to emit a call to __arm_tpidr2_save and clear TPIDR2_EL0.
59void emitTPIDR2Save(Module *M, IRBuilder<> &Builder) {
60 auto *TPIDR2SaveTy =
61 FunctionType::get(Builder.getVoidTy(), {}, /*IsVarArgs=*/false);
62 auto Attrs =
64 .addFnAttribute(M->getContext(), "aarch64_pstate_sm_compatible")
65 .addFnAttribute(M->getContext(), "aarch64_pstate_za_preserved");
66 FunctionCallee Callee =
67 M->getOrInsertFunction("__arm_tpidr2_save", TPIDR2SaveTy, Attrs);
68 CallInst *Call = Builder.CreateCall(Callee);
69 Call->setCallingConv(
71
72 // A save to TPIDR2 should be followed by clearing TPIDR2_EL0.
73 Function *WriteIntr =
74 Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_set_tpidr2);
75 Builder.CreateCall(WriteIntr->getFunctionType(), WriteIntr,
76 Builder.getInt64(0));
77}
78
79/// This function generates code to commit a lazy save at the beginning of a
80/// function marked with `aarch64_pstate_za_new`. If the value read from
81/// TPIDR2_EL0 is not null on entry to the function then the lazy-saving scheme
82/// is active and we should call __arm_tpidr2_save to commit the lazy save.
83/// Additionally, PSTATE.ZA should be enabled at the beginning of the function
84/// and disabled before returning.
85bool SMEABI::updateNewZAFunctions(Module *M, Function *F,
86 IRBuilder<> &Builder) {
87 LLVMContext &Context = F->getContext();
88 BasicBlock *OrigBB = &F->getEntryBlock();
89
90 // Create the new blocks for reading TPIDR2_EL0 & enabling ZA state.
91 auto *SaveBB = OrigBB->splitBasicBlock(OrigBB->begin(), "save.za", true);
92 auto *PreludeBB = BasicBlock::Create(Context, "prelude", F, SaveBB);
93
94 // Read TPIDR2_EL0 in PreludeBB & branch to SaveBB if not 0.
95 Builder.SetInsertPoint(PreludeBB);
96 Function *TPIDR2Intr =
97 Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_get_tpidr2);
98 auto *TPIDR2 = Builder.CreateCall(TPIDR2Intr->getFunctionType(), TPIDR2Intr,
99 {}, "tpidr2");
100 auto *Cmp =
101 Builder.CreateCmp(ICmpInst::ICMP_NE, TPIDR2, Builder.getInt64(0), "cmp");
102 Builder.CreateCondBr(Cmp, SaveBB, OrigBB);
103
104 // Create a call __arm_tpidr2_save, which commits the lazy save.
105 Builder.SetInsertPoint(&SaveBB->back());
106 emitTPIDR2Save(M, Builder);
107
108 // Enable pstate.za at the start of the function.
109 Builder.SetInsertPoint(&OrigBB->front());
110 Function *EnableZAIntr =
111 Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_za_enable);
112 Builder.CreateCall(EnableZAIntr->getFunctionType(), EnableZAIntr);
113
114 // ZA state must be zeroed upon entry to a function with NewZA
115 Function *ZeroIntr =
116 Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_zero);
117 Builder.CreateCall(ZeroIntr->getFunctionType(), ZeroIntr,
118 Builder.getInt32(0xff));
119
120 // Before returning, disable pstate.za
121 for (BasicBlock &BB : *F) {
122 Instruction *T = BB.getTerminator();
123 if (!T || !isa<ReturnInst>(T))
124 continue;
125 Builder.SetInsertPoint(T);
126 Function *DisableZAIntr =
127 Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_za_disable);
128 Builder.CreateCall(DisableZAIntr->getFunctionType(), DisableZAIntr);
129 }
130
131 F->addFnAttr("aarch64_expanded_pstate_za");
132 return true;
133}
134
135bool SMEABI::runOnFunction(Function &F) {
136 Module *M = F.getParent();
137 LLVMContext &Context = F.getContext();
138 IRBuilder<> Builder(Context);
139
140 if (F.isDeclaration() || F.hasFnAttribute("aarch64_expanded_pstate_za"))
141 return false;
142
143 bool Changed = false;
144 SMEAttrs FnAttrs(F);
145 if (FnAttrs.hasNewZABody())
146 Changed |= updateNewZAFunctions(M, &F, Builder);
147
148 return Changed;
149}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define F(x, y, z)
Definition: MD5.cpp:55
LLVMContext & Context
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
void emitTPIDR2Save(Module *M, IRBuilder<> &Builder)
Definition: SMEABIPass.cpp:59
static const char * name
Definition: SMEABIPass.cpp:48
#define DEBUG_TYPE
Definition: SMEABIPass.cpp:31
AttributeList addFnAttribute(LLVMContext &C, Attribute::AttrKind Kind) const
Add a function attribute to the list.
Definition: Attributes.h:522
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:437
const Instruction & front() const
Definition: BasicBlock.h:460
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:206
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="", bool Before=false)
Split the basic block into two basic blocks at the specified instruction.
Definition: BasicBlock.cpp:607
This class represents a function call, abstracting a target machine's calling convention.
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:168
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
FunctionType * getFunctionType() const
Returns the FunctionType for me.
Definition: Function.h:200
ConstantInt * getInt64(uint64_t C)
Get a constant 64-bit value.
Definition: IRBuilder.h:485
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:480
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2339
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Definition: IRBuilder.h:1114
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:180
Type * getVoidTy()
Fetch the type representing void.
Definition: IRBuilder.h:558
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2385
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2639
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
SMEAttrs is a utility class to parse the SME ACLE attributes on functions.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0
Preserve X0-X13, X19-X29, SP, Z0-Z31, P0-P15.
Definition: CallingConv.h:235
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createSMEABIPass()
Definition: SMEABIPass.cpp:52
void initializeSMEABIPass(PassRegistry &)