Skip to content

Commit 2f0b4f4

Browse files
authored
[flang][extension] support concatenation with absent optional (#112678)
Fix #112593 by adding support in lowering to concatenation with an absent optional _assumed length_ dummy argument because: 1. Most compilers seem to support it (most likely by accident). 2. This actually makes the compiler codegen simpler. Codegen was going out of its way to poke the LLVM optimizer bear by producing an undef argument for the length. I insist on the fact that no compiler support this with _explicit length_ optional arguments and the executable will segfault and I would discourage users from using that "feature" because runtime checks for bad optional dereference will kick when used (For instance, "nagfor -C=present" will produce an executable that abort with an error message . Flang does not have such runtime check option so far). Hence, I am not updating the Extensions.md document because this is not something I think we should advertise.
1 parent 9c80eb7 commit 2f0b4f4

File tree

2 files changed

+2
-14
lines changed

2 files changed

+2
-14
lines changed

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3373,19 +3373,7 @@ struct AbsentOpConversion : public fir::FIROpConversion<fir::AbsentOp> {
33733373
matchAndRewrite(fir::AbsentOp absent, OpAdaptor,
33743374
mlir::ConversionPatternRewriter &rewriter) const override {
33753375
mlir::Type ty = convertType(absent.getType());
3376-
mlir::Location loc = absent.getLoc();
3377-
3378-
if (mlir::isa<fir::BoxCharType>(absent.getType())) {
3379-
auto structTy = mlir::cast<mlir::LLVM::LLVMStructType>(ty);
3380-
assert(!structTy.isOpaque() && !structTy.getBody().empty());
3381-
auto undefStruct = rewriter.create<mlir::LLVM::UndefOp>(loc, ty);
3382-
auto nullField =
3383-
rewriter.create<mlir::LLVM::ZeroOp>(loc, structTy.getBody()[0]);
3384-
rewriter.replaceOpWithNewOp<mlir::LLVM::InsertValueOp>(
3385-
absent, undefStruct, nullField, 0);
3386-
} else {
3387-
rewriter.replaceOpWithNewOp<mlir::LLVM::ZeroOp>(absent, ty);
3388-
}
3376+
rewriter.replaceOpWithNewOp<mlir::LLVM::ZeroOp>(absent, ty);
33893377
return mlir::success();
33903378
}
33913379
};

flang/test/Fir/optional.fir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func.func @foo3(%arg0: !fir.boxchar<1>) -> i1 {
4747
// CHECK-LABEL: @bar3
4848
func.func @bar3() -> i1 {
4949
%0 = fir.absent !fir.boxchar<1>
50-
// CHECK: call i1 @foo3(ptr null, i64 undef)
50+
// CHECK: call i1 @foo3(ptr null, i64 0)
5151
%1 = fir.call @foo3(%0) : (!fir.boxchar<1>) -> i1
5252
return %1 : i1
5353
}

0 commit comments

Comments
 (0)