-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sub-byte emulation support to llvm-cpu and vulkan backend. (#14488)
Based off of ea49d2b Fixes #14481 --------- Co-authored-by: Hanhan Wang <[email protected]> Co-authored-by: yzhang93 <[email protected]>
- Loading branch information
1 parent
73b00a0
commit a83e543
Showing
12 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
compiler/src/iree/compiler/Codegen/Common/EmulateNarrowType.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2023 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
#include "iree/compiler/Codegen/Common/PassDetail.h" | ||
#include "iree/compiler/Codegen/Common/Passes.h" | ||
#include "iree/compiler/Dialect/HAL/IR/HALDialect.h" | ||
#include "iree/compiler/Dialect/HAL/IR/HALOps.h" | ||
#include "llvm/Support/FormatVariadic.h" | ||
#include "mlir/Dialect/Affine/IR/AffineOps.h" | ||
#include "mlir/Dialect/Arith/IR/Arith.h" | ||
#include "mlir/Dialect/Arith/Transforms/NarrowTypeEmulationConverter.h" | ||
#include "mlir/Dialect/Arith/Transforms/Passes.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Dialect/MemRef/IR/MemRef.h" | ||
#include "mlir/Dialect/MemRef/Transforms/Transforms.h" | ||
#include "mlir/Dialect/Vector/IR/VectorOps.h" | ||
#include "mlir/Dialect/Vector/Transforms/VectorTransforms.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
namespace mlir { | ||
namespace iree_compiler { | ||
|
||
namespace { | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Conversion patterns | ||
//===----------------------------------------------------------------------===// | ||
|
||
struct ConvertHalInterfaceBindingSubspan final | ||
: OpConversionPattern<IREE::HAL::InterfaceBindingSubspanOp> { | ||
using OpConversionPattern::OpConversionPattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(IREE::HAL::InterfaceBindingSubspanOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
Type newResultTy = getTypeConverter()->convertType(op.getType()); | ||
if (!newResultTy) | ||
return rewriter.notifyMatchFailure( | ||
op->getLoc(), | ||
llvm::formatv("failed to legalize memref type: {0}", op.getType())); | ||
|
||
rewriter.replaceOpWithNewOp<IREE::HAL::InterfaceBindingSubspanOp>( | ||
op, newResultTy, adaptor.getSet(), adaptor.getBinding(), | ||
adaptor.getDescriptorType(), adaptor.getByteOffset(), | ||
adaptor.getDynamicDims(), adaptor.getAlignmentAttr(), | ||
adaptor.getDescriptorFlagsAttr()); | ||
return success(); | ||
} | ||
}; | ||
|
||
static void populateIreeNarrowTypeEmulationPatterns( | ||
arith::NarrowTypeEmulationConverter &converter, | ||
RewritePatternSet &patterns) { | ||
patterns.add<ConvertHalInterfaceBindingSubspan>(converter, | ||
patterns.getContext()); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Pass Definition | ||
//===----------------------------------------------------------------------===// | ||
|
||
struct EmulateNarrowTypePass | ||
: public EmulateNarrowTypeBase<EmulateNarrowTypePass> { | ||
void getDependentDialects(DialectRegistry ®istry) const override { | ||
registry.insert<arith::ArithDialect, func::FuncDialect, | ||
memref::MemRefDialect, vector::VectorDialect, | ||
affine::AffineDialect, IREE::HAL::HALDialect>(); | ||
} | ||
|
||
void runOnOperation() override { | ||
// The number of bits used in a load/store op. | ||
constexpr unsigned kLoadStoreEmulateBitwidth = 8; | ||
static_assert( | ||
llvm::isPowerOf2_32(kLoadStoreEmulateBitwidth) && | ||
"only power of 2 is supported for narrow type load/store emulation"); | ||
|
||
MLIRContext *ctx = &getContext(); | ||
|
||
arith::NarrowTypeEmulationConverter typeConverter( | ||
kLoadStoreEmulateBitwidth); | ||
memref::populateMemRefNarrowTypeEmulationConversions(typeConverter); | ||
|
||
ConversionTarget target(*ctx); | ||
target.addDynamicallyLegalOp<func::FuncOp>([&typeConverter](Operation *op) { | ||
return typeConverter.isLegal(cast<func::FuncOp>(op).getFunctionType()); | ||
}); | ||
auto opLegalCallback = [&typeConverter](Operation *op) { | ||
return typeConverter.isLegal(op); | ||
}; | ||
target.addDynamicallyLegalOp<func::CallOp, func::ReturnOp>(opLegalCallback); | ||
target.addDynamicallyLegalDialect< | ||
arith::ArithDialect, vector::VectorDialect, memref::MemRefDialect, | ||
affine::AffineDialect, IREE::HAL::HALDialect>(opLegalCallback); | ||
|
||
RewritePatternSet patterns(ctx); | ||
arith::populateArithNarrowTypeEmulationPatterns(typeConverter, patterns); | ||
memref::populateMemRefNarrowTypeEmulationPatterns(typeConverter, patterns); | ||
vector::populateVectorNarrowTypeEmulationPatterns(typeConverter, patterns); | ||
populateIreeNarrowTypeEmulationPatterns(typeConverter, patterns); | ||
|
||
if (failed(applyPartialConversion(getOperation(), target, | ||
std::move(patterns)))) | ||
return signalPassFailure(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Public interface | ||
//===----------------------------------------------------------------------===// | ||
|
||
std::unique_ptr<OperationPass<ModuleOp>> createEmulateNarrowTypePass() { | ||
return std::make_unique<EmulateNarrowTypePass>(); | ||
} | ||
|
||
} // namespace iree_compiler | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
compiler/src/iree/compiler/Codegen/Common/test/emulate_narrow_type.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// RUN: iree-opt --split-input-file --iree-codegen-emulate-narrow-type %s | FileCheck %s | ||
|
||
func.func @memref_i4_to_i8() { | ||
%c0 = arith.constant 0 : index | ||
%0 = hal.interface.binding.subspan set(0) binding(0) type(storage_buffer) alignment(64) offset(%c0) flags(ReadOnly) : memref<8xi4> | ||
%1 = hal.interface.binding.subspan set(0) binding(1) type(storage_buffer) alignment(64) offset(%c0) : memref<8xf32> | ||
return | ||
} | ||
// CHECK-LABEL: func.func @memref_i4_to_i8 | ||
// CHECK: hal.interface.binding.subspan {{.+}} memref<8xi8> | ||
// CHECK: hal.interface.binding.subspan {{.+}} memref<8xf32> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#map = affine_map<(d0) -> (d0)> | ||
func.func @i4_to_f32() { | ||
%input = util.unfoldable_constant dense<[0, 1, 2, 3, 4, 5, 6, 7]> : tensor<8xi4> | ||
%0 = tensor.empty() : tensor<8xf32> | ||
%res = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel"]} | ||
ins(%input : tensor<8xi4>) outs(%0 : tensor<8xf32>) { | ||
^bb0(%in: i4, %out: f32): | ||
%2 = arith.extui %in : i4 to i32 | ||
%3 = arith.uitofp %2 : i32 to f32 | ||
linalg.yield %3 : f32 | ||
} -> tensor<8xf32> | ||
check.expect_eq_const(%res, dense<[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]> : tensor<8xf32>) : tensor<8xf32> | ||
return | ||
} |