[flang-commits] [flang] [flang] preserve logical operations in single FIR operation (PR #190771)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Wed Apr 8 08:58:45 PDT 2026


================
@@ -1639,6 +1639,82 @@ mlir::OpFoldResult fir::ConvertOp::fold(FoldAdaptor adaptor) {
   return {};
 }
 
+//===----------------------------------------------------------------------===//
+// Logical binary operations fold helpers
+//===----------------------------------------------------------------------===//
+
+/// If \p value is a fir.convert of an arith.constant i1, return the i1 value.
+static std::optional<bool> getLogicalConstant(mlir::Value value) {
+  auto convertOp = value.getDefiningOp<fir::ConvertOp>();
+  if (!convertOp)
+    return std::nullopt;
+  if (auto cst = fir::getIntIfConstant(convertOp.getValue()))
+    return *cst != 0;
+  return std::nullopt;
+}
+
+/// Build a logical constant: fir.convert(arith.constant) if the result type
+/// is fir.logical, or arith.constant if it is an integer type.
+static mlir::OpFoldResult logicalConstantFoldResult(mlir::Type resTy,
+                                                    bool value) {
+  if (mlir::isa<fir::LogicalType>(resTy))
+    return {};
----------------
vzakhari wrote:

Thank you for explaining the problem, Jean. Yes, it sounds like we will need `fir::ConstantOp` for `fir.logical` to make it work, though this might be a bit intrusive.

We can discuss this offline, and this PR does not have to be changed.

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


More information about the flang-commits mailing list