[Mlir-commits] [mlir] [mlir][vector]add foldConstantOp fold function and apply it to extractOp and insertOp. (PR #124399)

Oleksandr Alex Zinenko llvmlistbot at llvm.org
Sun Jan 26 00:34:10 PST 2025


================
@@ -1977,6 +1977,48 @@ static Value foldScalarExtractFromFromElements(ExtractOp extractOp) {
   return fromElementsOp.getElements()[flatIndex];
 }
 
+// If the dynamic operands of `extractOp` or `insertOp` is result of
+// `constantOp`, then fold it.
+template <typename T>
+static LogicalResult foldConstantOp(T op, SmallVectorImpl<Value> &operands) {
+  auto staticPosition = op.getStaticPosition().vec();
+  OperandRange dynamicPosition = op.getDynamicPosition();
+
+  // If the dynamic operands is empty, it is returned directly.
+  if (!dynamicPosition.size())
+    return failure();
+  unsigned index = 0;
+
+  // `opChange` is a flog. If it is true, it means to update `op` in place.
+  bool opChange = false;
+  for (unsigned i = 0, e = staticPosition.size(); i < e; ++i) {
+    if (!ShapedType::isDynamic(staticPosition[i]))
+      continue;
+    Value position = dynamicPosition[index++];
+
+    // If it is a block parameter, proceed to the next iteration.
+    if (!position.getDefiningOp()) {
+      operands.push_back(position);
+      continue;
+    }
+
+    APInt pos;
+    if (matchPattern(position, m_ConstantInt(&pos))) {
+      opChange = true;
+      staticPosition[i] = pos.getSExtValue();
+      continue;
+    }
----------------
ftynse wrote:

This shouldn't be necessary. The `fold` function takes an instance of `Adaptor` that will have attributes for any operand that has been deduced to be constant. And it will work transitively, e.g., if the operand itself is a result of constant folding, beyond explicit constants.

https://github.com/llvm/llvm-project/pull/124399


More information about the Mlir-commits mailing list