LLVM 24.0.0git
Delinearization.h
Go to the documentation of this file.
1//===---- Delinearization.h - MultiDimensional Index Delinearization ------===//
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 implements an analysis pass that tries to delinearize all GEP
10// instructions in all loops using the SCEV analysis functionality.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_DELINEARIZATION_H
15#define LLVM_ANALYSIS_DELINEARIZATION_H
16
17#include "llvm/IR/PassManager.h"
18#include "llvm/IR/Value.h"
19
20namespace llvm {
21class raw_ostream;
22template <typename T> class SmallVectorImpl;
24class ScalarEvolution;
25class SCEV;
26
27/// Compute the array dimensions Sizes from the set of Terms extracted from
28/// the memory access function of this SCEVAddRecExpr (second step of
29/// delinearization).
33 const SCEV *ElementSize);
34
35/// Collect parametric terms occurring in step expressions (first step of
36/// delinearization).
39
40/// Return in Subscripts the access functions for each dimension in Sizes
41/// (third step of delinearization).
45/// Split this SCEVAddRecExpr into two vectors of SCEVs representing the
46/// subscripts and sizes of an array access.
47///
48/// The delinearization is a 3 step process: the first two steps compute the
49/// sizes of each subscript and the third step computes the access functions
50/// for the delinearized array:
51///
52/// 1. Find the terms in the step functions
53/// 2. Compute the array size
54/// 3. Compute the access function: divide the SCEV by the array size
55/// starting with the innermost dimensions found in step 2. The Quotient
56/// is the SCEV to be divided in the next step of the recursion. The
57/// Remainder is the subscript of the innermost dimension. Loop over all
58/// array dimensions computed in step 2.
59///
60/// To compute a uniform array size for several memory accesses to the same
61/// object, one can collect in step 1 all the step terms for all the memory
62/// accesses, and compute in step 2 a unique array shape. This guarantees
63/// that the array shape will be the same across all memory accesses.
64///
65/// FIXME: We could derive the result of steps 1 and 2 from a description of
66/// the array shape given in metadata.
67///
68/// Example:
69///
70/// A[][n][m]
71///
72/// for i
73/// for j
74/// for k
75/// A[j+k][2i][5i] =
76///
77/// The initial SCEV:
78///
79/// A[{{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k]
80///
81/// 1. Find the different terms in the step functions:
82/// -> [2*m, 5, n*m, n*m]
83///
84/// 2. Compute the array size: sort and unique them
85/// -> [n*m, 2*m, 5]
86/// find the GCD of all the terms = 1
87/// divide by the GCD and erase constant terms
88/// -> [n*m, 2*m]
89/// GCD = m
90/// divide by GCD -> [n, 2]
91/// remove constant terms
92/// -> [n]
93/// size of the array is A[unknown][n][m]
94///
95/// 3. Compute the access function
96/// a. Divide {{{0,+,2*m+5}_i, +, n*m}_j, +, n*m}_k by the innermost size m
97/// Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k
98/// Remainder: {{{0,+,5}_i, +, 0}_j, +, 0}_k
99/// The remainder is the subscript of the innermost array dimension: [5i].
100///
101/// b. Divide Quotient: {{{0,+,2}_i, +, n}_j, +, n}_k by next outer size n
102/// Quotient: {{{0,+,0}_i, +, 1}_j, +, 1}_k
103/// Remainder: {{{0,+,2}_i, +, 0}_j, +, 0}_k
104/// The Remainder is the subscript of the next array dimension: [2i].
105///
106/// The subscript of the outermost dimension is the Quotient: [j+k].
107///
108/// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i].
109LLVM_ABI void delinearize(ScalarEvolution &SE, const SCEV *Expr,
112 const SCEV *ElementSize);
113
114/// Compute the dimensions of fixed size array from \Expr and save the results
115/// in \p Sizes.
117 const SCEV *Expr,
119 const SCEV *ElementSize);
120
121/// Split this SCEVAddRecExpr into two vectors of SCEVs representing the
122/// subscripts and sizes of an access to a fixed size array. This is a special
123/// case of delinearization for fixed size arrays.
124///
125/// The delinearization is a 2 step process: the first step estimates the sizes
126/// of each dimension of the array. The second step computes the access
127/// functions for the delinearized array:
128///
129/// 1. Compute the array size
130/// 2. Compute the access function: same as normal delinearization
131///
132/// Different from the normal delinearization, this function assumes that NO
133/// terms exist in the \p Expr. In other words, it assumes that the all step
134/// values are constant.
135///
136/// This function is intended to replace getIndexExpressionsFromGEP. They rely
137/// on the GEP source element type so that will be removed in the future.
138LLVM_ABI bool
142 const SCEV *ElementSize);
143
144/// Check that each subscript in \p Subscripts is within the corresponding size
145/// in \p Sizes. For the outermost dimension, the subscript being negative is
146/// allowed.
149 ArrayRef<const SCEV *> Subscripts);
150
151/// Gathers the individual index expressions from a GEP instruction.
152///
153/// This function optimistically assumes the GEP references into a fixed size
154/// array. If this is actually true, this function returns a list of array
155/// subscript expressions in \p Subscripts and a list of SCEV expressions
156/// describing the size of the individual array dimensions in \p Sizes. Both
157/// lists have either equal length or the size list is one element shorter in
158/// case there is no known size available for the outermost array dimension.
159/// Returns true if successful and false otherwise.
160LLVM_ABI bool
164
166 : public RequiredPassInfoMixin<DelinearizationPrinterPass> {
169
170private:
171 raw_ostream &OS;
172};
173} // namespace llvm
174
175#endif // LLVM_ANALYSIS_DELINEARIZATION_H
#define LLVM_ABI
Definition Compiler.h:211
Hexagon Common GEP
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
This class represents an analyzed expression in the program.
The main scalar evolution driver.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void collectParametricTerms(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Terms)
Collect parametric terms occurring in step expressions (first step of delinearization).
LLVM_ABI void findArrayDimensions(ScalarEvolution &SE, SmallVectorImpl< const SCEV * > &Terms, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize)
Compute the array dimensions Sizes from the set of Terms extracted from the memory access function of...
LLVM_ABI bool validateDelinearizationResult(ScalarEvolution &SE, ArrayRef< const SCEV * > Sizes, ArrayRef< const SCEV * > Subscripts)
Check that each subscript in Subscripts is within the corresponding size in Sizes.
LLVM_ABI void computeAccessFunctions(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes)
Return in Subscripts the access functions for each dimension in Sizes (third step of delinearization)...
LLVM_ABI bool delinearizeFixedSizeArray(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize)
Split this SCEVAddRecExpr into two vectors of SCEVs representing the subscripts and sizes of an acces...
LLVM_ABI void delinearize(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes, const SCEV *ElementSize)
Split this SCEVAddRecExpr into two vectors of SCEVs representing the subscripts and sizes of an array...
LLVM_ABI bool findFixedSizeArrayDimensions(ScalarEvolution &SE, const SCEV *Expr, SmallVectorImpl< uint64_t > &Sizes, const SCEV *ElementSize)
Compute the dimensions of fixed size array from \Expr and save the results in Sizes.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI bool getIndexExpressionsFromGEP(ScalarEvolution &SE, const GetElementPtrInst *GEP, SmallVectorImpl< const SCEV * > &Subscripts, SmallVectorImpl< const SCEV * > &Sizes)
Gathers the individual index expressions from a GEP instruction.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI DelinearizationPrinterPass(raw_ostream &OS)
A CRTP mix-in for passes that should not be skipped.