MLIR  15.0.0git
StaticValueUtils.cpp
Go to the documentation of this file.
1 //===- StaticValueUtils.cpp - Utilities for dealing with static values ----===//
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 
10 #include "mlir/IR/Matchers.h"
11 #include "mlir/Support/LLVM.h"
12 #include "llvm/ADT/APSInt.h"
13 
14 namespace mlir {
15 
16 /// Helper function to dispatch an OpFoldResult into `staticVec` if:
17 /// a) it is an IntegerAttr
18 /// In other cases, the OpFoldResult is dispached to the `dynamicVec`.
19 /// In such dynamic cases, a copy of the `sentinel` value is also pushed to
20 /// `staticVec`. This is useful to extract mixed static and dynamic entries that
21 /// come from an AttrSizedOperandSegments trait.
23  SmallVectorImpl<Value> &dynamicVec,
24  SmallVectorImpl<int64_t> &staticVec,
25  int64_t sentinel) {
26  auto v = ofr.dyn_cast<Value>();
27  if (!v) {
28  APInt apInt = ofr.get<Attribute>().cast<IntegerAttr>().getValue();
29  staticVec.push_back(apInt.getSExtValue());
30  return;
31  }
32  dynamicVec.push_back(v);
33  staticVec.push_back(sentinel);
34 }
35 
37  SmallVectorImpl<Value> &dynamicVec,
38  SmallVectorImpl<int64_t> &staticVec,
39  int64_t sentinel) {
40  for (OpFoldResult ofr : ofrs)
41  dispatchIndexOpFoldResult(ofr, dynamicVec, staticVec, sentinel);
42 }
43 
44 /// Extract int64_t values from the assumed ArrayAttr of IntegerAttr.
46  return llvm::to_vector<4>(
47  llvm::map_range(attr.cast<ArrayAttr>(), [](Attribute a) -> int64_t {
48  return a.cast<IntegerAttr>().getInt();
49  }));
50 }
51 
52 /// Given a value, try to extract a constant Attribute. If this fails, return
53 /// the original value.
55  Attribute attr;
56  if (matchPattern(val, m_Constant(&attr)))
57  return attr;
58  return val;
59 }
60 
61 /// Given an array of values, try to extract a constant Attribute from each
62 /// value. If this fails, return the original value.
64  return llvm::to_vector<4>(
65  llvm::map_range(values, [](Value v) { return getAsOpFoldResult(v); }));
66 }
67 
68 /// If ofr is a constant integer or an IntegerAttr, return the integer.
70  // Case 1: Check for Constant integer.
71  if (auto val = ofr.dyn_cast<Value>()) {
72  APSInt intVal;
73  if (matchPattern(val, m_ConstantInt(&intVal)))
74  return intVal.getSExtValue();
75  return llvm::None;
76  }
77  // Case 2: Check for IntegerAttr.
78  Attribute attr = ofr.dyn_cast<Attribute>();
79  if (auto intAttr = attr.dyn_cast_or_null<IntegerAttr>())
80  return intAttr.getValue().getSExtValue();
81  return llvm::None;
82 }
83 
84 /// Return true if `ofr` is constant integer equal to `value`.
86  auto val = getConstantIntValue(ofr);
87  return val && *val == value;
88 }
89 
90 /// Return true if ofr1 and ofr2 are the same integer constant attribute values
91 /// or the same SSA value.
92 /// Ignore integer bitwidth and type mismatch that come from the fact there is
93 /// no IndexAttr and that IndexType has no bitwidth.
95  auto cst1 = getConstantIntValue(ofr1), cst2 = getConstantIntValue(ofr2);
96  if (cst1 && cst2 && *cst1 == *cst2)
97  return true;
98  auto v1 = ofr1.dyn_cast<Value>(), v2 = ofr2.dyn_cast<Value>();
99  return v1 && v1 == v2;
100 }
101 } // namespace mlir
TODO: Remove this file when SCCP and integer range analysis have been ported to the new framework...
U cast() const
Definition: Attributes.h:130
U dyn_cast_or_null() const
Definition: Attributes.h:127
detail::constant_int_op_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
Definition: Matchers.h:356
void dispatchIndexOpFoldResults(ArrayRef< OpFoldResult > ofrs, SmallVectorImpl< Value > &dynamicVec, SmallVectorImpl< int64_t > &staticVec, int64_t sentinel)
Helper function to dispatch multiple OpFoldResults according to the behavior of dispatchIndexOpFoldRe...
This class represents a single result from folding an operation.
Definition: OpDefinition.h:229
static constexpr const bool value
Attributes are known-constant values of operations.
Definition: Attributes.h:24
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
Definition: Matchers.h:259
bool isConstantIntValue(OpFoldResult ofr, int64_t value)
Return true if ofr is constant integer equal to value.
void dispatchIndexOpFoldResult(OpFoldResult ofr, SmallVectorImpl< Value > &dynamicVec, SmallVectorImpl< int64_t > &staticVec, int64_t sentinel)
Helper function to dispatch an OpFoldResult into staticVec if: a) it is an IntegerAttr In other cases...
OpFoldResult getAsOpFoldResult(Value val)
Given a value, try to extract a constant Attribute.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:85
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
Definition: Matchers.h:333
SmallVector< int64_t, 4 > extractFromI64ArrayAttr(Attribute attr)
Extract int64_t values from the assumed ArrayAttr of IntegerAttr.
Optional< int64_t > getConstantIntValue(OpFoldResult ofr)
If ofr is a constant integer or an IntegerAttr, return the integer.
bool isEqualConstantIntOrValue(OpFoldResult ofr1, OpFoldResult ofr2)
Return true if ofr1 and ofr2 are the same integer constant attribute values or the same SSA value...