[flang] Fix crash in HLFIR generation (#118399)
[llvm-project.git] / llvm / lib / Target / AArch64 / SMEABIPass.cpp
blobbb885d86392fe6fd25aa72967bc3db96ed685a23
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.
13 //===----------------------------------------------------------------------===//
15 #include "AArch64.h"
16 #include "Utils/AArch64SMEAttributes.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Instructions.h"
20 #include "llvm/IR/IntrinsicsAArch64.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/InitializePasses.h"
24 #include "llvm/Transforms/Utils/Cloning.h"
26 using namespace llvm;
28 #define DEBUG_TYPE "aarch64-sme-abi"
30 namespace {
31 struct SMEABI : public FunctionPass {
32 static char ID; // Pass identification, replacement for typeid
33 SMEABI() : FunctionPass(ID) {
34 initializeSMEABIPass(*PassRegistry::getPassRegistry());
37 bool runOnFunction(Function &F) override;
39 private:
40 bool updateNewStateFunctions(Module *M, Function *F, IRBuilder<> &Builder,
41 SMEAttrs FnAttrs);
43 } // end anonymous namespace
45 char SMEABI::ID = 0;
46 static const char *name = "SME ABI Pass";
47 INITIALIZE_PASS_BEGIN(SMEABI, DEBUG_TYPE, name, false, false)
48 INITIALIZE_PASS_END(SMEABI, DEBUG_TYPE, name, false, false)
50 FunctionPass *llvm::createSMEABIPass() { return new SMEABI(); }
52 //===----------------------------------------------------------------------===//
53 // Utility functions
54 //===----------------------------------------------------------------------===//
56 // Utility function to emit a call to __arm_tpidr2_save and clear TPIDR2_EL0.
57 void emitTPIDR2Save(Module *M, IRBuilder<> &Builder) {
58 auto *TPIDR2SaveTy =
59 FunctionType::get(Builder.getVoidTy(), {}, /*IsVarArgs=*/false);
60 auto Attrs = AttributeList().addFnAttribute(M->getContext(),
61 "aarch64_pstate_sm_compatible");
62 FunctionCallee Callee =
63 M->getOrInsertFunction("__arm_tpidr2_save", TPIDR2SaveTy, Attrs);
64 CallInst *Call = Builder.CreateCall(Callee);
65 Call->setCallingConv(
66 CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0);
68 // A save to TPIDR2 should be followed by clearing TPIDR2_EL0.
69 Function *WriteIntr =
70 Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_set_tpidr2);
71 Builder.CreateCall(WriteIntr->getFunctionType(), WriteIntr,
72 Builder.getInt64(0));
75 /// This function generates code at the beginning and end of a function marked
76 /// with either `aarch64_new_za` or `aarch64_new_zt0`.
77 /// At the beginning of the function, the following code is generated:
78 /// - Commit lazy-save if active [Private-ZA Interface*]
79 /// - Enable PSTATE.ZA [Private-ZA Interface]
80 /// - Zero ZA [Has New ZA State]
81 /// - Zero ZT0 [Has New ZT0 State]
82 ///
83 /// * A function with new ZT0 state will not change ZA, so committing the
84 /// lazy-save is not strictly necessary. However, the lazy-save mechanism
85 /// may be active on entry to the function, with PSTATE.ZA set to 1. If
86 /// the new ZT0 function calls a function that does not share ZT0, we will
87 /// need to conditionally SMSTOP ZA before the call, setting PSTATE.ZA to 0.
88 /// For this reason, it's easier to always commit the lazy-save at the
89 /// beginning of the function regardless of whether it has ZA state.
90 ///
91 /// At the end of the function, PSTATE.ZA is disabled if the function has a
92 /// Private-ZA Interface. A function is considered to have a Private-ZA
93 /// interface if it does not share ZA or ZT0.
94 ///
95 bool SMEABI::updateNewStateFunctions(Module *M, Function *F,
96 IRBuilder<> &Builder, SMEAttrs FnAttrs) {
97 LLVMContext &Context = F->getContext();
98 BasicBlock *OrigBB = &F->getEntryBlock();
99 Builder.SetInsertPoint(&OrigBB->front());
101 // Commit any active lazy-saves if this is a Private-ZA function. If the
102 // value read from TPIDR2_EL0 is not null on entry to the function then
103 // the lazy-saving scheme is active and we should call __arm_tpidr2_save
104 // to commit the lazy save.
105 if (FnAttrs.hasPrivateZAInterface()) {
106 // Create the new blocks for reading TPIDR2_EL0 & enabling ZA state.
107 auto *SaveBB = OrigBB->splitBasicBlock(OrigBB->begin(), "save.za", true);
108 auto *PreludeBB = BasicBlock::Create(Context, "prelude", F, SaveBB);
110 // Read TPIDR2_EL0 in PreludeBB & branch to SaveBB if not 0.
111 Builder.SetInsertPoint(PreludeBB);
112 Function *TPIDR2Intr =
113 Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_get_tpidr2);
114 auto *TPIDR2 = Builder.CreateCall(TPIDR2Intr->getFunctionType(), TPIDR2Intr,
115 {}, "tpidr2");
116 auto *Cmp = Builder.CreateCmp(ICmpInst::ICMP_NE, TPIDR2,
117 Builder.getInt64(0), "cmp");
118 Builder.CreateCondBr(Cmp, SaveBB, OrigBB);
120 // Create a call __arm_tpidr2_save, which commits the lazy save.
121 Builder.SetInsertPoint(&SaveBB->back());
122 emitTPIDR2Save(M, Builder);
124 // Enable pstate.za at the start of the function.
125 Builder.SetInsertPoint(&OrigBB->front());
126 Function *EnableZAIntr =
127 Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_za_enable);
128 Builder.CreateCall(EnableZAIntr->getFunctionType(), EnableZAIntr);
131 if (FnAttrs.isNewZA()) {
132 Function *ZeroIntr =
133 Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_zero);
134 Builder.CreateCall(ZeroIntr->getFunctionType(), ZeroIntr,
135 Builder.getInt32(0xff));
138 if (FnAttrs.isNewZT0()) {
139 Function *ClearZT0Intr =
140 Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_zero_zt);
141 Builder.CreateCall(ClearZT0Intr->getFunctionType(), ClearZT0Intr,
142 {Builder.getInt32(0)});
145 if (FnAttrs.hasPrivateZAInterface()) {
146 // Before returning, disable pstate.za
147 for (BasicBlock &BB : *F) {
148 Instruction *T = BB.getTerminator();
149 if (!T || !isa<ReturnInst>(T))
150 continue;
151 Builder.SetInsertPoint(T);
152 Function *DisableZAIntr = Intrinsic::getOrInsertDeclaration(
153 M, Intrinsic::aarch64_sme_za_disable);
154 Builder.CreateCall(DisableZAIntr->getFunctionType(), DisableZAIntr);
158 F->addFnAttr("aarch64_expanded_pstate_za");
159 return true;
162 bool SMEABI::runOnFunction(Function &F) {
163 Module *M = F.getParent();
164 LLVMContext &Context = F.getContext();
165 IRBuilder<> Builder(Context);
167 if (F.isDeclaration() || F.hasFnAttribute("aarch64_expanded_pstate_za"))
168 return false;
170 bool Changed = false;
171 SMEAttrs FnAttrs(F);
172 if (FnAttrs.isNewZA() || FnAttrs.isNewZT0())
173 Changed |= updateNewStateFunctions(M, &F, Builder, FnAttrs);
175 return Changed;