[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 12:00:32 PST 2026
================
@@ -489,6 +490,30 @@ 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.
+ // Use getIsPresent() directly (per eugeneepshteyn's review) rather than a
+ // 0-sentinel arithmetic check. Building our own fir.if ensures an
+ // explicitly-passed DIM=0 loads the actual value and reaches the runtime
+ // bounds check (per vzakhari's review), while an absent DIM defaults to 1
+ // per Fortran §16.9.68.
+ mlir::Value isPresent = loweredActuals[2]->getIsPresent();
+ hlfir::Entity dimEntity = loweredActuals[2]->getActual(loc, builder);
+ mlir::Value dimRef =
+ hlfir::derefPointersAndAllocatables(loc, builder, dimEntity);
+ mlir::Type dimType = fir::unwrapRefType(dimRef.getType());
+ dim = builder.genIfOp(loc, {dimType}, isPresent, /*withElseRegion=*/true)
+ .genThen([&]() {
+ mlir::Value loaded = fir::LoadOp::create(builder, loc, dimRef);
----------------
Saieiei wrote:
I’ve removed the redundant re-load. `getOperandVector()` already goes through `loadOptionalValue()`, which emits the guarded `fir.if` and returns the loaded scalar (or `0` when the optional is absent), so `lowerImpl()` now just reuses that value directly with `arith.select isPresent, dim, 1` and no extra `fir.if`/`fir.load` is generated. The `DIM=0` runtime-error behavior is still preserved: when `isPresent=true`, the real value (even `0`) is forwarded unchanged to flang-rt, so it still diagnoses as before. I’ve applied the same cleanup to `EOSHIFT` as well, and updated the comments (no user-IDs, no special symbols).
https://github.com/llvm/llvm-project/pull/184431/changes/107f598adff01a0a66879a00d0d52fedea0b3b10#diff-ebc3576e4524f08d331f05e244a700049c1c9049bbba5faa8e01346f21c65d99
https://github.com/llvm/llvm-project/pull/184431
More information about the flang-commits
mailing list