diff --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp --- a/flang/lib/Lower/IntrinsicCall.cpp +++ b/flang/lib/Lower/IntrinsicCall.cpp @@ -485,6 +485,7 @@ mlir::Value genNot(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genNull(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genPack(mlir::Type, llvm::ArrayRef); + fir::ExtendedValue genPresent(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genProduct(mlir::Type, llvm::ArrayRef); void genRandomInit(llvm::ArrayRef); void genRandomNumber(llvm::ArrayRef); @@ -726,6 +727,10 @@ {"mask", asBox}, {"vector", asBox, handleDynamicOptional}}}, /*isElemental=*/false}, + {"present", + &I::genPresent, + {{{"a", asInquired}}}, + /*isElemental=*/false}, {"product", &I::genProduct, {{{"array", asBox}, @@ -2563,6 +2568,15 @@ "unexpected result for PACK"); } +// PRESENT +fir::ExtendedValue +IntrinsicLibrary::genPresent(mlir::Type, + llvm::ArrayRef args) { + assert(args.size() == 1); + return builder.create(loc, builder.getI1Type(), + fir::getBase(args[0])); +} + // PRODUCT fir::ExtendedValue IntrinsicLibrary::genProduct(mlir::Type resultType, diff --git a/flang/test/Lower/Intrinsics/present.f90 b/flang/test/Lower/Intrinsics/present.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/Intrinsics/present.f90 @@ -0,0 +1,10 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! CHECK-LABEL: present_test +! CHECK-SAME: %[[arg0:[^:]+]]: !fir.box> +subroutine present_test(a) + integer, optional :: a(:) + + if (present(a)) print *,a + ! CHECK: %{{.*}} = fir.is_present %[[arg0]] : (!fir.box>) -> i1 +end subroutine