Skip to content

Commit

Permalink
SQLLower
Browse files Browse the repository at this point in the history
  • Loading branch information
carlguo866 committed Mar 3, 2023
1 parent 465d74a commit 5441404
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 110 deletions.
110 changes: 0 additions & 110 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,110 +0,0 @@
cmake_minimum_required(VERSION 3.10)

include(CheckCXXSourceCompiles)

set(POLYGEIST_ENABLE_CUDA 0 CACHE BOOL "Enable CUDA compilation support")

if(POLICY CMP0068)
cmake_policy(SET CMP0068 NEW)
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
endif()

if(POLICY CMP0075)
cmake_policy(SET CMP0075 NEW)
endif()

if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
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)

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

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

set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")

message(STATUS "Searching for MLIRConfig.cmake in: ${MLIR_DIR}")
find_package(MLIR REQUIRED CONFIG)

set(Clang_DIR ${CLANG_DIR})
message(STATUS "Searching for ClangConfig.cmake in: ${Clang_DIR}")
find_package(Clang REQUIRED CONFIG)

# This is exported if we are building against a build area. If
# building against an install area, then assume we're using the
# submodule.
if(NOT LLVM_BUILD_MAIN_SRC_DIR)
set(LLVM_BUILD_MAIN_SRC_DIR ${CMAKE_SOURCE_DIR}/llvm-project/llvm)
endif()
set(LLVM_SOURCE_DIR ${LLVM_BUILD_MAIN_SRC_DIR} CACHE STRING "Location of LLVM source")

message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "Using ClangConfig.cmake in: ${CLANG_DIR}")
else ()
set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir)
set(MLIR_INCLUDE_DIRS ${MLIR_MAIN_SRC_DIR}/include)
set(MLIR_CMAKE_DIR ${MLIR_MAIN_SRC_DIR}/cmake/modules)
set(MLIR_TABLEGEN_EXE $<TARGET_FILE:mlir-tblgen>)
set(MLIR_TABLEGEN_OUTPUT_DIR ${LLVM_BINARY_DIR}/tools/mlir/include)
set(PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(POLYGEIST_TABLEGEN_OUTPUT_DIR ${LLVM_BINARY_DIR}/tools/polygeist/include)
include_directories(${MLIR_TABLEGEN_OUTPUT_DIR})
include_directories(${POLYGEIST_TABLEGEN_OUTPUT_DIR})
endif()

function(append value)
foreach(variable ${ARGN})
set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
endforeach(variable)
endfunction()

if( POLYGEIST_USE_LINKER )
append("-fuse-ld=${POLYGEIST_USE_LINKER}"
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
check_cxx_source_compiles("int main() { return 0; }" CXX_SUPPORTS_CUSTOM_LINKER)
if ( NOT CXX_SUPPORTS_CUSTOM_LINKER )
message(FATAL_ERROR "Host compiler does not support '-fuse-ld=${LLVM_USE_LINKER}'")
endif()
endif()

list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${CLANG_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
include(TableGen)
include(AddLLVM)
include(AddMLIR)
include(AddClang)
include(HandleLLVMOptions)

set(POLYGEIST_TOOLS_DIR ${CMAKE_BINARY_DIR})

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${CLANG_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_BINARY_DIR}/include)
link_directories(${LLVM_BUILD_LIBRARY_DIR})
add_definitions(${LLVM_DEFINITIONS})

set(POLYGEIST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(POLYGEIST_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(POLYGEIST_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include )
set(POLYGEIST_TOOLS_DIR ${CMAKE_BINARY_DIR}/bin)
set(POLYGEIST_UTILS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/utils)

set(LLVM_LIT_ARGS "-sv" CACHE STRING "lit default options")
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
include(sanitizers)

add_subdirectory(include)
add_subdirectory(lib)
add_subdirectory(tools)
add_subdirectory(test)
2 changes: 2 additions & 0 deletions include/sql/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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)
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)
48 changes: 48 additions & 0 deletions include/sql/Passes/Passes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#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> createParallelLowerPass();
} // 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
14 changes: 14 additions & 0 deletions include/sql/Passes/Passes.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef SQL_PASSES
#define SQL_PASSES

include "mlir/Pass/PassBase.td"


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

#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;
}
17 changes: 17 additions & 0 deletions lib/sql/Passes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add_mlir_dialect_library(MLIRSQLTransforms
SQLLower.cpp

DEPENDS
MLIRPolygeistOpsIncGen
MLIRPolygeistPassIncGen

LINK_LIBS PUBLIC
MLIRArithDialect
MLIRFuncDialect
MLIRFuncTransforms
MLIRIR
MLIRLLVMDialect
MLIRMathDialect
MLIRMemRefDialect
MLIRPass
)
Loading

0 comments on commit 5441404

Please sign in to comment.