Skip to content
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

Init commit for sql dialect #319

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
46 changes: 37 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ cmake_minimum_required(VERSION 3.10)

include(CheckCXXSourceCompiles)

set(POLYGEIST_ENABLE_CUDA 0 CACHE BOOL "Enable CUDA frontend and backend")
set(POLYGEIST_ENABLE_ROCM 0 CACHE BOOL "Enable ROCM backend")
set(POLYGEIST_ENABLE_CUDA 0 CACHE BOOL "Enable CUDA compilation support")

if(POLICY CMP0068)
cmake_policy(SET CMP0068 NEW)
Expand All @@ -21,16 +20,11 @@ endif()
option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON)
option(LLVM_BUILD_TOOLS "Build the LLVM tools. If OFF, just generate build targets." ON)

option(ENABLE_SQL "Build SQL dialect" OFF)

set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)

find_program(XXD_BIN xxd)

# TODO should depend on OS
set(POLYGEIST_PGO_DEFAULT_DATA_DIR "/var/tmp/polygeist/pgo/" CACHE STRING "Directory for PGO data")
set(POLYGEIST_PGO_ALTERNATIVE_ENV_VAR "POLYGEIST_PGO_ALTERNATIVE" CACHE STRING "Env var name to specify alternative to profile")
set(POLYGEIST_PGO_DATA_DIR_ENV_VAR "POLYGEIST_PGO_DATA_DIR" CACHE STRING "Env var name to specify PGO data dir")

if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
project(polygeist LANGUAGES CXX C)

Expand Down Expand Up @@ -112,6 +106,40 @@ set(LLVM_LIT_ARGS "-sv" CACHE STRING "lit default options")
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
include(sanitizers)

if (ENABLE_SQL)
include(FetchContent)
include(ExternalProject)

FetchContent_Declare(sqlparser_ext
GIT_REPOSITORY https://github.com/wsmoses/sql-parser
GIT_TAG c2471248cef8cd33081e698e8ac65d691283dbd4
)

FetchContent_GetProperties(sqlparser_ext)

FetchContent_MakeAvailable(sqlparser_ext)

ExternalProject_Add(sqlparser
SOURCE_DIR ${sqlparser_ext_SOURCE_DIR}
INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/sql/install
CONFIGURE_COMMAND ""
BUILD_COMMAND ${CMAKE_COMMAND} -E env
CXX=${CMAKE_CXX_COMPILER}
make static=yes -C ${sqlparser_ext_SOURCE_DIR}
BUILD_IN_SOURCE TRUE
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${sqlparser_ext_SOURCE_DIR}/libsqlparser.a
)


add_library(sqlparse_lib INTERFACE)

target_include_directories(sqlparse_lib INTERFACE "${sqlparser_ext_SOURCE_DIR}/src")
target_link_libraries(sqlparse_lib INTERFACE ${sqlparser_ext_SOURCE_DIR}/libsqlparser.a)
add_dependencies(sqlparse_lib sqlparser)

endif()

add_subdirectory(include)
add_subdirectory(lib)
add_subdirectory(tools)
Expand Down
3 changes: 3 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
add_subdirectory(polygeist)
if (ENABLE_SQL)
add_subdirectory(sql)
endif()
4 changes: 4 additions & 0 deletions include/sql/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_mlir_dialect(SQLOps sql)
# add_mlir_doc(SQLDialect -gen-dialect-doc SQLDialect SQL/)
# add_mlir_doc(SQLOps -gen-op-doc SQLOps SQL/)
add_subdirectory(Passes)
28 changes: 28 additions & 0 deletions include/sql/Parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===- Parser.h - SQL dialect -----------------*- C++ -*-===//
//
// This file is licensed 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 SQLPARSER_H
#define SQLPARSER_H

#include "mlir/IR/Dialect.h"

#include "mlir/IR/Value.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Location.h"
#include "mlir/IR/Attributes.h"
#include "llvm/ADT/SmallVector.h"
#include "mlir/IR/BuiltinTypes.h"

#include "sql/SQLDialect.h"
#include "sql/SQLOps.h"
#include "sql/SQLTypes.h"

mlir::Value parseSQL(mlir::Location loc, mlir::OpBuilder& builder, std::string str);

#endif // SQLPARSER_H
5 changes: 5 additions & 0 deletions include/sql/Passes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(LLVM_TARGET_DEFINITIONS Passes.td)
mlir_tablegen(Passes.h.inc -gen-pass-decls -name sql)
add_public_tablegen_target(MLIRSQLPassIncGen)

add_mlir_doc(Passes SQLPasses ./ -gen-pass-doc)
51 changes: 51 additions & 0 deletions include/sql/Passes/Passes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#ifndef SQL_DIALECT_SQL_PASSES_H
#define SQL_DIALECT_SQL_PASSES_H

#include "mlir/Conversion/LLVMCommon/LoweringOptions.h"
#include "mlir/Pass/Pass.h"
#include <memory>
namespace mlir {
class PatternRewriter;
class RewritePatternSet;
class DominanceInfo;
namespace sql {

std::unique_ptr<Pass> createSQLLowerPass();
std::unique_ptr<Pass> createSQLRaisingPass();
} // namespace sql
} // namespace mlir



namespace mlir {
// Forward declaration from Dialect.h
template <typename ConcreteDialect>
void registerDialect(DialectRegistry &registry);

namespace arith {
class ArithDialect;
} // end namespace arith

namespace scf {
class SCFDialect;
} // end namespace scf

namespace memref {
class MemRefDialect;
} // end namespace memref

namespace func {
class FuncDialect;
}

class AffineDialect;
namespace LLVM {
class LLVMDialect;
}

#define GEN_PASS_REGISTRATION
#include "sql/Passes/Passes.h.inc"

} // end namespace mlir

#endif // SQL_DIALECT_SQL_PASSES_H
22 changes: 22 additions & 0 deletions include/sql/Passes/Passes.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef SQL_PASSES
#define SQL_PASSES

include "mlir/Pass/PassBase.td"


def SQLLower : Pass<"sql-lower", "mlir::ModuleOp"> {
let summary = "Lower sql op to mlir";
let dependentDialects =
["arith::ArithDialect", "func::FuncDialect", "LLVM::LLVMDialect"];
let constructor = "mlir::sql::createSQLLowerPass()";
}


def SQLRaising : Pass<"sql-raising", "mlir::ModuleOp"> {
let summary = "Raise sql op to mlir";
let dependentDialects =
["arith::ArithDialect", "func::FuncDialect", "LLVM::LLVMDialect"];
let constructor = "mlir::sql::createSQLRaisingPass()";
}

#endif // SQL_PASSES
139 changes: 139 additions & 0 deletions include/sql/Passes/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#pragma once

#include "mlir/Dialect/Affine/IR/AffineOps.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/IR/BlockAndValueMapping.h"
#include "mlir/IR/IntegerSet.h"

static inline mlir::scf::IfOp
cloneWithResults(mlir::scf::IfOp op, mlir::OpBuilder &rewriter,
mlir::BlockAndValueMapping mapping = {}) {
using namespace mlir;
return rewriter.create<scf::IfOp>(op.getLoc(), op.getResultTypes(),
mapping.lookupOrDefault(op.getCondition()),
true);
}
static inline mlir::AffineIfOp
cloneWithResults(mlir::AffineIfOp op, mlir::OpBuilder &rewriter,
mlir::BlockAndValueMapping mapping = {}) {
using namespace mlir;
SmallVector<mlir::Value> lower;
for (auto o : op.getOperands())
lower.push_back(mapping.lookupOrDefault(o));
return rewriter.create<AffineIfOp>(op.getLoc(), op.getResultTypes(),
op.getIntegerSet(), lower, true);
}

static inline mlir::scf::IfOp
cloneWithoutResults(mlir::scf::IfOp op, mlir::OpBuilder &rewriter,
mlir::BlockAndValueMapping mapping = {},
mlir::TypeRange types = {}) {
using namespace mlir;
return rewriter.create<scf::IfOp>(
op.getLoc(), types, mapping.lookupOrDefault(op.getCondition()), true);
}
static inline mlir::AffineIfOp
cloneWithoutResults(mlir::AffineIfOp op, mlir::OpBuilder &rewriter,
mlir::BlockAndValueMapping mapping = {},
mlir::TypeRange types = {}) {
using namespace mlir;
SmallVector<mlir::Value> lower;
for (auto o : op.getOperands())
lower.push_back(mapping.lookupOrDefault(o));
return rewriter.create<AffineIfOp>(op.getLoc(), types, op.getIntegerSet(),
lower, true);
}

static inline mlir::scf::ForOp
cloneWithoutResults(mlir::scf::ForOp op, mlir::PatternRewriter &rewriter,
mlir::BlockAndValueMapping mapping = {}) {
using namespace mlir;
return rewriter.create<scf::ForOp>(
op.getLoc(), mapping.lookupOrDefault(op.getLowerBound()),
mapping.lookupOrDefault(op.getUpperBound()),
mapping.lookupOrDefault(op.getStep()));
}
static inline mlir::AffineForOp
cloneWithoutResults(mlir::AffineForOp op, mlir::PatternRewriter &rewriter,
mlir::BlockAndValueMapping mapping = {}) {
using namespace mlir;
SmallVector<Value> lower;
for (auto o : op.getLowerBoundOperands())
lower.push_back(mapping.lookupOrDefault(o));
SmallVector<Value> upper;
for (auto o : op.getUpperBoundOperands())
upper.push_back(mapping.lookupOrDefault(o));
return rewriter.create<AffineForOp>(op.getLoc(), lower, op.getLowerBoundMap(),
upper, op.getUpperBoundMap(),
op.getStep());
}

static inline void clearBlock(mlir::Block *block,
mlir::PatternRewriter &rewriter) {
for (auto &op : llvm::make_early_inc_range(llvm::reverse(*block))) {
assert(op.use_empty() && "expected 'op' to have no uses");
rewriter.eraseOp(&op);
}
}

static inline mlir::Block *getThenBlock(mlir::scf::IfOp op) {
return op.thenBlock();
}
static inline mlir::Block *getThenBlock(mlir::AffineIfOp op) {
return op.getThenBlock();
}
static inline mlir::Block *getElseBlock(mlir::scf::IfOp op) {
return op.elseBlock();
}
static inline mlir::Block *getElseBlock(mlir::AffineIfOp op) {
if (op.hasElse())
return op.getElseBlock();
else
return nullptr;
}

static inline mlir::Region &getThenRegion(mlir::scf::IfOp op) {
return op.getThenRegion();
}
static inline mlir::Region &getThenRegion(mlir::AffineIfOp op) {
return op.getThenRegion();
}
static inline mlir::Region &getElseRegion(mlir::scf::IfOp op) {
return op.getElseRegion();
}
static inline mlir::Region &getElseRegion(mlir::AffineIfOp op) {
return op.getElseRegion();
}

static inline mlir::scf::YieldOp getThenYield(mlir::scf::IfOp op) {
return op.thenYield();
}
static inline mlir::AffineYieldOp getThenYield(mlir::AffineIfOp op) {
return llvm::cast<mlir::AffineYieldOp>(op.getThenBlock()->getTerminator());
}
static inline mlir::scf::YieldOp getElseYield(mlir::scf::IfOp op) {
return op.elseYield();
}
static inline mlir::AffineYieldOp getElseYield(mlir::AffineIfOp op) {
return llvm::cast<mlir::AffineYieldOp>(op.getElseBlock()->getTerminator());
}

static inline bool inBound(mlir::scf::IfOp op, mlir::Value v) {
return op.getCondition() == v;
}
static inline bool inBound(mlir::AffineIfOp op, mlir::Value v) {
return llvm::any_of(op.getOperands(), [&](mlir::Value e) { return e == v; });
}
static inline bool inBound(mlir::scf::ForOp op, mlir::Value v) {
return op.getUpperBound() == v;
}
static inline bool inBound(mlir::AffineForOp op, mlir::Value v) {
return llvm::any_of(op.getUpperBoundOperands(),
[&](mlir::Value e) { return e == v; });
}
static inline bool hasElse(mlir::scf::IfOp op) {
return op.getElseRegion().getBlocks().size() > 0;
}
static inline bool hasElse(mlir::AffineIfOp op) {
return op.getElseRegion().getBlocks().size() > 0;
}
16 changes: 16 additions & 0 deletions include/sql/SQLDialect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//===- SQLDialect.h - SQL dialect -----------------*- C++ -*-===//
//
carlguo866 marked this conversation as resolved.
Show resolved Hide resolved
// This file is licensed 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 SQL_DIALECT_H
#define SQL_DIALECT_H

#include "mlir/IR/Dialect.h"

#include "sql/SQLOpsDialect.h.inc"

#endif
41 changes: 41 additions & 0 deletions include/sql/SQLDialect.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//===- SQLDialect.td - SQL dialect -----------*- tablegen -*-===//
//
carlguo866 marked this conversation as resolved.
Show resolved Hide resolved
// This file is licensed 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 SQL_DIALECT
#define SQL_DIALECT



include "mlir/IR/FunctionInterfaces.td"
include "mlir/IR/SymbolInterfaces.td"
include "mlir/Interfaces/SideEffectInterfaces.td"
include "mlir/Interfaces/CallInterfaces.td"
include "mlir/Interfaces/CastInterfaces.td"


def SQL_Dialect : Dialect {
let summary = "A dialect for SQL languages in MLIR.";
let description = [{
TBD
}];
let name = "sql";
let cppNamespace = "::mlir::sql";

let useDefaultTypePrinterParser = 1;
let extraClassDeclaration = [{
void registerTypes();
}];
}


#endif // SQL_DIALECT





Loading
Loading