[flang-commits] [flang] [flang] Inline hlfir.reshape as hlfir.elemental. (PR #124683)
Slava Zakharin via flang-commits
flang-commits at lists.llvm.org
Tue Jan 28 12:27:20 PST 2025
================
@@ -951,6 +951,213 @@ class DotProductConversion
}
};
+class ReshapeAsElementalConversion
+ : public mlir::OpRewritePattern<hlfir::ReshapeOp> {
+public:
+ using mlir::OpRewritePattern<hlfir::ReshapeOp>::OpRewritePattern;
+
+ llvm::LogicalResult
+ matchAndRewrite(hlfir::ReshapeOp reshape,
+ mlir::PatternRewriter &rewriter) const override {
+ // Do not inline RESHAPE with ORDER yet. The runtime implementation
+ // may be good enough, unless the temporary creation overhead
+ // is high.
+ // TODO: If ORDER is constant, then we can still easily inline.
+ // TODO: If the result's rank is 1, then we can assume ORDER == (/1/).
+ if (reshape.getOrder())
+ return rewriter.notifyMatchFailure(reshape,
+ "RESHAPE with ORDER argument");
+
+ // Verify that the element types of ARRAY, PAD and the result
+ // match before doing any transformations.
+ hlfir::Entity result = hlfir::Entity{reshape};
+ hlfir::Entity array = hlfir::Entity{reshape.getArray()};
+ mlir::Type elementType = array.getFortranElementType();
+ if (result.getFortranElementType() != elementType)
+ return rewriter.notifyMatchFailure(
+ reshape, "ARRAY and result have different types");
+ mlir::Value pad = reshape.getPad();
+ if (pad && hlfir::getFortranElementType(pad.getType()) != elementType)
+ return rewriter.notifyMatchFailure(reshape,
+ "ARRAY and PAD have different types");
----------------
vzakhari wrote:
Yes, and also avoid inlining in the dead code to (hopefully) get less code. I am also concerned about the inline implementation failing verification due to types mismatch in the newely generated operations, but maybe it will never happen.
https://github.com/llvm/llvm-project/pull/124683
More information about the flang-commits
mailing list