[Mlir-commits] [mlir] [mlir][vector]add foldConstantOp fold function and apply it to extractOp and insertOp. (PR #124399)
lonely eagle
llvmlistbot at llvm.org
Sat Jan 25 18:07:52 PST 2025
================
@@ -1977,6 +1977,46 @@ 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 void 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;
+ 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;
+ }
+
+ if (auto constantOp =
+ mlir::dyn_cast<arith::ConstantIndexOp>(position.getDefiningOp())) {
+ opChange = true;
+ staticPosition[i] = constantOp.value();
+ continue;
+ }
----------------
linuxlonelyeagle wrote:
I didn't test this further, I think the key point is that the operand returned by `adaptor` in the `insertOp` or `extractOp` case in the dynamic position is an array of `ArrayRef<Attribute>`,If it's `arith::constant`, it will return the correct value,so I'm making a more stringent judgment here that it's an `arith::constant`.
Talking about my other idea, at first I wanted to support fold `llvm.constant`.But later on I disabused of this idea, because I cited that `llvm.constant` and `vector.insert` are incompatible, `llvm.constant` returns `i64`, while `vector.insert` needs the `SSA Value of index`.They need `buildin.unrealized_conversion_cast` for adaptation.
https://github.com/llvm/llvm-project/pull/124399
More information about the Mlir-commits
mailing list