Skip to content

Commit b1d49a3

Browse files
quartersdgjpienaar
authored andcommitted
Adding test attribute and test
1 parent ea4ad1e commit b1d49a3

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

mlir/lib/AsmParser/Parser.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,6 @@ OptionalParseResult Parser::parseOptionalInteger(APInt &result) {
308308
return success();
309309
}
310310

311-
namespace {
312-
bool isBinOrHexOrOctIndicator(char c) {
313-
return (llvm::toLower(c) == 'x' || llvm::toLower(c) == 'b' ||
314-
llvm::isDigit(c));
315-
}
316-
} // namespace
317-
318311
/// Parse an optional integer value only in decimal format from the stream.
319312
OptionalParseResult Parser::parseOptionalDecimalInteger(APInt &result) {
320313
Token curToken = getToken();
@@ -329,10 +322,11 @@ OptionalParseResult Parser::parseOptionalDecimalInteger(APInt &result) {
329322
}
330323

331324
StringRef spelling = curTok.getSpelling();
332-
// If the integer is in bin, hex, or oct format, return only the 0 and reset
333-
// the lex pointer.
325+
// If the integer is in hexadecimal return only the 0. The lexer has already
326+
// moved past the entire hexidecimal encoded integer so we reset the lex
327+
// pointer to just past the 0 we actualy want to consume.
334328
if (spelling[0] == '0' && spelling.size() > 1 &&
335-
isBinOrHexOrOctIndicator(spelling[1])) {
329+
llvm::toLower(spelling[1]) == 'x') {
336330
result = 0;
337331
state.lex.resetPointer(spelling.data() + 1);
338332
consumeToken();

mlir/test/lib/Dialect/Test/TestAttrDefs.td

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ def AttrWithTrait : Test_Attr<"AttrWithTrait", [TestAttrTrait]> {
8181
let mnemonic = "attr_with_trait";
8282
}
8383

84+
// An attribute of decimal formatted integer.
85+
def TestDecimalIntegerAttr : Test_Attr<"TestDecimalInteger"> {
86+
let mnemonic = "decimal_integer";
87+
88+
let parameters = (ins "int64_t":$value);
89+
90+
let hasCustomAssemblyFormat = 1;
91+
}
92+
8493
// Test support for ElementsAttrInterface.
8594
def TestI64ElementsAttr : Test_Attr<"TestI64Elements", [ElementsAttrInterface]> {
8695
let mnemonic = "i64_elements";

mlir/test/lib/Dialect/Test/TestAttributes.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
#include "TestAttributes.h"
1515
#include "TestDialect.h"
16+
#include "TestTypes.h"
1617
#include "mlir/IR/Builders.h"
1718
#include "mlir/IR/DialectImplementation.h"
1819
#include "mlir/IR/ExtensibleDialect.h"
20+
#include "mlir/IR/OpImplementation.h"
1921
#include "mlir/IR/Types.h"
2022
#include "llvm/ADT/APFloat.h"
2123
#include "llvm/ADT/Hashing.h"
@@ -63,6 +65,25 @@ void CompoundAAttr::print(AsmPrinter &printer) const {
6365
// CompoundAAttr
6466
//===----------------------------------------------------------------------===//
6567

68+
69+
Attribute TestDecimalIntegerAttr::parse(AsmParser &parser, Type type) {
70+
if (parser.parseLess()){
71+
return Attribute();
72+
}
73+
uint64_t intVal;
74+
if (failed(*parser.parseOptionalDecimalInteger(intVal))) {
75+
return Attribute();
76+
}
77+
if (parser.parseGreater()) {
78+
return Attribute();
79+
}
80+
return get(parser.getContext(), intVal);
81+
}
82+
83+
void TestDecimalIntegerAttr::print(AsmPrinter &printer) const {
84+
printer << "<" << getValue() << ">";
85+
}
86+
6687
Attribute TestI64ElementsAttr::parse(AsmParser &parser, Type type) {
6788
SmallVector<uint64_t> elements;
6889
if (parser.parseLess() || parser.parseLSquare())

mlir/test/mlir-tblgen/testdialect-attrdefs.mlir

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt %s | mlir-opt -verify-diagnostics | FileCheck %s
1+
// RUN: mlir-opt %s -split-input-file -verify-diagnostics | FileCheck %s
22

33
// CHECK-LABEL: func private @compoundA()
44
// CHECK-SAME: #test.cmpnd_a<1, !test.smpla, [5, 6]>
@@ -19,3 +19,16 @@ func.func private @qualifiedAttr() attributes {foo = #test.cmpnd_nested_outer_qu
1919
func.func private @overriddenAttr() attributes {
2020
foo = #test.override_builder<5>
2121
}
22+
23+
// CHECK-LABEL: @decimalInteger
24+
// CHECK-SAME: foo = #test.decimal_integer<5>
25+
func.func private @decimalInteger() attributes {
26+
foo = #test.decimal_integer<5>
27+
}
28+
29+
// -----
30+
31+
func.func private @hexdecimalInteger() attributes {
32+
// expected-error @below {{expected '>'}}
33+
foo = #test.decimal_integer<0x5>
34+
}

0 commit comments

Comments
 (0)