-
Notifications
You must be signed in to change notification settings - Fork 53
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
Copy Propagation #107
Open
huangjd
wants to merge
2
commits into
simit-lang:master
Choose a base branch
from
huangjd:ProgramAnalysis
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Copy Propagation #107
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef SIMIT_INTRUSIVE_PTR_HASH_H | ||
#define SIMIT_INTRUSIVE_PTR_HASH_H | ||
|
||
#include <functional> | ||
|
||
#include "intrusive_ptr.h" | ||
#include "ir.h" | ||
|
||
namespace std { | ||
|
||
template <typename T> | ||
struct hash<simit::util::IntrusivePtr<T>> { | ||
size_t operator()(const simit::util::IntrusivePtr<T>& x) const { | ||
return (size_t) x.ptr; | ||
} | ||
}; | ||
|
||
#define HASH_INTRUSIVE_PTR(type) template <>\ | ||
struct hash<type> {\ | ||
size_t operator()(const type& x) const {\ | ||
return (size_t) x.ptr;\ | ||
}\ | ||
}; | ||
|
||
HASH_INTRUSIVE_PTR(simit::ir::Var) | ||
HASH_INTRUSIVE_PTR(simit::ir::IndexVar) | ||
HASH_INTRUSIVE_PTR(simit::ir::Expr) | ||
HASH_INTRUSIVE_PTR(simit::ir::Stmt) | ||
HASH_INTRUSIVE_PTR(simit::ir::Func) | ||
|
||
#undef HASH_INTRUSIVE_PTR | ||
} | ||
|
||
#endif |
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
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,282 @@ | ||
#ifndef SIMIT_IR_PATTERN_MATCHING_H | ||
#define SIMIT_IR_PATTERN_MATCHING_H | ||
|
||
#include "ir.h" | ||
#include <numeric> | ||
#include <functional> | ||
#include <type_traits> | ||
|
||
namespace simit { | ||
namespace ir { | ||
|
||
struct HandleCallbackArguments { | ||
template <typename T> | ||
static bool call(const T* node, std::nullptr_t) { | ||
return true; | ||
} | ||
|
||
#if __cplusplus < 201700L | ||
template <typename T, typename U, typename = | ||
typename std::enable_if<std::is_same<bool, | ||
typename std::result_of<U(const T*)>::type>::value>::type> | ||
static bool call(const T* node, U&& func) { | ||
return func(node); | ||
} | ||
|
||
template <typename T, typename U, typename = | ||
typename std::enable_if<std::is_same<void, | ||
typename std::result_of<U(const T*)>::type>::value>::type> | ||
static bool call(const T* node, U&& func, int* dummy = 0) { | ||
func(node); | ||
return true; | ||
} | ||
#else | ||
template <typename T, typename U, typename = | ||
typename std::enable_if<std::is_same<bool, | ||
typename std::invoke_result<U(const T*)>::type>::value>::type> | ||
static bool call(const T* node, U&& func) { | ||
return func(node); | ||
} | ||
|
||
template <typename T, typename U, typename = | ||
typename std::enable_if<std::is_same<void, | ||
typename std::invoke_result<U(const T*)>::type>::value>::type> | ||
static bool call(const T* node, U&& func, int* dummy = 0) { | ||
func(node); | ||
return true; | ||
} | ||
#endif | ||
}; | ||
|
||
template <typename T> | ||
class PatternMatch; | ||
|
||
template <typename T> | ||
class PatternMatch<T*> : public PatternMatch<T> { | ||
}; | ||
|
||
template <> | ||
class PatternMatch<void> { | ||
public: | ||
template <typename ExprOrStmt, typename Callback> | ||
static bool match(ExprOrStmt expr, Callback&&) { | ||
return true; | ||
} | ||
}; | ||
|
||
#define GENERATE_PATTERNMATCH_0(TYPE)\ | ||
template <>\ | ||
class PatternMatch<TYPE> {\ | ||
public:\ | ||
template <typename ExprOrStmt>\ | ||
static bool match(ExprOrStmt expr, std::nullptr_t) {\ | ||
return isa<TYPE>(expr);\ | ||
}\ | ||
\ | ||
template <typename ExprOrStmt>\ | ||
static bool match(ExprOrStmt expr, const std::tuple<std::nullptr_t>&) {\ | ||
return isa<TYPE>(expr);\ | ||
}\ | ||
\ | ||
template <typename ExprOrStmt, typename Callback>\ | ||
static bool match(ExprOrStmt expr, const std::tuple<Callback>& callbacks) {\ | ||
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. Might be better if |
||
if (isa<TYPE>(expr)) {\ | ||
const TYPE* node = to<TYPE>(expr);\ | ||
return HandleCallbackArguments::call(node, std::get<0>(callbacks));\ | ||
}\ | ||
return false;\ | ||
}\ | ||
}; | ||
|
||
#define GENERATE_PATTERNMATCH_1(TYPE, MEMBER1)\ | ||
template <typename T1>\ | ||
class PatternMatch<TYPE(T1)> {\ | ||
public:\ | ||
template <typename ExprOrStmt>\ | ||
static bool match(ExprOrStmt expr, std::nullptr_t) {\ | ||
return isa<TYPE>(expr);\ | ||
}\ | ||
\ | ||
template <typename ExprOrStmt>\ | ||
static bool match(ExprOrStmt expr, const std::tuple<std::nullptr_t>&) {\ | ||
return isa<TYPE>(expr);\ | ||
}\ | ||
\ | ||
template <typename ExprOrStmt, typename Callback, typename Sub1>\ | ||
static bool match(ExprOrStmt expr,\ | ||
const std::tuple<Callback, Sub1>& callbacks) {\ | ||
if (isa<TYPE>(expr)) {\ | ||
const TYPE* node = to<TYPE>(expr);\ | ||
return HandleCallbackArguments::call(node, std::get<0>(callbacks)) &&\ | ||
PatternMatch<T1>::match(node->MEMBER1, std::get<1>(callbacks));\ | ||
}\ | ||
return false;\ | ||
}\ | ||
}; | ||
|
||
#define GENERATE_PATTERNMATCH_2(TYPE, MEMBER1, MEMBER2)\ | ||
template <typename T1, typename T2>\ | ||
class PatternMatch<TYPE(T1, T2)> {\ | ||
public:\ | ||
template <typename ExprOrStmt>\ | ||
static bool match(ExprOrStmt expr, std::nullptr_t) {\ | ||
return isa<TYPE>(expr);\ | ||
}\ | ||
\ | ||
template <typename ExprOrStmt>\ | ||
static bool match(ExprOrStmt expr, const std::tuple<std::nullptr_t>&) {\ | ||
return isa<TYPE>(expr);\ | ||
}\ | ||
\ | ||
template <typename ExprOrStmt, typename Callback,\ | ||
typename Sub1, typename Sub2>\ | ||
static bool match(ExprOrStmt expr,\ | ||
const std::tuple<Callback,Sub1, Sub2>& callbacks) {\ | ||
if (isa<TYPE>(expr)) {\ | ||
const TYPE* node = to<TYPE>(expr);\ | ||
return HandleCallbackArguments::call(node, std::get<0>(callbacks)) &&\ | ||
PatternMatch<T1>::match(node->MEMBER1, std::get<1>(callbacks)) &&\ | ||
PatternMatch<T2>::match(node->MEMBER2, std::get<2>(callbacks));\ | ||
}\ | ||
return false;\ | ||
}\ | ||
}; | ||
|
||
#define GENERATE_PATTERNMATCH_3(TYPE, MEMBER1, MEMBER2, MEMBER3)\ | ||
template <typename T1, typename T2, typename T3>\ | ||
class PatternMatch<TYPE(T1, T2, T3)> {\ | ||
public:\ | ||
template <typename ExprOrStmt>\ | ||
static bool match(ExprOrStmt expr, std::nullptr_t) {\ | ||
return isa<TYPE>(expr);\ | ||
}\ | ||
\ | ||
template <typename ExprOrStmt>\ | ||
static bool match(ExprOrStmt expr, const std::tuple<std::nullptr_t>&) {\ | ||
return isa<TYPE>(expr);\ | ||
}\ | ||
\ | ||
template <typename ExprOrStmt, typename Callback,\ | ||
typename Sub1, typename Sub2, typename Sub3>\ | ||
static bool match(ExprOrStmt expr,\ | ||
const std::tuple<Callback, Sub1, Sub2, Sub3>& callbacks) {\ | ||
if (isa<TYPE>(expr)) {\ | ||
const TYPE* node = to<TYPE>(expr);\ | ||
return HandleCallbackArguments::call(node, std::get<0>(callbacks)) &&\ | ||
PatternMatch<T1>::match(node->MEMBER1, std::get<1>(callbacks)) &&\ | ||
PatternMatch<T2>::match(node->MEMBER2, std::get<2>(callbacks)) &&\ | ||
PatternMatch<T3>::match(node->MEMBER3, std::get<3>(callbacks));\ | ||
}\ | ||
return false;\ | ||
}\ | ||
}; | ||
|
||
GENERATE_PATTERNMATCH_0(Literal) | ||
GENERATE_PATTERNMATCH_0(VarExpr) | ||
GENERATE_PATTERNMATCH_2(Load, buffer, index) | ||
GENERATE_PATTERNMATCH_1(FieldRead, elementOrSet) | ||
GENERATE_PATTERNMATCH_0(Length) | ||
GENERATE_PATTERNMATCH_1(IndexRead, edgeSet) | ||
|
||
template <typename T1> | ||
class PatternMatch<UnaryExpr*(T1)> { | ||
public: | ||
template <typename ExprOrStmt> | ||
static bool match(ExprOrStmt expr, std::nullptr_t) { | ||
return isa<UnaryExpr>(expr); | ||
} | ||
|
||
template <typename ExprOrStmt> | ||
static bool match(ExprOrStmt expr, const std::tuple<std::nullptr_t>&) { | ||
return isa<UnaryExpr>(expr); | ||
} | ||
|
||
template <typename ExprOrStmt, typename Callback, typename Sub1> | ||
static bool match(ExprOrStmt expr, | ||
const std::tuple<Callback, Sub1>& callbacks) { | ||
if (isa<UnaryExpr>(expr)) { | ||
const UnaryExpr* node = to<UnaryExpr>(expr); | ||
return HandleCallbackArguments::call(node, std::get<0>(callbacks)) && | ||
PatternMatch<T1>::match(node->a, std::get<1>(callbacks)); | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
template <typename T1, typename T2> | ||
class PatternMatch<BinaryExpr*(T1, T2)> { | ||
public: | ||
template <typename ExprOrStmt> | ||
static bool match(ExprOrStmt expr, std::nullptr_t) { | ||
return isa<BinaryExpr>(expr); | ||
} | ||
|
||
template <typename ExprOrStmt> | ||
static bool match(ExprOrStmt expr, const std::tuple<std::nullptr_t>&) { | ||
return isa<BinaryExpr>(expr); | ||
} | ||
|
||
template <typename ExprOrStmt, typename Callback, typename Sub1, typename Sub2> | ||
static bool match(ExprOrStmt expr, | ||
const std::tuple<Callback,Sub1, Sub2>& callbacks) { | ||
if (isa<BinaryExpr>(expr)) { | ||
const BinaryExpr* node = to<BinaryExpr>(expr); | ||
return HandleCallbackArguments::call(node, std::get<0>(callbacks)) && | ||
PatternMatch<T1>::match(node->a, std::get<1>(callbacks)) && | ||
PatternMatch<T2>::match(node->b, std::get<2>(callbacks)); | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
GENERATE_PATTERNMATCH_1(Neg, a) | ||
GENERATE_PATTERNMATCH_2(Add, a, b) | ||
GENERATE_PATTERNMATCH_2(Sub, a, b) | ||
GENERATE_PATTERNMATCH_2(Mul, a, b) | ||
GENERATE_PATTERNMATCH_2(Div, a, b) | ||
GENERATE_PATTERNMATCH_2(Rem, a, b) | ||
|
||
GENERATE_PATTERNMATCH_1(Not, a) | ||
GENERATE_PATTERNMATCH_2(Eq, a, b) | ||
GENERATE_PATTERNMATCH_2(Ne, a, b) | ||
GENERATE_PATTERNMATCH_2(Gt, a, b) | ||
GENERATE_PATTERNMATCH_2(Lt, a, b) | ||
GENERATE_PATTERNMATCH_2(Ge, a, b) | ||
GENERATE_PATTERNMATCH_2(Le, a, b) | ||
GENERATE_PATTERNMATCH_2(And, a, b) | ||
GENERATE_PATTERNMATCH_2(Or, a, b) | ||
GENERATE_PATTERNMATCH_2(Xor, a, b) | ||
|
||
GENERATE_PATTERNMATCH_0(VarDecl) | ||
GENERATE_PATTERNMATCH_1(AssignStmt, value) | ||
GENERATE_PATTERNMATCH_3(Store, buffer, index, value) | ||
GENERATE_PATTERNMATCH_2(FieldWrite, elementOrSet, value) | ||
|
||
// call | ||
|
||
GENERATE_PATTERNMATCH_1(Scope, scopedStmt) | ||
GENERATE_PATTERNMATCH_3(IfThenElse, condition, thenBody, elseBody) | ||
GENERATE_PATTERNMATCH_3(ForRange, start, end, body) | ||
GENERATE_PATTERNMATCH_1(For, body) | ||
GENERATE_PATTERNMATCH_2(While, condition, body) | ||
GENERATE_PATTERNMATCH_1(Kernel, body) | ||
|
||
// block | ||
|
||
GENERATE_PATTERNMATCH_1(Print, expr) | ||
GENERATE_PATTERNMATCH_1(Comment,commentedStmt) | ||
GENERATE_PATTERNMATCH_0(Pass) | ||
|
||
GENERATE_PATTERNMATCH_2(UnnamedTupleRead, tuple, index) | ||
GENERATE_PATTERNMATCH_1(NamedTupleRead, tuple) | ||
// setread | ||
// tensorread | ||
//GENERATE_PATTERNMATCH_(TensorWrite) | ||
GENERATE_PATTERNMATCH_1(IndexedTensor, tensor) | ||
GENERATE_PATTERNMATCH_1(IndexExpr, value) | ||
//GENERATE_PATTERNMATCH_(Map) | ||
|
||
} | ||
} | ||
|
||
#endif |
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
Oops, something went wrong.
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.
Is this actually used anywhere?