//===-- BuiltinBytecode.td - Builtin bytecode defs ---------*- tablegen -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This is the Builtin bytecode reader/writer definition file. // //===----------------------------------------------------------------------===// #ifndef BUILTIN_BYTECODE #define BUILTIN_BYTECODE include "mlir/IR/BytecodeBase.td" def LocationAttr : AttributeKind; def ShapedType: WithType<"ShapedType", Type>; def Location : CompositeBytecode { dag members = (attr WithGetter<"(LocationAttr)$_attrType", WithType<"LocationAttr", LocationAttr>>:$value ); let cBuilder = "Location($_args)"; } def String : WithParser <"succeeded($_reader.readString($_var))", WithBuilder<"$_args", WithPrinter<"$_writer.writeOwnedString($_getter)", WithGetter <"$_attrType", WithType <"StringRef">>>>>; def ArrayAttr : DialectAttribute<(attr Array:$value )>; let cType = "StringAttr" in { def StringAttr : DialectAttribute<(attr String:$value )> { let printerPredicate = "::llvm::isa($_val.getType())"; } def StringAttrWithType : DialectAttribute<(attr String:$value, Type:$type )> { let printerPredicate = "!::llvm::isa($_val.getType())"; } } def NamedAttribute : CompositeBytecode { dag members = (attr StringAttr:$name, Attribute:$value ); let cBuilder = "NamedAttribute($_args)"; } def DictionaryAttr : DialectAttribute<(attr Array:$value )>; def FlatSymbolRefAttr: DialectAttribute<(attr StringAttr:$rootReference )>; def SymbolRefAttr: DialectAttribute<(attr StringAttr:$rootReference, Array:$nestedReferences )>; def TypeAttr: DialectAttribute<(attr Type:$value )>; def UnitAttr: DialectAttribute<(attr)>; def IntegerAttr: DialectAttribute<(attr Type:$type, KnownWidthAPInt<"type">:$value )> { let cBuilder = "getChecked<$_resultType>([&]() { return reader.emitError(); }, context, type, *value)"; } defvar FloatType = Type; def FloatAttr : DialectAttribute<(attr FloatType:$type, KnownSemanticsAPFloat<"type">:$value )> { let cBuilder = "getChecked<$_resultType>([&]() { return reader.emitError(); }, context, type, *value)"; } def CallSiteLoc : DialectAttribute<(attr LocationAttr:$callee, LocationAttr:$caller )>; let cType = "FileLineColRange" in { def FileLineColRange : DialectAttribute<(attr StringAttr:$filename, WithBuilder<"$_args", WithType<"SmallVector", WithParser<"succeeded(readFileLineColRangeLocs($_reader, $_var))", WithPrinter<"writeFileLineColRangeLocs($_writer, $_name)">>>>:$rawLocData )> { let cBuilder = "getFileLineColRange(context, filename, rawLocData)"; let printerPredicate = "!isStrictFileLineColLoc($_val)"; } def FileLineColLoc : DialectAttribute<(attr StringAttr:$filename, VarInt:$start_line, VarInt:$start_column )> { let printerPredicate = "isStrictFileLineColLoc($_val)"; } } let cType = "FusedLoc" in { def FusedLoc : DialectAttribute<(attr Array:$locations )> { let printerPredicate = "!$_val.getMetadata()"; // FusedLoc::get(context, locs) may return UnknownLoc when locs is empty. // Pass Attribute() explicitly to bypass the folding logic and always // construct a FusedLoc via Base::getChecked. let cBuilder = "cast(getChecked([&]() { return reader.emitError(); }, context, $_args, Attribute()))"; } def FusedLocWithMetadata : DialectAttribute<(attr Array:$locations, Attribute:$metadata )> { let printerPredicate = "$_val.getMetadata()"; let cBuilder = "cast(getChecked([&]() { return reader.emitError(); }, context, $_args))"; } } def NameLoc : DialectAttribute<(attr StringAttr:$name, LocationAttr:$childLoc )>; def UnknownLoc : DialectAttribute<(attr)>; def DenseResourceElementsAttr : DialectAttribute<(attr ShapedType:$type, ResourceHandle<"DenseResourceElementsHandle">:$rawHandle )> { // Note: order of serialization does not match order of builder. let cBuilder = "getChecked<$_resultType>([&]() { return reader.emitError(); }, context, type, *rawHandle)"; } let cType = "RankedTensorType" in { def RankedTensorType : DialectType<(type Array:$shape, Type:$elementType )> { let printerPredicate = "!$_val.getEncoding()"; } def RankedTensorTypeWithEncoding : DialectType<(type Attribute:$encoding, Array:$shape, Type:$elementType )> { let printerPredicate = "$_val.getEncoding()"; // Note: order of serialization does not match order of builder. let cBuilder = "getChecked<$_resultType>([&]() { return reader.emitError(); }, context, shape, elementType, encoding)"; } } def DenseArrayAttr : DialectAttribute<(attr Type:$elementType, VarInt:$size, Blob:$rawData )>; def DenseElementsAttr : WithType<"DenseElementsAttr", Attribute>; def DenseTypedElementsAttr : DialectAttribute<(attr ShapedType:$type, WithBuilder<"$_args", WithType<"SmallVector", WithParser<"succeeded(readDenseTypedElementsAttr($_reader, type, $_var))", WithPrinter<"writeDenseTypedElementsAttr($_writer, $_name)">>>>:$rawData )>; def DenseStringElementsAttr : DialectAttribute<(attr ShapedType:$type, WithGetter<"$_attrType.isSplat()", VarInt>:$_isSplat, WithBuilder<"$_args", WithType<"SmallVector", WithParser <"succeeded(readPotentiallySplatString($_reader, type, _isSplat, $_var))", WithPrinter<"writePotentiallySplatString($_writer, $_name)">>>>:$rawStringData )>; def DenseIntElementsAttr : WithType<"DenseIntElementsAttr", Attribute>; def SparseElementsAttr : DialectAttribute<(attr ShapedType:$type, DenseIntElementsAttr:$indices, DenseElementsAttr:$values )>; def DistinctAttr : DialectAttribute<(attr Attribute:$referencedAttr )>; // Make easy to disable until version number gets added. class EnableAffineMapPrintingJuly2026 : DialectAttribute; def AffineMapAttr : EnableAffineMapPrintingJuly2026<(attr WithParser<"succeeded(readAffineMap($_reader, context, $_var))", WithPrinter<"writeAffineMap($_writer, $_name)", WithType<"AffineMap">>>:$value )>; // Types // ----- def IntegerType : DialectType<(type // Yes not pretty, WithParser<"succeeded($_reader.readVarInt($_var))", WithBuilder<"$_args", WithPrinter<"$_writer.writeVarInt($_name.getWidth() << 2 | $_name.getSignedness())", WithType <"uint64_t">>>>:$_widthAndSignedness, // Split up parsed varint for create method. LocalVar<"uint64_t", "_widthAndSignedness >> 2">:$width, LocalVar<"IntegerType::SignednessSemantics", "static_cast(_widthAndSignedness & 0x3)">:$signedness )>; def IndexType : DialectType<(type)>; def FunctionType : DialectType<(type Array:$inputs, Array:$results )>; def BFloat16Type : DialectType<(type)>; def Float16Type : DialectType<(type)>; // Make it easy to stage the addition of new floating point types so that // readers can be updated first. This is enabled by default, but can be flipped // to DialectTypeNoPrint if staging needed. // Note: this will be removed post next release. class EnableFloatPrintingJune2026 : DialectType; def FloatTF32Type : EnableFloatPrintingJune2026<(type)>; def Float32Type : DialectType<(type)>; def Float64Type : DialectType<(type)>; def Float80Type : DialectType<(type)>; def Float128Type : DialectType<(type)>; def Float8E5M2Type : EnableFloatPrintingJune2026<(type)>; def Float8E4M3Type : EnableFloatPrintingJune2026<(type)>; def Float8E4M3FNType : EnableFloatPrintingJune2026<(type)>; def Float8E5M2FNUZType : EnableFloatPrintingJune2026<(type)>; def Float8E4M3FNUZType : EnableFloatPrintingJune2026<(type)>; def Float8E4M3B11FNUZType : EnableFloatPrintingJune2026<(type)>; def Float8E3M4Type : EnableFloatPrintingJune2026<(type)>; def Float4E2M1FNType : EnableFloatPrintingJune2026<(type)>; def Float6E2M3FNType : EnableFloatPrintingJune2026<(type)>; def Float6E3M2FNType : EnableFloatPrintingJune2026<(type)>; def Float8E8M0FNUType : EnableFloatPrintingJune2026<(type)>; def ComplexType : DialectType<(type Type:$elementType )>; def MemRefLayout: WithType<"MemRefLayoutAttrInterface", Attribute>; let cType = "MemRefType" in { def MemRefType : DialectType<(type Array:$shape, Type:$elementType, MemRefLayout:$layout )> { let printerPredicate = "!$_val.getMemorySpace()"; } def MemRefTypeWithMemSpace : DialectType<(type Attribute:$memorySpace, Array:$shape, Type:$elementType, MemRefLayout:$layout )> { let printerPredicate = "!!$_val.getMemorySpace()"; // Note: order of serialization does not match order of builder. let cBuilder = "getChecked<$_resultType>([&]() { return reader.emitError(); }, context, shape, elementType, layout, memorySpace)"; } } def NoneType : DialectType<(type)>; def TupleType : DialectType<(type Array:$types )>; let cType = "UnrankedMemRefType" in { def UnrankedMemRefType : DialectType<(type Type:$elementType )> { let printerPredicate = "!$_val.getMemorySpace()"; let cBuilder = "getChecked<$_resultType>([&]() { return reader.emitError(); }, context, elementType, Attribute())"; } def UnrankedMemRefTypeWithMemSpace : DialectType<(type Attribute:$memorySpace, Type:$elementType )> { let printerPredicate = "$_val.getMemorySpace()"; // Note: order of serialization does not match order of builder. let cBuilder = "getChecked<$_resultType>([&]() { return reader.emitError(); }, context, elementType, memorySpace)"; } } def UnrankedTensorType : DialectType<(type Type:$elementType )>; def TokenType : DialectType<(type)>; let cType = "VectorType" in { def VectorType : DialectType<(type Array:$shape, Type:$elementType )> { let printerPredicate = "!$_val.isScalable()"; // Use getChecked to produce a null type (and emit a diagnostic) instead of // asserting when the element type does not implement VectorElementTypeInterface. let cBuilder = "VectorType::getChecked([&]() { return reader.emitError(\"invalid vector type\"); }, shape, elementType)"; } def VectorTypeWithScalableDims : DialectType<(type Array:$scalableDims, Array:$shape, Type:$elementType )> { let printerPredicate = "$_val.isScalable()"; // Note: order of serialization does not match order of builder. let cBuilder = "getChecked<$_resultType>([&]() { return reader.emitError(); }, context, shape, elementType, scalableDims)"; } } /// This enum contains marker codes used to indicate which attribute is /// currently being decoded, and how it should be decoded. The order of these /// codes should generally be unchanged, as any changes will inevitably break /// compatibility with older bytecode. def BuiltinDialectAttributes : DialectAttributes<"Builtin"> { let elems = [ ArrayAttr, DictionaryAttr, StringAttr, StringAttrWithType, FlatSymbolRefAttr, SymbolRefAttr, TypeAttr, UnitAttr, IntegerAttr, FloatAttr, CallSiteLoc, FileLineColLoc, FusedLoc, FusedLocWithMetadata, NameLoc, UnknownLoc, DenseResourceElementsAttr, DenseArrayAttr, DenseTypedElementsAttr, DenseStringElementsAttr, SparseElementsAttr, DistinctAttr, FileLineColRange, AffineMapAttr, ]; } def BuiltinDialectTypes : DialectTypes<"Builtin"> { let elems = [ IntegerType, IndexType, FunctionType, BFloat16Type, Float16Type, Float32Type, Float64Type, Float80Type, Float128Type, ComplexType, MemRefType, MemRefTypeWithMemSpace, NoneType, RankedTensorType, RankedTensorTypeWithEncoding, TupleType, UnrankedMemRefType, UnrankedMemRefTypeWithMemSpace, UnrankedTensorType, VectorType, VectorTypeWithScalableDims, FloatTF32Type, Float8E5M2Type, Float8E4M3Type, Float8E4M3FNType, Float8E5M2FNUZType, Float8E4M3FNUZType, Float8E4M3B11FNUZType, Float8E3M4Type, Float4E2M1FNType, Float6E2M3FNType, Float6E3M2FNType, Float8E8M0FNUType, TokenType ]; } #endif // BUILTIN_BYTECODE