[flang-commits] [flang] [flang] Added definition of hlfir.cshift operation. (PR #118732)
via flang-commits
flang-commits at lists.llvm.org
Thu Dec 5 01:48:13 PST 2024
================
@@ -1341,6 +1341,91 @@ void hlfir::MatmulTransposeOp::getEffects(
getIntrinsicEffects(getOperation(), effects);
}
+//===----------------------------------------------------------------------===//
+// CShiftOp
+//===----------------------------------------------------------------------===//
+
+llvm::LogicalResult hlfir::CShiftOp::verify() {
+ mlir::Value array = getArray();
+ fir::SequenceType arrayTy = mlir::cast<fir::SequenceType>(
+ hlfir::getFortranElementOrSequenceType(array.getType()));
+ llvm::ArrayRef<int64_t> inShape = arrayTy.getShape();
+ std::size_t arrayRank = inShape.size();
+ mlir::Type eleTy = arrayTy.getEleTy();
+ hlfir::ExprType resultTy = mlir::cast<hlfir::ExprType>(getResult().getType());
+ llvm::ArrayRef<int64_t> resultShape = resultTy.getShape();
+ std::size_t resultRank = resultShape.size();
+ mlir::Type resultEleTy = resultTy.getEleTy();
+ mlir::Value shift = getShift();
+ mlir::Type shiftTy = hlfir::getFortranElementOrSequenceType(shift.getType());
+
+ if (eleTy != resultEleTy)
+ return emitOpError(
+ "input and output arrays should have the same element type");
+
+ if (arrayRank != resultRank)
+ return emitOpError("input and output arrays should have the same rank");
+
+ constexpr int64_t unknownExtent = fir::SequenceType::getUnknownExtent();
+ for (auto [inDim, resultDim] : llvm::zip(inShape, resultShape))
+ if (inDim != unknownExtent && resultDim != unknownExtent &&
+ inDim != resultDim)
+ return emitOpError(
+ "output array's shape conflicts with the input array's shape");
+
+ int64_t dimVal = -1;
+ if (!getDim())
+ dimVal = 1;
+ else if (auto dim = fir::getIntIfConstant(getDim()))
+ dimVal = *dim;
+
+ if (dimVal != -1) {
+ if (dimVal < 1)
+ return emitOpError("DIM must be >= 1");
+ if (dimVal > static_cast<int64_t>(arrayRank))
+ return emitOpError("DIM must be <= input array's rank");
+ }
+
+ if (auto shiftSeqTy = mlir::dyn_cast<fir::SequenceType>(shiftTy)) {
+ // SHIFT is an array. Verify the rank and the shape (if DIM is constant).
+ llvm::ArrayRef<int64_t> shiftShape = shiftSeqTy.getShape();
+ std::size_t shiftRank = shiftShape.size();
+ if (shiftRank != arrayRank - 1)
+ return emitOpError(
+ "SHIFT's rank must be 1 less than the input array's rank");
+
+ if (dimVal != -1) {
----------------
jeanPerier wrote:
Same here, this may be checking too much.
Maybe what we need for HLFIR ops is some kind of `verifyLanguageRequirements()` concept, that when false, would allow us to replace the operation with a no return abort + error message so that the optimizer can prune code after the invalid intrinsic and we can get some kind of semantic checking after optimizations without triggering bogus compile time errors in unreachable code.
https://github.com/llvm/llvm-project/pull/118732
More information about the flang-commits
mailing list