[Mlir-commits] [mlir] [mlir][llvm] Add zeroinitializer constant (PR #65508)

Tobias Gysi llvmlistbot at llvm.org
Wed Sep 6 12:25:13 PDT 2023


================
@@ -2292,3 +2292,132 @@ llvm.func @locally_streaming_func() attributes {arm_locally_streaming} {
 }
 
 // CHECK: attributes #[[ATTR]] = { "aarch64_pstate_sm_body" }
+
+// -----
+
+//
+// Zero-initialize operation.
+//
+
+llvm.mlir.global linkonce @zero_integer() : i32 {
+  %0 = llvm.mlir.zero : i32
+  llvm.return %0 : i32
+}
+// CHECK: @zero_integer = linkonce global i32 0
+
+llvm.mlir.global linkonce @zero_float() : f32 {
+  %0 = llvm.mlir.zero : f32
+  llvm.return %0 : f32
+}
+// CHECK: @zero_float = linkonce global float 0.000000e+00
+
+llvm.mlir.global linkonce @zero_array() : !llvm.array<5 x i32> {
+  %0 = llvm.mlir.zero : !llvm.array<5 x i32>
+  llvm.return %0 : !llvm.array<5 x i32>
+}
+// CHECK: @zero_array = linkonce global [5 x i32] zeroinitializer
+
+llvm.mlir.global linkonce @zero_struct() : !llvm.struct<(i32, f64, i8)> {
+  %0 = llvm.mlir.zero : !llvm.struct<(i32, f64, i8)>
+  llvm.return %0 : !llvm.struct<(i32, f64, i8)>
+}
+// CHECK: @zero_struct = linkonce global { i32, double, i8 } zeroinitializer
+
+llvm.mlir.global linkonce @zero_ptr() : !llvm.ptr<i32> {
+  %0 = llvm.mlir.zero : !llvm.ptr<i32>
+  llvm.return %0 : !llvm.ptr<i32>
+}
+// CHECK: @zero_ptr = linkonce global ptr null
+
+llvm.mlir.global linkonce @zero_vector() : !llvm.vec<42 x ptr<i32>> {
+  %0 = llvm.mlir.zero : !llvm.vec<42 x ptr<i32>>
+  llvm.return %0 : !llvm.vec<42 x ptr<i32>>
+}
+// CHECK: @zero_vector = linkonce global <42 x ptr> zeroinitializer
+
+llvm.mlir.global linkonce @zero_nested() : !llvm.struct<(i32, !llvm.struct<(i32, i32)>, f64)> {
+  %0 = llvm.mlir.zero : !llvm.struct<(i32, !llvm.struct<(i32, i32)>, f64)>
+  llvm.return %0 : !llvm.struct<(i32, !llvm.struct<(i32, i32)>, f64)>
+}
+// CHECK: @zero_nested = linkonce global { i32, { i32, i32 }, double } zeroinitializer
+
+llvm.mlir.global linkonce @zero_func_ptr() : !llvm.ptr<func<i32 (i32)>> {
+  %0 = llvm.mlir.zero : !llvm.ptr<func<i32 (i32)>>
+  llvm.return %0 : !llvm.ptr<func<i32 (i32)>>
+}
+// CHECK: @zero_func_ptr = linkonce global ptr null
+
+llvm.mlir.global linkonce @zero_complex_type() : !llvm.array<5 x !llvm.struct<(i32, !llvm.array<3 x !llvm.ptr<i32>>) >> {
+  %0 = llvm.mlir.zero : !llvm.array<5 x !llvm.struct<(i32, !llvm.array<3 x !llvm.ptr<i32>>) >>
+  llvm.return %0 : !llvm.array<5 x !llvm.struct<(i32, !llvm.array<3 x !llvm.ptr<i32>>) >>
+}
+// CHECK: @zero_complex_type = linkonce global [5 x { i32, [3 x ptr] }] zeroinitializer
+
+llvm.func @local_zero_initialize() {
+  %0 = llvm.mlir.constant(1 : i64) : i64
+
+  // Integer type
+  %local_integer = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr<i32>
+  %1 = llvm.mlir.zero : i32
+  llvm.store %1, %local_integer : !llvm.ptr<i32>
+  // CHECK: %[[#V1:]] = alloca i32, i64 1, align 4
+  // CHECK: store i32 0, ptr %[[#V1]], align 4
----------------
gysit wrote:

```suggestion
  // CHECK: %[[#V1:]] = alloca i32, i64 1, align 4
  %local_integer = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr<i32>
  %1 = llvm.mlir.zero : i32
   // CHECK: store i32 0, ptr %[[#V1]], align 4
   llvm.store %1, %local_integer : !llvm.ptr<i32>
```
ultra nit: We usually prefer to put a file check comment right above the statement it matches. This interspersed view is a bit easier to read especially for larger tests.

https://github.com/llvm/llvm-project/pull/65508


More information about the Mlir-commits mailing list