Skip to content

Commit c7db3f9

Browse files
committed
sql lowering workings with pragma
1 parent 946fdb2 commit c7db3f9

File tree

8 files changed

+354
-22
lines changed

8 files changed

+354
-22
lines changed

CMakeLists.txt

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
include(CheckCXXSourceCompiles)
4+
5+
set(POLYGEIST_ENABLE_CUDA 0 CACHE BOOL "Enable CUDA compilation support")
6+
7+
if(POLICY CMP0068)
8+
cmake_policy(SET CMP0068 NEW)
9+
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
10+
endif()
11+
12+
if(POLICY CMP0075)
13+
cmake_policy(SET CMP0075 NEW)
14+
endif()
15+
16+
if(POLICY CMP0077)
17+
cmake_policy(SET CMP0077 NEW)
18+
endif()
19+
20+
option(LLVM_INCLUDE_TOOLS "Generate build targets for the LLVM tools." ON)
21+
option(LLVM_BUILD_TOOLS "Build the LLVM tools. If OFF, just generate build targets." ON)
22+
23+
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
24+
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
25+
26+
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
27+
project(polygeist LANGUAGES CXX C)
28+
29+
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
30+
31+
message(STATUS "Searching for MLIRConfig.cmake in: ${MLIR_DIR}")
32+
find_package(MLIR REQUIRED CONFIG)
33+
34+
set(Clang_DIR ${CLANG_DIR})
35+
message(STATUS "Searching for ClangConfig.cmake in: ${Clang_DIR}")
36+
find_package(Clang REQUIRED CONFIG)
37+
38+
# This is exported if we are building against a build area. If
39+
# building against an install area, then assume we're using the
40+
# submodule.
41+
if(NOT LLVM_BUILD_MAIN_SRC_DIR)
42+
set(LLVM_BUILD_MAIN_SRC_DIR ${CMAKE_SOURCE_DIR}/llvm-project/llvm)
43+
endif()
44+
set(LLVM_SOURCE_DIR ${LLVM_BUILD_MAIN_SRC_DIR} CACHE STRING "Location of LLVM source")
45+
46+
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
47+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
48+
message(STATUS "Using ClangConfig.cmake in: ${CLANG_DIR}")
49+
else ()
50+
set(LLVM_SOURCE_DIR ${LLVM_MAIN_SRC_DIR})
51+
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir)
52+
set(MLIR_INCLUDE_DIRS ${MLIR_MAIN_SRC_DIR}/include)
53+
set(MLIR_CMAKE_DIR ${MLIR_MAIN_SRC_DIR}/cmake/modules)
54+
set(MLIR_TABLEGEN_EXE $<TARGET_FILE:mlir-tblgen>)
55+
set(MLIR_TABLEGEN_OUTPUT_DIR ${LLVM_BINARY_DIR}/tools/mlir/include)
56+
set(PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
57+
set(POLYGEIST_TABLEGEN_OUTPUT_DIR ${LLVM_BINARY_DIR}/tools/polygeist/include)
58+
include_directories(${MLIR_TABLEGEN_OUTPUT_DIR})
59+
include_directories(${POLYGEIST_TABLEGEN_OUTPUT_DIR})
60+
endif()
61+
62+
function(append value)
63+
foreach(variable ${ARGN})
64+
set(${variable} "${${variable}} ${value}" PARENT_SCOPE)
65+
endforeach(variable)
66+
endfunction()
67+
68+
if( POLYGEIST_USE_LINKER )
69+
append("-fuse-ld=${POLYGEIST_USE_LINKER}"
70+
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
71+
check_cxx_source_compiles("int main() { return 0; }" CXX_SUPPORTS_CUSTOM_LINKER)
72+
if ( NOT CXX_SUPPORTS_CUSTOM_LINKER )
73+
message(FATAL_ERROR "Host compiler does not support '-fuse-ld=${LLVM_USE_LINKER}'")
74+
endif()
75+
endif()
76+
77+
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
78+
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
79+
list(APPEND CMAKE_MODULE_PATH "${CLANG_CMAKE_DIR}")
80+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
81+
include(TableGen)
82+
include(AddLLVM)
83+
include(AddMLIR)
84+
include(AddClang)
85+
include(HandleLLVMOptions)
86+
87+
set(POLYGEIST_TOOLS_DIR ${CMAKE_BINARY_DIR})
88+
89+
include_directories(${LLVM_INCLUDE_DIRS})
90+
include_directories(${CLANG_INCLUDE_DIRS})
91+
include_directories(${MLIR_INCLUDE_DIRS})
92+
include_directories(${PROJECT_SOURCE_DIR}/include)
93+
include_directories(${PROJECT_BINARY_DIR}/include)
94+
link_directories(${LLVM_BUILD_LIBRARY_DIR})
95+
add_definitions(${LLVM_DEFINITIONS})
96+
97+
set(POLYGEIST_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
98+
set(POLYGEIST_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
99+
set(POLYGEIST_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include )
100+
set(POLYGEIST_TOOLS_DIR ${CMAKE_BINARY_DIR}/bin)
101+
set(POLYGEIST_UTILS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/utils)
102+
103+
set(LLVM_LIT_ARGS "-sv" CACHE STRING "lit default options")
104+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/modules")
105+
include(sanitizers)
106+
107+
add_subdirectory(include)
108+
add_subdirectory(lib)
109+
add_subdirectory(tools)
110+
add_subdirectory(test)

include/sql/Passes/Passes.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ class RewritePatternSet;
1010
class DominanceInfo;
1111
namespace sql {
1212

13-
std::unique_ptr<Pass> createParallelLowerPass();
13+
std::unique_ptr<Pass> createSQLLowerPass();
14+
std::unique_ptr<Pass> createSQLRaisingPass();
1415
} // namespace sql
1516
} // namespace mlir
1617

18+
19+
1720
namespace mlir {
1821
// Forward declaration from Dialect.h
1922
template <typename ConcreteDialect>

include/sql/Passes/Passes.td

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
include "mlir/Pass/PassBase.td"
55

66

7-
def ParallelLower : Pass<"sql-lower", "mlir::ModuleOp"> {
7+
def SQLLower : Pass<"sql-lower", "mlir::ModuleOp"> {
88
let summary = "Lower sql op to mlir";
99
let dependentDialects =
10-
["arith::AirthDialect", "func::FuncDialect", "LLVM::LLVMDialect"];
10+
["arith::ArithDialect", "func::FuncDialect", "LLVM::LLVMDialect"];
1111
let constructor = "mlir::sql::createSQLLowerPass()";
1212
}
1313

14+
15+
def SQLRaising : Pass<"sql-raising", "mlir::ModuleOp"> {
16+
let summary = "Raise sql op to mlir";
17+
let dependentDialects =
18+
["arith::ArithDialect", "func::FuncDialect", "LLVM::LLVMDialect"];
19+
let constructor = "mlir::sql::createSQLRaisingPass()";
20+
}
21+
1422
#endif // SQL_PASSES

lib/sql/Passes/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
add_mlir_dialect_library(MLIRSQLTransforms
22
SQLLower.cpp
3+
SQLRaising.cpp
34

45
DEPENDS
56
MLIRPolygeistOpsIncGen
67
MLIRPolygeistPassIncGen
8+
MLIRSQLPassIncGen
79

810
LINK_LIBS PUBLIC
911
MLIRArithDialect

lib/sql/Passes/PassDetails.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===- PassDetails.h - polygeist pass class details ----------------*- C++
2+
//-*-===//
3+
//
4+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// Stuff shared between the different polygeist passes.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
// clang-tidy seems to expect the absolute path in the header guard on some
15+
// systems, so just disable it.
16+
// NOLINTNEXTLINE(llvm-header-guard)
17+
#ifndef DIALECT_POLYGEIST_TRANSFORMS_PASSDETAILS_H
18+
#define DIALECT_POLYGEIST_TRANSFORMS_PASSDETAILS_H
19+
20+
#include "mlir/Pass/Pass.h"
21+
#include "sql/SQLOps.h"
22+
#include "sql/Passes/Passes.h"
23+
24+
namespace mlir {
25+
class FunctionOpInterface;
26+
// Forward declaration from Dialect.h
27+
template <typename ConcreteDialect>
28+
void registerDialect(DialectRegistry &registry);
29+
namespace sql {
30+
31+
class SQLDialect;
32+
33+
#define GEN_PASS_CLASSES
34+
#include "sql/Passes/Passes.h.inc"
35+
36+
} // namespace polygeist
37+
} // namespace mlir
38+
39+
#endif // DIALECT_POLYGEIST_TRANSFORMS_PASSDETAILS_H

lib/sql/Passes/SQLLower.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- SQLLower.cpp - Lower sql ops to mlir ------ -*-===//
1+
//===- SQLLower.cpp - Lower PostgreSQL to sql mlir ops ------ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -21,15 +21,17 @@
2121
#include "mlir/Transforms/Passes.h"
2222
#include "llvm/ADT/SetVector.h"
2323
#include "llvm/ADT/SmallPtrSet.h"
24+
#include "sql/SQLOps.h"
25+
#include "sql/Passes/Passes.h"
2426
#include <algorithm>
2527
#include <mutex>
2628

27-
#define DEBUG_TYPE "sql-opt"
29+
#define DEBUG_TYPE "sql-lower-opt"
2830

2931
using namespace mlir;
3032
using namespace mlir::arith;
3133
using namespace mlir::func;
32-
using namespace sql;
34+
using namespace mlir::sql;
3335

3436
namespace {
3537
struct SQLLower : public SQLLowerBase<SQLLower> {
@@ -45,26 +47,33 @@ struct NumResultsOpLowering : public OpRewritePattern<sql::NumResultsOp> {
4547
PatternRewriter &rewriter) const final {
4648
auto module = loop->getParentOfType<ModuleOp>();
4749

50+
SymbolTableCollection symbolTable;
51+
symbolTable.getSymbolTable(loop);
52+
4853
// 1) make sure the postgres_getresult function is declared
49-
auto rowsfn = dyn_cast_or_null<func::FuncOp>(symbolTable.lookupSymbolIn(
50-
module, builder.getStringAttr("PQcmdTuples")));
54+
auto rowsfn = dyn_cast_or_null<func::FuncOp>(
55+
symbolTable.lookupSymbolIn(module, rewriter.getStringAttr("PQcmdTuples")));
5156

5257
auto atoifn = dyn_cast_or_null<func::FuncOp>(
53-
symbolTable.lookupSymbolIn(module, builder.getStringAttr("atoi")));
58+
symbolTable.lookupSymbolIn(module, rewriter.getStringAttr("atoi")));
5459

5560
// 2) convert the args to valid args to postgres_getresult abi
5661
Value arg = loop.getHandle();
5762
arg = rewriter.create<arith::IndexCastOp>(loop.getLoc(),
58-
rewriter.getIntTy(64), arg);
63+
rewriter.getI64Type(), arg);
5964
arg = rewriter.create<LLVM::IntToPtrOp>(
60-
loop.getLoc(), LLVM::LLVMPointerType::get(builder.getInt8Ty()), arg);
65+
loop.getLoc(), LLVM::LLVMPointerType::get(rewriter.getI8Type()), arg);
6166

6267
// 3) call and replace
63-
Value args[] = {arg} Value res =
68+
Value args[] = {arg};
69+
70+
Value res =
6471
rewriter.create<mlir::func::CallOp>(loop.getLoc(), rowsfn, args)
6572
->getResult(0);
6673

67-
Value args2[] = {res} Value res2 =
74+
Value args2[] = {res};
75+
76+
Value res2 =
6877
rewriter.create<mlir::func::CallOp>(loop.getLoc(), atoifn, args2)
6978
->getResult(0);
7079

@@ -78,29 +87,32 @@ struct NumResultsOpLowering : public OpRewritePattern<sql::NumResultsOp> {
7887

7988
void SQLLower::runOnOperation() {
8089
auto module = getOperation();
90+
91+
SymbolTableCollection symbolTable;
92+
symbolTable.getSymbolTable(module);
8193
OpBuilder builder(module.getContext());
8294
builder.setInsertionPointToStart(module.getBody());
8395

8496
if (!dyn_cast_or_null<func::FuncOp>(symbolTable.lookupSymbolIn(
8597
module, builder.getStringAttr("PQcmdTuples")))) {
86-
mlir::Type argtypes[] = {LLVM::LLVMPointerType::get(builder.getInt8Ty())};
87-
mlir::Type rettypes[] = {LLVM::LLVMPointerType::get(builder.getInt8Ty())};
98+
mlir::Type argtypes[] = {LLVM::LLVMPointerType::get(builder.getI8Type())};
99+
mlir::Type rettypes[] = {LLVM::LLVMPointerType::get(builder.getI8Type())};
88100

89101
auto fn =
90102
builder.create<func::FuncOp>(module.getLoc(), "PQcmdTuples",
91-
builder.getFunctionType(argtys, rettys));
92-
SymbolTable::setSymbolVisibility(fn, SymbolTable::Private);
103+
builder.getFunctionType(argtypes, rettypes));
104+
SymbolTable::setSymbolVisibility(fn, SymbolTable::Visibility::Private);
93105
}
94106
if (!dyn_cast_or_null<func::FuncOp>(
95107
symbolTable.lookupSymbolIn(module, builder.getStringAttr("atoi")))) {
96-
mlir::Type argtypes[] = {LLVM::LLVMPointerType::get(builder.getInt8Ty())};
108+
mlir::Type argtypes[] = {LLVM::LLVMPointerType::get(builder.getI8Type())};
97109

98110
// todo use data layout
99-
mlir::Type rettypes[] = {builder.getIntTy(sizeof(int))};
111+
mlir::Type rettypes[] = {builder.getI64Type()};
100112

101113
auto fn = builder.create<func::FuncOp>(
102-
module.getLoc(), "atoi", builder.getFunctionType(argtys, rettys));
103-
SymbolTable::setSymbolVisibility(fn, SymbolTable::Private);
114+
module.getLoc(), "atoi", builder.getFunctionType(argtypes, rettypes));
115+
SymbolTable::setSymbolVisibility(fn, SymbolTable::Visibility::Private);
104116
}
105117

106118
RewritePatternSet patterns(&getContext());
@@ -112,7 +124,7 @@ void SQLLower::runOnOperation() {
112124
}
113125

114126
namespace mlir {
115-
namespace polygeist {
127+
namespace sql {
116128
std::unique_ptr<Pass> createSQLLowerPass() {
117129
return std::make_unique<SQLLower>();
118130
}

0 commit comments

Comments
 (0)