[mlir] do not emit bitcasts between structs in StandardToLLVM

The convresion of memref cast operaitons from the Standard dialect to the LLVM
dialect has been emitting bitcasts from a struct type to itself. Beyond being
useless, such casts are invalid as bitcast does not operate on aggregate types.
This kept working by accident because LLVM IR bitcast construction API skips
the construction if types are equal before it verifies that the types are
acceptable in a bitcast. Do not emit such bitcasts, the memref cast that only
adds/erases size information is in fact a noop on the current descriptor as it
always contains dynamic values for all sizes.

Reviewed By: pifon2a

Differential Revision: https://reviews.llvm.org/D85899
This commit is contained in:
Alex Zinenko 2020-08-13 14:45:59 +02:00
parent 215c2df647
commit 339eba0805
2 changed files with 12 additions and 12 deletions

View File

@ -2278,11 +2278,11 @@ struct MemRefCastOpLowering : public ConvertOpToLLVMPattern<MemRefCastOp> {
auto targetStructType = typeConverter.convertType(memRefCastOp.getType()); auto targetStructType = typeConverter.convertType(memRefCastOp.getType());
auto loc = op->getLoc(); auto loc = op->getLoc();
// MemRefCastOp reduce to bitcast in the ranked MemRef case. // For ranked/ranked case, just keep the original descriptor.
if (srcType.isa<MemRefType>() && dstType.isa<MemRefType>()) { if (srcType.isa<MemRefType>() && dstType.isa<MemRefType>())
rewriter.replaceOpWithNewOp<LLVM::BitcastOp>(op, targetStructType, return rewriter.replaceOp(op, {transformed.source()});
transformed.source());
} else if (srcType.isa<MemRefType>() && dstType.isa<UnrankedMemRefType>()) { if (srcType.isa<MemRefType>() && dstType.isa<UnrankedMemRefType>()) {
// Casting ranked to unranked memref type // Casting ranked to unranked memref type
// Set the rank in the destination from the memref type // Set the rank in the destination from the memref type
// Allocate space on the stack and copy the src memref descriptor // Allocate space on the stack and copy the src memref descriptor

View File

@ -324,49 +324,49 @@ func @mixed_store(%mixed : memref<42x?xf32>, %i : index, %j : index, %val : f32)
// CHECK-LABEL: func @memref_cast_static_to_dynamic // CHECK-LABEL: func @memref_cast_static_to_dynamic
func @memref_cast_static_to_dynamic(%static : memref<10x42xf32>) { func @memref_cast_static_to_dynamic(%static : memref<10x42xf32>) {
// CHECK: llvm.bitcast %{{.*}} : !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> to !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> // CHECK-NOT: llvm.bitcast
%0 = memref_cast %static : memref<10x42xf32> to memref<?x?xf32> %0 = memref_cast %static : memref<10x42xf32> to memref<?x?xf32>
return return
} }
// CHECK-LABEL: func @memref_cast_static_to_mixed // CHECK-LABEL: func @memref_cast_static_to_mixed
func @memref_cast_static_to_mixed(%static : memref<10x42xf32>) { func @memref_cast_static_to_mixed(%static : memref<10x42xf32>) {
// CHECK: llvm.bitcast %{{.*}} : !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> to !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> // CHECK-NOT: llvm.bitcast
%0 = memref_cast %static : memref<10x42xf32> to memref<?x42xf32> %0 = memref_cast %static : memref<10x42xf32> to memref<?x42xf32>
return return
} }
// CHECK-LABEL: func @memref_cast_dynamic_to_static // CHECK-LABEL: func @memref_cast_dynamic_to_static
func @memref_cast_dynamic_to_static(%dynamic : memref<?x?xf32>) { func @memref_cast_dynamic_to_static(%dynamic : memref<?x?xf32>) {
// CHECK: llvm.bitcast %{{.*}} : !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> to !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> // CHECK-NOT: llvm.bitcast
%0 = memref_cast %dynamic : memref<?x?xf32> to memref<10x12xf32> %0 = memref_cast %dynamic : memref<?x?xf32> to memref<10x12xf32>
return return
} }
// CHECK-LABEL: func @memref_cast_dynamic_to_mixed // CHECK-LABEL: func @memref_cast_dynamic_to_mixed
func @memref_cast_dynamic_to_mixed(%dynamic : memref<?x?xf32>) { func @memref_cast_dynamic_to_mixed(%dynamic : memref<?x?xf32>) {
// CHECK: llvm.bitcast %{{.*}} : !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> to !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> // CHECK-NOT: llvm.bitcast
%0 = memref_cast %dynamic : memref<?x?xf32> to memref<?x12xf32> %0 = memref_cast %dynamic : memref<?x?xf32> to memref<?x12xf32>
return return
} }
// CHECK-LABEL: func @memref_cast_mixed_to_dynamic // CHECK-LABEL: func @memref_cast_mixed_to_dynamic
func @memref_cast_mixed_to_dynamic(%mixed : memref<42x?xf32>) { func @memref_cast_mixed_to_dynamic(%mixed : memref<42x?xf32>) {
// CHECK: llvm.bitcast %{{.*}} : !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> to !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> // CHECK-NOT: llvm.bitcast
%0 = memref_cast %mixed : memref<42x?xf32> to memref<?x?xf32> %0 = memref_cast %mixed : memref<42x?xf32> to memref<?x?xf32>
return return
} }
// CHECK-LABEL: func @memref_cast_mixed_to_static // CHECK-LABEL: func @memref_cast_mixed_to_static
func @memref_cast_mixed_to_static(%mixed : memref<42x?xf32>) { func @memref_cast_mixed_to_static(%mixed : memref<42x?xf32>) {
// CHECK: llvm.bitcast %{{.*}} : !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> to !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> // CHECK-NOT: llvm.bitcast
%0 = memref_cast %mixed : memref<42x?xf32> to memref<42x1xf32> %0 = memref_cast %mixed : memref<42x?xf32> to memref<42x1xf32>
return return
} }
// CHECK-LABEL: func @memref_cast_mixed_to_mixed // CHECK-LABEL: func @memref_cast_mixed_to_mixed
func @memref_cast_mixed_to_mixed(%mixed : memref<42x?xf32>) { func @memref_cast_mixed_to_mixed(%mixed : memref<42x?xf32>) {
// CHECK: llvm.bitcast %{{.*}} : !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> to !llvm.struct<(ptr<float>, ptr<float>, i64, array<2 x i64>, array<2 x i64>)> // CHECK-NOT: llvm.bitcast
%0 = memref_cast %mixed : memref<42x?xf32> to memref<?x1xf32> %0 = memref_cast %mixed : memref<42x?xf32> to memref<?x1xf32>
return return
} }