From 0de16fafa57153a70168d69ca8591dbe8489905f Mon Sep 17 00:00:00 2001 From: Ramkumar Ramachandra Date: Mon, 19 Dec 2022 14:25:14 +0100 Subject: [PATCH] mlir/DialectConversion: use std::optional (NFC) This is part of an effort to migrate from llvm::Optional to std::optional. This patch touches DialectConversion, and modifies existing conversions and tests appropriately. See also: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716 Signed-off-by: Ramkumar Ramachandra Differential Revision: https://reviews.llvm.org/D140303 --- flang/lib/Optimizer/CodeGen/TypeConverter.h | 6 +- .../Conversion/MemRefToSPIRV/MemRefToSPIRV.h | 12 ++-- .../mlir/Dialect/SPIRV/IR/SPIRVTypes.h | 59 +++++++++--------- .../mlir/Transforms/DialectConversion.h | 48 ++++++++------- .../Conversion/ArithToSPIRV/ArithToSPIRV.cpp | 2 +- .../Conversion/AsyncToLLVM/AsyncToLLVM.cpp | 4 +- .../GPUToNVVM/LowerGpuOpsToNVVMOps.cpp | 2 +- .../Conversion/LLVMCommon/TypeConverter.cpp | 18 +++--- .../MathToSPIRV/MathToSPIRVPass.cpp | 2 +- .../MapMemRefStorageClassPass.cpp | 12 ++-- .../MemRefToSPIRV/MemRefToSPIRVPass.cpp | 2 +- .../Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp | 14 ++--- .../VectorToSPIRV/VectorToSPIRVPass.cpp | 2 +- .../Arith/Transforms/EmulateWideInt.cpp | 8 +-- .../Transforms/UnsignedWhenEquivalent.cpp | 4 +- .../MemRef/Transforms/EmulateWideInt.cpp | 2 +- mlir/lib/Dialect/SPIRV/IR/SPIRVTypes.cpp | 61 ++++++++++--------- .../SPIRV/Transforms/SPIRVConversion.cpp | 39 ++++++------ .../Transforms/UnifyAliasedResourcePass.cpp | 7 ++- .../Transforms/SparseTensorCodegen.cpp | 4 +- .../Transforms/SparseTensorConversion.cpp | 2 +- .../Transforms/Utils/DialectConversion.cpp | 50 +++++++-------- .../lib/Dialect/Arith/TestEmulateWideInt.cpp | 2 +- .../Func/TestDecomposeCallGraphTypes.cpp | 2 +- mlir/test/lib/Dialect/Test/TestPatterns.cpp | 7 ++- .../lib/Transforms/TestDialectConversion.cpp | 5 +- .../Transforms/DialectConversion.cpp | 24 ++++---- 27 files changed, 209 insertions(+), 191 deletions(-) diff --git a/flang/lib/Optimizer/CodeGen/TypeConverter.h b/flang/lib/Optimizer/CodeGen/TypeConverter.h index 737c28193f2d6..c129e1c40cc40 100644 --- a/flang/lib/Optimizer/CodeGen/TypeConverter.h +++ b/flang/lib/Optimizer/CodeGen/TypeConverter.h @@ -138,7 +138,7 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter { addSourceMaterialization( [&](mlir::OpBuilder &builder, mlir::Type resultType, mlir::ValueRange inputs, - mlir::Location loc) -> llvm::Optional { + mlir::Location loc) -> std::optional { if (inputs.size() != 1) return std::nullopt; return inputs[0]; @@ -148,7 +148,7 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter { addTargetMaterialization( [&](mlir::OpBuilder &builder, mlir::Type resultType, mlir::ValueRange inputs, - mlir::Location loc) -> llvm::Optional { + mlir::Location loc) -> std::optional { if (inputs.size() != 1) return std::nullopt; return inputs[0]; @@ -163,7 +163,7 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter { mlir::Type indexType() { return mlir::IntegerType::get(&getContext(), 64); } // fir.type --> llvm<"%name = { ty... }"> - llvm::Optional + std::optional convertRecordType(fir::RecordType derived, llvm::SmallVectorImpl &results, llvm::ArrayRef callStack) { diff --git a/mlir/include/mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h b/mlir/include/mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h index 48a82b19501c3..9463ceb4363ef 100644 --- a/mlir/include/mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h +++ b/mlir/include/mlir/Conversion/MemRefToSPIRV/MemRefToSPIRV.h @@ -23,23 +23,25 @@ class SPIRVTypeConverter; namespace spirv { /// Mapping from numeric MemRef memory spaces into SPIR-V symbolic ones. using MemorySpaceToStorageClassMap = - std::function(Attribute)>; + std::function(Attribute)>; /// Maps MemRef memory spaces to storage classes for Vulkan-flavored SPIR-V /// using the default rule. Returns std::nullopt if the memory space is unknown. -Optional mapMemorySpaceToVulkanStorageClass(Attribute); +std::optional + mapMemorySpaceToVulkanStorageClass(Attribute); /// Maps storage classes for Vulkan-flavored SPIR-V to MemRef memory spaces /// using the default rule. Returns std::nullopt if the storage class is /// unsupported. -Optional mapVulkanStorageClassToMemorySpace(spirv::StorageClass); +std::optional mapVulkanStorageClassToMemorySpace(spirv::StorageClass); /// Maps MemRef memory spaces to storage classes for OpenCL-flavored SPIR-V /// using the default rule. Returns std::nullopt if the memory space is unknown. -Optional mapMemorySpaceToOpenCLStorageClass(Attribute); +std::optional + mapMemorySpaceToOpenCLStorageClass(Attribute); /// Maps storage classes for OpenCL-flavored SPIR-V to MemRef memory spaces /// using the default rule. Returns std::nullopt if the storage class is /// unsupported. -Optional mapOpenCLStorageClassToMemorySpace(spirv::StorageClass); +std::optional mapOpenCLStorageClassToMemorySpace(spirv::StorageClass); /// Type converter for converting numeric MemRef memory spaces into SPIR-V /// symbolic ones. diff --git a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVTypes.h b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVTypes.h index 33a0f577f9ca5..daa4d61f7103f 100644 --- a/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVTypes.h +++ b/mlir/include/mlir/Dialect/SPIRV/IR/SPIRVTypes.h @@ -56,7 +56,7 @@ class SPIRVType : public Type { /// the given `storage` class. This method does not guarantee the uniqueness /// of extensions; the same extension may be appended multiple times. void getExtensions(ExtensionArrayRefVector &extensions, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); /// The capability requirements for each type are following the /// ((Capability::A OR Extension::B) AND (Capability::C OR Capability::D)) @@ -68,12 +68,12 @@ class SPIRVType : public Type { /// uniqueness of capabilities; the same capability may be appended multiple /// times. void getCapabilities(CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); /// Returns the size in bytes for each type. If no size can be calculated, /// returns `std::nullopt`. Note that if the type has explicit layout, it is /// also taken into account in calculation. - Optional getSizeInBytes(); + std::optional getSizeInBytes(); }; // SPIR-V scalar type: bool type, integer type, floating point type. @@ -89,11 +89,11 @@ class ScalarType : public SPIRVType { static bool isValid(IntegerType); void getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); - Optional getSizeInBytes(); + std::optional getSizeInBytes(); }; // SPIR-V composite type: VectorType, SPIR-V ArrayType, or SPIR-V StructType. @@ -117,11 +117,11 @@ class CompositeType : public SPIRVType { bool hasCompileTimeKnownNumElements() const; void getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); - Optional getSizeInBytes(); + std::optional getSizeInBytes(); }; // SPIR-V array type @@ -145,13 +145,13 @@ class ArrayType : public Type::TypeBase storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); /// Returns the array size in bytes. Since array type may have an explicit /// stride declaration (in bytes), we also include it in the calculation. - Optional getSizeInBytes(); + std::optional getSizeInBytes(); }; // SPIR-V image type @@ -188,9 +188,9 @@ class ImageType // TODO: Add support for Access qualifier void getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); }; // SPIR-V pointer type @@ -206,9 +206,9 @@ class PointerType : public Type::TypeBase storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); }; // SPIR-V run-time array type @@ -230,9 +230,9 @@ class RuntimeArrayType unsigned getArrayStride() const; void getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); }; // SPIR-V sampled image type @@ -253,9 +253,10 @@ class SampledImageType Type getImageType() const; void getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage = std::nullopt); - void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); + void + getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, + std::optional storage = std::nullopt); }; /// SPIR-V struct type. Two kinds of struct types are supported: @@ -389,9 +390,9 @@ class StructType ArrayRef memberDecorations = {}); void getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); }; llvm::hash_code @@ -416,9 +417,9 @@ class CooperativeMatrixNVType unsigned getColumns() const; void getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); }; // SPIR-V joint matrix type @@ -443,9 +444,9 @@ class JointMatrixINTELType MatrixLayout getMatrixLayout() const; void getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); }; // SPIR-V matrix type @@ -480,9 +481,9 @@ class MatrixType : public Type::TypeBase storage = std::nullopt); + std::optional storage = std::nullopt); void getCapabilities(SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage = std::nullopt); + std::optional storage = std::nullopt); }; } // namespace spirv diff --git a/mlir/include/mlir/Transforms/DialectConversion.h b/mlir/include/mlir/Transforms/DialectConversion.h index 871f4b0251fe1..f024d80e59743 100644 --- a/mlir/include/mlir/Transforms/DialectConversion.h +++ b/mlir/include/mlir/Transforms/DialectConversion.h @@ -54,7 +54,7 @@ class TypeConverter { ArrayRef getConvertedTypes() const { return argTypes; } /// Get the input mapping for the given argument. - Optional getInputMapping(unsigned input) const { + std::optional getInputMapping(unsigned input) const { return remappedInputs[input]; } @@ -81,7 +81,7 @@ class TypeConverter { unsigned newInputCount = 1); /// The remapping information for each of the original arguments. - SmallVector, 4> remappedInputs; + SmallVector, 4> remappedInputs; /// The set of new argument types. SmallVector argTypes; @@ -89,19 +89,20 @@ class TypeConverter { /// Register a conversion function. A conversion function must be convertible /// to any of the following forms(where `T` is a class derived from `Type`: - /// * Optional(T) + /// * std::optional(T) /// - This form represents a 1-1 type conversion. It should return nullptr /// or `std::nullopt` to signify failure. If `std::nullopt` is returned, /// the converter is allowed to try another conversion function to /// perform the conversion. - /// * Optional(T, SmallVectorImpl &) + /// * std::optional(T, SmallVectorImpl &) /// - This form represents a 1-N type conversion. It should return /// `failure` or `std::nullopt` to signify a failed conversion. If the /// new set of types is empty, the type is removed and any usages of the /// existing value are expected to be removed during conversion. If /// `std::nullopt` is returned, the converter is allowed to try another /// conversion function to perform the conversion. - /// * Optional(T, SmallVectorImpl &, ArrayRef) + /// * std::optional(T, SmallVectorImpl &, + /// ArrayRef) /// - This form represents a 1-N type conversion supporting recursive /// types. The first two arguments and the return value are the same as /// for the regular 1-N form. The third argument is contains is the @@ -119,7 +120,7 @@ class TypeConverter { /// Register a materialization function, which must be convertible to the /// following form: - /// `Optional(OpBuilder &, T, ValueRange, Location)`, + /// `std::optional(OpBuilder &, T, ValueRange, Location)`, /// where `T` is any subclass of `Type`. This function is responsible for /// creating an operation, using the OpBuilder and Location provided, that /// "casts" a range of values into a single value of the given type `T`. It @@ -203,7 +204,7 @@ class TypeConverter { /// This function converts the type signature of the given block, by invoking /// 'convertSignatureArg' for each argument. This function should return a /// valid conversion for the signature on success, std::nullopt otherwise. - Optional convertBlockSignature(Block *block); + std::optional convertBlockSignature(Block *block); /// Materialize a conversion from a set of types into one result type by /// generating a cast sequence of some kind. See the respective @@ -229,12 +230,12 @@ class TypeConverter { /// The signature of the callback used to convert a type. If the new set of /// types is empty, the type is removed and any usages of the existing value /// are expected to be removed during conversion. - using ConversionCallbackFn = std::function( + using ConversionCallbackFn = std::function( Type, SmallVectorImpl &, ArrayRef)>; /// The signature of the callback used to materialize a conversion. - using MaterializationCallbackFn = - std::function(OpBuilder &, Type, ValueRange, Location)>; + using MaterializationCallbackFn = std::function( + OpBuilder &, Type, ValueRange, Location)>; /// Attempt to materialize a conversion using one of the provided /// materialization functions. @@ -244,23 +245,24 @@ class TypeConverter { /// Generate a wrapper for the given callback. This allows for accepting /// different callback forms, that all compose into a single version. - /// With callback of form: `Optional(T)` + /// With callback of form: `std::optional(T)` template std::enable_if_t, ConversionCallbackFn> wrapCallback(FnT &&callback) { return wrapCallback( [callback = std::forward(callback)]( T type, SmallVectorImpl &results, ArrayRef) { - if (Optional resultOpt = callback(type)) { + if (std::optional resultOpt = callback(type)) { bool wasSuccess = static_cast(*resultOpt); if (wasSuccess) results.push_back(*resultOpt); - return Optional(success(wasSuccess)); + return std::optional(success(wasSuccess)); } - return Optional(); + return std::optional(); }); } - /// With callback of form: `Optional(T, SmallVectorImpl + /// With callback of form: `std::optional(T, + /// SmallVectorImpl /// &)` template std::enable_if_t &>, @@ -272,7 +274,8 @@ class TypeConverter { return callback(type, results); }); } - /// With callback of form: `Optional(T, SmallVectorImpl + /// With callback of form: `std::optional(T, + /// SmallVectorImpl /// &, ArrayRef)`. template std::enable_if_t< @@ -281,7 +284,7 @@ class TypeConverter { wrapCallback(FnT &&callback) { return [callback = std::forward(callback)]( Type type, SmallVectorImpl &results, - ArrayRef callStack) -> Optional { + ArrayRef callStack) -> std::optional { T derivedType = type.dyn_cast(); if (!derivedType) return std::nullopt; @@ -303,7 +306,7 @@ class TypeConverter { MaterializationCallbackFn wrapMaterialization(FnT &&callback) { return [callback = std::forward(callback)]( OpBuilder &builder, Type resultType, ValueRange inputs, - Location loc) -> Optional { + Location loc) -> std::optional { if (T derivedType = resultType.dyn_cast()) return callback(builder, derivedType, inputs, loc); return std::nullopt; @@ -681,7 +684,8 @@ class ConversionTarget { /// The signature of the callback used to determine if an operation is /// dynamically legal on the target. - using DynamicLegalityCallbackFn = std::function(Operation *)>; + using DynamicLegalityCallbackFn = + std::function(Operation *)>; ConversionTarget(MLIRContext &ctx) : ctx(ctx) {} virtual ~ConversionTarget() = default; @@ -830,7 +834,7 @@ class ConversionTarget { //===--------------------------------------------------------------------===// /// Get the legality action for the given operation. - Optional getOpAction(OperationName op) const; + std::optional getOpAction(OperationName op) const; /// If the given operation instance is legal on this target, a structure /// containing legality information is returned. If the operation is not @@ -841,7 +845,7 @@ class ConversionTarget { /// Note: Legality is actually a 4-state: Legal(recursive=true), /// Legal(recursive=false), Illegal or Unknown, where Unknown is treated /// either as Legal or Illegal depending on context. - Optional isLegal(Operation *op) const; + std::optional isLegal(Operation *op) const; /// Returns true is operation instance is illegal on this target. Returns /// false if operation is legal, operation legality wasn't registered by user @@ -873,7 +877,7 @@ class ConversionTarget { }; /// Get the legalization information for the given operation. - Optional getOpInfo(OperationName op) const; + std::optional getOpInfo(OperationName op) const; /// A deterministic mapping of operation name and its respective legality /// information. diff --git a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp index 9533494c5456c..873edc30099ee 100644 --- a/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp +++ b/mlir/lib/Conversion/ArithToSPIRV/ArithToSPIRV.cpp @@ -1156,7 +1156,7 @@ struct ConvertArithToSPIRVPass auto addUnrealizedCast = [](OpBuilder &builder, Type type, ValueRange inputs, Location loc) { auto cast = builder.create(loc, type, inputs); - return Optional(cast.getResult(0)); + return std::optional(cast.getResult(0)); }; typeConverter.addSourceMaterialization(addUnrealizedCast); typeConverter.addTargetMaterialization(addUnrealizedCast); diff --git a/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp b/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp index f2d6b9d2928e4..5f00c3da77ea2 100644 --- a/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp +++ b/mlir/lib/Conversion/AsyncToLLVM/AsyncToLLVM.cpp @@ -288,14 +288,14 @@ class AsyncRuntimeTypeConverter : public TypeConverter { auto addUnrealizedCast = [](OpBuilder &builder, Type type, ValueRange inputs, Location loc) { auto cast = builder.create(loc, type, inputs); - return Optional(cast.getResult(0)); + return std::optional(cast.getResult(0)); }; addSourceMaterialization(addUnrealizedCast); addTargetMaterialization(addUnrealizedCast); } - static Optional convertAsyncTypes(Type type) { + static std::optional convertAsyncTypes(Type type) { if (type.isa()) return AsyncAPI::opaquePointerType(type.getContext()); diff --git a/mlir/lib/Conversion/GPUToNVVM/LowerGpuOpsToNVVMOps.cpp b/mlir/lib/Conversion/GPUToNVVM/LowerGpuOpsToNVVMOps.cpp index e4d98f699178e..457355b263e4f 100644 --- a/mlir/lib/Conversion/GPUToNVVM/LowerGpuOpsToNVVMOps.cpp +++ b/mlir/lib/Conversion/GPUToNVVM/LowerGpuOpsToNVVMOps.cpp @@ -180,7 +180,7 @@ struct LowerGpuOpsToNVVMOpsPass // memory allocations as local `alloca`s in the default address space. This // converter drops the private memory space to support the use case above. LLVMTypeConverter converter(m.getContext(), options); - converter.addConversion([&](MemRefType type) -> Optional { + converter.addConversion([&](MemRefType type) -> std::optional { if (type.getMemorySpaceAsInt() != gpu::GPUDialect::getPrivateAddressSpace()) return std::nullopt; diff --git a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp index 2d7fb3ed90f10..f5b35b794bf11 100644 --- a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp +++ b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp @@ -42,13 +42,13 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx, // before the conversions below since conversions are attempted in reverse // order and those should take priority. addConversion([](Type type) { - return LLVM::isCompatibleType(type) ? llvm::Optional(type) + return LLVM::isCompatibleType(type) ? std::optional(type) : std::nullopt; }); // LLVM container types may (recursively) contain other types that must be // converted even when the outer type is compatible. - addConversion([&](LLVM::LLVMPointerType type) -> llvm::Optional { + addConversion([&](LLVM::LLVMPointerType type) -> std::optional { if (type.isOpaque()) return type; if (auto pointee = convertType(type.getElementType())) @@ -56,7 +56,7 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx, return std::nullopt; }); addConversion([&](LLVM::LLVMStructType type, SmallVectorImpl &results, - ArrayRef callStack) -> llvm::Optional { + ArrayRef callStack) -> std::optional { // Fastpath for types that won't be converted by this callback anyway. if (LLVM::isCompatibleType(type)) { results.push_back(type); @@ -99,12 +99,12 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx, type.getContext(), convertedSubtypes, type.isPacked())); return success(); }); - addConversion([&](LLVM::LLVMArrayType type) -> llvm::Optional { + addConversion([&](LLVM::LLVMArrayType type) -> std::optional { if (auto element = convertType(type.getElementType())) return LLVM::LLVMArrayType::get(element, type.getNumElements()); return std::nullopt; }); - addConversion([&](LLVM::LLVMFunctionType type) -> llvm::Optional { + addConversion([&](LLVM::LLVMFunctionType type) -> std::optional { Type convertedResType = convertType(type.getReturnType()); if (!convertedResType) return std::nullopt; @@ -123,7 +123,7 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx, // value represents a memref. addArgumentMaterialization( [&](OpBuilder &builder, UnrankedMemRefType resultType, ValueRange inputs, - Location loc) -> Optional { + Location loc) -> std::optional { if (inputs.size() == 1) return std::nullopt; return UnrankedMemRefDescriptor::pack(builder, loc, *this, resultType, @@ -131,7 +131,7 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx, }); addArgumentMaterialization([&](OpBuilder &builder, MemRefType resultType, ValueRange inputs, - Location loc) -> Optional { + Location loc) -> std::optional { // TODO: bare ptr conversion could be handled here but we would need a way // to distinguish between FuncOp and other regions. if (inputs.size() == 1) @@ -142,7 +142,7 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx, // non-LLVM types persist after an LLVM conversion. addSourceMaterialization([&](OpBuilder &builder, Type resultType, ValueRange inputs, - Location loc) -> Optional { + Location loc) -> std::optional { if (inputs.size() != 1) return std::nullopt; @@ -151,7 +151,7 @@ LLVMTypeConverter::LLVMTypeConverter(MLIRContext *ctx, }); addTargetMaterialization([&](OpBuilder &builder, Type resultType, ValueRange inputs, - Location loc) -> Optional { + Location loc) -> std::optional { if (inputs.size() != 1) return std::nullopt; diff --git a/mlir/lib/Conversion/MathToSPIRV/MathToSPIRVPass.cpp b/mlir/lib/Conversion/MathToSPIRV/MathToSPIRVPass.cpp index 6e9ab607fef84..ad455c297e1c4 100644 --- a/mlir/lib/Conversion/MathToSPIRV/MathToSPIRVPass.cpp +++ b/mlir/lib/Conversion/MathToSPIRV/MathToSPIRVPass.cpp @@ -47,7 +47,7 @@ void ConvertMathToSPIRVPass::runOnOperation() { auto addUnrealizedCast = [](OpBuilder &builder, Type type, ValueRange inputs, Location loc) { auto cast = builder.create(loc, type, inputs); - return Optional(cast.getResult(0)); + return std::optional(cast.getResult(0)); }; typeConverter.addSourceMaterialization(addUnrealizedCast); typeConverter.addTargetMaterialization(addUnrealizedCast); diff --git a/mlir/lib/Conversion/MemRefToSPIRV/MapMemRefStorageClassPass.cpp b/mlir/lib/Conversion/MemRefToSPIRV/MapMemRefStorageClassPass.cpp index 2a95199055f7e..884b5ea316bdf 100644 --- a/mlir/lib/Conversion/MemRefToSPIRV/MapMemRefStorageClassPass.cpp +++ b/mlir/lib/Conversion/MemRefToSPIRV/MapMemRefStorageClassPass.cpp @@ -56,7 +56,7 @@ using namespace mlir; MAP_FN(spirv::StorageClass::Input, 9) \ MAP_FN(spirv::StorageClass::Output, 10) -Optional +std::optional spirv::mapMemorySpaceToVulkanStorageClass(Attribute memorySpaceAttr) { // Handle null memory space attribute specially. if (!memorySpaceAttr) @@ -83,7 +83,7 @@ spirv::mapMemorySpaceToVulkanStorageClass(Attribute memorySpaceAttr) { #undef STORAGE_SPACE_MAP_FN } -Optional +std::optional spirv::mapVulkanStorageClassToMemorySpace(spirv::StorageClass storageClass) { #define STORAGE_SPACE_MAP_FN(storage, space) \ case storage: \ @@ -110,7 +110,7 @@ spirv::mapVulkanStorageClassToMemorySpace(spirv::StorageClass storageClass) { MAP_FN(spirv::StorageClass::Function, 6) \ MAP_FN(spirv::StorageClass::Image, 7) -Optional +std::optional spirv::mapMemorySpaceToOpenCLStorageClass(Attribute memorySpaceAttr) { // Handle null memory space attribute specially. if (!memorySpaceAttr) @@ -137,7 +137,7 @@ spirv::mapMemorySpaceToOpenCLStorageClass(Attribute memorySpaceAttr) { #undef STORAGE_SPACE_MAP_FN } -Optional +std::optional spirv::mapOpenCLStorageClassToMemorySpace(spirv::StorageClass storageClass) { #define STORAGE_SPACE_MAP_FN(storage, space) \ case storage: \ @@ -165,8 +165,8 @@ spirv::MemorySpaceToStorageClassConverter::MemorySpaceToStorageClassConverter( // Pass through for all other types. addConversion([](Type type) { return type; }); - addConversion([this](BaseMemRefType memRefType) -> Optional { - Optional storage = + addConversion([this](BaseMemRefType memRefType) -> std::optional { + std::optional storage = this->memorySpaceMap(memRefType.getMemorySpace()); if (!storage) { LLVM_DEBUG(llvm::dbgs() diff --git a/mlir/lib/Conversion/MemRefToSPIRV/MemRefToSPIRVPass.cpp b/mlir/lib/Conversion/MemRefToSPIRV/MemRefToSPIRVPass.cpp index b12311bc5624c..9effeabc78d37 100644 --- a/mlir/lib/Conversion/MemRefToSPIRV/MemRefToSPIRVPass.cpp +++ b/mlir/lib/Conversion/MemRefToSPIRV/MemRefToSPIRVPass.cpp @@ -48,7 +48,7 @@ void ConvertMemRefToSPIRVPass::runOnOperation() { auto addUnrealizedCast = [](OpBuilder &builder, Type type, ValueRange inputs, Location loc) { auto cast = builder.create(loc, type, inputs); - return Optional(cast.getResult(0)); + return std::optional(cast.getResult(0)); }; typeConverter.addSourceMaterialization(addUnrealizedCast); typeConverter.addTargetMaterialization(addUnrealizedCast); diff --git a/mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp b/mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp index d2993836bc970..0ba294f2ed799 100644 --- a/mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp +++ b/mlir/lib/Conversion/SPIRVToLLVM/SPIRVToLLVM.cpp @@ -184,7 +184,7 @@ static Value processCountOrOffset(Location loc, Value value, Type srcType, /// Converts SPIR-V struct with a regular (according to `VulkanLayoutUtils`) /// offset to LLVM struct. Otherwise, the conversion is not supported. -static Optional +static std::optional convertStructTypeWithOffset(spirv::StructType type, LLVMTypeConverter &converter) { if (type != VulkanLayoutUtils::decorateType(type)) @@ -247,8 +247,8 @@ static LogicalResult replaceWithLoadOrStore(Operation *op, ValueRange operands, /// Converts SPIR-V array type to LLVM array. Natural stride (according to /// `VulkanLayoutUtils`) is also mapped to LLVM array. This has to be respected /// when converting ops that manipulate array types. -static Optional convertArrayType(spirv::ArrayType type, - TypeConverter &converter) { +static std::optional convertArrayType(spirv::ArrayType type, + TypeConverter &converter) { unsigned stride = type.getArrayStride(); Type elementType = type.getElementType(); auto sizeInBytes = elementType.cast().getSizeInBytes(); @@ -271,8 +271,8 @@ static Type convertPointerType(spirv::PointerType type, /// Converts SPIR-V runtime array to LLVM array. Since LLVM allows indexing over /// the bounds, the runtime array is converted to a 0-sized LLVM array. There is /// no modelling of array stride at the moment. -static Optional convertRuntimeArrayType(spirv::RuntimeArrayType type, - TypeConverter &converter) { +static std::optional convertRuntimeArrayType(spirv::RuntimeArrayType type, + TypeConverter &converter) { if (type.getArrayStride() != 0) return std::nullopt; auto elementType = converter.convertType(type.getElementType()); @@ -281,8 +281,8 @@ static Optional convertRuntimeArrayType(spirv::RuntimeArrayType type, /// Converts SPIR-V struct to LLVM struct. There is no support of structs with /// member decorations. Also, only natural offset is supported. -static Optional convertStructType(spirv::StructType type, - LLVMTypeConverter &converter) { +static std::optional convertStructType(spirv::StructType type, + LLVMTypeConverter &converter) { SmallVector memberDecorations; type.getMemberDecorations(memberDecorations); if (!memberDecorations.empty()) diff --git a/mlir/lib/Conversion/VectorToSPIRV/VectorToSPIRVPass.cpp b/mlir/lib/Conversion/VectorToSPIRV/VectorToSPIRVPass.cpp index 1ab62428d0ad9..57646c86b6620 100644 --- a/mlir/lib/Conversion/VectorToSPIRV/VectorToSPIRVPass.cpp +++ b/mlir/lib/Conversion/VectorToSPIRV/VectorToSPIRVPass.cpp @@ -47,7 +47,7 @@ void ConvertVectorToSPIRVPass::runOnOperation() { auto addUnrealizedCast = [](OpBuilder &builder, Type type, ValueRange inputs, Location loc) { auto cast = builder.create(loc, type, inputs); - return Optional(cast.getResult(0)); + return std::optional(cast.getResult(0)); }; typeConverter.addSourceMaterialization(addUnrealizedCast); typeConverter.addTargetMaterialization(addUnrealizedCast); diff --git a/mlir/lib/Dialect/Arith/Transforms/EmulateWideInt.cpp b/mlir/lib/Dialect/Arith/Transforms/EmulateWideInt.cpp index 1fffb0312bb1d..db3ddab483b5a 100644 --- a/mlir/lib/Dialect/Arith/Transforms/EmulateWideInt.cpp +++ b/mlir/lib/Dialect/Arith/Transforms/EmulateWideInt.cpp @@ -1001,10 +1001,10 @@ arith::WideIntEmulationConverter::WideIntEmulationConverter( assert(widestIntSupportedByTarget >= 2 && "Integer type too narrow"); // Allow unknown types. - addConversion([](Type ty) -> Optional { return ty; }); + addConversion([](Type ty) -> std::optional { return ty; }); // Scalar case. - addConversion([this](IntegerType ty) -> Optional { + addConversion([this](IntegerType ty) -> std::optional { unsigned width = ty.getWidth(); if (width <= maxIntWidth) return ty; @@ -1017,7 +1017,7 @@ arith::WideIntEmulationConverter::WideIntEmulationConverter( }); // Vector case. - addConversion([this](VectorType ty) -> Optional { + addConversion([this](VectorType ty) -> std::optional { auto intTy = ty.getElementType().dyn_cast(); if (!intTy) return ty; @@ -1038,7 +1038,7 @@ arith::WideIntEmulationConverter::WideIntEmulationConverter( }); // Function case. - addConversion([this](FunctionType ty) -> Optional { + addConversion([this](FunctionType ty) -> std::optional { // Convert inputs and results, e.g.: // (i2N, i2N) -> i2N --> (vector<2xiN>, vector<2xiN>) -> vector<2xiN> SmallVector inputs; diff --git a/mlir/lib/Dialect/Arith/Transforms/UnsignedWhenEquivalent.cpp b/mlir/lib/Dialect/Arith/Transforms/UnsignedWhenEquivalent.cpp index bb566f861524a..53f2008324f05 100644 --- a/mlir/lib/Dialect/Arith/Transforms/UnsignedWhenEquivalent.cpp +++ b/mlir/lib/Dialect/Arith/Transforms/UnsignedWhenEquivalent.cpp @@ -128,11 +128,11 @@ struct ArithUnsignedWhenEquivalentPass target .addDynamicallyLegalOp( - [&solver](Operation *op) -> Optional { + [&solver](Operation *op) -> std::optional { return failed(staticallyNonNegative(solver, op)); }); target.addDynamicallyLegalOp( - [&solver](CmpIOp op) -> Optional { + [&solver](CmpIOp op) -> std::optional { return failed(isCmpIConvertable(solver, op)); }); diff --git a/mlir/lib/Dialect/MemRef/Transforms/EmulateWideInt.cpp b/mlir/lib/Dialect/MemRef/Transforms/EmulateWideInt.cpp index 3de4b806cb521..b0e884c285f0c 100644 --- a/mlir/lib/Dialect/MemRef/Transforms/EmulateWideInt.cpp +++ b/mlir/lib/Dialect/MemRef/Transforms/EmulateWideInt.cpp @@ -145,7 +145,7 @@ void memref::populateMemRefWideIntEmulationPatterns( void memref::populateMemRefWideIntEmulationConversions( arith::WideIntEmulationConverter &typeConverter) { typeConverter.addConversion( - [&typeConverter](MemRefType ty) -> Optional { + [&typeConverter](MemRefType ty) -> std::optional { auto intTy = ty.getElementType().dyn_cast(); if (!intTy) return ty; diff --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVTypes.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVTypes.cpp index 40082b1cb36af..7dc3fdef32c75 100644 --- a/mlir/lib/Dialect/SPIRV/IR/SPIRVTypes.cpp +++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVTypes.cpp @@ -66,19 +66,19 @@ Type ArrayType::getElementType() const { return getImpl()->elementType; } unsigned ArrayType::getArrayStride() const { return getImpl()->stride; } void ArrayType::getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { getElementType().cast().getExtensions(extensions, storage); } void ArrayType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { getElementType().cast().getCapabilities(capabilities, storage); } -Optional ArrayType::getSizeInBytes() { +std::optional ArrayType::getSizeInBytes() { auto elementType = getElementType().cast(); - Optional size = elementType.getSizeInBytes(); + std::optional size = elementType.getSizeInBytes(); if (!size) return std::nullopt; return (*size + getArrayStride()) * getNumElements(); @@ -153,7 +153,7 @@ bool CompositeType::hasCompileTimeKnownNumElements() const { void CompositeType::getExtensions( SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { TypeSwitch(*this) .Case( @@ -167,7 +167,7 @@ void CompositeType::getExtensions( void CompositeType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { TypeSwitch(*this) .Case( @@ -185,13 +185,13 @@ void CompositeType::getCapabilities( .Default([](Type) { llvm_unreachable("invalid composite type"); }); } -Optional CompositeType::getSizeInBytes() { +std::optional CompositeType::getSizeInBytes() { if (auto arrayType = dyn_cast()) return arrayType.getSizeInBytes(); if (auto structType = dyn_cast()) return structType.getSizeInBytes(); if (auto vectorType = dyn_cast()) { - Optional elementSize = + std::optional elementSize = vectorType.getElementType().cast().getSizeInBytes(); if (!elementSize) return std::nullopt; @@ -247,7 +247,7 @@ unsigned CooperativeMatrixNVType::getColumns() const { void CooperativeMatrixNVType::getExtensions( SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { getElementType().cast().getExtensions(extensions, storage); static const Extension exts[] = {Extension::SPV_NV_cooperative_matrix}; ArrayRef ref(exts, std::size(exts)); @@ -256,7 +256,7 @@ void CooperativeMatrixNVType::getExtensions( void CooperativeMatrixNVType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { getElementType().cast().getCapabilities(capabilities, storage); static const Capability caps[] = {Capability::CooperativeMatrixNV}; ArrayRef ref(caps, std::size(caps)); @@ -315,7 +315,7 @@ MatrixLayout JointMatrixINTELType::getMatrixLayout() const { void JointMatrixINTELType::getExtensions( SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { getElementType().cast().getExtensions(extensions, storage); static const Extension exts[] = {Extension::SPV_INTEL_joint_matrix}; ArrayRef ref(exts, std::size(exts)); @@ -324,7 +324,7 @@ void JointMatrixINTELType::getExtensions( void JointMatrixINTELType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { getElementType().cast().getCapabilities(capabilities, storage); static const Capability caps[] = {Capability::JointMatrixINTEL}; ArrayRef ref(caps, std::size(caps)); @@ -434,12 +434,13 @@ ImageSamplerUseInfo ImageType::getSamplerUseInfo() const { ImageFormat ImageType::getImageFormat() const { return getImpl()->format; } void ImageType::getExtensions(SPIRVType::ExtensionArrayRefVector &, - Optional) { + std::optional) { // Image types do not require extra extensions thus far. } void ImageType::getCapabilities( - SPIRVType::CapabilityArrayRefVector &capabilities, Optional) { + SPIRVType::CapabilityArrayRefVector &capabilities, + std::optional) { if (auto dimCaps = spirv::getCapabilities(getDim())) capabilities.push_back(*dimCaps); @@ -484,7 +485,7 @@ StorageClass PointerType::getStorageClass() const { } void PointerType::getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { // Use this pointer type's storage class because this pointer indicates we are // using the pointee type in that specific storage class. getPointeeType().cast().getExtensions(extensions, @@ -496,7 +497,7 @@ void PointerType::getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, void PointerType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { // Use this pointer type's storage class because this pointer indicates we are // using the pointee type in that specific storage class. getPointeeType().cast().getCapabilities(capabilities, @@ -544,13 +545,13 @@ unsigned RuntimeArrayType::getArrayStride() const { return getImpl()->stride; } void RuntimeArrayType::getExtensions( SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { getElementType().cast().getExtensions(extensions, storage); } void RuntimeArrayType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { { static const Capability caps[] = {Capability::Shader}; ArrayRef ref(caps, std::size(caps)); @@ -589,7 +590,7 @@ bool ScalarType::isValid(IntegerType type) { } void ScalarType::getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { // 8- or 16-bit integer/floating-point numbers will require extra extensions // to appear in interface storage classes. See SPV_KHR_16bit_storage and // SPV_KHR_8bit_storage for more details. @@ -621,7 +622,7 @@ void ScalarType::getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, void ScalarType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { unsigned bitwidth = getIntOrFloatBitWidth(); // 8- or 16-bit integer/floating-point numbers will require extra capabilities @@ -707,7 +708,7 @@ void ScalarType::getCapabilities( #undef WIDTH_CASE } -Optional ScalarType::getSizeInBytes() { +std::optional ScalarType::getSizeInBytes() { auto bitWidth = getIntOrFloatBitWidth(); // According to the SPIR-V spec: // "There is no physical size or bit pattern defined for values with boolean @@ -740,7 +741,7 @@ bool SPIRVType::isScalarOrVector() { } void SPIRVType::getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { if (auto scalarType = dyn_cast()) { scalarType.getExtensions(extensions, storage); } else if (auto compositeType = dyn_cast()) { @@ -760,7 +761,7 @@ void SPIRVType::getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, void SPIRVType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { if (auto scalarType = dyn_cast()) { scalarType.getCapabilities(capabilities, storage); } else if (auto compositeType = dyn_cast()) { @@ -778,7 +779,7 @@ void SPIRVType::getCapabilities( } } -Optional SPIRVType::getSizeInBytes() { +std::optional SPIRVType::getSizeInBytes() { if (auto scalarType = dyn_cast()) return scalarType.getSizeInBytes(); if (auto compositeType = dyn_cast()) @@ -828,13 +829,13 @@ SampledImageType::verify(function_ref emitError, void SampledImageType::getExtensions( SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { getImageType().cast().getExtensions(extensions, storage); } void SampledImageType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { getImageType().cast().getCapabilities(capabilities, storage); } @@ -1128,14 +1129,14 @@ StructType::trySetBody(ArrayRef memberTypes, } void StructType::getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { for (Type elementType : getElementTypes()) elementType.cast().getExtensions(extensions, storage); } void StructType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { for (Type elementType : getElementTypes()) elementType.cast().getCapabilities(capabilities, storage); } @@ -1227,13 +1228,13 @@ unsigned MatrixType::getNumElements() const { } void MatrixType::getExtensions(SPIRVType::ExtensionArrayRefVector &extensions, - Optional storage) { + std::optional storage) { getColumnType().cast().getExtensions(extensions, storage); } void MatrixType::getCapabilities( SPIRVType::CapabilityArrayRefVector &capabilities, - Optional storage) { + std::optional storage) { { static const Capability caps[] = {Capability::Matrix}; ArrayRef ref(caps, std::size(caps)); diff --git a/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp b/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp index 62e3a3de0f11b..b455993924b55 100644 --- a/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp +++ b/mlir/lib/Dialect/SPIRV/Transforms/SPIRVConversion.cpp @@ -129,8 +129,8 @@ bool SPIRVTypeConverter::allows(spirv::Capability capability) { // TODO: This is a utility function that should probably be exposed by the // SPIR-V dialect. Keeping it local till the use case arises. -static Optional getTypeNumBytes(const SPIRVConversionOptions &options, - Type type) { +static std::optional +getTypeNumBytes(const SPIRVConversionOptions &options, Type type) { if (type.isa()) { auto bitWidth = type.getIntOrFloatBitWidth(); // According to the SPIR-V spec: @@ -203,10 +203,10 @@ static Optional getTypeNumBytes(const SPIRVConversionOptions &options, } /// Converts a scalar `type` to a suitable type under the given `targetEnv`. -static Type convertScalarType(const spirv::TargetEnv &targetEnv, - const SPIRVConversionOptions &options, - spirv::ScalarType type, - Optional storageClass = {}) { +static Type +convertScalarType(const spirv::TargetEnv &targetEnv, + const SPIRVConversionOptions &options, spirv::ScalarType type, + std::optional storageClass = {}) { // Get extension and capability requirements for the given type. SmallVector, 1> extensions; SmallVector, 2> capabilities; @@ -243,10 +243,10 @@ static Type convertScalarType(const spirv::TargetEnv &targetEnv, } /// Converts a vector `type` to a suitable type under the given `targetEnv`. -static Type convertVectorType(const spirv::TargetEnv &targetEnv, - const SPIRVConversionOptions &options, - VectorType type, - Optional storageClass = {}) { +static Type +convertVectorType(const spirv::TargetEnv &targetEnv, + const SPIRVConversionOptions &options, VectorType type, + std::optional storageClass = {}) { auto scalarType = type.getElementType().cast(); if (type.getRank() <= 1 && type.getNumElements() == 1) return convertScalarType(targetEnv, options, scalarType, storageClass); @@ -299,8 +299,8 @@ static Type convertTensorType(const spirv::TargetEnv &targetEnv, return nullptr; } - Optional scalarSize = getTypeNumBytes(options, scalarType); - Optional tensorSize = getTypeNumBytes(options, type); + std::optional scalarSize = getTypeNumBytes(options, scalarType); + std::optional tensorSize = getTypeNumBytes(options, type); if (!scalarSize || !tensorSize) { LLVM_DEBUG(llvm::dbgs() << type << " illegal: cannot deduce element count\n"); @@ -311,7 +311,8 @@ static Type convertTensorType(const spirv::TargetEnv &targetEnv, auto arrayElemType = convertScalarType(targetEnv, options, scalarType); if (!arrayElemType) return nullptr; - Optional arrayElemSize = getTypeNumBytes(options, arrayElemType); + std::optional arrayElemSize = + getTypeNumBytes(options, arrayElemType); if (!arrayElemSize) { LLVM_DEBUG(llvm::dbgs() << type << " illegal: cannot deduce converted element size\n"); @@ -339,7 +340,8 @@ static Type convertBoolMemrefType(const spirv::TargetEnv &targetEnv, convertScalarType(targetEnv, options, elementType, storageClass); if (!arrayElemType) return nullptr; - Optional arrayElemSize = getTypeNumBytes(options, arrayElemType); + std::optional arrayElemSize = + getTypeNumBytes(options, arrayElemType); if (!arrayElemSize) { LLVM_DEBUG(llvm::dbgs() << type << " illegal: cannot deduce converted element size\n"); @@ -406,7 +408,8 @@ static Type convertMemrefType(const spirv::TargetEnv &targetEnv, if (!arrayElemType) return nullptr; - Optional arrayElemSize = getTypeNumBytes(options, arrayElemType); + std::optional arrayElemSize = + getTypeNumBytes(options, arrayElemType); if (!arrayElemSize) { LLVM_DEBUG(llvm::dbgs() << type << " illegal: cannot deduce converted element size\n"); @@ -426,7 +429,7 @@ static Type convertMemrefType(const spirv::TargetEnv &targetEnv, return wrapInStructAndGetPointer(arrayType, storageClass); } - Optional memrefSize = getTypeNumBytes(options, type); + std::optional memrefSize = getTypeNumBytes(options, type); if (!memrefSize) { LLVM_DEBUG(llvm::dbgs() << type << " illegal: cannot deduce element count\n"); @@ -458,13 +461,13 @@ SPIRVTypeConverter::SPIRVTypeConverter(spirv::TargetEnvAttr targetAttr, addConversion([this](IndexType /*indexType*/) { return getIndexType(); }); - addConversion([this](IntegerType intType) -> Optional { + addConversion([this](IntegerType intType) -> std::optional { if (auto scalarType = intType.dyn_cast()) return convertScalarType(this->targetEnv, this->options, scalarType); return Type(); }); - addConversion([this](FloatType floatType) -> Optional { + addConversion([this](FloatType floatType) -> std::optional { if (auto scalarType = floatType.dyn_cast()) return convertScalarType(this->targetEnv, this->options, scalarType); return Type(); diff --git a/mlir/lib/Dialect/SPIRV/Transforms/UnifyAliasedResourcePass.cpp b/mlir/lib/Dialect/SPIRV/Transforms/UnifyAliasedResourcePass.cpp index edb72bec52530..3e5b934be677c 100644 --- a/mlir/lib/Dialect/SPIRV/Transforms/UnifyAliasedResourcePass.cpp +++ b/mlir/lib/Dialect/SPIRV/Transforms/UnifyAliasedResourcePass.cpp @@ -84,7 +84,8 @@ static Type getRuntimeArrayElementType(Type type) { /// Given a list of resource element `types`, returns the index of the canonical /// resource that all resources should be unified into. Returns std::nullopt if /// unable to unify. -static Optional deduceCanonicalResource(ArrayRef types) { +static std::optional +deduceCanonicalResource(ArrayRef types) { // scalarNumBits: contains all resources' scalar types' bit counts. // vectorNumBits: only contains resources whose element types are vectors. // vectorIndices: each vector's original index in `types`. @@ -101,7 +102,7 @@ static Optional deduceCanonicalResource(ArrayRef types) { return std::nullopt; // Odd-sized vector has special layout // requirements. - Optional numBytes = type.getSizeInBytes(); + std::optional numBytes = type.getSizeInBytes(); if (!numBytes) return std::nullopt; @@ -280,7 +281,7 @@ void ResourceAliasAnalysis::recordIfUnifiable( elementTypes.push_back(type); } - Optional index = deduceCanonicalResource(elementTypes); + std::optional index = deduceCanonicalResource(elementTypes); if (!index) return; diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp index f3e4a9af8f483..6b406d8241bc6 100644 --- a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp +++ b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorCodegen.cpp @@ -178,7 +178,7 @@ static void createPushback(OpBuilder &builder, Location loc, } /// Maps a sparse tensor type to the appropriate compounded buffers. -static Optional +static std::optional convertSparseTensorType(Type type, SmallVectorImpl &fields) { auto enc = getSparseTensorEncoding(type); if (!enc) @@ -1039,7 +1039,7 @@ mlir::SparseTensorTypeToBufferConverter::SparseTensorTypeToBufferConverter() { // Required by scf.for 1:N type conversion. addSourceMaterialization([](OpBuilder &builder, RankedTensorType tp, ValueRange inputs, - Location loc) -> Optional { + Location loc) -> std::optional { if (!getSparseTensorEncoding(tp)) // Not a sparse tensor. return std::nullopt; diff --git a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp index a358f384bfb0c..1d5d28ca512af 100644 --- a/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp +++ b/mlir/lib/Dialect/SparseTensor/Transforms/SparseTensorConversion.cpp @@ -39,7 +39,7 @@ namespace { //===----------------------------------------------------------------------===// /// Maps each sparse tensor type to an opaque pointer. -static Optional convertSparseTensorTypes(Type type) { +static std::optional convertSparseTensorTypes(Type type) { if (getSparseTensorEncoding(type) != nullptr) return LLVM::LLVMPointerType::get(IntegerType::get(type.getContext(), 8)); return std::nullopt; diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp index 795293bb07988..c476e5f0c5696 100644 --- a/mlir/lib/Transforms/Utils/DialectConversion.cpp +++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp @@ -50,8 +50,9 @@ computeConversionSet(iterator_range region, // Don't check this operation's children for conversion if the operation // is recursively legal. - auto legalityInfo = target ? target->isLegal(&op) - : Optional(); + auto legalityInfo = + target ? target->isLegal(&op) + : std::optional(); if (legalityInfo && legalityInfo->isRecursivelyLegal) continue; for (auto ®ion : op.getRegions()) { @@ -496,7 +497,7 @@ struct ArgConverter { /// The conversion information for each of the arguments. The information is /// std::nullopt if the argument was dropped during conversion. - SmallVector, 1> argInfo; + SmallVector, 1> argInfo; /// The type converter used to convert the arguments. TypeConverter *converter; @@ -649,7 +650,7 @@ void ArgConverter::applyRewrites(ConversionValueMapping &mapping) { // Process the remapping for each of the original arguments. for (unsigned i = 0, e = origBlock->getNumArguments(); i != e; ++i) { - Optional &argInfo = blockInfo.argInfo[i]; + std::optional &argInfo = blockInfo.argInfo[i]; BlockArgument origArg = origBlock->getArgument(i); // Handle the case of a 1->0 value mapping. @@ -888,7 +889,8 @@ struct ConversionPatternRewriterImpl { /// success if the values could be remapped, failure otherwise. `valueDiagTag` /// is the tag used when describing a value within a diagnostic, e.g. /// "operand". - LogicalResult remapValues(StringRef valueDiagTag, Optional inputLoc, + LogicalResult remapValues(StringRef valueDiagTag, + std::optional inputLoc, PatternRewriter &rewriter, ValueRange values, SmallVectorImpl &remapped); @@ -1253,7 +1255,7 @@ void ConversionPatternRewriterImpl::undoBlockActions( } LogicalResult ConversionPatternRewriterImpl::remapValues( - StringRef valueDiagTag, Optional inputLoc, + StringRef valueDiagTag, std::optional inputLoc, PatternRewriter &rewriter, ValueRange values, SmallVectorImpl &remapped) { remapped.reserve(llvm::size(values)); @@ -2186,7 +2188,7 @@ void OperationLegalizer::buildLegalizationGraph( // Check to see if any of the generated operations are invalid. if (llvm::any_of(pattern->getGeneratedOps(), [&](OperationName op) { - Optional action = target.getOpAction(op); + std::optional action = target.getOpAction(op); return !legalizerPatterns.count(op) && (!action || action == LegalizationAction::Illegal); })) @@ -2370,7 +2372,7 @@ struct OperationConverter { LogicalResult legalizeUnresolvedMaterializations( ConversionPatternRewriter &rewriter, ConversionPatternRewriterImpl &rewriterImpl, - Optional>> &inverseMapping); + std::optional>> &inverseMapping); /// Legalize an operation result that was marked as "erased". LogicalResult @@ -2479,7 +2481,7 @@ LogicalResult OperationConverter::convertOperations( LogicalResult OperationConverter::finalize(ConversionPatternRewriter &rewriter) { - Optional>> inverseMapping; + std::optional>> inverseMapping; ConversionPatternRewriterImpl &rewriterImpl = rewriter.getImpl(); if (failed(legalizeUnresolvedMaterializations(rewriter, rewriterImpl, inverseMapping)) || @@ -2806,7 +2808,7 @@ static LogicalResult legalizeUnresolvedMaterialization( LogicalResult OperationConverter::legalizeUnresolvedMaterializations( ConversionPatternRewriter &rewriter, ConversionPatternRewriterImpl &rewriterImpl, - Optional>> &inverseMapping) { + std::optional>> &inverseMapping) { if (rewriterImpl.unresolvedMaterializations.empty()) return success(); inverseMapping = rewriterImpl.mapping.getInverse(); @@ -2961,7 +2963,7 @@ LogicalResult TypeConverter::convertType(Type t, auto popConversionCallStack = llvm::make_scope_exit([this]() { conversionCallStack.pop_back(); }); for (ConversionCallbackFn &converter : llvm::reverse(conversions)) { - if (Optional result = + if (std::optional result = converter(t, results, conversionCallStack)) { if (!succeeded(*result)) { cachedDirectConversions.try_emplace(t, nullptr); @@ -3039,13 +3041,13 @@ Value TypeConverter::materializeConversion( MutableArrayRef materializations, OpBuilder &builder, Location loc, Type resultType, ValueRange inputs) { for (MaterializationCallbackFn &fn : llvm::reverse(materializations)) - if (Optional result = fn(builder, resultType, inputs, loc)) + if (std::optional result = fn(builder, resultType, inputs, loc)) return *result; return nullptr; } auto TypeConverter::convertBlockSignature(Block *block) - -> Optional { + -> std::optional { SignatureConversion conversion(block->getNumArguments()); if (failed(convertSignatureArgs(block->getArgumentTypes(), conversion))) return std::nullopt; @@ -3138,14 +3140,14 @@ void ConversionTarget::setDialectAction(ArrayRef dialectNames, } auto ConversionTarget::getOpAction(OperationName op) const - -> Optional { - Optional info = getOpInfo(op); - return info ? info->action : Optional(); + -> std::optional { + std::optional info = getOpInfo(op); + return info ? info->action : std::optional(); } auto ConversionTarget::isLegal(Operation *op) const - -> Optional { - Optional info = getOpInfo(op->getName()); + -> std::optional { + std::optional info = getOpInfo(op->getName()); if (!info) return std::nullopt; @@ -3153,7 +3155,7 @@ auto ConversionTarget::isLegal(Operation *op) const auto isOpLegal = [&] { // Handle dynamic legality either with the provided legality function. if (info->action == LegalizationAction::Dynamic) { - Optional result = info->legalityFn(op); + std::optional result = info->legalityFn(op); if (result) return *result; } @@ -3179,12 +3181,12 @@ auto ConversionTarget::isLegal(Operation *op) const } bool ConversionTarget::isIllegal(Operation *op) const { - Optional info = getOpInfo(op->getName()); + std::optional info = getOpInfo(op->getName()); if (!info) return false; if (info->action == LegalizationAction::Dynamic) { - Optional result = info->legalityFn(op); + std::optional result = info->legalityFn(op); if (!result) return false; @@ -3201,8 +3203,8 @@ static ConversionTarget::DynamicLegalityCallbackFn composeLegalityCallbacks( return newCallback; auto chain = [oldCl = std::move(oldCallback), newCl = std::move(newCallback)]( - Operation *op) -> Optional { - if (Optional result = newCl(op)) + Operation *op) -> std::optional { + if (std::optional result = newCl(op)) return *result; return oldCl(op); @@ -3250,7 +3252,7 @@ void ConversionTarget::setLegalityCallback( } auto ConversionTarget::getOpInfo(OperationName op) const - -> Optional { + -> std::optional { // Check for info for this specific operation. auto it = legalOperations.find(op); if (it != legalOperations.end()) diff --git a/mlir/test/lib/Dialect/Arith/TestEmulateWideInt.cpp b/mlir/test/lib/Dialect/Arith/TestEmulateWideInt.cpp index c1ae321711fcd..8f0c13af9de5b 100644 --- a/mlir/test/lib/Dialect/Arith/TestEmulateWideInt.cpp +++ b/mlir/test/lib/Dialect/Arith/TestEmulateWideInt.cpp @@ -59,7 +59,7 @@ struct TestEmulateWideIntPass // TODO: Consider extending `arith.bitcast` to support scalar-to-1D-vector // casts (and vice versa) and using it insted of `llvm.bitcast`. auto addBitcast = [](OpBuilder &builder, Type type, ValueRange inputs, - Location loc) -> Optional { + Location loc) -> std::optional { auto cast = builder.create(loc, type, inputs); return cast->getResult(0); }; diff --git a/mlir/test/lib/Dialect/Func/TestDecomposeCallGraphTypes.cpp b/mlir/test/lib/Dialect/Func/TestDecomposeCallGraphTypes.cpp index 84338d6991235..9492d23b1778f 100644 --- a/mlir/test/lib/Dialect/Func/TestDecomposeCallGraphTypes.cpp +++ b/mlir/test/lib/Dialect/Func/TestDecomposeCallGraphTypes.cpp @@ -73,7 +73,7 @@ struct TestDecomposeCallGraphTypes typeConverter.addArgumentMaterialization( [](OpBuilder &builder, TupleType resultType, ValueRange inputs, - Location loc) -> Optional { + Location loc) -> std::optional { if (inputs.size() == 1) return std::nullopt; TupleType tuple = builder.getTupleType(inputs.getTypes()); diff --git a/mlir/test/lib/Dialect/Test/TestPatterns.cpp b/mlir/test/lib/Dialect/Test/TestPatterns.cpp index 4590419ed2493..f16636cbff161 100644 --- a/mlir/test/lib/Dialect/Test/TestPatterns.cpp +++ b/mlir/test/lib/Dialect/Test/TestPatterns.cpp @@ -777,8 +777,9 @@ struct TestTypeConverter : public TypeConverter { /// Hook for materializing a conversion. This is necessary because we generate /// 1->N type mappings. - static Optional materializeCast(OpBuilder &builder, Type resultType, - ValueRange inputs, Location loc) { + static std::optional materializeCast(OpBuilder &builder, + Type resultType, + ValueRange inputs, Location loc) { return builder.create(loc, resultType, inputs).getResult(); } }; @@ -1267,7 +1268,7 @@ struct TestTypeConversionDriver // Convert a recursive self-referring type into a non-self-referring // type named "outer_converted_type" that contains a SimpleAType. [&](test::TestRecursiveType type, SmallVectorImpl &results, - ArrayRef callStack) -> Optional { + ArrayRef callStack) -> std::optional { // If the type is already converted, return it to indicate that it is // legal. if (type.getName() == "outer_converted_type") { diff --git a/mlir/test/lib/Transforms/TestDialectConversion.cpp b/mlir/test/lib/Transforms/TestDialectConversion.cpp index 996b7b9e28861..97fe78c35e833 100644 --- a/mlir/test/lib/Transforms/TestDialectConversion.cpp +++ b/mlir/test/lib/Transforms/TestDialectConversion.cpp @@ -44,8 +44,9 @@ struct PDLLTypeConverter : public TypeConverter { return success(); } /// Hook for materializing a conversion. - static Optional materializeCast(OpBuilder &builder, Type resultType, - ValueRange inputs, Location loc) { + static std::optional materializeCast(OpBuilder &builder, + Type resultType, + ValueRange inputs, Location loc) { return builder.create(loc, resultType, inputs) .getResult(0); } diff --git a/mlir/unittests/Transforms/DialectConversion.cpp b/mlir/unittests/Transforms/DialectConversion.cpp index 0cac2975ea91c..f4a60af82d126 100644 --- a/mlir/unittests/Transforms/DialectConversion.cpp +++ b/mlir/unittests/Transforms/DialectConversion.cpp @@ -37,10 +37,11 @@ TEST(DialectConversionTest, DynamicallyLegalOpCallbackOrder) { }); int callbackCalled2 = 0; - target.addDynamicallyLegalOp([&](Operation *) -> Optional { - callbackCalled2 = ++index; - return std::nullopt; - }); + target.addDynamicallyLegalOp( + [&](Operation *) -> std::optional { + callbackCalled2 = ++index; + return std::nullopt; + }); auto *op = createOp(&context); EXPECT_TRUE(target.isLegal(op)); @@ -58,10 +59,11 @@ TEST(DialectConversionTest, DynamicallyLegalOpCallbackSkip) { int index = 0; int callbackCalled = 0; - target.addDynamicallyLegalOp([&](Operation *) -> Optional { - callbackCalled = ++index; - return std::nullopt; - }); + target.addDynamicallyLegalOp( + [&](Operation *) -> std::optional { + callbackCalled = ++index; + return std::nullopt; + }); auto *op = createOp(&context); EXPECT_FALSE(target.isLegal(op)); @@ -83,7 +85,7 @@ TEST(DialectConversionTest, DynamicallyLegalUnknownOpCallbackOrder) { }); int callbackCalled2 = 0; - target.markUnknownOpDynamicallyLegal([&](Operation *) -> Optional { + target.markUnknownOpDynamicallyLegal([&](Operation *) -> std::optional { callbackCalled2 = ++index; return std::nullopt; }); @@ -103,7 +105,7 @@ TEST(DialectConversionTest, DynamicallyLegalReturnNone) { ConversionTarget target(context); target.addDynamicallyLegalOp( - [&](Operation *) -> Optional { return std::nullopt; }); + [&](Operation *) -> std::optional { return std::nullopt; }); auto *op = createOp(&context); EXPECT_FALSE(target.isLegal(op)); @@ -120,7 +122,7 @@ TEST(DialectConversionTest, DynamicallyLegalUnknownReturnNone) { ConversionTarget target(context); target.markUnknownOpDynamicallyLegal( - [&](Operation *) -> Optional { return std::nullopt; }); + [&](Operation *) -> std::optional { return std::nullopt; }); auto *op = createOp(&context); EXPECT_FALSE(target.isLegal(op));