Skip to content

Commit bdeccd9

Browse files
committed
[mlir] ArithToLLVM: fix memref bitcast lowering
arith.bitcast is allowed on memrefs and such code can actually be generated by IREE `ConvertBf16ArithToF32Pass`. `LLVM::detail::vectorOneToOneRewrite` doesn't properly check its types and will generate bitcast between structs which is illegal. With the opaque pointers this is a no-op operation for memref so we can just add type check in `LLVM::detail::vectorOneToOneRewrite` and add a separate pattern which removes op if converted types are the same.
1 parent 7aabbf2 commit bdeccd9

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@ struct ConstrainedVectorConvertToLLVMPattern
5454
}
5555
};
5656

57+
/// No-op bitcast.
58+
struct IdentityBitcastLowering final
59+
: public OpConversionPattern<arith::BitcastOp> {
60+
using OpConversionPattern::OpConversionPattern;
61+
62+
LogicalResult
63+
matchAndRewrite(arith::BitcastOp op, OpAdaptor adaptor,
64+
ConversionPatternRewriter &rewriter) const final {
65+
Value src = adaptor.getIn();
66+
if (src.getType() != getTypeConverter()->convertType(op.getType()))
67+
return rewriter.notifyMatchFailure(op, "Types are different");
68+
69+
rewriter.replaceOp(op, src);
70+
return success();
71+
}
72+
};
73+
5774
//===----------------------------------------------------------------------===//
5875
// Straightforward Op Lowerings
5976
//===----------------------------------------------------------------------===//
@@ -524,6 +541,9 @@ void mlir::arith::registerConvertArithToLLVMInterface(
524541

525542
void mlir::arith::populateArithToLLVMConversionPatterns(
526543
const LLVMTypeConverter &converter, RewritePatternSet &patterns) {
544+
545+
patterns.add<IdentityBitcastLowering>(converter, patterns.getContext());
546+
527547
// clang-format off
528548
patterns.add<
529549
AddFOpLowering,

mlir/lib/Conversion/LLVMCommon/VectorPattern.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ LogicalResult LLVM::detail::handleMultidimensionalVectors(
103103
return success();
104104
}
105105

106+
static bool isVectorCompatibleType(Type type) {
107+
return isa<LLVM::LLVMArrayType, VectorType, IntegerType, FloatType>(type) &&
108+
LLVM::isCompatibleType(type);
109+
}
110+
106111
LogicalResult LLVM::detail::vectorOneToOneRewrite(
107112
Operation *op, StringRef targetOp, ValueRange operands,
108113
ArrayRef<NamedAttribute> targetAttrs,
@@ -111,7 +116,7 @@ LogicalResult LLVM::detail::vectorOneToOneRewrite(
111116
assert(!operands.empty());
112117

113118
// Cannot convert ops if their operands are not of LLVM type.
114-
if (!llvm::all_of(operands.getTypes(), isCompatibleType))
119+
if (!llvm::all_of(operands.getTypes(), isVectorCompatibleType))
115120
return failure();
116121

117122
auto llvmNDVectorTy = operands[0].getType();

mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,3 +727,15 @@ func.func @ops_supporting_overflow(%arg0: i64, %arg1: i64) {
727727
%3 = arith.shli %arg0, %arg1 overflow<nsw, nuw> : i64
728728
return
729729
}
730+
731+
// -----
732+
733+
// CHECK-LABEL: func @memref_bitcast
734+
// CHECK-SAME: (%[[ARG:.*]]: memref<?xi16>)
735+
// CHECK: %[[V1:.*]] = builtin.unrealized_conversion_cast %[[ARG]] : memref<?xi16> to !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)>
736+
// CHECK: %[[V2:.*]] = builtin.unrealized_conversion_cast %[[V1]] : !llvm.struct<(ptr, ptr, i64, array<1 x i64>, array<1 x i64>)> to memref<?xbf16>
737+
// CHECK: return %[[V2]]
738+
func.func @memref_bitcast(%1: memref<?xi16>) -> memref<?xbf16> {
739+
%2 = arith.bitcast %1 : memref<?xi16> to memref<?xbf16>
740+
func.return %2 : memref<?xbf16>
741+
}

0 commit comments

Comments
 (0)