LLVM 17.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"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/IR/Constants.h"
21#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/IntrinsicsAArch64.h"
25#include "llvm/IR/LLVMContext.h"
27#include "llvm/Support/Debug.h"
29
30using namespace llvm;
31
32#define DEBUG_TYPE "aarch64-sme-abi"
33
34namespace {
35struct SMEABI : public FunctionPass {
36 static char ID; // Pass identification, replacement for typeid
37 SMEABI() : FunctionPass(ID) {
39 }
40
41 bool runOnFunction(Function &F) override;
42
43private:
44 bool updateNewZAFunctions(Module *M, Function *F, IRBuilder<> &Builder);
45};
46} // end anonymous namespace
47
48char SMEABI::ID = 0;
49static const char *name = "SME ABI Pass";
50INITIALIZE_PASS_BEGIN(SMEABI, DEBUG_TYPE, name, false, false)
52
53FunctionPass *llvm::createSMEABIPass() { return new SMEABI(); }
54
55//===----------------------------------------------------------------------===//
56// Utility functions
57//===----------------------------------------------------------------------===//
58
59// Utility function to emit a call to __arm_tpidr2_save and clear TPIDR2_EL0.
60void emitTPIDR2Save(Module *M, IRBuilder<> &Builder) {
61 auto *TPIDR2SaveTy =
62 FunctionType::get(Builder.getVoidTy(), {}, /*IsVarArgs=*/false);
63 auto Attrs =
65 .addFnAttribute(M->getContext(), "aarch64_pstate_sm_compatible")
66 .addFnAttribute(M->getContext(), "aarch64_pstate_za_preserved");
68 M->getOrInsertFunction("__arm_tpidr2_save", TPIDR2SaveTy, Attrs);
69 CallInst *Call = Builder.CreateCall(Callee);
70 Call->setCallingConv(
72
73 // A save to TPIDR2 should be followed by clearing TPIDR2_EL0.
74 Function *WriteIntr =
75 Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_set_tpidr2);
76 Builder.CreateCall(WriteIntr->getFunctionType(), WriteIntr,
77 Builder.getInt64(0));
78}
79
80/// This function generates code to commit a lazy save at the beginning of a
81/// function marked with `aarch64_pstate_za_new`. If the value read from
82/// TPIDR2_EL0 is not null on entry to the function then the lazy-saving scheme
83/// is active and we should call __arm_tpidr2_save to commit the lazy save.
84/// Additionally, PSTATE.ZA should be enabled at the beginning of the function
85/// and disabled before returning.
86bool SMEABI::updateNewZAFunctions(Module *M, Function *F,
87 IRBuilder<> &Builder) {
88 LLVMContext &Context = F->getContext();
89 BasicBlock *OrigBB = &F->getEntryBlock();
90
91 // Create the new blocks for reading TPIDR2_EL0 & enabling ZA state.
92 auto *SaveBB = OrigBB->splitBasicBlock(OrigBB->begin(), "save.za", true);
93 auto *PreludeBB = BasicBlock::Create(Context, "prelude", F, SaveBB);
94
95 // Read TPIDR2_EL0 in PreludeBB & branch to SaveBB if not 0.
96 Builder.SetInsertPoint(PreludeBB);
97 Function *TPIDR2Intr =
98 Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_get_tpidr2);
99 auto *TPIDR2 = Builder.CreateCall(TPIDR2Intr->getFunctionType(), TPIDR2Intr,
100 {}, "tpidr2");
101 auto *Cmp =
102 Builder.CreateCmp(ICmpInst::ICMP_NE, TPIDR2, Builder.getInt64(0), "cmp");
103 Builder.CreateCondBr(Cmp, SaveBB, OrigBB);
104
105 // Create a call __arm_tpidr2_save, which commits the lazy save.
106 Builder.SetInsertPoint(&SaveBB->back());
107 emitTPIDR2Save(M, Builder);
108
109 // Enable pstate.za at the start of the function.
110 Builder.SetInsertPoint(&OrigBB->front());
111 Function *EnableZAIntr =
112 Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_za_enable);
113 Builder.CreateCall(EnableZAIntr->getFunctionType(), EnableZAIntr);
114
115 // Before returning, disable pstate.za
116 for (BasicBlock &BB : *F) {
117 Instruction *T = BB.getTerminator();
118 if (!T || !isa<ReturnInst>(T))
119 continue;
120 Builder.SetInsertPoint(T);
121 Function *DisableZAIntr =
122 Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_za_disable);
123 Builder.CreateCall(DisableZAIntr->getFunctionType(), DisableZAIntr);
124 }
125
126 F->addFnAttr("aarch64_expanded_pstate_za");
127 return true;
128}
129
130bool SMEABI::runOnFunction(Function &F) {
131 Module *M = F.getParent();
132 LLVMContext &Context = F.getContext();
133 IRBuilder<> Builder(Context);
134
135 if (F.isDeclaration() || F.hasFnAttribute("aarch64_expanded_pstate_za"))
136 return false;
137
138 bool Changed = false;
139 SMEAttrs FnAttrs(F);
140 if (FnAttrs.hasNewZAInterface())
141 Changed |= updateNewZAFunctions(M, &F, Builder);
142
143 return Changed;
144}
amdgpu Simplify well known AMD library false FunctionCallee Callee
assume Assume Builder
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:60
static const char * name
Definition: SMEABIPass.cpp:49
#define DEBUG_TYPE
Definition: SMEABIPass.cpp:32
This file defines the SmallVector class.
AttributeList addFnAttribute(LLVMContext &C, Attribute::AttrKind Kind) const
Add a function attribute to the list.
Definition: Attributes.h:519
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:314
const Instruction & front() const
Definition: BasicBlock.h:326
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition: BasicBlock.h:105
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:401
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:165
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
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:174
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2550
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:239
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:1502
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createSMEABIPass()
Definition: SMEABIPass.cpp:53
void initializeSMEABIPass(PassRegistry &)