Skip to content

Commit

Permalink
[mlir][Complex] Add convenience builder for complex.number attribute.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D130756
  • Loading branch information
akuegel committed Jul 29, 2022
1 parent a0f1304 commit 6e951b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 17 additions & 0 deletions mlir/include/mlir/Dialect/Complex/IR/ComplexAttributes.td
Expand Up @@ -37,8 +37,25 @@ def Complex_NumberAttr : Complex_Attr<"Number", "number"> {
let parameters = (ins APFloatParameter<"">:$real,
APFloatParameter<"">:$imag,
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 hasCustomAssemblyFormat = 1;
let skipDefaultBuilders = 1;
}

#endif // COMPLEX_ATTRIBUTE
10 changes: 1 addition & 9 deletions mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
Expand Up @@ -81,13 +81,5 @@ Attribute complex::NumberAttr::parse(AsmParser &parser, Type odsType) {
parser.parseFloat(imag) || parser.parseGreater())
return {};

bool unused = false;
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));
return NumberAttr::get(ComplexType::get(type), real, imag);
}

0 comments on commit 6e951b3

Please sign in to comment.