Skip to content

Commit

Permalink
Add a simple pass manager for KQIR (#2226)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragmaTwice authored Apr 6, 2024
1 parent 8ecccdd commit 1c43343
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/search/passes/manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/

#pragma once

#include <memory>
#include <utility>

#include "search/ir.h"
#include "search/ir_pass.h"
#include "search/passes/push_down_not_expr.h"
#include "search/passes/simplify_and_or_expr.h"
#include "search/passes/simplify_boolean.h"

namespace kqir {

struct PassManager {
template <typename... PN>
static std::unique_ptr<Node> Execute(std::unique_ptr<Node> node) {
return executeImpl<PN...>(std::move(node), std::make_index_sequence<sizeof...(PN)>{});
}

static constexpr auto Default = Execute<SimplifyAndOrExpr, PushDownNotExpr, SimplifyBoolean>;

private:
template <typename... PN, size_t... I>
static std::unique_ptr<Node> executeImpl(std::unique_ptr<Node> node, std::index_sequence<I...>) {
std::tuple<PN...> passes;

return std::move(((node = std::get<I>(passes).Transform(std::move(node))), ...));
}
};

} // namespace kqir
7 changes: 7 additions & 0 deletions tests/cppunit/ir_pass_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "search/ir_pass.h"

#include "gtest/gtest.h"
#include "search/passes/manager.h"
#include "search/passes/push_down_not_expr.h"
#include "search/passes/simplify_and_or_expr.h"
#include "search/passes/simplify_boolean.h"
Expand Down Expand Up @@ -96,3 +97,9 @@ TEST(IRPassTest, PushDownNotExpr) {
ASSERT_EQ(pdne.Transform(*Parse("select * from a where not (not a > 1 or (b < 3 and c hastag \"\"))"))->Dump(),
"select * from a where (and a > 1, (or b >= 3, not c hastag \"\"))");
}

TEST(IRPassTest, Manager) {
ASSERT_EQ(
PassManager::Default(*Parse("select * from a where not (x > 1 or (y < 2 or z = 3)) and (true or x = 1)"))->Dump(),
"select * from a where (and x <= 1, y >= 2, z != 3)");
}

0 comments on commit 1c43343

Please sign in to comment.