1 //===- StaticValueUtils.h - Utilities for static values ---------*- 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 // This header file defines utilities for dealing with static values, e.g., 10 // converting back and forth between Value and OpFoldResult. Such functionality 11 // is used in multiple dialects. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef MLIR_DIALECT_UTILS_STATICVALUEUTILS_H 16 #define MLIR_DIALECT_UTILS_STATICVALUEUTILS_H 17 18 #include "mlir/IR/OpDefinition.h" 19 #include "mlir/Support/LLVM.h" 20 #include "llvm/ADT/SmallVector.h" 21 22 namespace mlir { 23 24 /// Helper function to dispatch an OpFoldResult into `staticVec` if: 25 /// a) it is an IntegerAttr 26 /// In other cases, the OpFoldResult is dispached to the `dynamicVec`. 27 /// In such dynamic cases, a copy of the `sentinel` value is also pushed to 28 /// `staticVec`. This is useful to extract mixed static and dynamic entries that 29 /// come from an AttrSizedOperandSegments trait. 30 void dispatchIndexOpFoldResult(OpFoldResult ofr, 31 SmallVectorImpl<Value> &dynamicVec, 32 SmallVectorImpl<int64_t> &staticVec, 33 int64_t sentinel); 34 35 /// Helper function to dispatch multiple OpFoldResults according to the behavior 36 /// of `dispatchIndexOpFoldResult(OpFoldResult ofr` for a single OpFoldResult. 37 void dispatchIndexOpFoldResults(ArrayRef<OpFoldResult> ofrs, 38 SmallVectorImpl<Value> &dynamicVec, 39 SmallVectorImpl<int64_t> &staticVec, 40 int64_t sentinel); 41 42 /// Extract int64_t values from the assumed ArrayAttr of IntegerAttr. 43 SmallVector<int64_t, 4> extractFromI64ArrayAttr(Attribute attr); 44 45 /// Given a value, try to extract a constant Attribute. If this fails, return 46 /// the original value. 47 OpFoldResult getAsOpFoldResult(Value val); 48 49 /// Given an array of values, try to extract a constant Attribute from each 50 /// value. If this fails, return the original value. 51 SmallVector<OpFoldResult> getAsOpFoldResult(ArrayRef<Value> values); 52 53 /// Convert `arrayAttr` to a vector of OpFoldResult. 54 SmallVector<OpFoldResult> getAsOpFoldResult(ArrayAttr arrayAttr); 55 56 /// If ofr is a constant integer or an IntegerAttr, return the integer. 57 Optional<int64_t> getConstantIntValue(OpFoldResult ofr); 58 59 /// Return true if `ofr` is constant integer equal to `value`. 60 bool isConstantIntValue(OpFoldResult ofr, int64_t value); 61 62 /// Return true if ofr1 and ofr2 are the same integer constant attribute values 63 /// or the same SSA value. 64 /// Ignore integer bitwitdh and type mismatch that come from the fact there is 65 /// no IndexAttr and that IndexType have no bitwidth. 66 bool isEqualConstantIntOrValue(OpFoldResult ofr1, OpFoldResult ofr2); 67 68 } // namespace mlir 69 70 #endif // MLIR_DIALECT_UTILS_STATICVALUEUTILS_H 71