Skip to content

Commit 6e951b3

Browse files
committed
[mlir][Complex] Add convenience builder for complex.number attribute.
Differential Revision: https://reviews.llvm.org/D130756
1 parent a0f1304 commit 6e951b3

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

mlir/include/mlir/Dialect/Complex/IR/ComplexAttributes.td

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,25 @@ def Complex_NumberAttr : Complex_Attr<"Number", "number"> {
3737
let parameters = (ins APFloatParameter<"">:$real,
3838
APFloatParameter<"">:$imag,
3939
AttributeSelfTypeParameter<"">:$type);
40+
let builders = [
41+
AttrBuilderWithInferredContext<(ins "mlir::ComplexType":$type,
42+
"double":$real,
43+
"double":$imag), [{
44+
auto elementType = type.getElementType().cast<FloatType>();
45+
APFloat realFloat(real);
46+
bool unused;
47+
realFloat.convert(elementType.getFloatSemantics(),
48+
APFloat::rmNearestTiesToEven, &unused);
49+
APFloat imagFloat(imag);
50+
imagFloat.convert(elementType.getFloatSemantics(),
51+
APFloat::rmNearestTiesToEven, &unused);
52+
return $_get(type.getContext(), realFloat, imagFloat, type);
53+
}]>
54+
];
55+
4056
let genVerifyDecl = 1;
4157
let hasCustomAssemblyFormat = 1;
58+
let skipDefaultBuilders = 1;
4259
}
4360

4461
#endif // COMPLEX_ATTRIBUTE

mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,5 @@ Attribute complex::NumberAttr::parse(AsmParser &parser, Type odsType) {
8181
parser.parseFloat(imag) || parser.parseGreater())
8282
return {};
8383

84-
bool unused = false;
85-
APFloat realFloat(real);
86-
realFloat.convert(type.cast<FloatType>().getFloatSemantics(),
87-
APFloat::rmNearestTiesToEven, &unused);
88-
APFloat imagFloat(imag);
89-
imagFloat.convert(type.cast<FloatType>().getFloatSemantics(),
90-
APFloat::rmNearestTiesToEven, &unused);
91-
return NumberAttr::get(parser.getContext(), realFloat, imagFloat,
92-
ComplexType::get(type));
84+
return NumberAttr::get(ComplexType::get(type), real, imag);
9385
}

0 commit comments

Comments
 (0)