42 #define GEN_PASS_DEF_TOSACONVERTINTEGERTYPETOSIGNLESS
43 #include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"
46 class ToSignlessTensorTypeConverter :
public TypeConverter {
47 static Type convertType(Type type) {
48 const auto tensorType = dyn_cast<TensorType>(type);
52 const auto intType = dyn_cast<IntegerType>(tensorType.getElementType());
54 intType.getSignedness() == IntegerType::SignednessSemantics::Signless)
58 intType.getContext(), intType.getWidth(), IntegerType::Signless);
59 return tensorType.cloneWith(std::nullopt, signlessType);
63 explicit ToSignlessTensorTypeConverter() { addConversion(convertType); }
66 class ConvertGenericOpWithIntegerTensorType :
public ConversionPattern {
68 ConvertGenericOpWithIntegerTensorType(TypeConverter &typeConverter,
70 : ConversionPattern(typeConverter, MatchAnyOpTypeTag{}, 0, context) {}
73 matchAndRewrite(Operation *op, ArrayRef<Value> operands,
74 ConversionPatternRewriter &rewriter)
const final {
78 constexpr
unsigned int numResults = 4;
81 SmallVector<Type, numResults> resultTypes;
82 if (
failed(typeConverter->convertTypes(op->getResultTypes(), resultTypes)))
87 op->getLoc(), op->getName(), resultTypes, operands, op->getAttrs(),
88 op->getPropertiesStorage(), op->getSuccessors(), op->getNumRegions());
91 for (
auto regions : llvm::zip(op->getRegions(), newOp->getRegions())) {
92 Region &before = std::get<0>(regions);
93 Region &parent = std::get<1>(regions);
94 rewriter.inlineRegionBefore(before, parent, parent.end());
95 if (
failed(rewriter.convertRegionTypes(&parent, *typeConverter)))
100 rewriter.insert(newOp);
101 rewriter.replaceOp(op, newOp->getResults());
106 class TosaConvertIntegerTypeToSignless
107 :
public impl::TosaConvertIntegerTypeToSignlessBase<
108 TosaConvertIntegerTypeToSignless> {
110 void runOnOperation()
override {
112 ConversionTarget target(*context);
113 ToSignlessTensorTypeConverter typeConverter;
115 target.addDynamicallyLegalOp<func::FuncOp>([&](func::FuncOp op) {
116 return typeConverter.isSignatureLegal(op.getFunctionType()) &&
117 typeConverter.isLegal(&op.getBody());
119 target.markUnknownOpDynamicallyLegal([&](Operation *op) {
120 return typeConverter.isLegal(op->getOperandTypes()) &&
121 typeConverter.isLegal(op->getResultTypes());
124 RewritePatternSet
patterns(context);
125 populateFunctionOpInterfaceTypeConversionPattern<func::FuncOp>(
127 patterns.add<ConvertGenericOpWithIntegerTensorType>(typeConverter, context);
static MLIRContext * getContext(OpFoldResult val)
static Operation * create(Location location, OperationName name, TypeRange resultTypes, ValueRange operands, NamedAttrList &&attributes, OpaqueProperties properties, BlockRange successors, unsigned numRegions)
Create a new Operation with the specific fields.
Include the generated interface declarations.
LogicalResult applyFullConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Apply a complete conversion on the given operations, and all nested operations.
const FrozenRewritePatternSet & patterns
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...