[flang-commits] [flang] a7a6135 - [fir] Add fir.zero_bits conversion pattern to LLVM IR dialect

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Tue Nov 2 08:54:43 PDT 2021


Author: Valentin Clement
Date: 2021-11-02T16:54:34+01:00
New Revision: a7a61359253c8e288f239c11b34be802a9d07566

URL: https://github.com/llvm/llvm-project/commit/a7a61359253c8e288f239c11b34be802a9d07566
DIFF: https://github.com/llvm/llvm-project/commit/a7a61359253c8e288f239c11b34be802a9d07566.diff

LOG: [fir] Add fir.zero_bits conversion pattern to LLVM IR dialect

This patch adds the ZeroOpConversion pattern to LLVM IR dialect.
Conversion of aggregate types is not implemented yet and will trigger a
failure to legalize the operation. This is tested in the
convert-to-llvm-invalid.fir test file.

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

Reviewed By: kiranchandramohan

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

Co-authored-by: Jean Perier <jperier at nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>

Added: 
    flang/test/Fir/convert-to-llvm-invalid.fir

Modified: 
    flang/lib/Optimizer/CodeGen/CodeGen.cpp
    flang/test/Fir/convert-to-llvm.fir

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index c92948309dde6..ce4860c8c0c69 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -166,6 +166,30 @@ struct UndefOpConversion : public FIROpConversion<fir::UndefOp> {
     return success();
   }
 };
+
+struct ZeroOpConversion : public FIROpConversion<fir::ZeroOp> {
+  using FIROpConversion::FIROpConversion;
+
+  mlir::LogicalResult
+  matchAndRewrite(fir::ZeroOp zero, OpAdaptor,
+                  mlir::ConversionPatternRewriter &rewriter) const override {
+    auto ty = convertType(zero.getType());
+    if (ty.isa<mlir::LLVM::LLVMPointerType>()) {
+      rewriter.replaceOpWithNewOp<mlir::LLVM::NullOp>(zero, ty);
+    } else if (ty.isa<mlir::IntegerType>()) {
+      rewriter.replaceOpWithNewOp<mlir::LLVM::ConstantOp>(
+          zero, ty, mlir::IntegerAttr::get(zero.getType(), 0));
+    } else if (mlir::LLVM::isCompatibleFloatingPointType(ty)) {
+      rewriter.replaceOpWithNewOp<mlir::LLVM::ConstantOp>(
+          zero, ty, mlir::FloatAttr::get(zero.getType(), 0.0));
+    } else {
+      // TODO: create ConstantAggregateZero for FIR aggregate/array types.
+      return zero.emitOpError(
+          "conversion of fir.zero with aggregate type not implemented yet");
+    }
+    return success();
+  }
+};
 } // namespace
 
 namespace {
@@ -182,10 +206,9 @@ class FIRToLLVMLowering : public fir::FIRToLLVMLoweringBase<FIRToLLVMLowering> {
   void runOnOperation() override final {
     auto *context = getModule().getContext();
     fir::LLVMTypeConverter typeConverter{getModule()};
-    auto loc = mlir::UnknownLoc::get(context);
     mlir::OwningRewritePatternList pattern(context);
     pattern.insert<AddrOfOpConversion, HasValueOpConversion, GlobalOpConversion,
-                   UndefOpConversion>(typeConverter);
+                   UndefOpConversion, ZeroOpConversion>(typeConverter);
     mlir::populateStdToLLVMConversionPatterns(typeConverter, pattern);
     mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter,
                                                             pattern);
@@ -198,7 +221,6 @@ class FIRToLLVMLowering : public fir::FIRToLLVMLoweringBase<FIRToLLVMLowering> {
     // apply the patterns
     if (mlir::failed(mlir::applyFullConversion(getModule(), target,
                                                std::move(pattern)))) {
-      mlir::emitError(loc, "error in converting to LLVM-IR dialect\n");
       signalPassFailure();
     }
   }

diff  --git a/flang/test/Fir/convert-to-llvm-invalid.fir b/flang/test/Fir/convert-to-llvm-invalid.fir
new file mode 100644
index 0000000000000..00db627d05be9
--- /dev/null
+++ b/flang/test/Fir/convert-to-llvm-invalid.fir
@@ -0,0 +1,11 @@
+// Test FIR to LLVM IR conversion invalid cases and diagnostics.
+
+// RUN: fir-opt --split-input-file --fir-to-llvm-ir --verify-diagnostics %s
+
+func @zero_aggregate() {
+  // expected-error at +2{{'fir.zero_bits' op conversion of fir.zero with aggregate type not implemented yet}}
+  // expected-error at +1{{failed to legalize operation 'fir.zero_bits'}}
+  %a = fir.zero_bits !fir.array<10xf32>
+  return
+}
+

diff  --git a/flang/test/Fir/convert-to-llvm.fir b/flang/test/Fir/convert-to-llvm.fir
index 9e1b02590f193..a20037beeb8c7 100644
--- a/flang/test/Fir/convert-to-llvm.fir
+++ b/flang/test/Fir/convert-to-llvm.fir
@@ -81,3 +81,55 @@ fir.global internal @_QEmultiarray : !fir.array<32x32xi32> {
 // CHECK:   %[[CST:.*]] = llvm.mlir.constant(dense<1> : vector<32x32xi32>) : !llvm.array<32 x array<32 x i32>>
 // CHECK:   llvm.return %[[CST]] : !llvm.array<32 x array<32 x i32>>
 // CHECK: }
+
+// -----
+
+// Test fir.zero_bits operation with LLVM ptr type
+
+func @zero_test_ptr() {
+  %z = fir.zero_bits !llvm.ptr<f32>
+  return
+}
+
+// CHECK: %{{.*}} = llvm.mlir.null : !llvm.ptr<f32>
+// CHECK-NOT: fir.zero_bits
+
+// -----
+
+// Test fir.zero_bits operation with integer type.
+
+func @zero_test_integer() {
+  %z0 = fir.zero_bits i8
+  %z1 = fir.zero_bits i16
+  %z2 = fir.zero_bits i32
+  %z3 = fir.zero_bits i64
+  return
+}
+
+// CHECK: %{{.*}} = llvm.mlir.constant(0 : i8) : i8
+// CHECK: %{{.*}} = llvm.mlir.constant(0 : i16) : i16
+// CHECK: %{{.*}} = llvm.mlir.constant(0 : i32) : i32
+// CHECK: %{{.*}} = llvm.mlir.constant(0 : i64) : i64
+// CHECK-NOT: fir.zero_bits
+
+// -----
+
+// Test fir.zero_bits operation with floating points types.
+
+func @zero_test_float() {
+  %z0 = fir.zero_bits f16
+  %z1 = fir.zero_bits bf16
+  %z2 = fir.zero_bits f32
+  %z3 = fir.zero_bits f64
+  %z4 = fir.zero_bits f80
+  %z5 = fir.zero_bits f128
+  return
+}
+
+// CHECK: %{{.*}} = llvm.mlir.constant(0.000000e+00 : f16) : f16
+// CHECK: %{{.*}} = llvm.mlir.constant(0.000000e+00 : bf16) : bf16
+// CHECK: %{{.*}} = llvm.mlir.constant(0.000000e+00 : f32) : f32
+// CHECK: %{{.*}} = llvm.mlir.constant(0.000000e+00 : f64) : f64
+// CHECK: %{{.*}} = llvm.mlir.constant(0.000000e+00 : f80) : f80
+// CHECK: %{{.*}} = llvm.mlir.constant(0.000000e+00 : f128) : f128
+// CHECK-NOT: fir.zero_bits


        


More information about the flang-commits mailing list