[flang-commits] [flang] [mlir] [RFC][mlir] Conditional support for fast-math attributes. (PR #125620)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Fri Feb 7 12:37:47 PST 2025


================
@@ -66,3 +67,49 @@ Operation *arith::ArithDialect::materializeConstant(OpBuilder &builder,
 
   return ConstantOp::materialize(builder, value, type, loc);
 }
+
+/// Return true if the type is compatible with fast math, i.e.
+/// it is a float type or contains a float type.
+bool arith::ArithFastMathInterface::isCompatibleType(Type type) {
+  if (isa<FloatType>(type))
+    return true;
+
+  // ShapeType's with ValueSemantics represent containers
+  // passed around as values (not references), so look inside
+  // them to see if the element type is compatible with FastMath.
+  if (type.hasTrait<ValueSemantics>())
+    if (auto shapedType = dyn_cast<ShapedType>(type))
+      return isCompatibleType(shapedType.getElementType());
+
+  // ComplexType's element type is always a FloatType.
+  if (auto complexType = dyn_cast<ComplexType>(type))
+    return true;
+
+  // TODO: what about TupleType and custom dialect struct-like types?
+  // It seems that they worth an interface to get to the list of element types.
----------------
vzakhari wrote:

I was mostly thinking about `arith.select`, but it seems it will be a separate discussion and changes (if any).

> If these operations are not common, maybe it is best/cheaper for the rest of the usages to keep the logic here simple and have these operation do the type visit as needed like you did in HLFIR.

This might be an acceptable approach.  I was thinking about making the other dialects' life easier by handling it here, but I can postpone the decision until the need arises.

https://github.com/llvm/llvm-project/pull/125620


More information about the flang-commits mailing list