[flang-commits] [flang] [flang] Use integer arith.max/min operations for max/min lowering. (PR #186466)

via flang-commits flang-commits at lists.llvm.org
Mon Mar 16 07:49:13 PDT 2026


================
@@ -8782,26 +8776,31 @@ static mlir::Value genExtremumResult(mlir::Location loc,
 
     llvm_unreachable("unsupported FPMaxminBehavior");
   } else if (fir::isa_integer(type)) {
-    mlir::Value cmpLeft = left;
-    mlir::Value cmpRight = right;
+    // It is probably okay to use signed index.maxs/mins, but
+    // maybe the caller needs to specify signedness.
+    // There are currently no callers that pass values of index
+    // type, so just emit a TODO.
+    if (mlir::isa<mlir::IndexType>(type))
+      TODO(loc, "extremum for index type");
+
     if (type.isUnsignedInteger()) {
-      mlir::Type signlessType = mlir::IntegerType::get(
-          builder.getContext(), type.getIntOrFloatBitWidth(),
-          mlir::IntegerType::SignednessSemantics::Signless);
-      cmpLeft = builder.createConvert(loc, signlessType, left);
-      cmpRight = builder.createConvert(loc, signlessType, right);
+      if constexpr (isMax)
+        return mlir::arith::MaxUIOp::create(builder, loc, left, right);
----------------
jeanPerier wrote:

Maybe you could test it via some FIR to FIR tests starting with hlfir.maxval on unsigned (if we allow it).

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


More information about the flang-commits mailing list