LLVM 24.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"
22
23using namespace llvm;
24
25BasicBlock *TileInfo::CreateLoop(BasicBlock *Preheader, BasicBlock *Exit,
26 ConstantInt *Bound, ConstantInt *Step,
28 DomTreeUpdater &DTU, Loop *L, LoopInfo &LI) {
29 LLVMContext &Ctx = Preheader->getContext();
31 Preheader->getContext(), Name + ".header", Preheader->getParent(), Exit);
32 BasicBlock *Body = BasicBlock::Create(Header->getContext(), Name + ".body",
33 Header->getParent(), Exit);
34 BasicBlock *Latch = BasicBlock::Create(Header->getContext(), Name + ".latch",
35 Header->getParent(), Exit);
36
37 Type *I32Ty = Type::getInt64Ty(Ctx);
38 UncondBrInst::Create(Body, Header);
39 UncondBrInst::Create(Latch, Body);
40 PHINode *IV =
41 PHINode::Create(I32Ty, 2, Name + ".iv", Header->getTerminator()->getIterator());
42 IV->addIncoming(ConstantInt::get(I32Ty, 0), Preheader);
43
44 B.SetInsertPoint(Latch);
45 Value *Inc = B.CreateAdd(IV, Step, Name + ".step");
46 Value *Cond = B.CreateICmpNE(Inc, Bound, Name + ".cond");
47 auto *BR = B.CreateCondBr(Cond, Header, Exit);
48 assert(Step->getZExtValue() != 0 &&
49 "Expected a non-zero step size. This is chosen by the pass and "
50 "should always be non-zero to imply a finite loop.");
51 MDBuilder MDB(Preheader->getContext());
52 setFittedBranchWeights(*BR, {Bound->getZExtValue() / Step->getZExtValue(), 1},
53 false);
54 IV->addIncoming(Inc, Latch);
55
56 UncondBrInst *PreheaderBr = cast<UncondBrInst>(Preheader->getTerminator());
57 BasicBlock *Tmp = PreheaderBr->getSuccessor();
58 PreheaderBr->setSuccessor(0, Header);
60 {DominatorTree::Delete, Preheader, Tmp},
61 {DominatorTree::Insert, Header, Body},
62 {DominatorTree::Insert, Body, Latch},
63 {DominatorTree::Insert, Latch, Header},
65 {DominatorTree::Insert, Preheader, Header},
66 });
67
68 L->addBasicBlockToLoop(Header, LI);
69 L->addBasicBlockToLoop(Body, LI);
70 L->addBasicBlockToLoop(Latch, LI);
71 return Body;
72}
73
74// Creates the following loop nest skeleton:
75// for C = 0; C < NumColumns; C += TileSize
76// for R = 0; R < NumRows; R += TileSize
77// for K = 0; K < Inner ; K += TileSize
80 LoopInfo &LI) {
81 Loop *ColumnLoopInfo = LI.AllocateLoop();
82 Loop *RowLoopInfo = LI.AllocateLoop();
83 Loop *KLoopInfo = LI.AllocateLoop();
84 RowLoopInfo->addChildLoop(KLoopInfo);
85 ColumnLoopInfo->addChildLoop(RowLoopInfo);
86 if (Loop *ParentL = LI.getLoopFor(Start))
87 ParentL->addChildLoop(ColumnLoopInfo);
88 else
89 LI.addTopLevelLoop(ColumnLoopInfo);
90
91 BasicBlock *ColBody =
92 CreateLoop(Start, End, B.getInt64(NumColumns), B.getInt64(TileSize),
93 "cols", B, DTU, ColumnLoopInfo, LI);
94 ColumnLoop.Latch = ColBody->getSingleSuccessor();
95 BasicBlock *RowBody =
96 CreateLoop(ColBody, ColumnLoop.Latch, B.getInt64(NumRows),
97 B.getInt64(TileSize), "rows", B, DTU, RowLoopInfo, LI);
98 RowLoop.Latch = RowBody->getSingleSuccessor();
99
100 BasicBlock *InnerBody =
101 CreateLoop(RowBody, RowLoop.Latch, B.getInt64(NumInner),
102 B.getInt64(TileSize), "inner", B, DTU, KLoopInfo, LI);
103 KLoop.Latch = InnerBody->getSingleSuccessor();
104 ColumnLoop.Header = ColBody->getSinglePredecessor();
105 RowLoop.Header = RowBody->getSinglePredecessor();
106 KLoop.Header = InnerBody->getSinglePredecessor();
107 RowLoop.Index = &*RowLoop.Header->begin();
108 ColumnLoop.Index = &*ColumnLoop.Header->begin();
109 KLoop.Index = &*KLoop.Header->begin();
110
111 return InnerBody;
112}
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.
SmallVector< MachineOperand, 4 > 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; assumes that the block is well-formed.
Definition BasicBlock.h:237
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:111
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 * 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...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:295
void setSuccessor(BasicBlock *NewSucc)
static UncondBrInst * Create(BasicBlock *Target, InsertPosition InsertBefore=nullptr)
BasicBlock * getSuccessor(unsigned i=0) const
@ BR
Control flow instructions. These all have token chains.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
This is an optimization pass for GlobalISel generic memory operations.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320
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
LLVM_ABI 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