15 #include "llvm/ADT/APFloat.h" 16 #include "llvm/ADT/StringSwitch.h" 17 #include "llvm/Support/Format.h" 18 #include "llvm/Support/MathExtras.h" 19 #include "llvm/Support/SourceMgr.h" 20 #include "llvm/Support/raw_ostream.h" 23 using namespace quant;
31 unsigned storageTypeWidth = 0;
36 isSigned = !type.isUnsigned();
37 storageTypeWidth = type.getWidth();
40 if (!identifier.consume_front(
"u")) {
41 parser.
emitError(typeLoc,
"illegal storage type prefix");
44 if (identifier.getAsInteger(10, storageTypeWidth)) {
45 parser.
emitError(typeLoc,
"expected storage type width");
54 if (storageTypeWidth == 0 ||
56 parser.
emitError(typeLoc,
"illegal storage type size: ")
65 IntegerType storageType,
bool isSigned,
66 int64_t &storageTypeMin,
67 int64_t &storageTypeMax) {
69 isSigned, storageType.getWidth());
71 isSigned, storageType.getWidth());
73 storageTypeMin = defaultIntegerMin;
74 storageTypeMax = defaultIntegerMax;
84 if (storageTypeMin < defaultIntegerMin) {
85 return parser.
emitError(minLoc,
"illegal storage type minimum: ")
88 if (storageTypeMax > defaultIntegerMax) {
89 return parser.
emitError(maxLoc,
"illegal storage type maximum: ")
96 double &
min,
double &
max) {
101 parser.
emitError(typeLoc,
"expecting float expressed type");
108 parser.
emitError(typeLoc,
"calibrated values must be present");
122 IntegerType storageType;
124 unsigned typeFlags = 0;
125 int64_t storageTypeMin;
126 int64_t storageTypeMax;
133 bool isSigned =
false;
160 typeFlags, storageType, expressedType, storageTypeMin, storageTypeMax);
164 int64_t &zeroPoint) {
196 IntegerType storageType;
198 unsigned typeFlags = 0;
199 int64_t storageTypeMin;
200 int64_t storageTypeMax;
201 bool isPerAxis =
false;
202 int32_t quantizedDimension;
212 bool isSigned =
false;
255 scales.resize(scales.size() + 1);
256 zeroPoints.resize(zeroPoints.size() + 1);
272 if (!isPerAxis && scales.size() > 1) {
274 "multiple scales/zeroPoints provided, but " 275 "quantizedDimension wasn't specified"),
283 typeFlags, storageType, expressedType, scalesRef, zeroPointsRef,
284 quantizedDimension, storageTypeMin, storageTypeMax);
288 typeFlags, storageType, expressedType, scales.front(), zeroPoints.front(),
289 storageTypeMin, storageTypeMax);
309 if (!expressedType) {
323 StringRef typeNameSpelling;
327 if (typeNameSpelling ==
"uniform")
329 if (typeNameSpelling ==
"any")
331 if (typeNameSpelling ==
"calibrated")
335 "unknown quantized type " + typeNameSpelling);
344 out <<
"i" << storageWidth;
346 out <<
"u" << storageWidth;
350 int64_t defaultIntegerMin =
352 int64_t defaultIntegerMax =
364 if (zeroPoint != 0) {
365 out <<
":" << zeroPoint;
374 if (
Type expressedType = type.getExpressedType()) {
375 out <<
":" << expressedType;
385 out <<
":" << type.getExpressedType() <<
", ";
397 out <<
":" << type.getExpressedType() <<
":";
406 llvm::seq<size_t>(0, scales.size()), out,
417 out <<
"calibrated<" << type.getExpressedType();
418 out <<
"<" << type.
getMin() <<
":" << type.
getMax() <<
">";
433 llvm_unreachable(
"Unhandled quantized type");
Include the generated interface declarations.
int64_t getStorageTypeMin() const
The minimum value that storageType can take.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
static void printUniformQuantizedPerAxisType(UniformQuantizedPerAxisType type, DialectAsmPrinter &out)
Helper that prints a UniformQuantizedPerAxisType.
static FloatType parseExpressedTypeAndRange(DialectAsmParser &parser, double &min, double &max)
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
static int64_t getDefaultMinimumForInteger(bool isSigned, unsigned integralWidth)
Gets the minimum possible stored by a storageType.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseLBrace()=0
Parse a { token.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value...
A quantized type that infers its range from given min/max values.
static void printCalibratedQuantizedType(CalibratedQuantizedType type, DialectAsmPrinter &out)
Helper that prints a CalibratedQuantizedType.
void printType(Type type, AsmPrinter &printer)
Prints an LLVM Dialect type.
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value...
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseComma()=0
Parse a , token.
static Type parseCalibratedType(DialectAsmParser &parser)
Parses an CalibratedQuantizedType.
virtual ParseResult parseFloat(double &result)=0
Parse a floating point value from the stream.
static IntegerType parseStorageType(DialectAsmParser &parser, bool &isSigned)
virtual ParseResult parseOptionalComma()=0
Parse a , token if present.
static void printStorageType(QuantizedType type, DialectAsmPrinter &out)
virtual ParseResult parseColon()=0
Parse a : token.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
static void printQuantParams(double scale, int64_t zeroPoint, DialectAsmPrinter &out)
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseOptionalLess()=0
Parse a '<' token if present.
unsigned getStorageTypeIntegralWidth() const
Gets the integral bit width that the underlying storage type can exactly represent.
virtual ParseResult parseGreater()=0
Parse a '>' token.
A quantized type that maps storage to/from expressed types in an unspecified way. ...
static Type parseAnyType(DialectAsmParser &parser)
Parses an AnyQuantizedType.
static ParseResult parseStorageRange(DialectAsmParser &parser, IntegerType storageType, bool isSigned, int64_t &storageTypeMin, int64_t &storageTypeMax)
IntegerType getIntegerType(unsigned width)
virtual ParseResult parseLess()=0
Parse a '<' token.
int64_t getStorageTypeMax() const
The maximum value that storageType can take.
bool isSigned() const
Whether the storage type should be interpreted as a signed quantity (true) or an unsigned value (fals...
Type parseType(llvm::StringRef typeStr, MLIRContext *context)
This parses a single MLIR type to an MLIR context if it was valid.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
virtual ParseResult parseRBrace()=0
Parse a } token.
static Type parseUniformType(DialectAsmParser &parser)
Parses a UniformQuantizedType.
virtual ParseResult parseOptionalColon()=0
Parse a : token if present.
static void printAnyQuantizedType(AnyQuantizedType type, DialectAsmPrinter &out)
Helper that prints a AnyQuantizedType.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
This class implements Optional functionality for ParseResult.
static void printUniformQuantizedType(UniformQuantizedType type, DialectAsmPrinter &out)
Helper that prints a UniformQuantizedType.
static constexpr unsigned MaxStorageBits
The maximum number of bits supported for storage types.
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
Base class for all quantized types known to this dialect.
virtual ParseResult parseType(Type &result)=0
Parse a type.
bool has_value() const
Returns true if we contain a valid ParseResult value.
This class represents success/failure for parsing-like operations that find it important to chain tog...
static ParseResult parseQuantParams(DialectAsmParser &parser, double &scale, int64_t &zeroPoint)
virtual OptionalParseResult parseOptionalType(Type &result)=0
Parse an optional type.
auto getChecked(SMLoc loc, ParamsT &&...params)
Invoke the getChecked method of the given Attribute or Type class, using the provided location to emi...
The DialectAsmParser has methods for interacting with the asm parser when parsing attributes and type...
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static int64_t getDefaultMaximumForInteger(bool isSigned, unsigned integralWidth)
Gets the maximum possible stored by a storageType.