[flang-commits] [flang] [flang] Fix segfault in CSHIFT/EOSHIFT with dynamically optional DIM (PR #184431)

Sairudra More via flang-commits flang-commits at lists.llvm.org
Wed Mar 4 08:05:59 PST 2026


================
@@ -489,6 +489,17 @@ mlir::Value HlfirCShiftLowering::lowerImpl(
   if (!dim) {
     // If DIM is not present, drop the last element which is a null Value.
     operands.truncate(2);
+  } else if (loweredActuals[2] && loweredActuals[2]->handleDynamicOptional()) {
+    // DIM is a dynamically optional dummy argument. getOperandVector already
+    // loaded it under an isPresent guard, returning 0 when absent. Per the
+    // Fortran standard (ยง16.9.68), absent DIM defaults to 1.
+    mlir::Type dimType = dim.getType();
+    mlir::Value zero = builder.createIntegerConstant(loc, dimType, 0);
+    mlir::Value one = builder.createIntegerConstant(loc, dimType, 1);
+    mlir::Value dimIsAbsent = mlir::arith::CmpIOp::create(
----------------
Saieiei wrote:

Done
Replaced the `arith.cmpi eq` / `arith.select1` arithmetic zero-check with a direct use of `loweredActuals[2]->getIsPresent()` inside a `fir.if`. The else branch now returns `arith.constant` 1 directly. No generated `arith.cmpi` or `arith.select`
 Verified with `CHECK-NOT` assertions in the updated test.
 https://github.com/llvm/llvm-project/pull/184431/changes/784e752a855ef8a2fc10169c91a06b80d306ed6c#diff-ebc3576e4524f08d331f05e244a700049c1c9049bbba5faa8e01346f21c65d99

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


More information about the flang-commits mailing list