-
Notifications
You must be signed in to change notification settings - Fork 21
Tessera pass draft #1441
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
base: main
Are you sure you want to change the base?
Tessera pass draft #1441
Changes from all commits
dca7483
93f29cb
3451ba4
209bcee
cd024c4
5bec7b2
d886658
210eafd
5f196f5
0e8baeb
1be6cfa
c4d10eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements patterns to convert the Func dialect to the Tessera | ||
// dialect and from the Tessera dialect to the Func dialect. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "src/enzyme_ad/jax/Dialect/Tessera/Dialect.h" | ||
#include "src/enzyme_ad/jax/Passes/Passes.h" | ||
|
||
using namespace mlir; | ||
using namespace mlir::enzyme::tessera; | ||
|
||
namespace { | ||
} // namespace | ||
|
||
|
||
//===----------------------------------------------------------------------===// | ||
// Rewrite Patterns | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace { | ||
|
||
// Rewrite 'func.func' -> 'tessera.define' | ||
class FuncOpRewrite final : public OpRewritePattern<func::FuncOp> { | ||
public: | ||
using OpRewritePattern<func::FuncOp>::OpRewritePattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(func::FuncOp funcOp, | ||
PatternRewriter &rewriter) const override { | ||
FunctionType fnType = funcOp.getFunctionType(); | ||
|
||
if (fnType.getNumResults() > 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure this is required? |
||
return rewriter.notifyMatchFailure( | ||
funcOp, "only functions with zero or one result can be rewritten"); | ||
|
||
|
||
// Create the `tessera.define` op | ||
auto tesseraDefineOp = rewriter.create<tessera::DefineOp>( | ||
funcOp.getLoc(), funcOp.getName(), fnType); | ||
|
||
|
||
// Copy over all attributes other than the function name and type. | ||
for (const auto &namedAttr : funcOp->getAttrs()) { | ||
if (namedAttr.getName() != funcOp.getFunctionTypeAttrName() && | ||
namedAttr.getName() != SymbolTable::getSymbolAttrName()) | ||
tesseraDefineOp->setAttr(namedAttr.getName(), namedAttr.getValue()); | ||
} | ||
|
||
// Add `extern` to specifiers if `func.func` is declaration only. | ||
if (funcOp.isDeclaration()) { | ||
ArrayAttr specifiers = rewriter.getStrArrayAttr({"extern"}); | ||
tesseraDefineOp.setSpecifiersAttr(specifiers); | ||
} | ||
|
||
// Add `static` to specifiers if `func.func` is private but not a | ||
// declaration. | ||
if (funcOp.isPrivate() && !funcOp.isDeclaration()) { | ||
ArrayAttr specifiers = rewriter.getStrArrayAttr({"static"}); | ||
tesseraDefineOp.setSpecifiersAttr(specifiers); | ||
} | ||
|
||
if (!funcOp.isDeclaration()) { | ||
rewriter.inlineRegionBefore(funcOp.getBody(), tesseraDefineOp.getBody(), | ||
wsmoses marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tesseraDefineOp.end()); | ||
} | ||
|
||
|
||
rewriter.eraseOp(funcOp); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
// Rewrite 'func.call' -> 'tessera.call' | ||
class CallOpRewrite final : public OpRewritePattern<func::CallOp> { | ||
public: | ||
using OpRewritePattern<func::CallOp>::OpRewritePattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(func::CallOp callOp, | ||
PatternRewriter &rewriter) const override { | ||
|
||
rewriter.replaceOpWithNewOp<tessera::CallOp>(callOp, callOp.getResultTypes(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to get the symbol we're calling into, and only if that symbol is a tessera.define do we do the call conversion |
||
callOp.getOperands(), | ||
callOp->getAttrs()); | ||
|
||
return success(); | ||
} | ||
}; | ||
|
||
// Rewrite 'func.return' -> 'tessera.return' | ||
class ReturnOpRewrite final : public OpRewritePattern<func::ReturnOp> { | ||
public: | ||
using OpRewritePattern<func::ReturnOp>::OpRewritePattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(func::ReturnOp returnOp, | ||
PatternRewriter &rewriter) const override { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should first check whether our parent op is a tessera defineop and only then do the replacement |
||
rewriter.replaceOpWithNewOp<tessera::ReturnOp>(returnOp, | ||
returnOp.getOperands()); | ||
return success(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Pass to convert Func operations into Tessera operations | ||
//===----------------------------------------------------------------------===// | ||
|
||
struct FuncToTesseraPass | ||
: public PassWrapper<FuncToTesseraPass, OperationPass<ModuleOp>> { | ||
|
||
void runOnOperation() override { | ||
MLIRContext &ctx = patterns.getContext(); | ||
RewritePatternSet patterns(&ctx); | ||
|
||
patterns.add<CallOpRewrite, FuncOpRewrite, ReturnOpRewrite>(&ctx); | ||
|
||
if (failed(applyPatternsAndFoldGreedily(getOperation(), | ||
std::move(patterns)))) | ||
signalPassFailure(); | ||
} | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef ENZYME_AD_JAX_PASSES_TESSERA_PASSES_TD | ||
#define ENZYME_AD_JAX_PASSES_TESSERA_PASSES_TD | ||
|
||
include "mlir/Pass/PassBase.td" | ||
|
||
def FuncToTesseraPass : Pass<"func-to-tessera"> { | ||
let summary = "Convert operations in the FuncDialect to operations in the TesseraDialect and vice versa"; | ||
let dependentDialects = [ | ||
"func::FuncDialect", | ||
"tessera::TesseraDialect" | ||
]; | ||
} | ||
|
||
#endif // ENZYME_AD_JAX_PASSES_TESSERA_PASSES_TD |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eventually we will want to do some check if funcOp.hasAttr("tessera.convert") then do this conversion, otherwise leave the funcop alone