29 #define GEN_PASS_DEF_STRIPFUNCQUANTTYPES
30 #include "mlir/Dialect/Quant/Transforms/Passes.h.inc"
34 class QuantizedTypeConverter :
public TypeConverter {
36 static Type convertQuantizedType(QuantizedType quantizedType) {
37 return quantizedType.getStorageType();
40 static Type convertTensorType(TensorType tensorType) {
41 if (
auto quantizedType =
42 dyn_cast<QuantizedType>(tensorType.getElementType()))
43 return tensorType.clone(convertQuantizedType(quantizedType));
48 ValueRange inputs, Location loc) {
49 return builder.create<quant::StorageCastOp>(loc, type,
50 llvm::getSingleElement(inputs));
54 explicit QuantizedTypeConverter() {
55 addConversion([](Type type) {
return type; });
56 addConversion(convertQuantizedType);
57 addConversion(convertTensorType);
65 class StripFuncQuantTypes
66 :
public impl::StripFuncQuantTypesBase<StripFuncQuantTypes> {
69 void runOnOperation()
override {
71 auto moduleOp = cast<ModuleOp>(getOperation());
74 QuantizedTypeConverter typeConverter;
75 ConversionTarget target(*context);
80 target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
81 return typeConverter.isSignatureLegal(op.getFunctionType()) &&
82 typeConverter.isLegal(&op.getBody());
84 target.addDynamicallyLegalOp<func::ReturnOp>(
85 [&](func::ReturnOp op) {
return typeConverter.isLegal(op); });
86 target.addDynamicallyLegalOp<func::CallOp>(
87 [&](func::CallOp op) {
return typeConverter.isLegal(op); });
90 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.