[flang-commits] [flang] eafafba - [flang] Lower scalar negation
Valentin Clement via flang-commits
flang-commits at lists.llvm.org
Thu Feb 17 23:09:17 PST 2022
Author: Valentin Clement
Date: 2022-02-18T08:09:09+01:00
New Revision: eafafbae92f4d68b432989ee01ff38aa9e187a5f
URL: https://github.com/llvm/llvm-project/commit/eafafbae92f4d68b432989ee01ff38aa9e187a5f
DIFF: https://github.com/llvm/llvm-project/commit/eafafbae92f4d68b432989ee01ff38aa9e187a5f.diff
LOG: [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 at nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>
Added:
Modified:
flang/lib/Lower/ConvertExpr.cpp
flang/test/Lower/assignment.f90
Removed:
################################################################################
diff --git a/flang/lib/Lower/ConvertExpr.cpp b/flang/lib/Lower/ConvertExpr.cpp
index 497d1eaf06a0b..466a74fe031eb 100644
--- a/flang/lib/Lower/ConvertExpr.cpp
+++ b/flang/lib/Lower/ConvertExpr.cpp
@@ -189,18 +189,21 @@ class ScalarExprLowering {
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
diff --git a/flang/test/Lower/assignment.f90 b/flang/test/Lower/assignment.f90
index 6cb2e32095cee..32c2086de7de2 100644
--- a/flang/test/Lower/assignment.f90
+++ b/flang/test/Lower/assignment.f90
@@ -22,3 +22,46 @@ subroutine sub2(a, b)
! 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>
More information about the flang-commits
mailing list