Skip to content

Conversation

valerydmit
Copy link
Contributor

Lower character comparison into hlfir.cmpchar operation and
then lower the operation into a runtime call at intrinsic lowering.

@valerydmit valerydmit requested a review from vzakhari August 26, 2025 17:57
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Aug 26, 2025
@valerydmit valerydmit requested review from tblah and luporl August 26, 2025 17:57
@llvmbot
Copy link
Member

llvmbot commented Aug 26, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: Valery Dmitriev (valerydmit)

Changes

Lower character comparison into hlfir.cmpchar operation and
then lower the operation into a runtime call at intrinsic lowering.


Patch is 35.79 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/155458.diff

10 Files Affected:

  • (modified) flang/include/flang/Optimizer/HLFIR/HLFIROps.td (+20)
  • (modified) flang/lib/Lower/ConvertExprToHLFIR.cpp (+2-11)
  • (modified) flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp (+34)
  • (modified) flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp (+43-7)
  • (added) flang/test/HLFIR/cmpchar-lowering.fir (+242)
  • (modified) flang/test/HLFIR/invalid.fir (+11)
  • (modified) flang/test/Lower/HLFIR/binary-ops.f90 (+2-7)
  • (modified) flang/test/Lower/HLFIR/elemental-array-ops.f90 (+1-9)
  • (modified) flang/test/Lower/Intrinsics/lge_lgt_lle_llt.f90 (+2-2)
  • (modified) flang/test/Lower/character-compare.f90 (+1-1)
diff --git a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
index db3fb0b90464d..f58dde589aaf5 100644
--- a/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
+++ b/flang/include/flang/Optimizer/HLFIR/HLFIROps.td
@@ -348,6 +348,26 @@ def hlfir_ConcatOp : hlfir_Op<"concat",
   let hasVerifier = 1;
 }
 
+def hlfir_CmpCharOp : hlfir_Op<"cmpchar",
+    [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
+  let summary = "compare two characters";
+  let description = [{
+    Compare two character strings of a same character kind.
+  }];
+
+  let arguments = (ins Arith_CmpIPredicateAttr:$predicate,
+                AnyScalarCharacterEntity:$lchr,
+                AnyScalarCharacterEntity:$rchr);
+
+  let results = (outs I1);
+
+  let assemblyFormat = [{
+      $predicate $lchr $rchr attr-dict `:` functional-type(operands, results)
+  }];
+
+  let hasVerifier = 1;
+}
+
 def hlfir_AllOp : hlfir_Op<"all", [DeclareOpInterfaceMethods<MemoryEffectsOpInterface>]> {
   let summary = "ALL transformational intrinsic";
   let description = [{
diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index 9930dd69e0c0a..81e09a1ca5bc9 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -26,7 +26,6 @@
 #include "flang/Optimizer/Builder/Complex.h"
 #include "flang/Optimizer/Builder/IntrinsicCall.h"
 #include "flang/Optimizer/Builder/MutableBox.h"
-#include "flang/Optimizer/Builder/Runtime/Character.h"
 #include "flang/Optimizer/Builder/Runtime/Derived.h"
 #include "flang/Optimizer/Builder/Runtime/Pointer.h"
 #include "flang/Optimizer/Builder/Todo.h"
@@ -1286,16 +1285,8 @@ struct BinaryOp<Fortran::evaluate::Relational<
                                          fir::FirOpBuilder &builder,
                                          const Op &op, hlfir::Entity lhs,
                                          hlfir::Entity rhs) {
-    auto [lhsExv, lhsCleanUp] =
-        hlfir::translateToExtendedValue(loc, builder, lhs);
-    auto [rhsExv, rhsCleanUp] =
-        hlfir::translateToExtendedValue(loc, builder, rhs);
-    auto cmp = fir::runtime::genCharCompare(
-        builder, loc, translateSignedRelational(op.opr), lhsExv, rhsExv);
-    if (lhsCleanUp)
-      (*lhsCleanUp)();
-    if (rhsCleanUp)
-      (*rhsCleanUp)();
+    auto cmp = hlfir::CmpCharOp::create(
+        builder, loc, translateSignedRelational(op.opr), lhs, rhs);
     return hlfir::EntityWithAttributes{cmp};
   }
 };
diff --git a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
index 3c5095da0145a..964d183631186 100644
--- a/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
+++ b/flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
@@ -820,6 +820,40 @@ void hlfir::ConcatOp::getEffects(
   getIntrinsicEffects(getOperation(), effects);
 }
 
+//===----------------------------------------------------------------------===//
+// CmpCharOp
+//===----------------------------------------------------------------------===//
+
+llvm::LogicalResult hlfir::CmpCharOp::verify() {
+  mlir::Value lchr = getLchr();
+  mlir::Value rchr = getRchr();
+
+  unsigned kind = getCharacterKind(lchr.getType());
+  if (kind != getCharacterKind(rchr.getType()))
+    return emitOpError("character arguments must have the same KIND");
+
+  switch (getPredicate()) {
+  case mlir::arith::CmpIPredicate::slt:
+  case mlir::arith::CmpIPredicate::sle:
+  case mlir::arith::CmpIPredicate::eq:
+  case mlir::arith::CmpIPredicate::ne:
+  case mlir::arith::CmpIPredicate::sgt:
+  case mlir::arith::CmpIPredicate::sge:
+    break;
+  default:
+    return emitOpError("expected signed predicate");
+  }
+
+  return mlir::success();
+}
+
+void hlfir::CmpCharOp::getEffects(
+    llvm::SmallVectorImpl<
+        mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
+        &effects) {
+  getIntrinsicEffects(getOperation(), effects);
+}
+
 //===----------------------------------------------------------------------===//
 // NumericalReductionOp
 //===----------------------------------------------------------------------===//
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp b/flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp
index e0167cc12b8a3..0416df6d1d1d0 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/LowerHLFIRIntrinsics.cpp
@@ -551,6 +551,41 @@ class ReshapeOpConversion : public HlfirIntrinsicConversion<hlfir::ReshapeOp> {
   }
 };
 
+class CmpCharOpConversion : public HlfirIntrinsicConversion<hlfir::CmpCharOp> {
+  using HlfirIntrinsicConversion<hlfir::CmpCharOp>::HlfirIntrinsicConversion;
+
+  llvm::LogicalResult
+  matchAndRewrite(hlfir::CmpCharOp cmp,
+                  mlir::PatternRewriter &rewriter) const override {
+    fir::FirOpBuilder builder{rewriter, cmp.getOperation()};
+    const mlir::Location &loc = cmp->getLoc();
+    hlfir::Entity lhs{cmp.getLchr()};
+    hlfir::Entity rhs{cmp.getRchr()};
+
+    auto [lhsExv, lhsCleanUp] =
+        hlfir::translateToExtendedValue(loc, builder, lhs);
+    auto [rhsExv, rhsCleanUp] =
+        hlfir::translateToExtendedValue(loc, builder, rhs);
+
+    auto resultVal = fir::runtime::genCharCompare(
+        builder, loc, cmp.getPredicate(), lhsExv, rhsExv);
+    if (lhsCleanUp || rhsCleanUp) {
+      auto oldInsertionPoint = builder.saveInsertionPoint();
+      builder.setInsertionPointAfter(cmp);
+      if (lhsCleanUp)
+        (*lhsCleanUp)();
+      if (rhsCleanUp)
+        (*rhsCleanUp)();
+      builder.restoreInsertionPoint(oldInsertionPoint);
+    }
+    auto resultEntity = hlfir::EntityWithAttributes{resultVal};
+
+    processReturnValue(cmp, resultEntity, /*mustBeFreed=*/false, builder,
+                       rewriter);
+    return mlir::success();
+  }
+};
+
 class LowerHLFIRIntrinsics
     : public hlfir::impl::LowerHLFIRIntrinsicsBase<LowerHLFIRIntrinsics> {
 public:
@@ -558,13 +593,14 @@ class LowerHLFIRIntrinsics
     mlir::ModuleOp module = this->getOperation();
     mlir::MLIRContext *context = &getContext();
     mlir::RewritePatternSet patterns(context);
-    patterns.insert<
-        MatmulOpConversion, MatmulTransposeOpConversion, AllOpConversion,
-        AnyOpConversion, SumOpConversion, ProductOpConversion,
-        TransposeOpConversion, CountOpConversion, DotProductOpConversion,
-        MaxvalOpConversion, MinvalOpConversion, MinlocOpConversion,
-        MaxlocOpConversion, ArrayShiftOpConversion<hlfir::CShiftOp>,
-        ArrayShiftOpConversion<hlfir::EOShiftOp>, ReshapeOpConversion>(context);
+    patterns.insert<MatmulOpConversion, MatmulTransposeOpConversion,
+                    AllOpConversion, AnyOpConversion, SumOpConversion,
+                    ProductOpConversion, TransposeOpConversion,
+                    CountOpConversion, DotProductOpConversion,
+                    MaxvalOpConversion, MinvalOpConversion, MinlocOpConversion,
+                    MaxlocOpConversion, ArrayShiftOpConversion<hlfir::CShiftOp>,
+                    ArrayShiftOpConversion<hlfir::EOShiftOp>,
+                    ReshapeOpConversion, CmpCharOpConversion>(context);
 
     // While conceptually this pass is performing dialect conversion, we use
     // pattern rewrites here instead of dialect conversion because this pass
diff --git a/flang/test/HLFIR/cmpchar-lowering.fir b/flang/test/HLFIR/cmpchar-lowering.fir
new file mode 100644
index 0000000000000..7621c968c30ba
--- /dev/null
+++ b/flang/test/HLFIR/cmpchar-lowering.fir
@@ -0,0 +1,242 @@
+// Test hlfir.cmpchar operation lowering to a fir runtime call
+// RUN: fir-opt %s -lower-hlfir-intrinsics | FileCheck %s
+
+// HLFIR for the test below has been produced from reduced flang/test/Lower/Intrinsics/lge_lgt_lle_llt.f90
+func.func @_QPlge_test() {
+// CHECK-LABEL:   func.func @_QPlge_test() {
+// CHECK:           %[[VAL_0:.*]] = arith.constant 0 : i32
+// CHECK:           %[[VAL_1:.*]] = arith.constant 7 : index
+// CHECK:           %[[VAL_2:.*]] = arith.constant 3 : index
+// CHECK:           %[[VAL_3:.*]] = fir.dummy_scope : !fir.dscope
+// CHECK:           %[[VAL_4:.*]] = fir.alloca !fir.array<3x!fir.char<1,3>> {bindc_name = "c1", uniq_name = "_QFlge_testEc1"}
+// CHECK:           %[[VAL_5:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
+// CHECK:           %[[VAL_6:.*]]:2 = hlfir.declare %[[VAL_4]](%[[VAL_5]]) typeparams %[[VAL_2]] {uniq_name = "_QFlge_testEc1"} : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, !fir.shape<1>, index) -> (!fir.ref<!fir.array<3x!fir.char<1,3>>>, !fir.ref<!fir.array<3x!fir.char<1,3>>>)
+// CHECK:           %[[VAL_7:.*]] = fir.alloca !fir.array<3x!fir.char<1,7>> {bindc_name = "c2", uniq_name = "_QFlge_testEc2"}
+// CHECK:           %[[VAL_8:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
+// CHECK:           %[[VAL_9:.*]]:2 = hlfir.declare %[[VAL_7]](%[[VAL_8]]) typeparams %[[VAL_1]] {uniq_name = "_QFlge_testEc2"} : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, !fir.shape<1>, index) -> (!fir.ref<!fir.array<3x!fir.char<1,7>>>, !fir.ref<!fir.array<3x!fir.char<1,7>>>)
+// CHECK:           %[[VAL_10:.*]] = fir.alloca !fir.array<3x!fir.logical<4>> {bindc_name = "l", uniq_name = "_QFlge_testEl"}
+// CHECK:           %[[VAL_11:.*]] = fir.shape %[[VAL_2]] : (index) -> !fir.shape<1>
+// CHECK:           %[[VAL_12:.*]]:2 = hlfir.declare %[[VAL_10]](%[[VAL_11]]) {uniq_name = "_QFlge_testEl"} : (!fir.ref<!fir.array<3x!fir.logical<4>>>, !fir.shape<1>) -> (!fir.ref<!fir.array<3x!fir.logical<4>>>, !fir.ref<!fir.array<3x!fir.logical<4>>>)
+// CHECK:           %[[VAL_13:.*]] = hlfir.elemental %[[VAL_5]] unordered : (!fir.shape<1>) -> !hlfir.expr<3x!fir.logical<4>> {
+    %0 = fir.dummy_scope : !fir.dscope
+    %c3 = arith.constant 3 : index
+    %c3_0 = arith.constant 3 : index
+    %1 = fir.alloca !fir.array<3x!fir.char<1,3>> {bindc_name = "c1", uniq_name = "_QFlge_testEc1"}
+    %2 = fir.shape %c3_0 : (index) -> !fir.shape<1>
+    %3:2 = hlfir.declare %1(%2) typeparams %c3 {uniq_name = "_QFlge_testEc1"} : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, !fir.shape<1>, index) -> (!fir.ref<!fir.array<3x!fir.char<1,3>>>, !fir.ref<!fir.array<3x!fir.char<1,3>>>)
+    %c7 = arith.constant 7 : index
+    %c3_1 = arith.constant 3 : index
+    %4 = fir.alloca !fir.array<3x!fir.char<1,7>> {bindc_name = "c2", uniq_name = "_QFlge_testEc2"}
+    %5 = fir.shape %c3_1 : (index) -> !fir.shape<1>
+    %6:2 = hlfir.declare %4(%5) typeparams %c7 {uniq_name = "_QFlge_testEc2"} : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, !fir.shape<1>, index) -> (!fir.ref<!fir.array<3x!fir.char<1,7>>>, !fir.ref<!fir.array<3x!fir.char<1,7>>>)
+    %c3_2 = arith.constant 3 : index
+    %7 = fir.alloca !fir.array<3x!fir.logical<4>> {bindc_name = "l", uniq_name = "_QFlge_testEl"}
+    %8 = fir.shape %c3_2 : (index) -> !fir.shape<1>
+    %9:2 = hlfir.declare %7(%8) {uniq_name = "_QFlge_testEl"} : (!fir.ref<!fir.array<3x!fir.logical<4>>>, !fir.shape<1>) -> (!fir.ref<!fir.array<3x!fir.logical<4>>>, !fir.ref<!fir.array<3x!fir.logical<4>>>)
+    %10 = hlfir.elemental %2 unordered : (!fir.shape<1>) -> !hlfir.expr<3x!fir.logical<4>> {
+    ^bb0(%arg0: index):
+      %14 = hlfir.designate %3#0 (%arg0)  typeparams %c3 : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, index, index) -> !fir.ref<!fir.char<1,3>>
+      %15 = hlfir.designate %6#0 (%arg0)  typeparams %c7 : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, index, index) -> !fir.ref<!fir.char<1,7>>
+      %16 = hlfir.cmpchar sge %14 %15 : (!fir.ref<!fir.char<1,3>>, !fir.ref<!fir.char<1,7>>) -> i1
+      %17 = fir.convert %16 : (i1) -> !fir.logical<4>
+      hlfir.yield_element %17 : !fir.logical<4>
+    }
+    hlfir.assign %10 to %9#0 : !hlfir.expr<3x!fir.logical<4>>, !fir.ref<!fir.array<3x!fir.logical<4>>>
+    hlfir.destroy %10 : !hlfir.expr<3x!fir.logical<4>>
+// CHECK:           ^bb0(%[[VAL_14:.*]]: index):
+// CHECK:             %[[VAL_15:.*]] = hlfir.designate %[[VAL_6]]#0 (%[[VAL_14]])  typeparams %[[VAL_2]] : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, index, index) -> !fir.ref<!fir.char<1,3>>
+// CHECK:             %[[VAL_16:.*]] = hlfir.designate %[[VAL_9]]#0 (%[[VAL_14]])  typeparams %[[VAL_1]] : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, index, index) -> !fir.ref<!fir.char<1,7>>
+// CHECK:             %[[VAL_17:.*]] = fir.convert %[[VAL_15]] : (!fir.ref<!fir.char<1,3>>) -> !fir.ref<i8>
+// CHECK:             %[[VAL_18:.*]] = fir.convert %[[VAL_16]] : (!fir.ref<!fir.char<1,7>>) -> !fir.ref<i8>
+// CHECK:             %[[VAL_19:.*]] = fir.convert %[[VAL_2]] : (index) -> i64
+// CHECK:             %[[VAL_20:.*]] = fir.convert %[[VAL_1]] : (index) -> i64
+// CHECK:             %[[VAL_21:.*]] = fir.call @_FortranACharacterCompareScalar1(%[[VAL_17]], %[[VAL_18]], %[[VAL_19]], %[[VAL_20]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32
+// CHECK:             %[[VAL_22:.*]] = arith.cmpi sge, %[[VAL_21]], %[[VAL_0]] : i32
+// CHECK:             %[[VAL_23:.*]] = fir.convert %[[VAL_22]] : (i1) -> !fir.logical<4>
+// CHECK:             hlfir.yield_element %[[VAL_23]] : !fir.logical<4>
+// CHECK:           }
+// CHECK:           hlfir.assign %[[VAL_13]] to %[[VAL_12]]#0 : !hlfir.expr<3x!fir.logical<4>>, !fir.ref<!fir.array<3x!fir.logical<4>>>
+// CHECK:           hlfir.destroy %[[VAL_13]] : !hlfir.expr<3x!fir.logical<4>>
+    %11 = hlfir.elemental %2 unordered : (!fir.shape<1>) -> !hlfir.expr<3x!fir.logical<4>> {
+    ^bb0(%arg0: index):
+      %14 = hlfir.designate %3#0 (%arg0)  typeparams %c3 : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, index, index) -> !fir.ref<!fir.char<1,3>>
+      %15 = hlfir.designate %6#0 (%arg0)  typeparams %c7 : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, index, index) -> !fir.ref<!fir.char<1,7>>
+      %16 = hlfir.cmpchar sgt %14 %15 : (!fir.ref<!fir.char<1,3>>, !fir.ref<!fir.char<1,7>>) -> i1
+      %17 = fir.convert %16 : (i1) -> !fir.logical<4>
+      hlfir.yield_element %17 : !fir.logical<4>
+    }
+    hlfir.assign %11 to %9#0 : !hlfir.expr<3x!fir.logical<4>>, !fir.ref<!fir.array<3x!fir.logical<4>>>
+    hlfir.destroy %11 : !hlfir.expr<3x!fir.logical<4>>
+// CHECK:           %[[VAL_24:.*]] = hlfir.elemental %[[VAL_5]] unordered : (!fir.shape<1>) -> !hlfir.expr<3x!fir.logical<4>> {
+// CHECK:           ^bb0(%[[VAL_25:.*]]: index):
+// CHECK:             %[[VAL_26:.*]] = hlfir.designate %[[VAL_6]]#0 (%[[VAL_25]])  typeparams %[[VAL_2]] : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, index, index) -> !fir.ref<!fir.char<1,3>>
+// CHECK:             %[[VAL_27:.*]] = hlfir.designate %[[VAL_9]]#0 (%[[VAL_25]])  typeparams %[[VAL_1]] : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, index, index) -> !fir.ref<!fir.char<1,7>>
+// CHECK:             %[[VAL_28:.*]] = fir.convert %[[VAL_26]] : (!fir.ref<!fir.char<1,3>>) -> !fir.ref<i8>
+// CHECK:             %[[VAL_29:.*]] = fir.convert %[[VAL_27]] : (!fir.ref<!fir.char<1,7>>) -> !fir.ref<i8>
+// CHECK:             %[[VAL_30:.*]] = fir.convert %[[VAL_2]] : (index) -> i64
+// CHECK:             %[[VAL_31:.*]] = fir.convert %[[VAL_1]] : (index) -> i64
+// CHECK:             %[[VAL_32:.*]] = fir.call @_FortranACharacterCompareScalar1(%[[VAL_28]], %[[VAL_29]], %[[VAL_30]], %[[VAL_31]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32
+// CHECK:             %[[VAL_33:.*]] = arith.cmpi sgt, %[[VAL_32]], %[[VAL_0]] : i32
+// CHECK:             %[[VAL_34:.*]] = fir.convert %[[VAL_33]] : (i1) -> !fir.logical<4>
+// CHECK:             hlfir.yield_element %[[VAL_34]] : !fir.logical<4>
+// CHECK:           }
+// CHECK:           hlfir.assign %[[VAL_24]] to %[[VAL_12]]#0 : !hlfir.expr<3x!fir.logical<4>>, !fir.ref<!fir.array<3x!fir.logical<4>>>
+// CHECK:           hlfir.destroy %[[VAL_24]] : !hlfir.expr<3x!fir.logical<4>>
+    %12 = hlfir.elemental %2 unordered : (!fir.shape<1>) -> !hlfir.expr<3x!fir.logical<4>> {
+    ^bb0(%arg0: index):
+      %14 = hlfir.designate %3#0 (%arg0)  typeparams %c3 : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, index, index) -> !fir.ref<!fir.char<1,3>>
+      %15 = hlfir.designate %6#0 (%arg0)  typeparams %c7 : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, index, index) -> !fir.ref<!fir.char<1,7>>
+      %16 = hlfir.cmpchar sle %14 %15 : (!fir.ref<!fir.char<1,3>>, !fir.ref<!fir.char<1,7>>) -> i1
+      %17 = fir.convert %16 : (i1) -> !fir.logical<4>
+      hlfir.yield_element %17 : !fir.logical<4>
+    }
+    hlfir.assign %12 to %9#0 : !hlfir.expr<3x!fir.logical<4>>, !fir.ref<!fir.array<3x!fir.logical<4>>>
+    hlfir.destroy %12 : !hlfir.expr<3x!fir.logical<4>>
+// CHECK:           %[[VAL_35:.*]] = hlfir.elemental %[[VAL_5]] unordered : (!fir.shape<1>) -> !hlfir.expr<3x!fir.logical<4>> {
+// CHECK:           ^bb0(%[[VAL_36:.*]]: index):
+// CHECK:             %[[VAL_37:.*]] = hlfir.designate %[[VAL_6]]#0 (%[[VAL_36]])  typeparams %[[VAL_2]] : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, index, index) -> !fir.ref<!fir.char<1,3>>
+// CHECK:             %[[VAL_38:.*]] = hlfir.designate %[[VAL_9]]#0 (%[[VAL_36]])  typeparams %[[VAL_1]] : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, index, index) -> !fir.ref<!fir.char<1,7>>
+// CHECK:             %[[VAL_39:.*]] = fir.convert %[[VAL_37]] : (!fir.ref<!fir.char<1,3>>) -> !fir.ref<i8>
+// CHECK:             %[[VAL_40:.*]] = fir.convert %[[VAL_38]] : (!fir.ref<!fir.char<1,7>>) -> !fir.ref<i8>
+// CHECK:             %[[VAL_41:.*]] = fir.convert %[[VAL_2]] : (index) -> i64
+// CHECK:             %[[VAL_42:.*]] = fir.convert %[[VAL_1]] : (index) -> i64
+// CHECK:             %[[VAL_43:.*]] = fir.call @_FortranACharacterCompareScalar1(%[[VAL_39]], %[[VAL_40]], %[[VAL_41]], %[[VAL_42]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32
+// CHECK:             %[[VAL_44:.*]] = arith.cmpi sle, %[[VAL_43]], %[[VAL_0]] : i32
+// CHECK:             %[[VAL_45:.*]] = fir.convert %[[VAL_44]] : (i1) -> !fir.logical<4>
+// CHECK:             hlfir.yield_element %[[VAL_45]] : !fir.logical<4>
+// CHECK:           }
+// CHECK:           hlfir.assign %[[VAL_35]] to %[[VAL_12]]#0 : !hlfir.expr<3x!fir.logical<4>>, !fir.ref<!fir.array<3x!fir.logical<4>>>
+// CHECK:           hlfir.destroy %[[VAL_35]] : !hlfir.expr<3x!fir.logical<4>>
+    %13 = hlfir.elemental %2 unordered : (!fir.shape<1>) -> !hlfir.expr<3x!fir.logical<4>> {
+    ^bb0(%arg0: index):
+      %14 = hlfir.designate %3#0 (%arg0)  typeparams %c3 : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, index, index) -> !fir.ref<!fir.char<1,3>>
+      %15 = hlfir.designate %6#0 (%arg0)  typeparams %c7 : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, index, index) -> !fir.ref<!fir.char<1,7>>
+      %16 = hlfir.cmpchar slt %14 %15 : (!fir.ref<!fir.char<1,3>>, !fir.ref<!fir.char<1,7>>) -> i1
+      %17 = fir.convert %16 : (i1) -> !fir.logical<4>
+      hlfir.yield_element %17 : !fir.logical<4>
+    }
+    hlfir.assign %13 to %9#0 : !hlfir.expr<3x!fir.logical<4>>, !fir.ref<!fir.array<3x!fir.logical<4>>>
+    hlfir.destroy %13 : !hlfir.expr<3x!fir.logical<4>>
+    return
+}
+// CHECK:           %[[VAL_46:.*]] = hlfir.elemental %[[VAL_5]] unordered : (!fir.shape<1>) -> !hlfir.expr<3x!fir.logical<4>> {
+// CHECK:           ^bb0(%[[VAL_47:.*]]: index):
+// CHECK:             %[[VAL_48:.*]] = hlfir.designate %[[VAL_6]]#0 (%[[VAL_47]])  typeparams %[[VAL_2]] : (!fir.ref<!fir.array<3x!fir.char<1,3>>>, index, index) -> !fir.ref<!fir.char<1,3>>
+// CHECK:             %[[VAL_49:.*]] = hlfir.designate %[[VAL_9]]#0 (%[[VAL_47]])  typeparams %[[VAL_1]] : (!fir.ref<!fir.array<3x!fir.char<1,7>>>, index, index) -> !fir.ref<!fir.char<1,7>>
+// CHECK:             %[[VAL_50:.*]] = fir.convert %[[VAL_48]] : (!fir.ref<!fir.char<1,3>>) -> !fir.ref<i8>
+// CHECK:             %[[VAL_51:.*]] = fir.convert %[[VAL_49]] : (!fir.ref<!fir.char<1,7>>) -> !fir.ref<i8>
+// CHECK:             %[[VAL_52:.*]] = fir.convert %[[VAL_2]] : (index) -> i64
+// CHECK:             %[[VAL_53:.*]] = fir.convert %[[VAL_1]] : (index) -> i64
+// CHECK:             %[[VAL_54:.*]] = fir.call @_FortranACharacterCompareScalar1(%[[VAL_50]], %[[VAL_51]], %[[VAL_52]], %[[VAL_53]]) : (!fir.ref<i8>, !fir.ref<i8>, i64, i64) -> i32
+// CHECK:             %[[VAL_55:.*]] = arith.cmpi slt, %[[VAL_54]], %[[VAL_0]] : i32
+// CHECK:             %[[VAL_56:.*]] = fir.convert %[[VAL_55]] : (i1) -> !fir.logical<4>
+// CHECK:             hlfir.yield_element %[[VAL_56]] : !fir.logical<4>
+// CHECK:           }
+// CHECK:           hlfir.assign %[[VAL_46]] to %[[VAL_12]]#0 : !hlfir.expr<3x!fir.logical<4>>, !fir.ref<!fir.array<3x!fir.logical<4>>>
+// CHECK:           hlfir.destroy %[[VAL_...
[truncated]

@valerydmit
Copy link
Contributor Author

The commit list contains hlfir.cmpcharop definition for convenience which is a separate PR 155457

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Looks great!

auto resultVal = fir::runtime::genCharCompare(
builder, loc, cmp.getPredicate(), lhsExv, rhsExv);
if (lhsCleanUp || rhsCleanUp) {
auto oldInsertionPoint = builder.saveInsertionPoint();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you may use mlir::OpBuilder::InsertionGuard guard(builder); that will automatically save and restore the insertion point on the scope entry/exit.

Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

Lower character comparison into hlfir.cmpchar operation and
then lower the operation into a runtime call at intrinsic lowering.
@valerydmit valerydmit force-pushed the hlfir_cmp_char_lowering branch from 00239fc to 37d7067 Compare August 27, 2025 17:26
@valerydmit valerydmit merged commit 0df4463 into llvm:main Aug 27, 2025
9 checks passed
Copy link

@valerydmit Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants