File tree Expand file tree Collapse file tree 3 files changed +35
-0
lines changed
include/mlir/Dialect/Math/IR Expand file tree Collapse file tree 3 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -724,6 +724,7 @@ def Math_SqrtOp : Math_FloatUnaryOp<"sqrt"> {
724
724
%x = math.sqrt %y : tensor<4x?xf32>
725
725
```
726
726
}];
727
+ let hasFolder = 1;
727
728
}
728
729
729
730
//===----------------------------------------------------------------------===//
Original file line number Diff line number Diff line change @@ -101,6 +101,31 @@ OpFoldResult math::PowFOp::fold(ArrayRef<Attribute> operands) {
101
101
return {};
102
102
}
103
103
104
+ OpFoldResult math::SqrtOp::fold (ArrayRef<Attribute> operands) {
105
+ auto constOperand = operands.front ();
106
+ if (!constOperand)
107
+ return {};
108
+
109
+ auto attr = constOperand.dyn_cast <FloatAttr>();
110
+ if (!attr)
111
+ return {};
112
+
113
+ auto ft = getType ().cast <FloatType>();
114
+
115
+ APFloat apf = attr.getValue ();
116
+
117
+ if (apf.isNegative ())
118
+ return {};
119
+
120
+ if (ft.getWidth () == 64 )
121
+ return FloatAttr::get (getType (), sqrt (apf.convertToDouble ()));
122
+
123
+ if (ft.getWidth () == 32 )
124
+ return FloatAttr::get (getType (), sqrtf (apf.convertToFloat ()));
125
+
126
+ return {};
127
+ }
128
+
104
129
// / Materialize an integer or floating point constant.
105
130
Operation *math::MathDialect::materializeConstant (OpBuilder &builder,
106
131
Attribute value, Type type,
Original file line number Diff line number Diff line change @@ -82,3 +82,12 @@ func @powf_fold() -> f32 {
82
82
%r = math.powf %c , %c : f32
83
83
return %r : f32
84
84
}
85
+
86
+ // CHECK-LABEL: @sqrt_fold
87
+ // CHECK: %[[cst:.+]] = arith.constant 2.000000e+00 : f32
88
+ // CHECK: return %[[cst]]
89
+ func @sqrt_fold () -> f32 {
90
+ %c = arith.constant 4.0 : f32
91
+ %r = math.sqrt %c : f32
92
+ return %r : f32
93
+ }
You can’t perform that action at this time.
0 commit comments