[flang-commits] [flang] [flang] Added hlfir.reshape definition/lowering/codegen. (PR #124226)

via flang-commits flang-commits at lists.llvm.org
Fri Jan 24 04:53:50 PST 2025


================
@@ -1444,6 +1444,63 @@ void hlfir::CShiftOp::getEffects(
   getIntrinsicEffects(getOperation(), effects);
 }
 
+//===----------------------------------------------------------------------===//
+// ReshapeOp
+//===----------------------------------------------------------------------===//
+
+llvm::LogicalResult hlfir::ReshapeOp::verify() {
+  auto results = this->getOperation()->getResultTypes();
+  assert(results.size() == 1);
+  hlfir::ExprType resultType = mlir::cast<hlfir::ExprType>(results[0]);
+  mlir::Value array = this->getArray();
+  auto arrayType = mlir::cast<fir::SequenceType>(
+      hlfir::getFortranElementOrSequenceType(array.getType()));
+  if (hlfir::getFortranElementType(resultType) != arrayType.getElementType())
+    return this->emitOpError(
+        "ARRAY and the result must have the same element type");
+  if (hlfir::isPolymorphicType(resultType) !=
+      hlfir::isPolymorphicType(array.getType()))
+    return this->emitOpError(
+        "ARRAY must be polymorphic iff result is polymorphic");
+
+  mlir::Value shape = this->getShape();
+  auto shapeArrayType = mlir::cast<fir::SequenceType>(
+      hlfir::getFortranElementOrSequenceType(shape.getType()));
+  if (shapeArrayType.getDimension() != 1)
+    return this->emitOpError("SHAPE must be an array of rank 1");
+  if (!mlir::isa<mlir::IntegerType>(shapeArrayType.getElementType()))
+    return this->emitOpError("SHAPE must be an integer array");
+  if (shapeArrayType.hasDynamicExtents())
+    return this->emitOpError("SHAPE must have known size");
+  if (shapeArrayType.getConstantArraySize() != resultType.getRank())
+    return this->emitOpError("SHAPE's extent must match the result rank");
+
+  if (mlir::Value pad = this->getPad()) {
+    auto padArrayType = mlir::cast<fir::SequenceType>(
+        hlfir::getFortranElementOrSequenceType(pad.getType()));
+    if (arrayType.getElementType() != padArrayType.getElementType())
----------------
jeanPerier wrote:

Needs to be relaxed for characters I think: dynamic length vs constant length is possible (must match at runtime):

```
subroutine test(c1, c2, pad)
    character(*)  :: c1(10), c2(2,4)
    character(5) :: pad(2)
    c1 = reshape(c2, shape = [10],  pad = pad)
end subroutine
``` 

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


More information about the flang-commits mailing list