1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
// RUN: mlir-opt %s --pass-pipeline="builtin.module(llvm.func(sroa))" --split-input-file | FileCheck %s
// CHECK-LABEL: llvm.func @basic_struct
llvm.func @basic_struct() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x i32
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%2 = llvm.getelementptr inbounds %1[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, i32)>
// CHECK: %[[RES:.*]] = llvm.load %[[ALLOCA]]
%3 = llvm.load %2 : !llvm.ptr -> i32
// CHECK: llvm.return %[[RES]] : i32
llvm.return %3 : i32
}
// -----
// CHECK-LABEL: llvm.func @basic_array
llvm.func @basic_array() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x i32
%1 = llvm.alloca %0 x !llvm.array<10 x i32> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%2 = llvm.getelementptr inbounds %1[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<10 x i32>
// CHECK: %[[RES:.*]] = llvm.load %[[ALLOCA]]
%3 = llvm.load %2 : !llvm.ptr -> i32
// CHECK: llvm.return %[[RES]] : i32
llvm.return %3 : i32
}
// -----
// CHECK-LABEL: llvm.func @multi_level_direct
llvm.func @multi_level_direct() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x i32
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, struct<"bar", (i8, array<10 x array<10 x i32>>, i8)>)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%2 = llvm.getelementptr inbounds %1[0, 2, 1, 5, 8] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, struct<"bar", (i8, array<10 x array<10 x i32>>, i8)>)>
// CHECK: %[[RES:.*]] = llvm.load %[[ALLOCA]]
%3 = llvm.load %2 : !llvm.ptr -> i32
// CHECK: llvm.return %[[RES]] : i32
llvm.return %3 : i32
}
// -----
// The first application of SROA would generate a GEP with indices [0, 0]. This
// test ensures this GEP is not eliminated during the first application. Even
// though doing it would be correct, it would prevent the second application
// of SROA to eliminate the array. GEPs should be eliminated only when they are
// truly trivial (with indices [0]).
// CHECK-LABEL: llvm.func @multi_level_direct_two_applications
llvm.func @multi_level_direct_two_applications() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x i32
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, array<10 x i32>, i8)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%2 = llvm.getelementptr inbounds %1[0, 2, 0] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, array<10 x i32>, i8)>
// CHECK: %[[RES:.*]] = llvm.load %[[ALLOCA]]
%3 = llvm.load %2 : !llvm.ptr -> i32
// CHECK: llvm.return %[[RES]] : i32
llvm.return %3 : i32
}
// -----
// CHECK-LABEL: llvm.func @multi_level_indirect
llvm.func @multi_level_indirect() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x i32
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, struct<"bar", (i8, array<10 x array<10 x i32>>, i8)>)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%2 = llvm.getelementptr inbounds %1[0, 2, 1, 5] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, struct<"bar", (i8, array<10 x array<10 x i32>>, i8)>)>
%3 = llvm.getelementptr inbounds %2[0, 8] : (!llvm.ptr) -> !llvm.ptr, !llvm.array<10 x i32>
// CHECK: %[[RES:.*]] = llvm.load %[[ALLOCA]]
%4 = llvm.load %3 : !llvm.ptr -> i32
// CHECK: llvm.return %[[RES]] : i32
llvm.return %4 : i32
}
// -----
// CHECK-LABEL: llvm.func @resolve_alias
// CHECK-SAME: (%[[ARG:.*]]: i32)
llvm.func @resolve_alias(%arg: i32) -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x i32
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%2 = llvm.getelementptr %1[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, i32)>
%3 = llvm.getelementptr inbounds %1[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, i32)>
// CHECK: llvm.store %[[ARG]], %[[ALLOCA]]
llvm.store %arg, %2 : i32, !llvm.ptr
// CHECK: %[[RES:.*]] = llvm.load %[[ALLOCA]]
%4 = llvm.load %3 : !llvm.ptr -> i32
// CHECK: llvm.return %[[RES]] : i32
llvm.return %4 : i32
}
// -----
// CHECK-LABEL: llvm.func @no_non_single_support
llvm.func @no_non_single_support() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant
%0 = llvm.mlir.constant(2 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
// CHECK-NOT: = llvm.alloca
%2 = llvm.getelementptr inbounds %1[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, i32)>
%3 = llvm.load %2 : !llvm.ptr -> i32
llvm.return %3 : i32
}
// -----
// CHECK-LABEL: llvm.func @no_pointer_indexing
llvm.func @no_pointer_indexing() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
// CHECK-NOT: = llvm.alloca
%2 = llvm.getelementptr %1[1, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, i32)>
%3 = llvm.load %2 : !llvm.ptr -> i32
llvm.return %3 : i32
}
// -----
// CHECK-LABEL: llvm.func @no_direct_use
llvm.func @no_direct_use() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
// CHECK-NOT: = llvm.alloca
%2 = llvm.getelementptr %1[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, i32)>
%3 = llvm.load %2 : !llvm.ptr -> i32
llvm.call @use(%1) : (!llvm.ptr) -> ()
llvm.return %3 : i32
}
llvm.func @use(!llvm.ptr)
// -----
// CHECK-LABEL: llvm.func @direct_promotable_use_is_fine
llvm.func @direct_promotable_use_is_fine() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x i32
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%2 = llvm.getelementptr %1[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, i32)>
// CHECK: %[[RES:.*]] = llvm.load %[[ALLOCA]]
%3 = llvm.load %2 : !llvm.ptr -> i32
// This is a direct use of the slot but it can be removed because it implements PromotableOpInterface.
llvm.intr.lifetime.start 2, %1 : !llvm.ptr
// CHECK: llvm.return %[[RES]] : i32
llvm.return %3 : i32
}
// -----
// CHECK-LABEL: llvm.func @direct_promotable_use_is_fine_on_accessor
llvm.func @direct_promotable_use_is_fine_on_accessor() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x i32
%1 = llvm.alloca %0 x !llvm.struct<"foo", (i32, f64, i32)> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%2 = llvm.getelementptr %1[0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<"foo", (i32, f64, i32)>
// CHECK: %[[RES:.*]] = llvm.load %[[ALLOCA]]
%3 = llvm.load %2 : !llvm.ptr -> i32
// This does not provide side-effect info but it can be removed because it implements PromotableOpInterface.
llvm.intr.lifetime.start 2, %2 : !llvm.ptr
// CHECK: llvm.return %[[RES]] : i32
llvm.return %3 : i32
}
// -----
// CHECK-LABEL: llvm.func @no_dynamic_indexing
// CHECK-SAME: (%[[ARG:.*]]: i32)
llvm.func @no_dynamic_indexing(%arg: i32) -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x !llvm.array<10 x i32> {alignment = 8 : i64} : (i32) -> !llvm.ptr
%1 = llvm.alloca %0 x !llvm.array<10 x i32> {alignment = 8 : i64} : (i32) -> !llvm.ptr
// CHECK-NOT: = llvm.alloca
// CHECK: %[[GEP:.*]] = llvm.getelementptr %[[ALLOCA]][0, %[[ARG]]]
%2 = llvm.getelementptr %1[0, %arg] : (!llvm.ptr, i32) -> !llvm.ptr, !llvm.array<10 x i32>
// CHECK: %[[RES:.*]] = llvm.load %[[GEP]]
%3 = llvm.load %2 : !llvm.ptr -> i32
// CHECK: llvm.return %[[RES]] : i32
llvm.return %3 : i32
}
// -----
// CHECK-LABEL: llvm.func @no_typed_pointers
llvm.func @no_typed_pointers() -> i32 {
// CHECK: %[[SIZE:.*]] = llvm.mlir.constant(1 : i32)
%0 = llvm.mlir.constant(1 : i32) : i32
// CHECK: %[[ALLOCA:.*]] = llvm.alloca %[[SIZE]] x !llvm.array<10 x i32> {alignment = 8 : i64} : (i32) -> !llvm.ptr<array<10 x i32>>
%1 = llvm.alloca %0 x !llvm.array<10 x i32> {alignment = 8 : i64} : (i32) -> !llvm.ptr<array<10 x i32>>
// CHECK-NOT: = llvm.alloca
%2 = llvm.getelementptr %1[0, 1] : (!llvm.ptr<array<10 x i32>>) -> !llvm.ptr<i32>
%3 = llvm.load %2 : !llvm.ptr<i32>
llvm.return %3 : i32
}
|