Avoid hardcoded 4096 constant
This commit creates a static constexpr limit for the IntegerType bitwidth and uses it. The check had to be moved because Token is not aware of IR/Type and it was a sign the abstraction leaked: bitwidth limit is not a property of the Token but of the IntegerType. Added a positive and a negative test at the limit. PiperOrigin-RevId: 210388192
This commit is contained in:
parent
6cc9786c3e
commit
a124e9c4a5
|
@ -134,7 +134,7 @@ inline raw_ostream &operator<<(raw_ostream &os, const Type &type) {
|
|||
return os;
|
||||
}
|
||||
|
||||
/// Integer types can have arbitrary bitwidth up to a large fixed limit of 4096.
|
||||
/// Integer types can have arbitrary bitwidth up to a large fixed limit.
|
||||
class IntegerType : public Type {
|
||||
public:
|
||||
static IntegerType *get(unsigned width, MLIRContext *context);
|
||||
|
@ -148,6 +148,9 @@ public:
|
|||
static bool classof(const Type *type) {
|
||||
return type->getKind() == Kind::Integer;
|
||||
}
|
||||
|
||||
/// Integer representation maximal bitwidth.
|
||||
static constexpr unsigned kMaxWidth = 4096;
|
||||
private:
|
||||
unsigned width;
|
||||
IntegerType(unsigned width, MLIRContext *context);
|
||||
|
|
|
@ -23,6 +23,7 @@ using namespace mlir;
|
|||
|
||||
IntegerType::IntegerType(unsigned width, MLIRContext *context)
|
||||
: Type(Kind::Integer, context), width(width) {
|
||||
assert(width <= kMaxWidth && "admissible integer bitwidth exceeded");
|
||||
}
|
||||
|
||||
FloatType::FloatType(Kind kind, MLIRContext *context) : Type(kind, context) {}
|
||||
|
|
|
@ -316,6 +316,10 @@ Type *Parser::parseType() {
|
|||
auto width = getToken().getIntTypeBitwidth();
|
||||
if (!width.hasValue())
|
||||
return (emitError("invalid integer width"), nullptr);
|
||||
if (width > IntegerType::kMaxWidth)
|
||||
return (emitError("integer bitwidth is limited to " +
|
||||
Twine(IntegerType::kMaxWidth) + " bits"),
|
||||
nullptr);
|
||||
consumeToken(Token::inttype);
|
||||
return builder.getIntegerType(width.getValue());
|
||||
}
|
||||
|
|
|
@ -72,9 +72,7 @@ Optional<double> Token::getFloatingPointValue() const {
|
|||
Optional<unsigned> Token::getIntTypeBitwidth() const {
|
||||
unsigned result = 0;
|
||||
if (spelling[1] == '0' ||
|
||||
spelling.drop_front().getAsInteger(10, result) ||
|
||||
// Arbitrary but large limit on bitwidth.
|
||||
result > 4096 || result == 0)
|
||||
spelling.drop_front().getAsInteger(10, result) || result == 0)
|
||||
return None;
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -101,6 +101,15 @@ bb0:
|
|||
|
||||
// -----
|
||||
|
||||
cfgfunc @intlimit2() {
|
||||
bb:
|
||||
%0 = "constant"() {value: 0} : () -> i4096
|
||||
%1 = "constant"() {value: 1} : () -> i4097 // expected-error {{integer bitwidth is limited to 4096 bits}}
|
||||
return
|
||||
}
|
||||
|
||||
// -----
|
||||
|
||||
mlfunc @mlfunc_constant() {
|
||||
%x = "constant"(){value: "xyz"} : () -> i32 // expected-error {{'constant' op requires 'value' to be an integer for an integer result type}}
|
||||
return
|
||||
|
@ -112,4 +121,3 @@ mlfunc @calls(%arg0 : i32) {
|
|||
%x = call @calls() : () -> i32 // expected-error {{reference to function with mismatched type}}
|
||||
return
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue