diff --git a/mlir/include/mlir/Dialect/Complex/IR/ComplexAttributes.td b/mlir/include/mlir/Dialect/Complex/IR/ComplexAttributes.td --- a/mlir/include/mlir/Dialect/Complex/IR/ComplexAttributes.td +++ b/mlir/include/mlir/Dialect/Complex/IR/ComplexAttributes.td @@ -37,8 +37,23 @@ 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 diff --git a/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp b/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp --- a/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp +++ b/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp @@ -81,13 +81,5 @@ 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); }