[PATCH] D120071: [flang] Lower scalar negation
Valentin Clement via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 17 09:59:05 PST 2022
clementval created this revision.
clementval added reviewers: jeanPerier, PeteSteinfeld, schweitz, svedanayagam, klausler.
Herald added a subscriber: mehdi_amini.
Herald added a project: Flang.
clementval requested review of this revision.
Herald added subscribers: llvm-commits, jdoerfert.
Herald added a project: LLVM.
Handle negation on scalar expression.
res = -a
This patch is part of the upstreaming effort from fir-dev branch.
Co-authored-by: Jean Perier <jperier at nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D120071
Files:
flang/lib/Lower/ConvertExpr.cpp
flang/test/Lower/assignment.f90
Index: flang/test/Lower/assignment.f90
===================================================================
--- flang/test/Lower/assignment.f90
+++ flang/test/Lower/assignment.f90
@@ -22,3 +22,46 @@
! 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>
Index: flang/lib/Lower/ConvertExpr.cpp
===================================================================
--- flang/lib/Lower/ConvertExpr.cpp
+++ flang/lib/Lower/ConvertExpr.cpp
@@ -189,18 +189,21 @@
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120071.409703.patch
Type: text/x-patch
Size: 3583 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220217/da17522f/attachment.bin>
More information about the llvm-commits
mailing list