LLVM 23.0.0git
MatrixUtils.cpp
Go to the documentation of this file.
1//===- MatrixUtils.cpp - Utilities to lower matrix intrinsics ---*- C++ -*-===//
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// Utilities for generating tiled loops for matrix operations.
10//
11//===----------------------------------------------------------------------===//
12
16#include "llvm/IR/BasicBlock.h"
17#include "llvm/IR/Dominators.h"
18#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/MDBuilder.h"
21#include "llvm/IR/Type.h"
23
24using namespace llvm;
25
26namespace llvm {
28} // end namespace llvm
29
30BasicBlock *TileInfo::CreateLoop(BasicBlock *Preheader, BasicBlock *Exit,
31 ConstantInt *Bound, ConstantInt *Step,
33 DomTreeUpdater &DTU, Loop *L, LoopInfo &LI) {
34 LLVMContext &Ctx = Preheader->getContext();
36 Preheader->getContext(), Name + ".header", Preheader->getParent(), Exit);
37 BasicBlock *Body = BasicBlock::Create(Header->getContext(), Name + ".body",
38 Header->getParent(), Exit);
39 BasicBlock *Latch = BasicBlock::Create(Header->getContext(), Name + ".latch",
40 Header->getParent(), Exit);
41
42 Type *I32Ty = Type::getInt64Ty(Ctx);
43 BranchInst::Create(Body, Header);
44 BranchInst::Create(Latch, Body);
45 PHINode *IV =
46 PHINode::Create(I32Ty, 2, Name + ".iv", Header->getTerminator()->getIterator());
47 IV->addIncoming(ConstantInt::get(I32Ty, 0), Preheader);
48
49 B.SetInsertPoint(Latch);
50 Value *Inc = B.CreateAdd(IV, Step, Name + ".step");
51 Value *Cond = B.CreateICmpNE(Inc, Bound, Name + ".cond");
52 auto *BR = BranchInst::Create(Header, Exit, Cond, Latch);
54 assert(Step->getZExtValue() != 0 &&
55 "Expected a non-zero step size. This is chosen by the pass and "
56 "should always be non-zero to imply a finite loop.");
57 MDBuilder MDB(Preheader->getContext());
59 *BR, {Bound->getZExtValue() / Step->getZExtValue(), 1}, false);
60 }
61 IV->addIncoming(Inc, Latch);
62
63 BranchInst *PreheaderBr = cast<BranchInst>(Preheader->getTerminator());
64 BasicBlock *Tmp = PreheaderBr->getSuccessor(0);
65 PreheaderBr->setSuccessor(0, Header);
67 {DominatorTree::Delete, Preheader, Tmp},
68 {DominatorTree::Insert, Header, Body},
69 {DominatorTree::Insert, Body, Latch},
70 {DominatorTree::Insert, Latch, Header},
72 {DominatorTree::Insert, Preheader, Header},
73 });
74
75 L->addBasicBlockToLoop(Header, LI);
76 L->addBasicBlockToLoop(Body, LI);
77 L->addBasicBlockToLoop(Latch, LI);
78 return Body;
79}
80
81// Creates the following loop nest skeleton:
82// for C = 0; C < NumColumns; C += TileSize
83// for R = 0; R < NumRows; R += TileSize
84// for K = 0; K < Inner ; K += TileSize
87 LoopInfo &LI) {
88 Loop *ColumnLoopInfo = LI.AllocateLoop();
89 Loop *RowLoopInfo = LI.AllocateLoop();
90 Loop *KLoopInfo = LI.AllocateLoop();
91 RowLoopInfo->addChildLoop(KLoopInfo);
92 ColumnLoopInfo->addChildLoop(RowLoopInfo);
93 if (Loop *ParentL = LI.getLoopFor(Start))
94 ParentL->addChildLoop(ColumnLoopInfo);
95 else
96 LI.addTopLevelLoop(ColumnLoopInfo);
97
98 BasicBlock *ColBody =
99 CreateLoop(Start, End, B.getInt64(NumColumns), B.getInt64(TileSize),
100 "cols", B, DTU, ColumnLoopInfo, LI);
101 ColumnLoop.Latch = ColBody->getSingleSuccessor();
102 BasicBlock *RowBody =
103 CreateLoop(ColBody, ColumnLoop.Latch, B.getInt64(NumRows),
104 B.getInt64(TileSize), "rows", B, DTU, RowLoopInfo, LI);
105 RowLoop.Latch = RowBody->getSingleSuccessor();
106
107 BasicBlock *InnerBody =
108 CreateLoop(RowBody, RowLoop.Latch, B.getInt64(NumInner),
109 B.getInt64(TileSize), "inner", B, DTU, KLoopInfo, LI);
110 KLoop.Latch = InnerBody->getSingleSuccessor();
111 ColumnLoop.Header = ColBody->getSinglePredecessor();
112 RowLoop.Header = RowBody->getSinglePredecessor();
113 KLoop.Header = InnerBody->getSinglePredecessor();
114 RowLoop.Index = &*RowLoop.Header->begin();
115 ColumnLoop.Index = &*ColumnLoop.Header->begin();
116 KLoop.Index = &*KLoop.Header->begin();
117
118 return InnerBody;
119}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
static const uint32_t IV[8]
Definition blake3_impl.h:83
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
BasicBlock * getSuccessor(unsigned i) const
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
void applyUpdatesPermissive(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
LoopT * AllocateLoop(ArgsTy &&...Args)
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:297
@ BR
Control flow instructions. These all have token chains.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
cl::opt< bool > ProfcheckDisableMetadataFixes
Definition Metadata.cpp:64
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI void setFittedBranchWeights(Instruction &I, ArrayRef< uint64_t > Weights, bool IsExpected, bool ElideAllZero=false)
Variant of setBranchWeights where the Weights will be fit first to uint32_t by shifting right.
unsigned NumInner
Number of columns of the first matrix of a multiply / number of rows of the second matrix of a multip...
Definition MatrixUtils.h:41
MatrixLoop ColumnLoop
The loop iterating on the columns.
Definition MatrixUtils.h:58
MatrixLoop RowLoop
The loop iterating on the rows.
Definition MatrixUtils.h:56
unsigned NumRows
Number of rows of the matrix.
Definition MatrixUtils.h:34
BasicBlock * CreateTiledLoops(BasicBlock *Start, BasicBlock *End, IRBuilderBase &B, DomTreeUpdater &DTU, LoopInfo &LI)
Creates an IR loop nests for tiling of the form below.
unsigned NumColumns
Number of columns of the matrix.
Definition MatrixUtils.h:37
unsigned TileSize
Number of rows/columns in a tile.
Definition MatrixUtils.h:44
MatrixLoop KLoop
The loop iterating on k (inner dimension).
Definition MatrixUtils.h:60