20LogicalResult BitVectorAttr::verify(
23 if (value.getBitWidth() < 1)
24 return emitError() <<
"bit-width must be at least 1, but got "
25 << value.getBitWidth();
29std::string BitVectorAttr::getValueAsString(
bool prefix)
const {
30 unsigned width = getValue().getBitWidth();
32 StringRef pref = prefix ?
"#" :
"";
34 getValue().toString(toPrint, 16,
false,
false,
false);
38 return (pref +
"x" + Twine(leadingZeros) + toPrint).str();
41 getValue().toString(toPrint, 2,
false,
false,
false);
44 return (pref +
"b" + Twine(leadingZeros) + toPrint).str();
48static FailureOr<APInt>
55 return emitError() <<
"expected at least one digit";
58 return APInt(value.size() - 2, std::string(value.begin() + 2, value.end()),
62 return APInt((value.size() - 2) * 4,
63 std::string(value.begin() + 2, value.end()), 16);
65 return emitError() <<
"expected either 'b' or 'x'";
68BitVectorAttr BitVectorAttr::get(
MLIRContext *context, StringRef value) {
71 assert(succeeded(maybeValue) &&
"string must have SMT-LIB format");
72 return Base::get(context, *maybeValue);
82 return Base::getChecked(
emitError, context, *maybeValue);
85BitVectorAttr BitVectorAttr::get(
MLIRContext *context, uint64_t value,
87 return Base::get(context, APInt(width, value));
94 if (width < 64 && value >= (UINT64_C(1) << width)) {
95 emitError() <<
"value does not fit in a bit-vector of desired width";
98 return Base::getChecked(
emitError, context, APInt(width, value));
110 if (!odsType || !llvm::isa<BitVectorType>(odsType)) {
111 odsParser.
emitError(loc) <<
"explicit bit-vector type required";
115 unsigned width = llvm::cast<BitVectorType>(odsType).getWidth();
117 if (width > val.getBitWidth()) {
121 val = val.sext(width);
122 }
else if (width < val.getBitWidth()) {
125 unsigned neededBits =
126 val.isNegative() ? val.getSignificantBits() : val.getActiveBits();
127 if (width < neededBits) {
129 <<
"integer value out of range for given bit-vector type " << odsType;
132 val = val.trunc(width);
135 return BitVectorAttr::get(odsParser.
getContext(), val);
138void BitVectorAttr::print(
AsmPrinter &odsPrinter)
const {
142 odsPrinter <<
"<" << getValue() <<
">";
145Type BitVectorAttr::getType()
const {
153#define GET_ATTRDEF_CLASSES
154#include "mlir/Dialect/SMT/IR/SMTAttributes.cpp.inc"
156void SMTDialect::registerAttributes() {
158#define GET_ATTRDEF_LIST
159#include "mlir/Dialect/SMT/IR/SMTAttributes.cpp.inc"
static unsigned getBitWidth(Type type)
static FailureOr< APInt > parseBitVectorString(function_ref< InFlightDiagnostic()> emitError, StringRef value)
Parse an SMT-LIB formatted bit-vector string.
This base class exposes generic asm parser hooks, usable across the various derived parsers.
MLIRContext * getContext() const
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseGreater()=0
Parse a '>' token.
This base class exposes generic asm printer hooks, usable across the various derived printers.
Attributes are known-constant values of operations.
This class represents a diagnostic that is inflight and set to be reported.
MLIRContext is the top-level object for a collection of MLIR operations.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::function_ref< Fn > function_ref