[flang-commits] [flang] [flang] "Almost NFC" changes to fir::runtime::genCharCompare() (PR #168563)

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Tue Nov 18 08:35:24 PST 2025


https://github.com/eugeneepshteyn created https://github.com/llvm/llvm-project/pull/168563

As part of investigating a related issue, I made the following changes to fir::runtime::genCharCompare():
- Renamed a variable
- Added an error check for the same kind of input args
- Updated another error check to use the same error found elsewhere in this source file

>From a232909ce7377206c276c0b671614076f7d4ea4b Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Sun, 16 Nov 2025 15:12:15 -0500
Subject: [PATCH] [flang] Optimization for length 1 char 1 compare in lowering

---
 .../Optimizer/Builder/Runtime/Character.cpp   | 23 ++++++++++++-------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/flang/lib/Optimizer/Builder/Runtime/Character.cpp b/flang/lib/Optimizer/Builder/Runtime/Character.cpp
index 540ecba299dc3..f8f1346ca730c 100644
--- a/flang/lib/Optimizer/Builder/Runtime/Character.cpp
+++ b/flang/lib/Optimizer/Builder/Runtime/Character.cpp
@@ -94,27 +94,34 @@ fir::runtime::genCharCompare(fir::FirOpBuilder &builder, mlir::Location loc,
                              mlir::arith::CmpIPredicate cmp,
                              mlir::Value lhsBuff, mlir::Value lhsLen,
                              mlir::Value rhsBuff, mlir::Value rhsLen) {
-  mlir::func::FuncOp beginFunc;
-  switch (discoverKind(lhsBuff.getType())) {
+  int lhsKind = discoverKind(lhsBuff.getType());
+  int rhsKind = discoverKind(rhsBuff.getType());
+  if (lhsKind != rhsKind) {
+    fir::emitFatalError(
+        loc, "runtime does not support comparison of different CHARACTER kind values");
+  }
+  mlir::func::FuncOp func;
+  switch (lhsKind) {
   case 1:
-    beginFunc = fir::runtime::getRuntimeFunc<mkRTKey(CharacterCompareScalar1)>(
+    func = fir::runtime::getRuntimeFunc<mkRTKey(CharacterCompareScalar1)>(
         loc, builder);
     break;
   case 2:
-    beginFunc = fir::runtime::getRuntimeFunc<mkRTKey(CharacterCompareScalar2)>(
+    func = fir::runtime::getRuntimeFunc<mkRTKey(CharacterCompareScalar2)>(
         loc, builder);
     break;
   case 4:
-    beginFunc = fir::runtime::getRuntimeFunc<mkRTKey(CharacterCompareScalar4)>(
+    func = fir::runtime::getRuntimeFunc<mkRTKey(CharacterCompareScalar4)>(
         loc, builder);
     break;
   default:
-    llvm_unreachable("runtime does not support CHARACTER KIND");
+    fir::emitFatalError(
+        loc, "unsupported CHARACTER kind value. Runtime expects 1, 2, or 4.");
   }
-  auto fTy = beginFunc.getFunctionType();
+  auto fTy = func.getFunctionType();
   auto args = fir::runtime::createArguments(builder, loc, fTy, lhsBuff, rhsBuff,
                                             lhsLen, rhsLen);
-  auto tri = fir::CallOp::create(builder, loc, beginFunc, args).getResult(0);
+  auto tri = fir::CallOp::create(builder, loc, func, args).getResult(0);
   auto zero = builder.createIntegerConstant(loc, tri.getType(), 0);
   return mlir::arith::CmpIOp::create(builder, loc, cmp, tri, zero);
 }



More information about the flang-commits mailing list