24 #define GEN_PASS_DEF_STRIPFUNCQUANTTYPES
25 #include "mlir/Dialect/Quant/Transforms/Passes.h.inc"
29 class QuantizedTypeConverter :
public TypeConverter {
31 static Type convertQuantizedType(QuantizedType quantizedType) {
32 return quantizedType.getStorageType();
35 static Type convertTensorType(TensorType tensorType) {
36 if (
auto quantizedType =
37 dyn_cast<QuantizedType>(tensorType.getElementType()))
38 return tensorType.clone(convertQuantizedType(quantizedType));
43 ValueRange inputs, Location loc) {
44 return quant::StorageCastOp::create(builder, loc, type,
45 llvm::getSingleElement(inputs));
49 explicit QuantizedTypeConverter() {
50 addConversion([](Type type) {
return type; });
51 addConversion(convertQuantizedType);
52 addConversion(convertTensorType);
60 class StripFuncQuantTypes
61 :
public impl::StripFuncQuantTypesBase<StripFuncQuantTypes> {
64 void runOnOperation()
override {
66 auto moduleOp = cast<ModuleOp>(getOperation());
69 QuantizedTypeConverter typeConverter;
70 ConversionTarget target(*context);
75 target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
76 return typeConverter.isSignatureLegal(op.getFunctionType()) &&
77 typeConverter.isLegal(&op.getBody());
79 target.addDynamicallyLegalOp<func::ReturnOp>(
80 [&](func::ReturnOp op) {
return typeConverter.isLegal(op); });
81 target.addDynamicallyLegalOp<func::CallOp>(
82 [&](func::CallOp op) {
return typeConverter.isLegal(op); });
85 populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(
static MLIRContext * getContext(OpFoldResult val)
static Value materializeConversion(const DialectInlinerInterface *interface, SmallVectorImpl< Operation * > &castOps, OpBuilder &castBuilder, Value arg, Type type, Location conversionLoc)
Utility function used to generate a cast operation from the given interface, or return nullptr if a c...
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
void populateCallOpTypeConversionPattern(RewritePatternSet &patterns, const TypeConverter &converter)
Add a pattern to the given pattern list to convert the operand and result types of a CallOp with the ...
void populateReturnOpTypeConversionPattern(RewritePatternSet &patterns, const TypeConverter &converter)
Add a pattern to the given pattern list to rewrite return ops to use operands that have been legalize...
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.