11#include "llvm/ADT/TypeSwitch.h"
12#include "mlir/IR/Builders.h"
13#include "mlir/IR/DialectImplementation.h"
25LogicalResult BitVectorAttr::verify(
26 function_ref<InFlightDiagnostic()> emitError,
29 if (value.getBitWidth() < 1) {
30 return emitError() <<
"bit-width must be at least 1, but got " << value.getBitWidth();
35std::string BitVectorAttr::getValueAsString(
bool prefix)
const {
36 unsigned width = getValue().getBitWidth();
37 SmallVector<char> toPrint;
38 StringRef pref = prefix ?
"#" :
"";
40 getValue().toString(toPrint, 16,
false,
false,
false);
43 SmallVector<char> leadingZeros(width / 4 - toPrint.size(),
'0');
44 return (pref +
"x" + Twine(leadingZeros) + toPrint).str();
47 getValue().toString(toPrint, 2,
false,
false,
false);
49 SmallVector<char> leadingZeros(width - toPrint.size(),
'0');
50 return (pref +
"b" + Twine(leadingZeros) + toPrint).str();
54static FailureOr<APInt>
55parseBitVectorString(function_ref<InFlightDiagnostic()> emitError, StringRef value) {
56 if (value[0] !=
'#') {
57 return emitError() <<
"expected '#'";
60 if (value.size() < 3) {
61 return emitError() <<
"expected at least one digit";
64 if (value[1] ==
'b') {
65 return APInt(value.size() - 2, std::string(value.begin() + 2, value.end()), 2);
68 if (value[1] ==
'x') {
69 return APInt((value.size() - 2) * 4, std::string(value.begin() + 2, value.end()), 16);
72 return emitError() <<
"expected either 'b' or 'x'";
75BitVectorAttr BitVectorAttr::get(MLIRContext *context, StringRef value) {
76 auto maybeValue = parseBitVectorString(
nullptr, value);
78 assert(succeeded(maybeValue) &&
"string must have SMT-LIB format");
79 return Base::get(context, *maybeValue);
82BitVectorAttr BitVectorAttr::getChecked(
83 function_ref<InFlightDiagnostic()> emitError, MLIRContext *context, StringRef value
85 auto maybeValue = parseBitVectorString(emitError, value);
86 if (failed(maybeValue)) {
90 return Base::getChecked(emitError, context, *maybeValue);
93BitVectorAttr BitVectorAttr::get(MLIRContext *context, uint64_t value,
unsigned width) {
94 return Base::get(context, APInt(width, value));
97BitVectorAttr BitVectorAttr::getChecked(
98 function_ref<InFlightDiagnostic()> emitError, MLIRContext *context, uint64_t value,
101 if (width < 64 && value >= (UINT64_C(1) << width)) {
102 emitError() <<
"value does not fit in a bit-vector of desired width";
105 return Base::getChecked(emitError, context, APInt(width, value));
108Attribute BitVectorAttr::parse(AsmParser &odsParser, Type odsType) {
109 llvm::SMLoc loc = odsParser.getCurrentLocation();
112 if (odsParser.parseLess() || odsParser.parseInteger(val) || odsParser.parseGreater()) {
117 if (!odsType || !llvm::isa<BitVectorType>(odsType)) {
118 odsParser.emitError(loc) <<
"explicit bit-vector type required";
122 unsigned width = llvm::cast<BitVectorType>(odsType).getWidth();
124 if (width > val.getBitWidth()) {
128 val = val.sext(width);
129 }
else if (width < val.getBitWidth()) {
132 unsigned neededBits = val.isNegative() ? val.getSignificantBits() : val.getActiveBits();
133 if (width < neededBits) {
134 odsParser.emitError(loc) <<
"integer value out of range for given bit-vector type "
138 val = val.trunc(width);
141 return BitVectorAttr::get(odsParser.getContext(), val);
144void BitVectorAttr::print(AsmPrinter &odsPrinter)
const {
148 odsPrinter <<
"<" << getValue() <<
">";
151Type BitVectorAttr::getType()
const {
159#define GET_ATTRDEF_CLASSES
164#define GET_ATTRDEF_LIST
static BitVectorType get(::mlir::MLIRContext *context, int64_t width)
void registerAttributes()