Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions flang/include/flang/Optimizer/Builder/CUFCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ int computeElementByteSize(mlir::Location loc, mlir::Type type,
fir::KindMapping &kindMap,
bool emitErrorOnFailure = true);

mlir::Value computeElementCount(mlir::PatternRewriter &rewriter,
mlir::Location loc, mlir::Value shapeOperand,
mlir::Type seqType, mlir::Type targetType);

} // namespace cuf

#endif // FORTRAN_OPTIMIZER_TRANSFORMS_CUFCOMMON_H_
41 changes: 41 additions & 0 deletions flang/lib/Optimizer/Builder/CUFCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,44 @@ int cuf::computeElementByteSize(mlir::Location loc, mlir::Type type,
mlir::emitError(loc, "unsupported type");
return 0;
}

mlir::Value cuf::computeElementCount(mlir::PatternRewriter &rewriter,
mlir::Location loc,
mlir::Value shapeOperand,
mlir::Type seqType,
mlir::Type targetType) {
if (shapeOperand) {
// Dynamic extent - extract from shape operand
llvm::SmallVector<mlir::Value> extents;
if (auto shapeOp =
mlir::dyn_cast<fir::ShapeOp>(shapeOperand.getDefiningOp())) {
extents = shapeOp.getExtents();
} else if (auto shapeShiftOp = mlir::dyn_cast<fir::ShapeShiftOp>(
shapeOperand.getDefiningOp())) {
for (auto i : llvm::enumerate(shapeShiftOp.getPairs()))
if (i.index() & 1)
extents.push_back(i.value());
}

if (extents.empty())
return mlir::Value();

// Compute total element count by multiplying all dimensions
mlir::Value count =
fir::ConvertOp::create(rewriter, loc, targetType, extents[0]);
for (unsigned i = 1; i < extents.size(); ++i) {
auto operand =
fir::ConvertOp::create(rewriter, loc, targetType, extents[i]);
count = mlir::arith::MulIOp::create(rewriter, loc, count, operand);
}
return count;
} else {
// Static extent - use constant array size
if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(seqType)) {
mlir::IntegerAttr attr =
rewriter.getIntegerAttr(targetType, seqTy.getConstantArraySize());
return mlir::arith::ConstantOp::create(rewriter, loc, targetType, attr);
}
}
return mlir::Value();
}
27 changes: 2 additions & 25 deletions flang/lib/Optimizer/Transforms/CUFOpConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,31 +651,8 @@ struct CUFDataTransferOpConversion
}

mlir::Type i64Ty = builder.getI64Type();
mlir::Value nbElement;
if (op.getShape()) {
llvm::SmallVector<mlir::Value> extents;
if (auto shapeOp =
mlir::dyn_cast<fir::ShapeOp>(op.getShape().getDefiningOp())) {
extents = shapeOp.getExtents();
} else if (auto shapeShiftOp = mlir::dyn_cast<fir::ShapeShiftOp>(
op.getShape().getDefiningOp())) {
for (auto i : llvm::enumerate(shapeShiftOp.getPairs()))
if (i.index() & 1)
extents.push_back(i.value());
}

nbElement = fir::ConvertOp::create(rewriter, loc, i64Ty, extents[0]);
for (unsigned i = 1; i < extents.size(); ++i) {
auto operand =
fir::ConvertOp::create(rewriter, loc, i64Ty, extents[i]);
nbElement =
mlir::arith::MulIOp::create(rewriter, loc, nbElement, operand);
}
} else {
if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(dstTy))
nbElement = builder.createIntegerConstant(
loc, i64Ty, seqTy.getConstantArraySize());
}
mlir::Value nbElement =
cuf::computeElementCount(rewriter, loc, op.getShape(), dstTy, i64Ty);
unsigned width = 0;
if (fir::isa_derived(fir::unwrapSequenceType(dstTy))) {
mlir::Type structTy =
Expand Down