Skip to content

Commit b0366ee

Browse files
authored
[MLIR] Add support for int8/uint8 properties (#145019)
This patch is adding the ability to print a uint8_t/int8_t as an int instead of a char and demonstrate support for int8_t/uin8_t properties. Fix #144993
1 parent a50cb6c commit b0366ee

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed

mlir/include/mlir/IR/ODSSupport.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ convertFromAttribute(int32_t &storage, Attribute attr,
4343
/// Convert the provided int32_t to an IntegerAttr attribute.
4444
Attribute convertToAttribute(MLIRContext *ctx, int32_t storage);
4545

46+
/// Convert an IntegerAttr attribute to an int8_t, or return an error if the
47+
/// attribute isn't an IntegerAttr. If the optional diagnostic is provided an
48+
/// error message is also emitted.
49+
LogicalResult
50+
convertFromAttribute(int8_t &storage, Attribute attr,
51+
function_ref<InFlightDiagnostic()> emitError);
52+
53+
/// Convert the provided int8_t to an IntegerAttr attribute.
54+
Attribute convertToAttribute(MLIRContext *ctx, int8_t storage);
55+
56+
/// Convert an IntegerAttr attribute to an uint8_t, or return an error if the
57+
/// attribute isn't an IntegerAttr. If the optional diagnostic is provided an
58+
/// error message is also emitted.
59+
LogicalResult
60+
convertFromAttribute(uint8_t &storage, Attribute attr,
61+
function_ref<InFlightDiagnostic()> emitError);
62+
63+
/// Convert the provided uint8_t to an IntegerAttr attribute.
64+
Attribute convertToAttribute(MLIRContext *ctx, uint8_t storage);
65+
4666
/// Extract the string from `attr` into `storage`. If `attr` is not a
4767
/// `StringAttr`, return failure and emit an error into the diagnostic from
4868
/// `emitError`.

mlir/include/mlir/IR/OpImplementation.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,19 @@ class AsmPrinter {
135135
/// hook on the AsmParser.
136136
virtual void printFloat(const APFloat &value);
137137

138+
/// Print the given integer value. This is useful to force a uint8_t/int8_t to
139+
/// be printed as an integer instead of a char.
140+
template <typename IntT>
141+
std::enable_if_t<std::is_integral_v<IntT>, void> printInteger(IntT value) {
142+
// Handle int8_t/uint8_t specially to avoid printing as char
143+
if constexpr (std::is_same_v<IntT, int8_t> ||
144+
std::is_same_v<IntT, uint8_t>) {
145+
getStream() << static_cast<int>(value);
146+
} else {
147+
getStream() << value;
148+
}
149+
}
150+
138151
virtual void printType(Type type);
139152
virtual void printAttribute(Attribute attr);
140153

mlir/include/mlir/IR/Properties.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ class IntProp<string storageTypeParam, string desc = ""> :
219219
let optionalParser = [{
220220
return $_parser.parseOptionalInteger($_storage);
221221
}];
222+
let printer = "$_printer.printInteger($_storage)";
222223
let writeToMlirBytecode = [{
223224
$_writer.writeVarInt($_storage);
224225
}];

mlir/lib/IR/ODSSupport.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,40 @@ Attribute mlir::convertToAttribute(MLIRContext *ctx, int32_t storage) {
4848
return IntegerAttr::get(IntegerType::get(ctx, 32), storage);
4949
}
5050

51+
LogicalResult
52+
mlir::convertFromAttribute(int8_t &storage, Attribute attr,
53+
function_ref<InFlightDiagnostic()> emitError) {
54+
auto valueAttr = dyn_cast<IntegerAttr>(attr);
55+
if (!valueAttr) {
56+
emitError() << "expected IntegerAttr for key `value`";
57+
return failure();
58+
}
59+
storage = valueAttr.getValue().getSExtValue();
60+
return success();
61+
}
62+
63+
Attribute mlir::convertToAttribute(MLIRContext *ctx, int8_t storage) {
64+
/// Convert the provided int8_t to an IntegerAttr attribute.
65+
return IntegerAttr::get(IntegerType::get(ctx, 8), storage);
66+
}
67+
68+
LogicalResult
69+
mlir::convertFromAttribute(uint8_t &storage, Attribute attr,
70+
function_ref<InFlightDiagnostic()> emitError) {
71+
auto valueAttr = dyn_cast<IntegerAttr>(attr);
72+
if (!valueAttr) {
73+
emitError() << "expected IntegerAttr for key `value`";
74+
return failure();
75+
}
76+
storage = valueAttr.getValue().getZExtValue();
77+
return success();
78+
}
79+
80+
Attribute mlir::convertToAttribute(MLIRContext *ctx, uint8_t storage) {
81+
/// Convert the provided uint8_t to an IntegerAttr attribute.
82+
return IntegerAttr::get(IntegerType::get(ctx, 8), storage);
83+
}
84+
5185
LogicalResult
5286
mlir::convertFromAttribute(std::string &storage, Attribute attr,
5387
function_ref<InFlightDiagnostic()> emitError) {

mlir/test/IR/properties.mlir

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,15 @@ test.with_default_valued_properties 1 "foo" 0 unit
5959
// CHECK: test.with_optional_properties
6060
// CHECK-SAME: simple = 0
6161
// GENERIC: "test.with_optional_properties"()
62-
// GENERIC-SAME: <{hasDefault = [], hasUnit = false, longSyntax = [], maybeUnit = [], nested = [], nonTrivialStorage = [], simple = [0]}> : () -> ()
62+
// GENERIC-SAME: <{hasDefault = [], hasUnit = false, longSyntax = [], maybeUnit = [], nested = [], nonTrivialStorage = [], simple = [0], simplei8 = [], simpleui8 = []}> : () -> ()
6363
test.with_optional_properties simple = 0
6464

65+
// CHECK: test.with_optional_properties
66+
// CHECK-SAME: simple = 1 simplei8 = -1 simpleui8 = 255
67+
// GENERIC: "test.with_optional_properties"()
68+
// GENERIC-SAME: <{hasDefault = [], hasUnit = false, longSyntax = [], maybeUnit = [], nested = [], nonTrivialStorage = [], simple = [1], simplei8 = [-1 : i8], simpleui8 = [-1 : i8]}> : () -> ()
69+
test.with_optional_properties simple = 1 simplei8 = -1 simpleui8 = 255
70+
6571
// CHECK: test.with_optional_properties{{$}}
6672
// GENERIC: "test.with_optional_properties"()
6773
// GENERIC-SAME: simple = []
@@ -70,7 +76,7 @@ test.with_optional_properties
7076
// CHECK: test.with_optional_properties
7177
// CHECK-SAME: anAttr = 0 simple = 1 nonTrivialStorage = "foo" hasDefault = some<0> nested = some<1> longSyntax = some<"bar"> hasUnit maybeUnit = some<unit>
7278
// GENERIC: "test.with_optional_properties"()
73-
// GENERIC-SAME: <{anAttr = 0 : i32, hasDefault = [0], hasUnit, longSyntax = ["bar"], maybeUnit = [unit], nested = {{\[}}[1]], nonTrivialStorage = ["foo"], simple = [1]}> : () -> ()
79+
// GENERIC-SAME: <{anAttr = 0 : i32, hasDefault = [0], hasUnit, longSyntax = ["bar"], maybeUnit = [unit], nested = {{\[}}[1]], nonTrivialStorage = ["foo"], simple = [1], simplei8 = [], simpleui8 = []}> : () -> ()
7480
test.with_optional_properties
7581
anAttr = 0
7682
simple = 1

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3406,6 +3406,8 @@ def TestOpWithOptionalProperties : TEST_Op<"with_optional_properties"> {
34063406
let assemblyFormat = [{
34073407
(`anAttr` `=` $anAttr^)?
34083408
(`simple` `=` $simple^)?
3409+
(`simplei8` `=` $simplei8^)?
3410+
(`simpleui8` `=` $simpleui8^)?
34093411
(`nonTrivialStorage` `=` $nonTrivialStorage^)?
34103412
(`hasDefault` `=` $hasDefault^)?
34113413
(`nested` `=` $nested^)?
@@ -3417,6 +3419,8 @@ def TestOpWithOptionalProperties : TEST_Op<"with_optional_properties"> {
34173419
let arguments = (ins
34183420
OptionalAttr<I32Attr>:$anAttr,
34193421
OptionalProp<I64Prop>:$simple,
3422+
OptionalProp<IntProp<"int8_t">>:$simplei8,
3423+
OptionalProp<IntProp<"uint8_t">>:$simpleui8,
34203424
OptionalProp<StringProp>:$nonTrivialStorage,
34213425
// Confirm that properties with default values now default to nullopt and have
34223426
// the long syntax.

0 commit comments

Comments
 (0)