File tree Expand file tree Collapse file tree 3 files changed +44
-1
lines changed
include/mlir/Dialect/Math/IR Expand file tree Collapse file tree 3 files changed +44
-1
lines changed Original file line number Diff line number Diff line change @@ -684,6 +684,7 @@ def Math_PowFOp : Math_FloatBinaryOp<"powf"> {
684
684
%x = math.powf %y, %z : tensor<4x?xbf16>
685
685
```
686
686
}];
687
+ let hasFolder = 1;
687
688
}
688
689
689
690
//===----------------------------------------------------------------------===//
Original file line number Diff line number Diff line change @@ -63,7 +63,40 @@ OpFoldResult math::Log2Op::fold(ArrayRef<Attribute> operands) {
63
63
return FloatAttr::get (getType (), log2 (apf.convertToDouble ()));
64
64
65
65
if (ft.getWidth () == 32 )
66
- return FloatAttr::get (getType (), log2f (apf.convertToDouble ()));
66
+ return FloatAttr::get (getType (), log2f (apf.convertToFloat ()));
67
+
68
+ return {};
69
+ }
70
+
71
+ // ===----------------------------------------------------------------------===//
72
+ // PowFOp folder
73
+ // ===----------------------------------------------------------------------===//
74
+
75
+ OpFoldResult math::PowFOp::fold (ArrayRef<Attribute> operands) {
76
+ auto ft = getType ().dyn_cast <FloatType>();
77
+ if (!ft)
78
+ return {};
79
+
80
+ APFloat vals[2 ]{APFloat (ft.getFloatSemantics ()),
81
+ APFloat (ft.getFloatSemantics ())};
82
+ for (int i = 0 ; i < 2 ; ++i) {
83
+ if (!operands[i])
84
+ return {};
85
+
86
+ auto attr = operands[i].dyn_cast <FloatAttr>();
87
+ if (!attr)
88
+ return {};
89
+
90
+ vals[i] = attr.getValue ();
91
+ }
92
+
93
+ if (ft.getWidth () == 64 )
94
+ return FloatAttr::get (
95
+ getType (), pow (vals[0 ].convertToDouble (), vals[1 ].convertToDouble ()));
96
+
97
+ if (ft.getWidth () == 32 )
98
+ return FloatAttr::get (
99
+ getType (), powf (vals[0 ].convertToFloat (), vals[1 ].convertToFloat ()));
67
100
68
101
return {};
69
102
}
Original file line number Diff line number Diff line change @@ -73,3 +73,12 @@ func @log2_nofold2_64() -> f64 {
73
73
%r = math.log2 %c : f64
74
74
return %r : f64
75
75
}
76
+
77
+ // CHECK-LABEL: @powf_fold
78
+ // CHECK: %[[cst:.+]] = arith.constant 4.000000e+00 : f32
79
+ // CHECK: return %[[cst]]
80
+ func @powf_fold () -> f32 {
81
+ %c = arith.constant 2.0 : f32
82
+ %r = math.powf %c , %c : f32
83
+ return %r : f32
84
+ }
You can’t perform that action at this time.
0 commit comments