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

via flang-commits flang-commits at lists.llvm.org
Tue Nov 18 08:54:33 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

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

Author: Eugene Epshteyn (eugeneepshteyn)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/168563.diff


1 Files Affected:

- (modified) flang/lib/Optimizer/Builder/Runtime/Character.cpp (+15-8) 


``````````diff
diff --git a/flang/lib/Optimizer/Builder/Runtime/Character.cpp b/flang/lib/Optimizer/Builder/Runtime/Character.cpp
index 540ecba299dc3..2f1772f602ac4 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);
 }

``````````

</details>


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


More information about the flang-commits mailing list