diff --git a/externals/llvm-project b/externals/llvm-project index 3a020527c..7ccbb4dff 160000 --- a/externals/llvm-project +++ b/externals/llvm-project @@ -1 +1 @@ -Subproject commit 3a020527c2af10741b12e756de45bd6f774885a4 +Subproject commit 7ccbb4dff10efe6c26219204e361ddb0264938b8 diff --git a/externals/mlir-hlo b/externals/mlir-hlo index 8df20065b..8c703fabd 160000 --- a/externals/mlir-hlo +++ b/externals/mlir-hlo @@ -1 +1 @@ -Subproject commit 8df20065b22be628f2d365c387200df7d02b80c1 +Subproject commit 8c703fabd60d4447bc86f432446e9ad0eacab600 diff --git a/include/torch-mlir/Conversion/TorchToTosa/TosaLegalizeCommon.h b/include/torch-mlir/Conversion/TorchToTosa/TosaLegalizeCommon.h index 0c538e08c..bb5f4ce34 100644 --- a/include/torch-mlir/Conversion/TorchToTosa/TosaLegalizeCommon.h +++ b/include/torch-mlir/Conversion/TorchToTosa/TosaLegalizeCommon.h @@ -17,43 +17,43 @@ namespace mlir { namespace tosa { // Lowers ReduceAll to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceAllOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims); // Lowers ReduceAny to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceAnyOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims); // Lowers ReduceMin to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceMinOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims); // Lowers ReduceMax to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceMaxOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims); // Lowers ReduceProd to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceProdOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims); // Lowers ReduceSum to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceSumOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims); // Lowers ReduceMean to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceMeanOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims); diff --git a/include/torch-mlir/Conversion/TorchToTosa/TosaLegalizeUtils.h b/include/torch-mlir/Conversion/TorchToTosa/TosaLegalizeUtils.h index 3d8e4bffc..15df74e21 100644 --- a/include/torch-mlir/Conversion/TorchToTosa/TosaLegalizeUtils.h +++ b/include/torch-mlir/Conversion/TorchToTosa/TosaLegalizeUtils.h @@ -50,8 +50,8 @@ Value getTosaConstTensorSingleF32(PatternRewriter &rewriter, Operation *op, // Default template creates a constant tensor in T. // To create INT48 TOSA constant, need to pass in llvm::APInt instead. template -llvm::Optional getConstTensor(PatternRewriter &rewriter, Operation *op, - ArrayRef vec, ArrayRef shape); +std::optional getConstTensor(PatternRewriter &rewriter, Operation *op, + ArrayRef vec, ArrayRef shape); LogicalResult tosaCastTensorToType(PatternRewriter &rewriter, Operation *op, Value src, Type destType, Value &result); diff --git a/include/torch-mlir/Conversion/Utils/Utils.h b/include/torch-mlir/Conversion/Utils/Utils.h index 7a9c10836..00917c91c 100644 --- a/include/torch-mlir/Conversion/Utils/Utils.h +++ b/include/torch-mlir/Conversion/Utils/Utils.h @@ -82,9 +82,8 @@ SmallVector getTypeConvertedValues(OpBuilder &b, Location loc, // Convert a scalar value to the target type. The scalar value can be an element // from a tensor or a scalar in the pytorch dialect. Both the scalar and dtype // should be converted builtin types. -Value convertScalarToDtype( - OpBuilder &b, Location loc, Value scalar, Type dtype, - llvm::Optional srcOriginalDtype = std::nullopt); +Value convertScalarToDtype(OpBuilder &b, Location loc, Value scalar, Type dtype, + std::optional srcOriginalDtype = std::nullopt); } // namespace Torch } // namespace torch diff --git a/include/torch-mlir/Dialect/Torch/IR/TorchOps.h b/include/torch-mlir/Dialect/Torch/IR/TorchOps.h index 30ba246d9..9499e34b9 100644 --- a/include/torch-mlir/Dialect/Torch/IR/TorchOps.h +++ b/include/torch-mlir/Dialect/Torch/IR/TorchOps.h @@ -174,11 +174,11 @@ m_TorchListOfConstantInts(SmallVectorImpl &bind_values) { namespace detail { /// Matches the optional constant integers stored in a `torch.ListConstruct`. struct torch_list_of_optional_constant_ints_op_binder { - SmallVectorImpl> &bind_values; + SmallVectorImpl> &bind_values; /// Creates a matcher instance that binds the value to bvs if match succeeds. torch_list_of_optional_constant_ints_op_binder( - SmallVectorImpl> &bvs) + SmallVectorImpl> &bvs) : bind_values(bvs) {} bool match(Operation *op) { @@ -203,7 +203,7 @@ struct torch_list_of_optional_constant_ints_op_binder { /// `torch.prim.ListConstruct`. inline detail::torch_list_of_optional_constant_ints_op_binder m_TorchListOfOptionalConstantInts( - SmallVectorImpl> &bind_values) { + SmallVectorImpl> &bind_values) { return detail::torch_list_of_optional_constant_ints_op_binder(bind_values); } diff --git a/include/torch-mlir/Dialect/Torch/IR/TorchTypes.h b/include/torch-mlir/Dialect/Torch/IR/TorchTypes.h index 59be99c88..de77a1a8f 100644 --- a/include/torch-mlir/Dialect/Torch/IR/TorchTypes.h +++ b/include/torch-mlir/Dialect/Torch/IR/TorchTypes.h @@ -31,8 +31,8 @@ class ValueTensorType; /// Common getter function signature that covers all tensor types. /// Used for sharing code between NonValueTensorType and ValueTensorType. -using GetTensorTypeFn = - llvm::function_ref>, Type)>; +using GetTensorTypeFn = llvm::function_ref>, Type)>; /// The representation of an unknown dimension size in an ArrayRef. constexpr static int64_t kUnknownSize = -1; @@ -45,7 +45,7 @@ public: /// /// It is expected that for many users, `hasSizes`/`getSizes` will be a more /// convenient API. - Optional> getOptionalSizes() const; + std::optional> getOptionalSizes() const; /// Get the raw nullable Type representing the dtype of this tensor type. /// @@ -90,7 +90,7 @@ public: /// Return a type of the same kind as this one, but with given raw optional /// sizes and raw optional dtype. - Type getWithSizesAndDtype(Optional> optionalSizes, + Type getWithSizesAndDtype(std::optional> optionalSizes, Type optionalDtype) const; /// Return a type with the same shape and dtype as this one, but with @@ -127,7 +127,8 @@ namespace mlir { namespace torch { namespace Torch { -inline Optional> BaseTensorType::getOptionalSizes() const { +inline std::optional> +BaseTensorType::getOptionalSizes() const { if (auto tensor = dyn_cast()) return tensor.getOptionalSizes(); if (auto tensor = dyn_cast()) diff --git a/include/torch-mlir/Dialect/Torch/IR/TorchTypes.td b/include/torch-mlir/Dialect/Torch/IR/TorchTypes.td index 226250453..f6f64d6ca 100644 --- a/include/torch-mlir/Dialect/Torch/IR/TorchTypes.td +++ b/include/torch-mlir/Dialect/Torch/IR/TorchTypes.td @@ -47,7 +47,7 @@ def Torch_NnModuleType : Torch_Type<"NnModule", "nn.Module"> { // For standard ArrayRefs, which require allocation. class OptionalArrayRefTorchParameter : AttrOrTypeParameter< - "::llvm::Optional<::llvm::ArrayRef<" # arrayOf # ">>", desc> { + "::std::optional<::llvm::ArrayRef<" # arrayOf # ">>", desc> { let allocator = [{ if ($_self.has_value()) { $_dst.value() = $_allocator.copyInto($_self.value()); diff --git a/include/torch-mlir/Dialect/Torch/Utils/Utils.h b/include/torch-mlir/Dialect/Torch/Utils/Utils.h index 55e840301..a5cbcf52c 100644 --- a/include/torch-mlir/Dialect/Torch/Utils/Utils.h +++ b/include/torch-mlir/Dialect/Torch/Utils/Utils.h @@ -23,8 +23,8 @@ bool getListConstructElements(Value v, SmallVectorImpl &elems); /// Returns the index indicated by `v` for a list of given `length`. /// If the index is negative, it is adjusted to `length` + `v`. /// `None` is returned the index is not an integer in the range [0,`length). -llvm::Optional matchLegalConstantIndexIntoListOfSize(Value v, - int64_t length); +std::optional matchLegalConstantIndexIntoListOfSize(Value v, + int64_t length); torch_upstream::ScalarType getScalarTypeForType(Type type); Type getTypeForScalarType( MLIRContext *context, torch_upstream::ScalarType dtypeInt, @@ -62,7 +62,7 @@ bool isBuiltInType(Type type); // Helper funtion to get rank of `Base tensor type`. // std::nullopt is returned if the tensorRank can't be determined. -Optional getTensorRank(Value tensor); +std::optional getTensorRank(Value tensor); bool isViewLikeOp(Operation *op); diff --git a/lib/CAPI/TorchTypes.cpp b/lib/CAPI/TorchTypes.cpp index 98c2e6085..4d50530f1 100644 --- a/lib/CAPI/TorchTypes.cpp +++ b/lib/CAPI/TorchTypes.cpp @@ -198,7 +198,7 @@ MlirType torchMlirTorchNonValueTensorTypeGet(MlirContext context, intptr_t numSizes, const int64_t *optionalSizes, MlirType optionalDtype) { - Optional> optionalSizesArrayRef = std::nullopt; + std::optional> optionalSizesArrayRef = std::nullopt; // if numSizes == -1, then it is unranked. if (numSizes > -1) optionalSizesArrayRef = llvm::makeArrayRef(optionalSizes, numSizes); @@ -232,7 +232,7 @@ MlirType torchMlirTorchValueTensorTypeGet(MlirContext context, intptr_t numSizes, const int64_t *optionalSizes, MlirType optionalDtype) { - Optional> optionalSizesArrayRef = std::nullopt; + std::optional> optionalSizesArrayRef = std::nullopt; // if numSizes == -1, then it is unranked. if (numSizes > -1) optionalSizesArrayRef = llvm::makeArrayRef(optionalSizes, numSizes); diff --git a/lib/Conversion/TorchToLinalg/DataMovement.cpp b/lib/Conversion/TorchToLinalg/DataMovement.cpp index 563720360..6ace4926d 100644 --- a/lib/Conversion/TorchToLinalg/DataMovement.cpp +++ b/lib/Conversion/TorchToLinalg/DataMovement.cpp @@ -393,7 +393,7 @@ public: // is violated for the cases of dynamic dimensions. SmallVector outputShape(resultRank, kUnknownSize); SmallVector unchangedDims; - llvm::Optional inferredDimension; + std::optional inferredDimension; for (auto en : llvm::enumerate(outputSizeTorchInt)) { int64_t inputDim; int64_t size; @@ -644,8 +644,8 @@ public: makeShapeLLVMCompatible(inputShapeVec), resultType.getElementType()); Value castedInput = rewriter.create(loc, adjustedInputType, input); - llvm::Optional expandedInput; - llvm::Optional collapsedInput; + std::optional expandedInput; + std::optional collapsedInput; if (llvm::any_of(inputAssociations, [](ReassociationIndices indices) { return indices.size() > 1; diff --git a/lib/Conversion/TorchToMhlo/MhloLegalizeUtils.cpp b/lib/Conversion/TorchToMhlo/MhloLegalizeUtils.cpp index 04ba232ae..b9fb00aff 100644 --- a/lib/Conversion/TorchToMhlo/MhloLegalizeUtils.cpp +++ b/lib/Conversion/TorchToMhlo/MhloLegalizeUtils.cpp @@ -49,8 +49,8 @@ Value getMhloConstTensorSingleF64(PatternRewriter &rewriter, Operation *op, // T: storage C type. // Default template creates a constant tensor in T. template -llvm::Optional getConstTensor(PatternRewriter &rewriter, Operation *op, - ArrayRef vec, ArrayRef shape) { +std::optional getConstTensor(PatternRewriter &rewriter, Operation *op, + ArrayRef vec, ArrayRef shape) { uint64_t num_total_elements = 1; for (int64_t a : shape) { num_total_elements *= a; @@ -72,9 +72,9 @@ llvm::Optional getConstTensor(PatternRewriter &rewriter, Operation *op, // Template specialization for APInt template <> -llvm::Optional getConstTensor(PatternRewriter &rewriter, - Operation *op, ArrayRef vec, - ArrayRef shape) { +std::optional getConstTensor(PatternRewriter &rewriter, + Operation *op, ArrayRef vec, + ArrayRef shape) { uint64_t num_total_elements = 1; for (int64_t a : shape) { num_total_elements *= a; @@ -95,9 +95,9 @@ llvm::Optional getConstTensor(PatternRewriter &rewriter, // Template specialization for float template <> -llvm::Optional getConstTensor(PatternRewriter &rewriter, - Operation *op, ArrayRef vec, - ArrayRef shape) { +std::optional getConstTensor(PatternRewriter &rewriter, + Operation *op, ArrayRef vec, + ArrayRef shape) { uint64_t num_total_elements = 1; for (int64_t a : shape) { num_total_elements *= a; @@ -117,9 +117,9 @@ llvm::Optional getConstTensor(PatternRewriter &rewriter, } template <> -llvm::Optional -getConstTensor(PatternRewriter &rewriter, Operation *op, - ArrayRef vec, ArrayRef shape) { +std::optional getConstTensor(PatternRewriter &rewriter, + Operation *op, ArrayRef vec, + ArrayRef shape) { uint64_t num_total_elements = 1; for (int64_t a : shape) { num_total_elements *= a; @@ -139,15 +139,15 @@ getConstTensor(PatternRewriter &rewriter, Operation *op, } // Template instantiation -template llvm::Optional getConstTensor(PatternRewriter &, - Operation *, - ArrayRef vec, - ArrayRef shape); +template std::optional getConstTensor(PatternRewriter &, + Operation *, + ArrayRef vec, + ArrayRef shape); -template llvm::Optional getConstTensor(PatternRewriter &, - Operation *, - ArrayRef vec, - ArrayRef shape); +template std::optional getConstTensor(PatternRewriter &, + Operation *, + ArrayRef vec, + ArrayRef shape); template static bool isInValidRange(bool isFloat, const double &doubleValue, bool isInt, diff --git a/lib/Conversion/TorchToMhlo/MhloLegalizeUtils.h b/lib/Conversion/TorchToMhlo/MhloLegalizeUtils.h index 466cadb81..dc7daa42d 100644 --- a/lib/Conversion/TorchToMhlo/MhloLegalizeUtils.h +++ b/lib/Conversion/TorchToMhlo/MhloLegalizeUtils.h @@ -35,8 +35,8 @@ Value getMhloConstTensorSingleF64(PatternRewriter &rewriter, Operation *op, // Default template creates a constant tensor in T. // To create INT48 MHLO constant, need to pass in llvm::APInt instead. template -llvm::Optional getConstTensor(PatternRewriter &rewriter, Operation *op, - ArrayRef vec, ArrayRef shape); +std::optional getConstTensor(PatternRewriter &rewriter, Operation *op, + ArrayRef vec, ArrayRef shape); template Value getSplatConstTensor(ConversionPatternRewriter &rewriter, Operation *op, diff --git a/lib/Conversion/TorchToMhlo/Reduction.cpp b/lib/Conversion/TorchToMhlo/Reduction.cpp index 4fdb614f5..20a6c377e 100644 --- a/lib/Conversion/TorchToMhlo/Reduction.cpp +++ b/lib/Conversion/TorchToMhlo/Reduction.cpp @@ -71,7 +71,7 @@ static Value createInitialValueForReduceOp(Operation *op, Type elementTy, } // Util for converting AtenArgmaxOp and AtenMaxDimOp -static llvm::Optional +static std::optional getMaxInDim(ConversionPatternRewriter &rewriter, Operation *op, Value &input, ArrayRef inputShapeVec, int64_t dim, size_t dimSizeIndexBits) { diff --git a/lib/Conversion/TorchToMhlo/ViewLike.cpp b/lib/Conversion/TorchToMhlo/ViewLike.cpp index 8934d0ff2..29284d50e 100644 --- a/lib/Conversion/TorchToMhlo/ViewLike.cpp +++ b/lib/Conversion/TorchToMhlo/ViewLike.cpp @@ -109,9 +109,9 @@ Value getDynamicSliceInternal(PatternRewriter &rewriter, Operation *op, // endIndex(default to dimSize), and step(default to 1) can be optional. FailureOr getDynamicSlice(PatternRewriter &rewriter, Operation *op, Type outTy, Value input, - llvm::Optional startIndexOpt, - llvm::Optional endIndexOpt, - llvm::Optional stepOpt, int64_t dim, + std::optional startIndexOpt, + std::optional endIndexOpt, + std::optional stepOpt, int64_t dim, size_t dimSizeIndexBits) { auto loc = op->getLoc(); auto inputTy = input.getType().dyn_cast(); @@ -267,7 +267,7 @@ LogicalResult ConvertAtenOp::matchAndRewrite( return rewriter.notifyMatchFailure( op, "only constant dim is currently supported"); - auto getOptionalVal = [&](Value val) -> llvm::Optional { + auto getOptionalVal = [&](Value val) -> std::optional { if (val.getType().isa()) { return std::nullopt; } else { @@ -275,9 +275,9 @@ LogicalResult ConvertAtenOp::matchAndRewrite( } }; - llvm::Optional start = getOptionalVal(adaptor.getStart()); - llvm::Optional end = getOptionalVal(adaptor.getEnd()); - llvm::Optional step = getOptionalVal(adaptor.getStep()); + std::optional start = getOptionalVal(adaptor.getStart()); + std::optional end = getOptionalVal(adaptor.getEnd()); + std::optional step = getOptionalVal(adaptor.getStep()); FailureOr sliceInfo = getDynamicSlice(rewriter, op, outTy, self, start, end, step, dim, diff --git a/lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp b/lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp index 53103809c..b80c35c14 100644 --- a/lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp +++ b/lib/Conversion/TorchToTMTensor/TorchToTMTensor.cpp @@ -279,7 +279,7 @@ public: // to i32 as required for the scatter op. // 2.) `values` is mapped to `updates` in scatter op. // 3.) `input` is mapped to `original` in scatter op. - Optional indexTensorRank = getTensorRank(indexTensor); + std::optional indexTensorRank = getTensorRank(indexTensor); if (!indexTensorRank || *indexTensorRank != 1) return rewriter.notifyMatchFailure( op, "unimplemented: index tensor with rank != 1 is not supported"); diff --git a/lib/Conversion/TorchToTosa/TorchToTosa.cpp b/lib/Conversion/TorchToTosa/TorchToTosa.cpp index 3bebc5885..41a2b9d80 100644 --- a/lib/Conversion/TorchToTosa/TorchToTosa.cpp +++ b/lib/Conversion/TorchToTosa/TorchToTosa.cpp @@ -595,10 +595,10 @@ LogicalResult ConvertAtenOp::matchAndRewrite( return success(); } -using ReductionConvFunc = llvm::Optional (*)(PatternRewriter &, - Operation *, - RankedTensorType, Value, - ElementsAttr, bool); +using ReductionConvFunc = std::optional (*)(PatternRewriter &, + Operation *, + RankedTensorType, Value, + ElementsAttr, bool); // They all constitute a common form invoking the appropriate // converion function in TosaLegalizeCommon.cpp @@ -642,7 +642,7 @@ public: keepDims))) return failure(); - llvm::Optional result = + std::optional result = ConversionFuncT(rewriter, op, outputTy, self, reduceDimsAttr, keepDims); if (!result) @@ -1274,7 +1274,7 @@ public: auto transposedLhsType = RankedTensorType::get( makeShapeLLVMCompatible(transposedLhsShape), rhsElemTy); - llvm::Optional transposedLhsDimsConst = + std::optional transposedLhsDimsConst = tosa::getConstTensor( rewriter, op, /*vec=*/transposedLhsDims, @@ -1354,7 +1354,7 @@ public: auto transposedRhsValue = rankBroadcastedRhs; if (rhsNeedsTranspose) { - llvm::Optional transposedRhsDimsConst = + std::optional transposedRhsDimsConst = tosa::getConstTensor( rewriter, op, /*vec=*/transposedRhsDims, @@ -1510,7 +1510,7 @@ public: if (opNeedsTranspose) { - llvm::Optional transposedOpShapeConst = + std::optional transposedOpShapeConst = tosa::getConstTensor( rewriter, op, /*vec=*/transposedOpDims, @@ -1699,7 +1699,7 @@ public: std::swap(transposedRhsShape[rhsRank - 1], transposedRhsShape[rhsRank - 2]); std::swap(transposedRhsDims[rhsRank - 1], transposedRhsDims[rhsRank - 2]); - llvm::Optional transposedRhsShapeConst = + std::optional transposedRhsShapeConst = tosa::getConstTensor( rewriter, op, /*vec=*/transposedRhsDims, @@ -1860,7 +1860,7 @@ LogicalResult ConvertAtenOp::matchAndRewrite( "non-const dilation list unsupported"); // TOSA works in NHWC and takes OHWI weights. Perform the necessary transpose. - llvm::Optional nchwToNhwcTransposeConst = + std::optional nchwToNhwcTransposeConst = tosa::getConstTensor(rewriter, op, /*vec=*/{0, 2, 3, 1}, /*shape=*/{static_cast(4)}); @@ -1920,7 +1920,7 @@ LogicalResult ConvertAtenOp::matchAndRewrite( rewriter.getI64ArrayAttr(dilation)) .getResult(); - llvm::Optional nhwcToNchwTransposeConst = + std::optional nhwcToNchwTransposeConst = tosa::getConstTensor(rewriter, op, /*vec=*/{0, 3, 1, 2}, /*shape=*/{static_cast(4)}); @@ -3364,7 +3364,7 @@ public: auto inputShape = makeShapeTorchCompatible(inputTy.getShape()); auto inputRank = inputTy.getRank(); - llvm::Optional transposeDimsConst = tosa::getConstTensor( + std::optional transposeDimsConst = tosa::getConstTensor( rewriter, op, /*vec=*/transposeDims, /*shape=*/{static_cast(inputRank)}); diff --git a/lib/Conversion/TorchToTosa/TosaLegalizeCommon.cpp b/lib/Conversion/TorchToTosa/TosaLegalizeCommon.cpp index 71ebddce4..18d9f5c36 100644 --- a/lib/Conversion/TorchToTosa/TosaLegalizeCommon.cpp +++ b/lib/Conversion/TorchToTosa/TosaLegalizeCommon.cpp @@ -29,7 +29,7 @@ namespace tosa { // Common function for lowering reduce operations to TOSA ops. template -llvm::Optional convertReduceOpCommon( +std::optional convertReduceOpCommon( PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims, Type reduce_element_type, bool is_quantized, double input_scale, @@ -94,7 +94,7 @@ llvm::Optional convertReduceOpCommon( } // Lowers ReduceAll to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceAllOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims) { @@ -109,7 +109,7 @@ convertReduceAllOp(PatternRewriter &rewriter, Operation *op, } // Lowers ReduceAny to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceAnyOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims) { @@ -124,7 +124,7 @@ convertReduceAnyOp(PatternRewriter &rewriter, Operation *op, } // Lowers ReduceMin to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceMinOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims) { @@ -139,7 +139,7 @@ convertReduceMinOp(PatternRewriter &rewriter, Operation *op, } // Lowers ReduceMax to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceMaxOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims) { @@ -154,7 +154,7 @@ convertReduceMaxOp(PatternRewriter &rewriter, Operation *op, } // Lowers ReduceProd to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceProdOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims) { @@ -180,7 +180,7 @@ convertReduceProdOp(PatternRewriter &rewriter, Operation *op, } // Lowers ReduceSum to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceSumOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims) { @@ -231,7 +231,7 @@ convertReduceSumOp(PatternRewriter &rewriter, Operation *op, } // Lowers ReduceMean to a sequence of TOSA ops. -llvm::Optional +std::optional convertReduceMeanOp(PatternRewriter &rewriter, Operation *op, RankedTensorType output_type, Value input_value, ElementsAttr axes_elems, bool keep_dims) { diff --git a/lib/Conversion/TorchToTosa/TosaLegalizeUtils.cpp b/lib/Conversion/TorchToTosa/TosaLegalizeUtils.cpp index 6a1479dac..a6aaaf579 100644 --- a/lib/Conversion/TorchToTosa/TosaLegalizeUtils.cpp +++ b/lib/Conversion/TorchToTosa/TosaLegalizeUtils.cpp @@ -153,8 +153,8 @@ Value getTosaConstTensorSingleF32(PatternRewriter &rewriter, Operation *op, // T: storage C type. // Default template creates a constant tensor in T. template -llvm::Optional getConstTensor(PatternRewriter &rewriter, Operation *op, - ArrayRef vec, ArrayRef shape) { +std::optional getConstTensor(PatternRewriter &rewriter, Operation *op, + ArrayRef vec, ArrayRef shape) { uint64_t num_total_elements = 1; for (int64_t a : shape) { num_total_elements *= a; @@ -176,9 +176,9 @@ llvm::Optional getConstTensor(PatternRewriter &rewriter, Operation *op, // Template specialization for APInt template <> -llvm::Optional getConstTensor(PatternRewriter &rewriter, - Operation *op, ArrayRef vec, - ArrayRef shape) { +std::optional getConstTensor(PatternRewriter &rewriter, + Operation *op, ArrayRef vec, + ArrayRef shape) { uint64_t num_total_elements = 1; for (int64_t a : shape) { num_total_elements *= a; @@ -200,9 +200,9 @@ llvm::Optional getConstTensor(PatternRewriter &rewriter, // Template specialization for float template <> -llvm::Optional getConstTensor(PatternRewriter &rewriter, - Operation *op, ArrayRef vec, - ArrayRef shape) { +std::optional getConstTensor(PatternRewriter &rewriter, + Operation *op, ArrayRef vec, + ArrayRef shape) { uint64_t num_total_elements = 1; for (int64_t a : shape) { num_total_elements *= a; @@ -254,7 +254,7 @@ LogicalResult tosaCastTensorToType(PatternRewriter &rewriter, Operation *op, for (int64_t a : srcShape) num_total_elements *= a; - llvm::Optional constOp; + std::optional constOp; if (srcElemTy.isInteger(64)) { SmallVector values(num_total_elements, 0); constOp = @@ -283,14 +283,14 @@ LogicalResult tosaCastTensorToType(PatternRewriter &rewriter, Operation *op, } // Template instantiation -template llvm::Optional getConstTensor(PatternRewriter &, - Operation *, - ArrayRef vec, - ArrayRef shape); +template std::optional getConstTensor(PatternRewriter &, + Operation *, + ArrayRef vec, + ArrayRef shape); -template llvm::Optional getConstTensor(PatternRewriter &, - Operation *, - ArrayRef vec, - ArrayRef shape); +template std::optional getConstTensor(PatternRewriter &, + Operation *, + ArrayRef vec, + ArrayRef shape); } // namespace tosa } // namespace mlir diff --git a/lib/Conversion/Utils/Utils.cpp b/lib/Conversion/Utils/Utils.cpp index 341e2f20f..83715bd58 100644 --- a/lib/Conversion/Utils/Utils.cpp +++ b/lib/Conversion/Utils/Utils.cpp @@ -241,7 +241,7 @@ SmallVector getTypeConvertedValues(OpBuilder &b, Location loc, // from a tensor or a scalar in the pytorch dialect. Both the scalar and dtype // should be converted builtin types. Value convertScalarToDtype(OpBuilder &b, Location loc, Value scalar, Type dtype, - llvm::Optional srcOriginalDtype) { + std::optional srcOriginalDtype) { Type scalarType = scalar.getType(); if (scalarType == dtype) return scalar; diff --git a/lib/Dialect/Torch/IR/TorchOps.cpp b/lib/Dialect/Torch/IR/TorchOps.cpp index 82fe80590..3386f4dd8 100644 --- a/lib/Dialect/Torch/IR/TorchOps.cpp +++ b/lib/Dialect/Torch/IR/TorchOps.cpp @@ -136,7 +136,7 @@ static Value getScalarValue(Value input, Location loc, } Value scalar = nullptr; if (auto valueTensorLiteralOp = input.getDefiningOp()) { - Optional tensorRank = + std::optional tensorRank = getTensorRank(valueTensorLiteralOp.getResult()); if (valueTensorLiteralOp && tensorRank && *tensorRank == 0) { auto tensorType = @@ -296,13 +296,13 @@ LogicalResult ClassTypeOp::verify() { //===----------------------------------------------------------------------===// OperandRange -PrimLoopOp::getSuccessorEntryOperands(Optional index) { +PrimLoopOp::getSuccessorEntryOperands(std::optional index) { assert(index.has_value() && index.value() == 0); return getIterArgsInit(); } void PrimLoopOp::getSuccessorRegions( - Optional index, ArrayRef operands, + std::optional index, ArrayRef operands, SmallVectorImpl ®ions) { (void)operands; @@ -324,8 +324,8 @@ bool PrimLoopOp::isForLike() { // PrimLoopConditionOp //===----------------------------------------------------------------------===// -MutableOperandRange -PrimLoopConditionOp::getMutableSuccessorOperands(Optional index) { +MutableOperandRange PrimLoopConditionOp::getMutableSuccessorOperands( + std::optional index) { // Pass all operands except the condition to the successor which is the // parent loop op. return getIterArgsMutable(); @@ -374,7 +374,7 @@ void PrimIfOp::print(OpAsmPrinter &p) { p.printOptionalAttrDict((*this)->getAttrs()); } -void PrimIfOp::getSuccessorRegions(Optional index, +void PrimIfOp::getSuccessorRegions(std::optional index, ArrayRef operands, SmallVectorImpl ®ions) { // The `then` and the `else` region branch back to the parent operation. @@ -1022,9 +1022,9 @@ void AtenDivTensorModeOp::getCanonicalizationPatterns( // dimension size or returns failure if such a type was not found. If `dim` is // `None`, then all dimension's sizes must be known. static FailureOr -traceKnownSizeTensorType(Value value, llvm::Optional dim) { +traceKnownSizeTensorType(Value value, std::optional dim) { // Function to check if we found a type that contains the queried information. - auto foundType = [](BaseTensorType tensorType, llvm::Optional(dim)) { + auto foundType = [](BaseTensorType tensorType, std::optional(dim)) { if (!tensorType.hasSizes()) return false; @@ -1386,7 +1386,7 @@ void AtenSortIntOp::getCanonicalizationPatterns(RewritePatternSet &patterns, //===----------------------------------------------------------------------===// LogicalResult NonValueTensorLiteralOp::inferReturnTypes( - MLIRContext *context, Optional location, ValueRange operands, + MLIRContext *context, std::optional location, ValueRange operands, DictionaryAttr attributes, RegionRange regions, SmallVectorImpl &inferredReturnTypes) { auto attr = attributes.get("value").dyn_cast_or_null(); @@ -1426,7 +1426,7 @@ bool NonValueTensorLiteralOp::isCompatibleReturnTypes(TypeRange inferred, //===----------------------------------------------------------------------===// LogicalResult ValueTensorLiteralOp::inferReturnTypes( - MLIRContext *context, Optional location, ValueRange operands, + MLIRContext *context, std::optional location, ValueRange operands, DictionaryAttr attributes, RegionRange regions, SmallVectorImpl &inferredReturnTypes) { auto attr = attributes.get("value").dyn_cast_or_null(); @@ -1500,7 +1500,7 @@ LogicalResult CopyToNonValueTensorOp::verify() { } LogicalResult CopyToNonValueTensorOp::inferReturnTypes( - MLIRContext *context, Optional location, ValueRange operands, + MLIRContext *context, std::optional location, ValueRange operands, DictionaryAttr attributes, RegionRange regions, SmallVectorImpl &inferredReturnTypes) { auto resultType = operands[0].getType().cast(); @@ -1527,7 +1527,7 @@ LogicalResult CopyToValueTensorOp::verify() { } LogicalResult CopyToValueTensorOp::inferReturnTypes( - MLIRContext *context, Optional location, ValueRange operands, + MLIRContext *context, std::optional location, ValueRange operands, DictionaryAttr attributes, RegionRange regions, SmallVectorImpl &inferredReturnTypes) { auto resultType = operands[0].getType().cast(); @@ -1714,7 +1714,7 @@ void Aten__Getitem__TOp::getCanonicalizationPatterns( return failure(); // Get the index, but be careful because it might be statically invalid. - llvm::Optional indexOpt = matchLegalConstantIndexIntoListOfSize( + std::optional indexOpt = matchLegalConstantIndexIntoListOfSize( op.getOperand(1), listConstruct.getNumOperands()); if (!indexOpt) return rewriter.notifyMatchFailure(op, "statically invalid index"); @@ -2326,7 +2326,7 @@ OpFoldResult PrimMinSelfIntOp::fold(ArrayRef operands) { template static void -getSuccessorRegionsForCalculateOp(CalculateOp op, Optional index, +getSuccessorRegionsForCalculateOp(CalculateOp op, std::optional index, ArrayRef operands, SmallVectorImpl ®ions) { if (!index.has_value()) { @@ -2345,7 +2345,7 @@ getSuccessorRegionsForCalculateOp(CalculateOp op, Optional index, } void ShapeCalculateOp::getSuccessorRegions( - Optional index, ArrayRef operands, + std::optional index, ArrayRef operands, SmallVectorImpl ®ions) { getSuccessorRegionsForCalculateOp(*this, index, operands, regions); } @@ -2355,7 +2355,7 @@ void ShapeCalculateOp::getSuccessorRegions( //===----------------------------------------------------------------------===// void DtypeCalculateOp::getSuccessorRegions( - Optional index, ArrayRef operands, + std::optional index, ArrayRef operands, SmallVectorImpl ®ions) { getSuccessorRegionsForCalculateOp(*this, index, operands, regions); } @@ -2365,7 +2365,7 @@ void DtypeCalculateOp::getSuccessorRegions( //===----------------------------------------------------------------------===// MutableOperandRange ShapeCalculateYieldShapesOp::getMutableSuccessorOperands( - Optional index) { + std::optional index) { // The shape operands don't get forwarded to the body. // MutableOperandRange always has an owning operation, even if empty, so // create a 0-length range. @@ -2384,7 +2384,7 @@ LogicalResult ShapeCalculateYieldShapesOp::verify() { //===----------------------------------------------------------------------===// MutableOperandRange DtypeCalculateYieldDtypesOp::getMutableSuccessorOperands( - Optional index) { + std::optional index) { // The dtype operands don't get forwarded to the body. // MutableOperandRange always has an owning operation, even if empty, so // create a 0-length range. diff --git a/lib/Dialect/Torch/IR/TorchTypes.cpp b/lib/Dialect/Torch/IR/TorchTypes.cpp index 88caf7da9..f97804032 100644 --- a/lib/Dialect/Torch/IR/TorchTypes.cpp +++ b/lib/Dialect/Torch/IR/TorchTypes.cpp @@ -86,7 +86,7 @@ bool Torch::isValidSubtype(Type subtype, Type type) { //===----------------------------------------------------------------------===// // Parse the `` of a type such as `!torch.tuple`. -static Optional> +static std::optional> parseMultipleContainedTypes(AsmParser &parser) { if (parser.parseLess()) return std::nullopt; @@ -185,7 +185,7 @@ Type BaseTensorType::getWithSizesAndDtypeFrom(BaseTensorType other) const { } Type BaseTensorType::getWithSizesAndDtype( - Optional> optionalSizes, Type optionalDtype) const { + std::optional> optionalSizes, Type optionalDtype) const { if (isa()) return NonValueTensorType::get(getContext(), optionalSizes, optionalDtype); if (isa()) @@ -203,7 +203,7 @@ ValueTensorType BaseTensorType::getWithValueSemantics() const { static LogicalResult verifyTensorType(function_ref emitError, - Optional> optionalSizes, + std::optional> optionalSizes, Type optionalDtype) { if (optionalDtype && !isValidTorchDtype(optionalDtype)) { emitError() << "invalid dtype " << optionalDtype @@ -274,7 +274,7 @@ Type parseTensorType(MLIRContext *context, AsmParser &parser, } if (parser.parseGreater()) return Type(); - Optional> optionalSizes; + std::optional> optionalSizes; if (hasSizes) optionalSizes.emplace(sizes); @@ -286,7 +286,7 @@ Type parseTensorType(MLIRContext *context, AsmParser &parser, } static void printTensorType(AsmPrinter &printer, - Optional> optionalSizes, + std::optional> optionalSizes, Type optionalDtype) { if (!optionalSizes && !optionalDtype) return; @@ -331,7 +331,7 @@ NonValueTensorType::getWithLeastStaticInformation(MLIRContext *context) { LogicalResult NonValueTensorType::verify(function_ref emitError, - Optional> optionalSizes, + std::optional> optionalSizes, Type optionalDtype) { return verifyTensorType(emitError, optionalSizes, optionalDtype); } @@ -340,7 +340,7 @@ Type NonValueTensorType::parse(AsmParser &parser) { MLIRContext *context = parser.getContext(); return parseTensorType( context, parser, - [](MLIRContext *context, Optional> optionalSizes, + [](MLIRContext *context, std::optional> optionalSizes, Type optionalType) { return NonValueTensorType::get(context, optionalSizes, optionalType); }); @@ -392,7 +392,7 @@ TensorType ValueTensorType::toBuiltinTensor() const { LogicalResult ValueTensorType::verify(function_ref emitError, - Optional> optionalSizes, + std::optional> optionalSizes, Type optionalDtype) { return verifyTensorType(emitError, optionalSizes, optionalDtype); } @@ -401,7 +401,7 @@ Type ValueTensorType::parse(AsmParser &parser) { MLIRContext *context = parser.getContext(); return parseTensorType( context, parser, - [](MLIRContext *context, Optional> optionalSizes, + [](MLIRContext *context, std::optional> optionalSizes, Type optionalType) { return ValueTensorType::get(context, optionalSizes, optionalType); }); diff --git a/lib/Dialect/Torch/Transforms/DecomposeComplexOps.cpp b/lib/Dialect/Torch/Transforms/DecomposeComplexOps.cpp index 5ad93487f..a04cbcc02 100644 --- a/lib/Dialect/Torch/Transforms/DecomposeComplexOps.cpp +++ b/lib/Dialect/Torch/Transforms/DecomposeComplexOps.cpp @@ -69,7 +69,7 @@ static Type computeReductionType(PatternRewriter &rewriter, Operation *op, } Type resultType = tensorType.getWithSizesAndDtype( - sizes.size() == 0 ? Optional>() + sizes.size() == 0 ? std::optional>() : llvm::makeArrayRef(sizes), tensorType.getDtype()); return resultType; @@ -105,7 +105,7 @@ static Value createMaxAlongDimension(PatternRewriter &rewriter, Location loc, BaseTensorType indexType = valueType .getWithSizesAndDtype( - !valueType.hasSizes() ? Optional>() + !valueType.hasSizes() ? std::optional>() : llvm::makeArrayRef(valueType.getSizes()), IntegerType::get(op->getContext(), 64, IntegerType::Signed)) .cast(); @@ -228,7 +228,7 @@ public: Location loc = op.getLoc(); Value self = op.getSelf(); MLIRContext *context = op.getContext(); - Optional maybeRank = getTensorRank(self); + std::optional maybeRank = getTensorRank(self); if (!maybeRank) return rewriter.notifyMatchFailure(op, "Unimplemented: unranked tensor"); unsigned rank = *maybeRank; @@ -548,7 +548,7 @@ public: BaseTensorType inputType = input.getType().cast(); BaseTensorType indicesTensorType = result.getType().cast(); - Optional maybeInputRank = getTensorRank(input); + std::optional maybeInputRank = getTensorRank(input); if (!maybeInputRank) { return rewriter.notifyMatchFailure( op, "expected input tensor to have a rank"); @@ -681,8 +681,8 @@ public: Value lhs = op.getSelf(); Value rhs = op.getOther(); - Optional maybeLhsRank = getTensorRank(lhs); - Optional maybeRhsRank = getTensorRank(rhs); + std::optional maybeLhsRank = getTensorRank(lhs); + std::optional maybeRhsRank = getTensorRank(rhs); if (!maybeLhsRank || !maybeRhsRank) { return rewriter.notifyMatchFailure( op, "expected input tensors to have a rank"); @@ -786,7 +786,7 @@ public: LogicalResult matchAndRewrite(AtenTOp op, PatternRewriter &rewriter) const override { Value lhs = op.getSelf(); - Optional lhsRank = getTensorRank(lhs); + std::optional lhsRank = getTensorRank(lhs); auto loc = op.getLoc(); if (!lhsRank) { @@ -861,7 +861,7 @@ public: loc, listType, llvm::ArrayRef{slice0, slice1}); return rewriter.create(loc, self.getType(), slices, dim); }; - Optional maybeRank = getTensorRank(self); + std::optional maybeRank = getTensorRank(self); if (!maybeRank) return rewriter.notifyMatchFailure(op, "Unimplemented: unranked tensor"); unsigned rank = *maybeRank; @@ -917,7 +917,7 @@ public: Location loc = op.getLoc(); Value self = op.getSelf(); MLIRContext *context = op.getContext(); - Optional maybeRank = getTensorRank(self); + std::optional maybeRank = getTensorRank(self); if (!maybeRank) return rewriter.notifyMatchFailure(op, "Unimplemented: unranked tensor"); unsigned rank = *maybeRank; @@ -1020,7 +1020,7 @@ public: Location loc = op.getLoc(); Value self = op.getSelf(); MLIRContext *context = op.getContext(); - Optional maybeRank = getTensorRank(self); + std::optional maybeRank = getTensorRank(self); if (!maybeRank) return rewriter.notifyMatchFailure(op, "unimplemented: unranked tensor"); unsigned rank = *maybeRank; @@ -1258,7 +1258,7 @@ public: Location loc = op.getLoc(); MLIRContext *context = op.getContext(); Value gradOutput = op.getGradOutput(); - Optional maybeGradRank = getTensorRank(gradOutput); + std::optional maybeGradRank = getTensorRank(gradOutput); if (!maybeGradRank) { return rewriter.notifyMatchFailure(op, "expected grad output to have a rank"); @@ -1410,8 +1410,8 @@ public: Value input = op.getSelf(); Value mat1 = op.getMat1(); Value mat2 = op.getMat2(); - Optional mat1Rank = getTensorRank(mat1); - Optional mat2Rank = getTensorRank(mat2); + std::optional mat1Rank = getTensorRank(mat1); + std::optional mat2Rank = getTensorRank(mat2); // The operands `mat1`, `mat2` to aten.addmm must be of rank 2. if (!mat1Rank || !mat2Rank || *mat1Rank != 2 || *mat2Rank != 2) { @@ -1472,7 +1472,7 @@ public: PatternRewriter &rewriter) const override { Location loc = op.getLoc(); Value input = op.getSelf(); - Optional maybeInputRank = getTensorRank(input); + std::optional maybeInputRank = getTensorRank(input); if (!maybeInputRank) { return rewriter.notifyMatchFailure(op, "expected input to have a rank"); } @@ -1602,7 +1602,7 @@ public: PatternRewriter &rewriter) const override { Location loc = op.getLoc(); Value self = op.getSelf(); - Optional maybeInputRank = getTensorRank(self); + std::optional maybeInputRank = getTensorRank(self); if (!maybeInputRank) { return rewriter.notifyMatchFailure(op, "expected input to have a rank"); } @@ -2163,7 +2163,7 @@ class DecomposeAtenNativeBatchNormOp // Rank of the input tensor must be greater than or equal to 2. The shape of // the `input` is supposed to be (N, C, D?, H?, W?). - Optional maybeInputRank = getTensorRank(input); + std::optional maybeInputRank = getTensorRank(input); if (!maybeInputRank || *maybeInputRank < 2) return rewriter.notifyMatchFailure( op, "input must have rank greater than or equal to 2"); @@ -2177,8 +2177,8 @@ class DecomposeAtenNativeBatchNormOp op, "running stats must not be None in inference mode"); // Rank of `runningMean` and `runningVar` must be exactly 1. - Optional runningMeanRank = getTensorRank(runningMean); - Optional runningVarRank = getTensorRank(runningVar); + std::optional runningMeanRank = getTensorRank(runningMean); + std::optional runningVarRank = getTensorRank(runningVar); if (!runningMeanRank || !runningVarRank || *runningMeanRank != 1 || *runningVarRank != 1) return rewriter.notifyMatchFailure( @@ -2229,7 +2229,7 @@ class DecomposeAtenNativeBatchNormOp Value batchNormOutput = normalizedInput; if (!weight.getType().isa()) { // Rank of `weight` must be exactly 1. - Optional weightRank = getTensorRank(weight); + std::optional weightRank = getTensorRank(weight); if (!weightRank || *weightRank != 1) return rewriter.notifyMatchFailure(op, "expected weight to be rank 1"); weight = rewriter.create(loc, reshapeType, weight, @@ -2239,7 +2239,7 @@ class DecomposeAtenNativeBatchNormOp } if (!bias.getType().isa()) { // Rank of `bias` must be exactly 1. - Optional biasRank = getTensorRank(bias); + std::optional biasRank = getTensorRank(bias); if (!biasRank || *biasRank != 1) return rewriter.notifyMatchFailure(op, "expected bias to be rank 1"); bias = rewriter.create(loc, reshapeType, bias, @@ -2659,7 +2659,7 @@ class DecomposeAtenAdaptiveAvgPool2dOp MLIRContext *context = op.getContext(); Value input = op.getSelf(); - Optional maybeRank = getTensorRank(input); + std::optional maybeRank = getTensorRank(input); if (!maybeRank) { return rewriter.notifyMatchFailure(op, "expected input to have a rank"); } @@ -2832,7 +2832,7 @@ class DecomposeAtenNumpyTOp : public OpRewritePattern { PatternRewriter &rewriter) const override { Location loc = op.getLoc(); Value self = op.getSelf(); - Optional maybeInputRank = getTensorRank(self); + std::optional maybeInputRank = getTensorRank(self); if (!maybeInputRank) { return rewriter.notifyMatchFailure(op, "expected input to have a rank"); } @@ -2879,7 +2879,7 @@ static LogicalResult calculateVariance(OpTy op, PatternRewriter &rewriter, inputTensorTy = self.getType().cast(); } - Optional maybeInputRank = getTensorRank(self); + std::optional maybeInputRank = getTensorRank(self); if (!maybeInputRank) { return rewriter.notifyMatchFailure(op, "expected input to have a rank"); } @@ -3368,7 +3368,8 @@ private: template void addPatternIfTargetOpIsIllegal(RewritePatternSet &patterns) { MLIRContext *context = &getContext(); - Optional opName = DecomposePattern(context).getRootKind(); + std::optional opName = + DecomposePattern(context).getRootKind(); // Because the `DecomposeComplexOpsPass` uses a greedy algorithm // to apply patterns, only patterns that we for sure know we want to run // must be added. This restricts the set of patterns allowed in this file to diff --git a/lib/Dialect/Torch/Transforms/GlobalizeObjectGraph.cpp b/lib/Dialect/Torch/Transforms/GlobalizeObjectGraph.cpp index 3f2600f0a..a93db8d30 100644 --- a/lib/Dialect/Torch/Transforms/GlobalizeObjectGraph.cpp +++ b/lib/Dialect/Torch/Transforms/GlobalizeObjectGraph.cpp @@ -81,8 +81,8 @@ public: assert(it != slotLinkageInfo.end()); return it->second; } - Optional getFuncLinkageInfo(NnModuleOp instance, - func::FuncOp methodFunc) { + std::optional getFuncLinkageInfo(NnModuleOp instance, + func::FuncOp methodFunc) { auto it = funcLinkageInfo.find({instance, methodFunc}); if (it == funcLinkageInfo.end()) return std::nullopt; @@ -638,7 +638,7 @@ static LogicalResult globalizeObjectGraph(ModuleOp module) { for (auto &monomorphization : tracker.getMonomorphizations()) { auto newFunc = cast(monomorphization.func->clone()); newFuncs[monomorphization] = newFunc; - Optional linkageInfo = std::nullopt; + std::optional linkageInfo = std::nullopt; // If it is potentially a method, check its linkage info. if (monomorphization.argInstances.size() != 0 && monomorphization.argInstances[0].argIndex == 0) { diff --git a/lib/Dialect/Torch/Transforms/LowerToBackendContract.cpp b/lib/Dialect/Torch/Transforms/LowerToBackendContract.cpp index 5194263d1..6ad45492a 100644 --- a/lib/Dialect/Torch/Transforms/LowerToBackendContract.cpp +++ b/lib/Dialect/Torch/Transforms/LowerToBackendContract.cpp @@ -367,8 +367,8 @@ static void markDecomposedOpsAsIllegal(MLIRContext *context, target.addIllegalOp(); target.addIllegalOp(); target.addDynamicallyLegalOp([](AtenMatmulOp op) { - Optional lhsRank = getTensorRank(op.getSelf()); - Optional rhsRank = getTensorRank(op.getOther()); + std::optional lhsRank = getTensorRank(op.getSelf()); + std::optional rhsRank = getTensorRank(op.getOther()); if (!lhsRank || !rhsRank) return false; // Make aten.matmul legal if the following condition is satisfied. diff --git a/lib/Dialect/Torch/Transforms/MaximizeValueSemantics.cpp b/lib/Dialect/Torch/Transforms/MaximizeValueSemantics.cpp index a600eca65..121c759a6 100644 --- a/lib/Dialect/Torch/Transforms/MaximizeValueSemantics.cpp +++ b/lib/Dialect/Torch/Transforms/MaximizeValueSemantics.cpp @@ -40,7 +40,7 @@ public: SmallVector copyLikeOps; SmallVector viewLikeOps; SmallVector overwriteTensorContentsOps; - Optional returnOp; + std::optional returnOp; }; // Check that graph rewriting is possible by doing an abstract diff --git a/lib/Dialect/Torch/Transforms/RefineTypes.cpp b/lib/Dialect/Torch/Transforms/RefineTypes.cpp index 4e3d6d0a7..0cef4ee60 100644 --- a/lib/Dialect/Torch/Transforms/RefineTypes.cpp +++ b/lib/Dialect/Torch/Transforms/RefineTypes.cpp @@ -115,7 +115,7 @@ static torch_upstream::TypeKind getTypeKind(Type type) { /// Returns `std::nullopt` if the types are contradictory. Note this can only /// be used on the `dtype` from tensors and can't be used on other types like /// scalar types. -static Optional meetElementTypes(Type lhs, Type rhs) { +static std::optional meetElementTypes(Type lhs, Type rhs) { auto isNullOrBuiltIn = [](Type type) { return !type || isBuiltInType(type); }; (void)isNullOrBuiltIn; assert(isNullOrBuiltIn(lhs) && "`lhs` must be a builtin type"); @@ -138,7 +138,7 @@ enum class OptionalKnowledge { /// Returns the OptionalKnowledge that assumes information from both `lhs` and /// `rhs`. Returns `std::nullopt` if the knowledges are contradictory. -static Optional +static std::optional meetOptionalKnowledge(OptionalKnowledge lhs, OptionalKnowledge rhs) { if (lhs == OptionalKnowledge::unKnown) return rhs; @@ -328,20 +328,20 @@ struct ValueKnowledge { // Given two pieces of static knowledge, calculate new knowledge that assumes // the facts from both. // If the two pieces of knowledge are contradictory, std::nullopt is returned. - static Optional meet(const ValueKnowledge &lhs, - const ValueKnowledge &rhs) { + static std::optional meet(const ValueKnowledge &lhs, + const ValueKnowledge &rhs) { if (!lhs.isInitialized) return lhs; if (!rhs.isInitialized) return rhs; - Optional knowledge = meetTypes(lhs, rhs); + std::optional knowledge = meetTypes(lhs, rhs); if (!knowledge.has_value()) return std::nullopt; ValueKnowledge result = knowledge.value(); - Optional optional = + std::optional optional = meetOptionalKnowledge(lhs.optional, rhs.optional); if (!optional.has_value()) return std::nullopt; @@ -349,8 +349,8 @@ struct ValueKnowledge { return result; } - static Optional meetTypes(const ValueKnowledge &lhs, - const ValueKnowledge &rhs) { + static std::optional meetTypes(const ValueKnowledge &lhs, + const ValueKnowledge &rhs) { if (!lhs.isInitialized) return lhs; if (!rhs.isInitialized) @@ -449,8 +449,8 @@ private: void visitAtenArangeStartStepOp(AtenArangeStartStepOp op); void visitAtenArangeStartOp(AtenArangeStartOp op); void visitAtenArangeOp(AtenArangeOp op); - void visitAtenArangeLikeOpHelper(Operation *op, llvm::Optional start, - Value end, llvm::Optional step, + void visitAtenArangeLikeOpHelper(Operation *op, std::optional start, + Value end, std::optional step, Value dtype); void visitReductionAlongAllDimsOp(Operation *op, Type dtype, ArrayRef operands); @@ -464,7 +464,7 @@ private: template void visitScalarToTensorConversionOp(OpTy op); void visitAtenTensorOp(AtenTensorOp op); template - void visitConstantTensorAllocOp(OpTy op, llvm::Optional dataType); + void visitConstantTensorAllocOp(OpTy op, std::optional dataType); template void visitConstantTensorAllocLikeOp(OpTy op, ArrayRef operands); @@ -518,7 +518,7 @@ updateResultTypeState(Type scalarType, // unknown (None variant of the optional). static torch_upstream::ResultTypeState updateResultTypeState(const ValueKnowledge *tensor, - Optional rankIsNonZero, + std::optional rankIsNonZero, const torch_upstream::ResultTypeState &inState, bool skipRankCheck = false) { if (!rankIsNonZero.has_value() && !skipRankCheck) @@ -566,8 +566,9 @@ static Type getPromotedResultDType(ValueKnowledge *tensor, Type scalarType) { return getTypeForScalarType(scalarType.getContext(), result_type(state)); } -static SmallVector> getRankIsNonZeroArray(ValueRange values) { - SmallVector> rankIsNonZero; +static SmallVector> +getRankIsNonZeroArray(ValueRange values) { + SmallVector> rankIsNonZero; for (Value v : values) { if (auto tensorType = v.getType().dyn_cast()) { if (tensorType.hasSizes()) { @@ -588,13 +589,13 @@ static SmallVector> getRankIsNonZeroArray(ValueRange values) { // Returns most generic type Type() if the tensor dtype is unknown. static Type getPromotedResultType(MLIRContext *context, ArrayRef tensors, - ArrayRef> rankIsNonZero, + ArrayRef> rankIsNonZero, bool skipRankCheck = false) { torch_upstream::ResultTypeState state = {}; assert(tensors.size() == rankIsNonZero.size()); for (auto t : llvm::zip(tensors, rankIsNonZero)) { const ValueKnowledge *tensor = std::get<0>(t); - Optional rankIsNonZero = std::get<1>(t); + std::optional rankIsNonZero = std::get<1>(t); if (!tensor->dtype) return Type(); state = updateResultTypeState(tensor, rankIsNonZero, state, skipRankCheck); @@ -604,7 +605,7 @@ static Type getPromotedResultType(MLIRContext *context, static Type getPromotedResultTypeAssumingNonZeroRank( MLIRContext *context, ArrayRef tensors) { - SmallVector> rankIsNonZero(tensors.size(), true); + SmallVector> rankIsNonZero(tensors.size(), true); return getPromotedResultType(context, tensors, rankIsNonZero, /*skipRankCheck=*/true); } @@ -1232,9 +1233,9 @@ void TypeAnalysis::visitAtenEmbeddingBagOp(Operation *op) { // Arange like ops returns a 1-D tensor of size ceil(end - start). void TypeAnalysis::visitAtenArangeLikeOpHelper(Operation *op, - llvm::Optional start, + std::optional start, Value end, - llvm::Optional step, + std::optional step, Value dtype) { auto knowledge = ValueKnowledge::getTensorPessimisticValueState(op->getContext()); @@ -1343,7 +1344,7 @@ void TypeAnalysis::visitAtenTensorOp(AtenTensorOp op) { template void TypeAnalysis::visitConstantTensorAllocOp(OpTy op, - llvm::Optional dataType) { + std::optional dataType) { auto knowledge = ValueKnowledge::getTensorPessimisticValueState(op->getContext()); if (!dataType) diff --git a/lib/Dialect/Torch/Transforms/SimplifyDtypeCalculations.cpp b/lib/Dialect/Torch/Transforms/SimplifyDtypeCalculations.cpp index 14381d2f7..2e2ac366d 100644 --- a/lib/Dialect/Torch/Transforms/SimplifyDtypeCalculations.cpp +++ b/lib/Dialect/Torch/Transforms/SimplifyDtypeCalculations.cpp @@ -83,7 +83,7 @@ public: using OpRewritePattern::OpRewritePattern; LogicalResult matchAndRewrite(PromoteDtypesOp op, PatternRewriter &rewriter) const override { - SmallVector> ranks; + SmallVector> ranks; SmallVector dtypes; if (!matchPattern(op.getRanks(), m_TorchListOfOptionalConstantInts(ranks))) { return rewriter.notifyMatchFailure( @@ -107,13 +107,13 @@ public: torch_upstream::ResultTypeState state{}; for (auto ranksAndDtypes : llvm::zip(ranks, dtypes)) { - Optional rank; + std::optional rank; int64_t dtype; std::tie(rank, dtype) = ranksAndDtypes; auto scalarType = static_cast(dtype); bool isScalarOnlyOp = llvm::all_of( - ranks, [](Optional rank) { return !rank.has_value(); }); + ranks, [](std::optional rank) { return !rank.has_value(); }); if (!rank.has_value()) { // If `rank` does not have a value, then we are dealing with a scalar diff --git a/lib/Dialect/Torch/Transforms/SimplifyShapeCalculations.cpp b/lib/Dialect/Torch/Transforms/SimplifyShapeCalculations.cpp index 1d7677a3b..20a6748ec 100644 --- a/lib/Dialect/Torch/Transforms/SimplifyShapeCalculations.cpp +++ b/lib/Dialect/Torch/Transforms/SimplifyShapeCalculations.cpp @@ -174,9 +174,8 @@ public: if (!setItem.use_empty()) return rewriter.notifyMatchFailure( op, "Expected `Aten_SetItemTOp` to not have users"); - llvm::Optional indexOpt = - matchLegalConstantIndexIntoListOfSize(setItem.getIdx(), - runningList.size()); + std::optional indexOpt = matchLegalConstantIndexIntoListOfSize( + setItem.getIdx(), runningList.size()); // The index might be statically out of bounds. if (!indexOpt) return rewriter.notifyMatchFailure( @@ -311,7 +310,7 @@ static LogicalResult refineShapeCalculateResult(ShapeCalculateOp op, if (auto setItem = dyn_cast(user)) { // If the index is statically known, we can clobber only a single index. // Otherwise, we conservatively clobber all of them. - llvm::Optional indexOpt = matchLegalConstantIndexIntoListOfSize( + std::optional indexOpt = matchLegalConstantIndexIntoListOfSize( setItem.getIdx(), listConstruct->getNumOperands()); if (indexOpt) clobberedElements.set(*indexOpt); diff --git a/lib/Dialect/Torch/Utils/Utils.cpp b/lib/Dialect/Torch/Utils/Utils.cpp index b29eb4ff0..1d67b24e8 100644 --- a/lib/Dialect/Torch/Utils/Utils.cpp +++ b/lib/Dialect/Torch/Utils/Utils.cpp @@ -23,7 +23,7 @@ bool Torch::isValidDim(int64_t dim, int64_t inputRank) { return dim >= 0 && dim < inputRank; } -llvm::Optional +std::optional Torch::matchLegalConstantIndexIntoListOfSize(Value v, int64_t length) { int64_t dim; if (!matchPattern(v, m_TorchConstantInt(&dim))) @@ -181,7 +181,7 @@ bool Torch::isBuiltInType(Type type) { return isa(type.getDialect()); } -Optional Torch::getTensorRank(Value tensor) { +std::optional Torch::getTensorRank(Value tensor) { BaseTensorType tensorType = tensor.getType().cast(); if (!tensorType.hasSizes()) return std::nullopt; diff --git a/lib/Dialect/TorchConversion/IR/TorchConversionOps.cpp b/lib/Dialect/TorchConversion/IR/TorchConversionOps.cpp index 6abdd3c13..1b83cce37 100644 --- a/lib/Dialect/TorchConversion/IR/TorchConversionOps.cpp +++ b/lib/Dialect/TorchConversion/IR/TorchConversionOps.cpp @@ -45,7 +45,7 @@ LogicalResult ToBuiltinTensorOp::verify() { } LogicalResult ToBuiltinTensorOp::inferReturnTypes( - MLIRContext *context, Optional location, ValueRange operands, + MLIRContext *context, std::optional location, ValueRange operands, DictionaryAttr attributes, RegionRange regions, SmallVectorImpl &inferredReturnTypes) { auto resultType =