Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,18 @@ OpFoldResult CastOp::fold(FoldAdaptor adaptor) {
}
}

// Fold cast from bf16 -> f32 -> bf16 into no-op.
if (auto cast = getInput().getDefiningOp<CastOp>()) {
auto sourceElTy = cast.getInput().getType().getElementType();
auto intermediateElTy = cast.getType().getElementType();
auto finalElTy = getType().getElementType();
if (isa<BFloat16Type>(sourceElTy) && isa<Float32Type>(intermediateElTy) &&
isa<BFloat16Type>(finalElTy)) {
getInputMutable().assign(cast.getInput());
return getResult();
}
}

auto operand = llvm::dyn_cast_if_present<ElementsAttr>(adaptor.getInput());
if (!operand)
return {};
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/Dialect/Tosa/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func.func @cast_fold_double(%arg0: tensor<?x1xf32>) -> tensor<?x1xi8> {
return %1 : tensor<?x1xi8>
}

// CHECK-LABEL: @cast_fold_double
func.func @cast_fold_double2(%arg0: tensor<?x1xbf16>) -> tensor<?x1xbf16> {
// CHECK: return %arg0
%0 = tosa.cast %arg0 : (tensor<?x1xbf16>) -> tensor<?x1xf32>
%1 = tosa.cast %0 : (tensor<?x1xf32>) -> tensor<?x1xbf16>
return %1 : tensor<?x1xbf16>
}

// CHECK-LABEL: @cast_no_fold_double1
func.func @cast_no_fold_double1(%arg0: tensor<?x1xf32>) -> tensor<?x1xi8> {
// CHECK: tosa.cast{{.*}} (tensor<?x1xf32>) -> tensor<?x1xui16>
Expand Down
Loading