Skip to content

[flang] Handle absent optional in LOC #73530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions flang/lib/Optimizer/Builder/IntrinsicCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4138,17 +4138,46 @@ IntrinsicLibrary::genCharacterCompare(mlir::Type resultType,
fir::getBase(args[1]), fir::getLen(args[1]));
}

static bool isOptional(mlir::Value value) {
auto varIface = mlir::dyn_cast_or_null<fir::FortranVariableOpInterface>(
value.getDefiningOp());
return varIface && varIface.isOptional();
}

// LOC
fir::ExtendedValue
IntrinsicLibrary::genLoc(mlir::Type resultType,
llvm::ArrayRef<fir::ExtendedValue> args) {
assert(args.size() == 1);
mlir::Value argValue = fir::getBase(args[0]);
assert(fir::isa_box_type(argValue.getType()) &&
mlir::Value box = fir::getBase(args[0]);
assert(fir::isa_box_type(box.getType()) &&
"argument must have been lowered to box type");
bool isFunc = argValue.getType().isa<fir::BoxProcType>();
mlir::Value argAddr = getAddrFromBox(builder, loc, args[0], isFunc);
return builder.createConvert(loc, fir::unwrapRefType(resultType), argAddr);
bool isFunc = box.getType().isa<fir::BoxProcType>();
if (!isOptional(box)) {
mlir::Value argAddr = getAddrFromBox(builder, loc, args[0], isFunc);
return builder.createConvert(loc, resultType, argAddr);
}
// Optional assumed shape case. Although this is not specified in this GNU
// intrinsic extension, LOC accepts absent optional and returns zero in that
// case.
// Note that the other OPTIONAL cases do not fall here since `box` was
// created when preparing the argument cases, but the box can be safely be
// used for all those cases and the address will be null if absent.
mlir::Value isPresent =
builder.create<fir::IsPresentOp>(loc, builder.getI1Type(), box);
return builder
.genIfOp(loc, {resultType}, isPresent,
/*withElseRegion=*/true)
.genThen([&]() {
mlir::Value argAddr = getAddrFromBox(builder, loc, args[0], isFunc);
mlir::Value cast = builder.createConvert(loc, resultType, argAddr);
builder.create<fir::ResultOp>(loc, cast);
})
.genElse([&]() {
mlir::Value zero = builder.createIntegerConstant(loc, resultType, 0);
builder.create<fir::ResultOp>(loc, zero);
})
.getResults()[0];
}

// MASKL, MASKR
Expand Down
Loading