Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOSA: add folder for tosa.sub #72

Merged
merged 4 commits into from
Aug 16, 2023
Merged
Changes from 1 commit
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
35 changes: 34 additions & 1 deletion mlir/lib/Dialect/Tosa/Transforms/TosaFolders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,38 @@ struct TosaFoldConstantAdd : public TosaFoldConstantBinary<TosaFoldConstantAdd,
}
};

struct TosaFoldConstantSub : public TosaFoldConstantBinary<TosaFoldConstantSub, SubOp> {
using TosaFoldConstantBinary<TosaFoldConstantSub, SubOp>::TosaFoldConstantBinary;

DenseElementsAttr computeInteger(DenseElementsAttr lhsValues,
DenseElementsAttr rhsValues,
PatternRewriter &rewriter, SubOp op) const {
bool addOverflowed = false;
mgehre-amd marked this conversation as resolved.
Show resolved Hide resolved
auto intAdd = [&addOverflowed](const APInt &first, const APInt &second) {
bool didOverflow;
auto res = first.ssub_ov(second, didOverflow);
addOverflowed |= didOverflow;
return res;
};
auto newTensor = applyElementWise<APInt, APInt>(lhsValues, rhsValues,
op.getType(), intAdd);
if (addOverflowed) {
op->emitWarning("Addition did overflow. The results are unspecified.");
}
return newTensor;
}

DenseElementsAttr computeFloat(DenseElementsAttr lhsValues,
DenseElementsAttr rhsValues,
PatternRewriter &rewriter, SubOp op) const {
auto floatAdd = [](const APFloat &first, const APFloat &second) {
return first - second;
};
return applyElementWise<APFloat, APFloat>(lhsValues, rhsValues,
op.getType(), floatAdd);
}
};

struct TosaFoldConstantGreater : public TosaFoldConstantBinary<TosaFoldConstantGreater, GreaterOp> {
using TosaFoldConstantBinary<TosaFoldConstantGreater, GreaterOp>::TosaFoldConstantBinary;

Expand Down Expand Up @@ -1382,6 +1414,7 @@ void mlir::tosa::populateTosaFoldConstantPatterns(
patterns.add<TosaFoldConstantFloatCasts>(ctx, foldSplatOrSingleUseOnly);
}
patterns.add<TosaFoldConstantAdd>(ctx, foldSplatOrSingleUseOnly);
patterns.add<TosaFoldConstantSub>(ctx, foldSplatOrSingleUseOnly);
patterns.add<TosaFoldConstantGreater>(ctx, foldSplatOrSingleUseOnly);
patterns.add<TosaFoldConstantBitwiseNot>(ctx, foldSplatOrSingleUseOnly);
patterns.add<TosaFoldConstantCeil>(ctx, foldSplatOrSingleUseOnly);
Expand All @@ -1394,4 +1427,4 @@ void mlir::tosa::populateTosaFoldConstantPatterns(
patterns.add<TosaFoldConstantMinimum>(ctx, foldSplatOrSingleUseOnly);
patterns.add<TosaFoldConstantMaximum>(ctx, foldSplatOrSingleUseOnly);
patterns.add<TosaFoldConstantPad>(ctx, foldSplatOrSingleUseOnly);
}
}
mgehre-amd marked this conversation as resolved.
Show resolved Hide resolved