-
Notifications
You must be signed in to change notification settings - Fork 72
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
Dialect Conversion #20
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
05a17d1
Add poly-to-standard lowering pass shell
j2kun d9bcf1f
Add conversion target
j2kun 3421df1
add poly type converter
j2kun 78bf9a5
add empty lowering for poly.add
j2kun bf9fbc9
lower poly.add
j2kun 25b284b
add structural conversion helpers for func, etc.
j2kun 191b7b0
add poly.to_tensor op
j2kun e99916b
lower poly sub, to_tensor, from_tensor ops
j2kun b8c4d86
lower poly.constant
j2kun 46aa345
lower poly.mul as a naive loop
j2kun 06de5d2
Lower poly.eval
j2kun 46ff536
add CMake build files
j2kun b8f089b
add a test that chains together many poly ops
j2kun edf22e4
demonstrate what a materialization would involve
j2kun c3a8384
Revert "demonstrate what a materialization would involve"
j2kun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library") | ||
|
||
package( | ||
default_visibility = ["//visibility:public"], | ||
) | ||
|
||
gentbl_cc_library( | ||
name = "pass_inc_gen", | ||
tbl_outs = [ | ||
( | ||
[ | ||
"-gen-pass-decls", | ||
"-name=PolyToStandard", | ||
], | ||
"PolyToStandard.h.inc", | ||
), | ||
], | ||
tblgen = "@llvm-project//mlir:mlir-tblgen", | ||
td_file = "PolyToStandard.td", | ||
deps = [ | ||
"@llvm-project//mlir:OpBaseTdFiles", | ||
"@llvm-project//mlir:PassBaseTdFiles", | ||
], | ||
) | ||
|
||
cc_library( | ||
name = "PolyToStandard", | ||
srcs = ["PolyToStandard.cpp"], | ||
hdrs = ["PolyToStandard.h"], | ||
deps = [ | ||
"pass_inc_gen", | ||
"//lib/Dialect/Poly", | ||
"@llvm-project//mlir:ArithDialect", | ||
"@llvm-project//mlir:IR", | ||
"@llvm-project//mlir:Pass", | ||
"@llvm-project//mlir:TensorDialect", | ||
"@llvm-project//mlir:Transforms", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include "lib/Conversion/PolyToStandard/PolyToStandard.h" | ||
|
||
#include "lib/Dialect/Poly/PolyOps.h" | ||
#include "lib/Dialect/Poly/PolyTypes.h" | ||
#include "mlir/include/mlir/Transforms/DialectConversion.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace tutorial { | ||
namespace poly { | ||
|
||
#define GEN_PASS_DEF_POLYTOSTANDARD | ||
#include "lib/Conversion/PolyToStandard/PolyToStandard.h.inc" | ||
|
||
class PolyToStandardTypeConverter : public TypeConverter { | ||
public: | ||
PolyToStandardTypeConverter(MLIRContext *ctx) { | ||
addConversion([](Type type) { return type; }); | ||
addConversion([ctx](PolynomialType type) -> Type { | ||
int degreeBound = type.getDegreeBound(); | ||
IntegerType elementTy = | ||
IntegerType::get(ctx, 32, IntegerType::SignednessSemantics::Signless); | ||
return RankedTensorType::get({degreeBound}, elementTy); | ||
}); | ||
|
||
// We don't include any custom materialization hooks because this lowering | ||
// is all done in a single pass. The dialect conversion framework works by | ||
// resolving intermediate (mid-pass) type conflicts by inserting | ||
// unrealized_conversion_cast ops, and only converting those to custom | ||
// materializations if they persist at the end of the pass. In our case, | ||
// we'd only need to use custom materializations if we split this lowering | ||
// across multiple passes. | ||
} | ||
}; | ||
|
||
struct ConvertAdd : public OpConversionPattern<AddOp> { | ||
ConvertAdd(mlir::MLIRContext *context) | ||
: OpConversionPattern<AddOp>(context) {} | ||
|
||
using OpConversionPattern::OpConversionPattern; | ||
|
||
LogicalResult matchAndRewrite( | ||
AddOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
// TODO: implement | ||
return success(); | ||
} | ||
}; | ||
|
||
struct PolyToStandard : impl::PolyToStandardBase<PolyToStandard> { | ||
using PolyToStandardBase::PolyToStandardBase; | ||
|
||
void runOnOperation() override { | ||
MLIRContext *context = &getContext(); | ||
auto *module = getOperation(); | ||
|
||
ConversionTarget target(*context); | ||
target.addLegalDialect<arith::ArithDialect>(); | ||
target.addIllegalDialect<PolyDialect>(); | ||
|
||
RewritePatternSet patterns(context); | ||
PolyToStandardTypeConverter typeConverter(context); | ||
patterns.add<ConvertAdd>(typeConverter, context); | ||
|
||
if (failed(applyPartialConversion(module, target, std::move(patterns)))) { | ||
signalPassFailure(); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace poly | ||
} // namespace tutorial | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef LIB_CONVERSION_POLYTOSTANDARD_POLYTOSTANDARD_H_ | ||
#define LIB_CONVERSION_POLYTOSTANDARD_POLYTOSTANDARD_H_ | ||
|
||
#include "mlir/include/mlir/Pass/Pass.h" // from @llvm-project | ||
|
||
// Extra includes needed for dependent dialects | ||
#include "mlir/include/mlir/Dialect/Arith/IR/Arith.h" // from @llvm-project | ||
#include "mlir/include/mlir/Dialect/Tensor/IR/Tensor.h" // from @llvm-project | ||
|
||
namespace mlir { | ||
namespace tutorial { | ||
namespace poly { | ||
|
||
#define GEN_PASS_DECL | ||
#include "lib/Conversion/PolyToStandard/PolyToStandard.h.inc" | ||
|
||
#define GEN_PASS_REGISTRATION | ||
#include "lib/Conversion/PolyToStandard/PolyToStandard.h.inc" | ||
|
||
} // namespace poly | ||
} // namespace tutorial | ||
} // namespace mlir | ||
|
||
#endif // LIB_CONVERSION_POLYTOSTANDARD_POLYTOSTANDARD_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef LIB_CONVERSION_POLYTOSTANDARD_POLYTOSTANDARD_TD_ | ||
#define LIB_CONVERSION_POLYTOSTANDARD_POLYTOSTANDARD_TD_ | ||
|
||
include "mlir/Pass/PassBase.td" | ||
|
||
def PolyToStandard : Pass<"poly-to-standard"> { | ||
let summary = "Lower `poly` to standard MLIR dialects."; | ||
|
||
let description = [{ | ||
This pass lowers the `poly` dialect to standard MLIR, a mixture of affine, | ||
tensor, and arith. | ||
}]; | ||
let dependentDialects = [ | ||
"mlir::arith::ArithDialect", | ||
"mlir::tutorial::poly::PolyDialect", | ||
"mlir::tensor::TensorDialect", | ||
]; | ||
} | ||
|
||
#endif // LIB_CONVERSION_POLYTOSTANDARD_POLYTOSTANDARD_TD_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm not sure of before but in MLIR 17+, I've had to instead implement this constructor,
ConvertAdd(TypeConverter& type_converter, MLIRContext* context) : OpConversionPattern<AddOp>(type_converter, context) {}
otherwise the call to
patterns.add<ConvertAdd>(typeConverter, context);
fails at compile time sayingno matching constructor for initialization
since the constructor we implemented here requires one argument but we provide two.Also, it maybe just a minor preference thing but
struct A : public B
is equivalent tostruct A : B
since structs have public inheritance by default.