Skip to content

Commit 10766b7

Browse files
clementvalvdonaldsonjeanPerier
committed
[flang] Lower adjustl and adjustr intrinsics
This patch adds lowering for the `adjustl` and `adjustr` intrinsics. This patch is part of the upstreaming effort from fir-dev branch. Reviewed By: jeanPerier Differential Revision: https://reviews.llvm.org/D121780 Co-authored-by: V Donaldson <[email protected]> Co-authored-by: Jean Perier <[email protected]>
1 parent a16eddb commit 10766b7

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

flang/lib/Lower/IntrinsicCall.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ struct IntrinsicLibrary {
427427
/// if the argument is an integer, into llvm intrinsics if the argument is
428428
/// real and to the `hypot` math routine if the argument is of complex type.
429429
mlir::Value genAbs(mlir::Type, llvm::ArrayRef<mlir::Value>);
430+
template <void (*CallRuntime)(fir::FirOpBuilder &, mlir::Location loc,
431+
mlir::Value, mlir::Value)>
432+
fir::ExtendedValue genAdjustRtCall(mlir::Type,
433+
llvm::ArrayRef<fir::ExtendedValue>);
430434
mlir::Value genAimag(mlir::Type, llvm::ArrayRef<mlir::Value>);
431435
fir::ExtendedValue genAll(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
432436
fir::ExtendedValue genAllocated(mlir::Type,
@@ -562,6 +566,14 @@ static constexpr bool handleDynamicOptional = true;
562566
/// should be provided for all the intrinsic arguments for completeness.
563567
static constexpr IntrinsicHandler handlers[]{
564568
{"abs", &I::genAbs},
569+
{"adjustl",
570+
&I::genAdjustRtCall<fir::runtime::genAdjustL>,
571+
{{{"string", asAddr}}},
572+
/*isElemental=*/true},
573+
{"adjustr",
574+
&I::genAdjustRtCall<fir::runtime::genAdjustR>,
575+
{{{"string", asAddr}}},
576+
/*isElemental=*/true},
565577
{"aimag", &I::genAimag},
566578
{"all",
567579
&I::genAll,
@@ -1430,6 +1442,37 @@ mlir::Value IntrinsicLibrary::genAbs(mlir::Type resultType,
14301442
llvm_unreachable("unexpected type in ABS argument");
14311443
}
14321444

1445+
// ADJUSTL & ADJUSTR
1446+
template <void (*CallRuntime)(fir::FirOpBuilder &, mlir::Location loc,
1447+
mlir::Value, mlir::Value)>
1448+
fir::ExtendedValue
1449+
IntrinsicLibrary::genAdjustRtCall(mlir::Type resultType,
1450+
llvm::ArrayRef<fir::ExtendedValue> args) {
1451+
assert(args.size() == 1);
1452+
mlir::Value string = builder.createBox(loc, args[0]);
1453+
// Create a mutable fir.box to be passed to the runtime for the result.
1454+
fir::MutableBoxValue resultMutableBox =
1455+
fir::factory::createTempMutableBox(builder, loc, resultType);
1456+
mlir::Value resultIrBox =
1457+
fir::factory::getMutableIRBox(builder, loc, resultMutableBox);
1458+
1459+
// Call the runtime -- the runtime will allocate the result.
1460+
CallRuntime(builder, loc, resultIrBox, string);
1461+
1462+
// Read result from mutable fir.box and add it to the list of temps to be
1463+
// finalized by the StatementContext.
1464+
fir::ExtendedValue res =
1465+
fir::factory::genMutableBoxRead(builder, loc, resultMutableBox);
1466+
return res.match(
1467+
[&](const fir::CharBoxValue &box) -> fir::ExtendedValue {
1468+
addCleanUpForTemp(loc, fir::getBase(box));
1469+
return box;
1470+
},
1471+
[&](const auto &) -> fir::ExtendedValue {
1472+
fir::emitFatalError(loc, "result of ADJUSTL is not a scalar character");
1473+
});
1474+
}
1475+
14331476
// AIMAG
14341477
mlir::Value IntrinsicLibrary::genAimag(mlir::Type resultType,
14351478
llvm::ArrayRef<mlir::Value> args) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s
2+
3+
! CHECK-LABLE: adjustl_test
4+
subroutine adjustl_test
5+
character(len=12) :: adjust_str = ' 0123456789'
6+
! CHECK: %[[strBox:.*]] = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
7+
! CHECK: %[[addr0:.*]] = fir.address_of(@_QFadjustl{{.*}}) : !fir.ref<!fir.char<1,12>>
8+
! CHECK: %[[eBox:.*]] = fir.embox %[[addr0]] : (!fir.ref<!fir.char<1,12>>) -> !fir.box<!fir.char<1,12>>
9+
! CHECK: %[[r0:.*]] = fir.zero_bits !fir.heap<!fir.char<1,?>>
10+
! CHECK: %[[r1:.*]] = fir.embox %[[r0]] typeparams %{{.*}} : (!fir.heap<!fir.char<1,?>>, index) -> !fir.box<!fir.heap<!fir.char<1,?>>>
11+
! CHECK: fir.store %[[r1]] to %[[strBox]] : !fir.ref<!fir.box<!fir.heap<!fir.char<1,?>>>>
12+
! CHECK: %[[r2:.*]] = fir.address_of(@_QQ{{.*}}) : !fir.ref<!fir.char<1,{{.*}}>>
13+
! CHECK: %[[r3:.*]] = fir.convert %[[strBox]] : (!fir.ref<!fir.box<!fir.heap<!fir.char<1,?>>>>) -> !fir.ref<!fir.box<none>>
14+
! CHECK: %[[r4:.*]] = fir.convert %[[eBox]] : (!fir.box<!fir.char<1,12>>) -> !fir.box<none>
15+
! CHECK: %[[r5:.*]] = fir.convert %[[r2]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
16+
! CHECK: %[[r6:.*]] = fir.call @_FortranAAdjustl(%[[r3]], %[[r4]], %[[r5]], %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) -> none
17+
adjust_str = adjustl(adjust_str)
18+
end subroutine
19+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s
2+
3+
! CHECK-LABLE: adjustr_test
4+
subroutine adjustr_test
5+
character(len=12) :: adjust_str = '0123456789 '
6+
! CHECK: %[[strBox:.*]] = fir.alloca !fir.box<!fir.heap<!fir.char<1,?>>>
7+
! CHECK: %[[addr0:.*]] = fir.address_of(@_QFadjustr{{.*}}) : !fir.ref<!fir.char<1,12>>
8+
! CHECK: %[[eBox:.*]] = fir.embox %[[addr0]] : (!fir.ref<!fir.char<1,12>>) -> !fir.box<!fir.char<1,12>>
9+
! CHECK: %[[r0:.*]] = fir.zero_bits !fir.heap<!fir.char<1,?>>
10+
! CHECK: %[[r1:.*]] = fir.embox %[[r0]] typeparams %{{.*}} : (!fir.heap<!fir.char<1,?>>, index) -> !fir.box<!fir.heap<!fir.char<1,?>>>
11+
! CHECK: fir.store %[[r1]] to %[[strBox]] : !fir.ref<!fir.box<!fir.heap<!fir.char<1,?>>>>
12+
! CHECK: %[[r2:.*]] = fir.address_of(@_QQ{{.*}}) : !fir.ref<!fir.char<1,{{.*}}>>
13+
! CHECK: %[[r3:.*]] = fir.convert %[[strBox]] : (!fir.ref<!fir.box<!fir.heap<!fir.char<1,?>>>>) -> !fir.ref<!fir.box<none>>
14+
! CHECK: %[[r4:.*]] = fir.convert %[[eBox]] : (!fir.box<!fir.char<1,12>>) -> !fir.box<none>
15+
! CHECK: %[[r5:.*]] = fir.convert %[[r2]] : (!fir.ref<!fir.char<1,{{.*}}>>) -> !fir.ref<i8>
16+
! CHECK: %[[r6:.*]] = fir.call @_FortranAAdjustr(%[[r3]], %[[r4]], %[[r5]], %{{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.ref<i8>, i32) -> none
17+
adjust_str = adjustr(adjust_str)
18+
end subroutine
19+

0 commit comments

Comments
 (0)