[mlir][Complex] Add convenience builder for complex.number attribute.

Differential Revision: https://reviews.llvm.org/D130756
This commit is contained in:
Adrian Kuegel 2022-07-29 13:30:29 +02:00
parent a0f1304616
commit 6e951b3ec9
2 changed files with 18 additions and 9 deletions

View File

@ -37,8 +37,25 @@ def Complex_NumberAttr : Complex_Attr<"Number", "number"> {
let parameters = (ins APFloatParameter<"">:$real, let parameters = (ins APFloatParameter<"">:$real,
APFloatParameter<"">:$imag, APFloatParameter<"">:$imag,
AttributeSelfTypeParameter<"">:$type); AttributeSelfTypeParameter<"">:$type);
let builders = [
AttrBuilderWithInferredContext<(ins "mlir::ComplexType":$type,
"double":$real,
"double":$imag), [{
auto elementType = type.getElementType().cast<FloatType>();
APFloat realFloat(real);
bool unused;
realFloat.convert(elementType.getFloatSemantics(),
APFloat::rmNearestTiesToEven, &unused);
APFloat imagFloat(imag);
imagFloat.convert(elementType.getFloatSemantics(),
APFloat::rmNearestTiesToEven, &unused);
return $_get(type.getContext(), realFloat, imagFloat, type);
}]>
];
let genVerifyDecl = 1; let genVerifyDecl = 1;
let hasCustomAssemblyFormat = 1; let hasCustomAssemblyFormat = 1;
let skipDefaultBuilders = 1;
} }
#endif // COMPLEX_ATTRIBUTE #endif // COMPLEX_ATTRIBUTE

View File

@ -81,13 +81,5 @@ Attribute complex::NumberAttr::parse(AsmParser &parser, Type odsType) {
parser.parseFloat(imag) || parser.parseGreater()) parser.parseFloat(imag) || parser.parseGreater())
return {}; return {};
bool unused = false; return NumberAttr::get(ComplexType::get(type), real, imag);
APFloat realFloat(real);
realFloat.convert(type.cast<FloatType>().getFloatSemantics(),
APFloat::rmNearestTiesToEven, &unused);
APFloat imagFloat(imag);
imagFloat.convert(type.cast<FloatType>().getFloatSemantics(),
APFloat::rmNearestTiesToEven, &unused);
return NumberAttr::get(parser.getContext(), realFloat, imagFloat,
ComplexType::get(type));
} }