21OpFoldResult ConstantOp::fold(FoldAdaptor adaptor) {
return getValue(); }
23void ConstantOp::getAsmResultNames(
25 setNameFn(getResult(),
"cst");
29 if (
auto arrAttr = llvm::dyn_cast<ArrayAttr>(value)) {
30 auto complexTy = llvm::dyn_cast<ComplexType>(type);
31 if (!complexTy || arrAttr.size() != 2)
33 auto complexEltTy = complexTy.getElementType();
34 if (
auto fre = llvm::dyn_cast<FloatAttr>(arrAttr[0])) {
35 auto im = llvm::dyn_cast<FloatAttr>(arrAttr[1]);
36 return im && fre.getType() == complexEltTy &&
37 im.getType() == complexEltTy;
39 if (
auto ire = llvm::dyn_cast<IntegerAttr>(arrAttr[0])) {
40 auto im = llvm::dyn_cast<IntegerAttr>(arrAttr[1]);
41 return im && ire.getType() == complexEltTy &&
42 im.getType() == complexEltTy;
48LogicalResult ConstantOp::verify() {
50 if (arrayAttr.size() != 2) {
52 "requires 'value' to be a complex constant, represented as array of "
56 auto complexEltTy =
getType().getElementType();
57 if (!isa<FloatAttr, IntegerAttr>(arrayAttr[0]) ||
58 !isa<FloatAttr, IntegerAttr>(arrayAttr[1]))
60 "requires attribute's elements to be float or integer attributes");
61 auto re = llvm::dyn_cast<TypedAttr>(arrayAttr[0]);
62 auto im = llvm::dyn_cast<TypedAttr>(arrayAttr[1]);
63 if (complexEltTy != re.getType() || complexEltTy != im.getType()) {
65 <<
"requires attribute's element types (" << re.getType() <<
", "
67 <<
") to match the element type of the op's return type ("
68 << complexEltTy <<
")";
84LogicalResult BitcastOp::verify() {
85 auto operandType = getOperand().getType();
89 if (operandType == resultType)
92 if (!operandType.isIntOrFloat() && !isa<ComplexType>(operandType)) {
93 return emitOpError(
"operand must be int/float/complex");
96 if (!resultType.isIntOrFloat() && !isa<ComplexType>(resultType)) {
97 return emitOpError(
"result must be int/float/complex");
100 if (isa<ComplexType>(operandType) == isa<ComplexType>(resultType)) {
102 "requires that either input or output has a complex type");
105 if (isa<ComplexType>(resultType))
106 std::swap(operandType, resultType);
108 int32_t operandBitwidth = dyn_cast<ComplexType>(operandType)
110 .getIntOrFloatBitWidth() *
112 int32_t resultBitwidth = resultType.getIntOrFloatBitWidth();
114 if (operandBitwidth != resultBitwidth) {
115 return emitOpError(
"casting bitwidths do not match");
126 if (
auto defining = op.getOperand().getDefiningOp<BitcastOp>()) {
127 if (isa<ComplexType>(op.getType()) ||
128 isa<ComplexType>(defining.getOperand().getType())) {
131 defining.getOperand());
134 defining.getOperand());
139 if (
auto defining = op.getOperand().getDefiningOp<arith::BitcastOp>()) {
141 defining.getOperand());
154 if (
auto defining = op.getOperand().getDefiningOp<complex::BitcastOp>()) {
156 defining.getOperand());
175 if (
auto reOp = getOperand(0).getDefiningOp<ReOp>()) {
176 if (
auto imOp = getOperand(1).getDefiningOp<ImOp>()) {
177 if (reOp.getOperand() == imOp.getOperand()) {
178 return reOp.getOperand();
191 llvm::dyn_cast_if_present<ArrayAttr>(adaptor.getComplex());
192 if (arrayAttr && arrayAttr.size() == 2)
194 if (
auto createOp = getOperand().getDefiningOp<CreateOp>())
195 return createOp.getOperand(1);
200template <
typename OpKind,
int ComponentIndex>
202 using OpRewritePattern<OpKind>::OpRewritePattern;
204 LogicalResult matchAndRewrite(OpKind op,
205 PatternRewriter &rewriter)
const override {
206 auto negOp = op.getOperand().template getDefiningOp<NegOp>();
210 auto createOp = negOp.getComplex().template getDefiningOp<CreateOp>();
214 Type elementType = createOp.getType().getElementType();
215 assert(isa<FloatType>(elementType));
218 op, elementType, createOp.getOperand(ComponentIndex));
226 results.
add<FoldComponentNeg<ImOp, 1>>(context);
235 llvm::dyn_cast_if_present<ArrayAttr>(adaptor.getComplex());
236 if (arrayAttr && arrayAttr.size() == 2)
238 if (
auto createOp = getOperand().getDefiningOp<CreateOp>())
239 return createOp.getOperand(0);
245 results.
add<FoldComponentNeg<ReOp, 0>>(context);
257 if (arith::bitEnumContainsAll(getFastmath(), arith::FastMathFlags::reassoc |
258 arith::FastMathFlags::nsz)) {
259 if (
auto sub = getLhs().getDefiningOp<SubOp>())
260 if (getRhs() == sub.getRhs() &&
261 arith::bitEnumContainsAll(sub.getFastmath(),
262 arith::FastMathFlags::reassoc))
265 if (
auto sub = getRhs().getDefiningOp<SubOp>())
266 if (getLhs() == sub.getRhs() &&
267 arith::bitEnumContainsAll(sub.getFastmath(),
268 arith::FastMathFlags::reassoc))
273 if (
auto constantOp = getRhs().getDefiningOp<ConstantOp>()) {
274 auto arrayAttr = constantOp.getValue();
275 if (llvm::cast<FloatAttr>(arrayAttr[0]).getValue().isNegZero() &&
276 llvm::cast<FloatAttr>(arrayAttr[1]).getValue().isNegZero()) {
292 if (arith::bitEnumContainsAll(getFastmath(), arith::FastMathFlags::reassoc |
293 arith::FastMathFlags::nsz))
294 if (
auto add = getLhs().getDefiningOp<AddOp>())
295 if (getRhs() ==
add.getRhs() &&
296 arith::bitEnumContainsAll(
add.getFastmath(),
297 arith::FastMathFlags::reassoc))
301 if (
auto constantOp = getRhs().getDefiningOp<ConstantOp>()) {
302 auto arrayAttr = constantOp.getValue();
303 if (llvm::cast<FloatAttr>(arrayAttr[0]).getValue().isZero() &&
304 llvm::cast<FloatAttr>(arrayAttr[1]).getValue().isZero()) {
318 if (
auto negOp = getOperand().getDefiningOp<NegOp>())
319 return negOp.getOperand();
333 if (arith::bitEnumContainsAll(getFastmath(), arith::FastMathFlags::reassoc))
334 if (
auto logOp = getOperand().getDefiningOp<LogOp>())
335 if (arith::bitEnumContainsAll(logOp.getFastmath(),
336 arith::FastMathFlags::reassoc |
337 arith::FastMathFlags::nnan |
338 arith::FastMathFlags::ninf))
339 return logOp.getOperand();
350 if (
auto conjOp = getOperand().getDefiningOp<ConjOp>())
351 return conjOp.getOperand();
361 auto constant = getRhs().getDefiningOp<ConstantOp>();
365 ArrayAttr arrayAttr = constant.getValue();
366 APFloat
real = cast<FloatAttr>(arrayAttr[0]).getValue();
367 APFloat
imag = cast<FloatAttr>(arrayAttr[1]).getValue();
373 if (
real == APFloat(
real.getSemantics(), 1))
390 bool isLhsComplexHasNan =
false;
391 ArrayAttr lhsArrayAttr = dyn_cast_if_present<ArrayAttr>(lhs);
392 if (lhsArrayAttr && lhsArrayAttr.size() == 2) {
393 APFloat lhsReal = cast<FloatAttr>(lhsArrayAttr[0]).getValue();
394 APFloat lhsImag = cast<FloatAttr>(lhsArrayAttr[1]).getValue();
395 isLhsComplexHasNan = lhsReal.isNaN() || lhsImag.isNaN();
396 if (isLhsComplexHasNan) {
397 Attribute nanValue = lhsReal.isNaN() ? lhsArrayAttr[0] : lhsArrayAttr[1];
398 return ArrayAttr::get(
getContext(), {nanValue, nanValue});
402 ArrayAttr rhsArrayAttr = dyn_cast_if_present<ArrayAttr>(rhs);
403 if (!rhsArrayAttr || rhsArrayAttr.size() != 2)
407 APFloat rhsImag = cast<FloatAttr>(rhsArrayAttr[1]).getValue();
408 APFloat rhsReal = cast<FloatAttr>(rhsArrayAttr[0]).getValue();
409 if (!rhsImag.isZero() || rhsReal != APFloat(rhsReal.getSemantics(), 1))
414 if ((lhsArrayAttr && !isLhsComplexHasNan) ||
415 arith::bitEnumContainsAll(getFastmath(), arith::FastMathFlags::nnan))
425#define GET_OP_CLASSES
426#include "mlir/Dialect/Complex/IR/ComplexOps.cpp.inc"
Attributes are known-constant values of operations.
MLIRContext is the top-level object for a collection of MLIR operations.
This class represents a single result from folding an operation.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
constexpr T real(const NonFloatComplex< T > &x)
constexpr T imag(const NonFloatComplex< T > &x)
llvm::function_ref< Fn > function_ref
LogicalResult matchAndRewrite(arith::BitcastOp op, PatternRewriter &rewriter) const override
LogicalResult matchAndRewrite(BitcastOp op, PatternRewriter &rewriter) const override
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})