-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[flang][Evaluate] Implement rewriting framework for evaluate::Expr #153037
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
59ca403
[flang][OpenMP] Refactor creating atomic analysis, NFC
kparzysz 2120125
[flang][Evaluate] Implement rewriting framework for evaluate::Expr
kparzysz d2ecbf0
Update flang/include/flang/Evaluate/rewrite.h
kparzysz 3c6b36e
Merge branch 'main' into users/kparzysz/w02-expr-rewriter
kparzysz 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 hidden or 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,160 @@ | ||
| //===-- include/flang/Evaluate/rewrite.h ------------------------*- C++ -*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| #ifndef FORTRAN_EVALUATE_REWRITE_H_ | ||
| #define FORTRAN_EVALUATE_REWRITE_H_ | ||
|
|
||
| #include "flang/Common/visit.h" | ||
| #include "flang/Evaluate/expression.h" | ||
| #include "flang/Support/Fortran.h" | ||
| #include "llvm/ADT/STLExtras.h" | ||
|
|
||
| #include <tuple> | ||
| #include <type_traits> | ||
| #include <utility> | ||
| #include <variant> | ||
|
|
||
| namespace Fortran::evaluate { | ||
| namespace rewrite { | ||
| namespace detail { | ||
| template <typename, typename = void> // | ||
| struct IsOperation { | ||
| static constexpr bool value{false}; | ||
| }; | ||
|
|
||
| template <typename T> | ||
| struct IsOperation<T, std::void_t<decltype(T::operands)>> { | ||
| static constexpr bool value{true}; | ||
| }; | ||
| } // namespace detail | ||
|
|
||
| template <typename T> | ||
| constexpr bool is_operation_v{detail::IsOperation<T>::value}; | ||
|
|
||
| /// Individual Expr<T> rewriter that simply constructs an expression that is | ||
| /// identical to the input. This is a suitable base class for all user-defined | ||
| /// rewriters. | ||
| struct Identity { | ||
| template <typename T, typename U> | ||
| Expr<T> operator()(Expr<T> &&x, const U &op) { | ||
| return std::move(x); | ||
| } | ||
| }; | ||
|
|
||
| /// Bottom-up Expr<T> rewriter. | ||
| /// | ||
| /// The Mutator traverses and reconstructs given Expr<T>. Going bottom-up, | ||
| /// whenever the traversal visits a sub-node of type Expr<U> (for some U), | ||
| /// it will invoke the user-provided rewriter via the () operator. | ||
| /// | ||
| /// If x is of type Expr<U>, it will call (in pseudo-code): | ||
| /// rewriter_(x, active_member_of(x.u)) | ||
| /// The second parameter is there to make it easier to overload the () operator | ||
| /// for specific operations in Expr<...>. | ||
| /// | ||
| /// The user rewriter is only invoked for Expr<U>, not for Operation, nor any | ||
| /// other subobject. | ||
| template <typename Rewriter> struct Mutator { | ||
| Mutator(Rewriter &rewriter) : rewriter_(rewriter) {} | ||
|
|
||
| template <typename T, typename U = llvm::remove_cvref_t<T>> | ||
| U operator()(T &&x) { | ||
| if constexpr (std::is_lvalue_reference_v<T>) { | ||
| return Mutate(U(x)); | ||
| } else { | ||
| return Mutate(std::move(x)); | ||
| } | ||
| } | ||
|
|
||
| private: | ||
| template <typename T> struct LambdaWithRvalueCapture { | ||
| LambdaWithRvalueCapture(Rewriter &r, Expr<T> &&c) | ||
| : rewriter_(r), capture_(std::move(c)) {} | ||
| template <typename S> Expr<T> operator()(const S &s) { | ||
| return rewriter_(std::move(capture_), s); | ||
| } | ||
|
|
||
| private: | ||
| Rewriter &rewriter_; | ||
| Expr<T> &&capture_; | ||
| }; | ||
|
|
||
| template <typename T, typename = std::enable_if_t<!is_operation_v<T>>> | ||
| T Mutate(T &&x) const { | ||
| return std::move(x); | ||
| } | ||
|
|
||
| template <typename D, typename = std::enable_if_t<is_operation_v<D>>> | ||
| D Mutate(D &&op, std::make_index_sequence<D::operands> t = {}) const { | ||
| return MutateOp(std::move(op), t); | ||
| } | ||
|
|
||
| template <typename T> // | ||
| Expr<T> Mutate(Expr<T> &&x) const { | ||
| // First construct the new expression with the rewritten op. | ||
| Expr<T> n{common::visit( | ||
| [&](auto &&s) { // | ||
| return Expr<T>(Mutate(std::move(s))); | ||
| }, | ||
| std::move(x.u))}; | ||
| // Return the rewritten expression. The second visit it to make sure | ||
| // that the second argument in the call to the rewriter is a part of | ||
| // the Expr<T> passed to it. | ||
| return common::visit( | ||
| LambdaWithRvalueCapture<T>(rewriter_, std::move(n)), std::move(n.u)); | ||
| } | ||
|
|
||
| template <typename... Ts> | ||
| std::variant<Ts...> Mutate(std::variant<Ts...> &&u) const { | ||
| return common::visit( | ||
| [this](auto &&s) { return Mutate(std::move(s)); }, std::move(u)); | ||
| } | ||
|
|
||
| template <typename... Ts> | ||
| std::tuple<Ts...> Mutate(std::tuple<Ts...> &&t) const { | ||
| return MutateTuple(std::move(t), std::index_sequence_for<Ts...>{}); | ||
| } | ||
|
|
||
| template <typename... Ts, size_t... Is> | ||
| std::tuple<Ts...> MutateTuple( | ||
| std::tuple<Ts...> &&t, std::index_sequence<Is...>) const { | ||
| return std::make_tuple(Mutate(std::move(std::get<Is>(t))...)); | ||
| } | ||
|
|
||
| template <typename D, size_t... Is> | ||
| D MutateOp(D &&op, std::index_sequence<Is...>) const { | ||
| return D(Mutate(std::move(op.template operand<Is>()))...); | ||
| } | ||
|
|
||
| template <typename T, size_t... Is> | ||
| Extremum<T> MutateOp(Extremum<T> &&op, std::index_sequence<Is...>) const { | ||
| return Extremum<T>( | ||
| op.ordering, Mutate(std::move(op.template operand<Is>()))...); | ||
| } | ||
|
|
||
| template <int K, size_t... Is> | ||
| ComplexComponent<K> MutateOp( | ||
| ComplexComponent<K> &&op, std::index_sequence<Is...>) const { | ||
| return ComplexComponent<K>( | ||
| op.isImaginaryPart, Mutate(std::move(op.template operand<Is>()))...); | ||
| } | ||
|
|
||
| template <int K, size_t... Is> | ||
| LogicalOperation<K> MutateOp( | ||
| LogicalOperation<K> &&op, std::index_sequence<Is...>) const { | ||
| return LogicalOperation<K>( | ||
| op.logicalOperator, Mutate(std::move(op.template operand<Is>()))...); | ||
| } | ||
|
|
||
| Rewriter &rewriter_; | ||
| }; | ||
|
|
||
| template <typename Rewriter> Mutator(Rewriter &) -> Mutator<Rewriter>; | ||
| } // namespace rewrite | ||
| } // namespace Fortran::evaluate | ||
|
|
||
| #endif // FORTRAN_EVALUATE_REWRITE_H_ | ||
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.