[flang] Lower scalar negation

Handle negation on scalar expression.

```
res = -a
```

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D120071

Co-authored-by: Jean Perier <jperier@nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
This commit is contained in:
Valentin Clement 2022-02-18 08:08:34 +01:00
parent b1d9136da1
commit eafafbae92
No known key found for this signature in database
GPG Key ID: 086D54783C928776
2 changed files with 49 additions and 3 deletions

View File

@ -189,18 +189,21 @@ public:
template <int KIND>
ExtValue genval(const Fortran::evaluate::Negate<Fortran::evaluate::Type<
Fortran::common::TypeCategory::Integer, KIND>> &op) {
TODO(getLoc(), "genval Negate integer");
mlir::Value input = genunbox(op.left());
// Like LLVM, integer negation is the binary op "0 - value"
mlir::Value zero = genIntegerConstant<KIND>(builder.getContext(), 0);
return builder.create<mlir::arith::SubIOp>(getLoc(), zero, input);
}
template <int KIND>
ExtValue genval(const Fortran::evaluate::Negate<Fortran::evaluate::Type<
Fortran::common::TypeCategory::Real, KIND>> &op) {
TODO(getLoc(), "genval Negate real");
return builder.create<mlir::arith::NegFOp>(getLoc(), genunbox(op.left()));
}
template <int KIND>
ExtValue genval(const Fortran::evaluate::Negate<Fortran::evaluate::Type<
Fortran::common::TypeCategory::Complex, KIND>> &op) {
TODO(getLoc(), "genval Negate complex");
return builder.create<fir::NegcOp>(getLoc(), genunbox(op.left()));
}
#undef GENBIN

View File

@ -22,3 +22,46 @@ end
! CHECK: %[[B_VAL:.*]] = fir.load %arg1 : !fir.ref<i64>
! CHECK: %[[B_CONV:.*]] = fir.convert %[[B_VAL]] : (i64) -> i32
! CHECK: fir.store %[[B_CONV]] to %[[A]] : !fir.ref<i32>
integer function negi(a)
integer :: a
negi = -a
end
! CHECK-LABEL: func @_QPnegi(
! CHECK-SAME: %[[A:.*]]: !fir.ref<i32> {fir.bindc_name = "a"}) -> i32 {
! CHECK: %[[FCTRES:.*]] = fir.alloca i32 {bindc_name = "negi", uniq_name = "_QFnegiEnegi"}
! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref<i32>
! CHECK: %[[C0:.*]] = arith.constant 0 : i32
! CHECK: %[[NEG:.*]] = arith.subi %[[C0]], %[[A_VAL]] : i32
! CHECK: fir.store %[[NEG]] to %[[FCTRES]] : !fir.ref<i32>
! CHECK: %[[RET:.*]] = fir.load %[[FCTRES]] : !fir.ref<i32>
! CHECK: return %[[RET]] : i32
real function negr(a)
real :: a
negr = -a
end
! CHECK-LABEL: func @_QPnegr(
! CHECK-SAME: %[[A:.*]]: !fir.ref<f32> {fir.bindc_name = "a"}) -> f32 {
! CHECK: %[[FCTRES:.*]] = fir.alloca f32 {bindc_name = "negr", uniq_name = "_QFnegrEnegr"}
! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref<f32>
! CHECK: %[[NEG:.*]] = arith.negf %[[A_VAL]] : f32
! CHECK: fir.store %[[NEG]] to %[[FCTRES]] : !fir.ref<f32>
! CHECK: %[[RET:.*]] = fir.load %[[FCTRES]] : !fir.ref<f32>
! CHECK: return %[[RET]] : f32
complex function negc(a)
complex :: a
negc = -a
end
! CHECK-LABEL: func @_QPnegc(
! CHECK-SAME: %[[A:.*]]: !fir.ref<!fir.complex<4>> {fir.bindc_name = "a"}) -> !fir.complex<4> {
! CHECK: %[[FCTRES:.*]] = fir.alloca !fir.complex<4> {bindc_name = "negc", uniq_name = "_QFnegcEnegc"}
! CHECK: %[[A_VAL:.*]] = fir.load %[[A]] : !fir.ref<!fir.complex<4>>
! CHECK: %[[NEG:.*]] = fir.negc %[[A_VAL]] : !fir.complex<4>
! CHECK: fir.store %[[NEG]] to %[[FCTRES]] : !fir.ref<!fir.complex<4>>
! CHECK: %[[RET:.*]] = fir.load %[[FCTRES]] : !fir.ref<!fir.complex<4>>
! CHECK: return %[[RET]] : !fir.complex<4>