Skip to content

Commit

Permalink
Fix test and update coding style
Browse files Browse the repository at this point in the history
This closes #874
  • Loading branch information
karthijrk committed Jun 29, 2016
1 parent 1b26fbf commit 45f2aa9
Show file tree
Hide file tree
Showing 24 changed files with 576 additions and 361 deletions.
1 change: 0 additions & 1 deletion src/backend/codegen/codegen_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ using gpcodegen::ExecEvalExprCodegen;

// Current code generator manager that oversees all code generators
static void* ActiveCodeGeneratorManager = nullptr;
static bool is_codegen_initalized = false;

extern bool codegen; // defined from guc
extern bool init_codegen; // defined from guc
Expand Down
24 changes: 13 additions & 11 deletions src/backend/codegen/const_expr_tree_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// Copyright (C) 2016 Pivotal Software, Inc.
//
// @filename:
// base_codegen.h
// const_expr_tree_generator.cc
//
// @doc:
// Base class for expression tree to generate code
// Object that generate code for const expression.
//
//---------------------------------------------------------------------------

Expand All @@ -16,8 +16,7 @@
#include "llvm/IR/Value.h"

extern "C" {
#include "postgres.h"
#include "utils/elog.h"
#include "postgres.h" // NOLINT(build/include)
#include "nodes/execnodes.h"
}

Expand All @@ -27,24 +26,27 @@ using gpcodegen::ExprTreeGenerator;
bool ConstExprTreeGenerator::VerifyAndCreateExprTree(
ExprState* expr_state,
ExprContext* econtext,
std::unique_ptr<ExprTreeGenerator>& expr_tree) {
assert(nullptr != expr_state && nullptr != expr_state->expr && T_Const == nodeTag(expr_state->expr));
expr_tree.reset(new ConstExprTreeGenerator(expr_state));
std::unique_ptr<ExprTreeGenerator>* expr_tree) {
assert(nullptr != expr_state &&
nullptr != expr_state->expr &&
T_Const == nodeTag(expr_state->expr) &&
nullptr != expr_tree);
expr_tree->reset(new ConstExprTreeGenerator(expr_state));
return true;
}

ConstExprTreeGenerator::ConstExprTreeGenerator(ExprState* expr_state) :
ExprTreeGenerator(expr_state, ExprTreeNodeType::kConst) {

}

bool ConstExprTreeGenerator::GenerateCode(CodegenUtils* codegen_utils,
ExprContext* econtext,
llvm::Function* llvm_main_func,
llvm::BasicBlock* llvm_error_block,
llvm::Value* llvm_isnull_arg,
llvm::Value* & value) {
Const* const_expr = (Const*)expr_state()->expr;
value = codegen_utils->GetConstant(const_expr->constvalue);
llvm::Value** llvm_out_value) {
assert(nullptr != llvm_out_value);
Const* const_expr = reinterpret_cast<Const*>(expr_state()->expr);
*llvm_out_value = codegen_utils->GetConstant(const_expr->constvalue);
return true;
}
89 changes: 9 additions & 80 deletions src/backend/codegen/exec_eval_expr_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "llvm/Support/Casting.h"

extern "C" {
#include "postgres.h"
#include "postgres.h" // NOLINT(build/include)
#include "utils/elog.h"
#include "nodes/execnodes.h"
}
Expand All @@ -47,69 +47,6 @@ using gpcodegen::ExecEvalExprCodegen;

constexpr char ExecEvalExprCodegen::kExecEvalExprPrefix[];

class ElogWrapper {
public:
ElogWrapper(gpcodegen::CodegenUtils* codegen_utils) :
codegen_utils_(codegen_utils) {
SetupElog();
}
~ElogWrapper() {
TearDownElog();
}

template<typename... V>
void CreateElog(
llvm::Value* llvm_elevel,
llvm::Value* llvm_fmt,
V ... args ) {

assert(NULL != llvm_elevel);
assert(NULL != llvm_fmt);

codegen_utils_->ir_builder()->CreateCall(
llvm_elog_start_, {
codegen_utils_->GetConstant(""), // Filename
codegen_utils_->GetConstant(0), // line number
codegen_utils_->GetConstant("") // function name
});
codegen_utils_->ir_builder()->CreateCall(
llvm_elog_finish_, {
llvm_elevel,
llvm_fmt,
args...
});
}
template<typename... V>
void CreateElog(
int elevel,
const char* fmt,
V ... args ) {
CreateElog(codegen_utils_->GetConstant(elevel),
codegen_utils_->GetConstant(fmt),
args...);
}
private:
llvm::Function* llvm_elog_start_;
llvm::Function* llvm_elog_finish_;

gpcodegen::CodegenUtils* codegen_utils_;

void SetupElog(){
assert(codegen_utils_ != nullptr);
llvm_elog_start_ = codegen_utils_->RegisterExternalFunction(elog_start);
assert(llvm_elog_start_ != nullptr);
llvm_elog_finish_ = codegen_utils_->RegisterExternalFunction(elog_finish);
assert(llvm_elog_finish_ != nullptr);
}

void TearDownElog(){
llvm_elog_start_ = nullptr;
llvm_elog_finish_ = nullptr;
}

};


ExecEvalExprCodegen::ExecEvalExprCodegen
(
ExecEvalExprFn regular_func_ptr,
Expand All @@ -119,22 +56,20 @@ ExecEvalExprCodegen::ExecEvalExprCodegen
BaseCodegen(kExecEvalExprPrefix,
regular_func_ptr, ptr_to_regular_func_ptr),
exprstate_(exprstate),
econtext_(econtext){
econtext_(econtext) {
}


bool ExecEvalExprCodegen::GenerateExecEvalExpr(
gpcodegen::CodegenUtils* codegen_utils) {
gpcodegen::GpCodegenUtils* codegen_utils) {

assert(NULL != codegen_utils);
if (nullptr == exprstate_ ||
nullptr == exprstate_->expr ||
nullptr == econtext_) {
return false;
}

ElogWrapper elogwrapper(codegen_utils);
//TODO : krajaraman move to better place
// TODO(krajaraman): move to better place
OpExprTreeGenerator::InitializeSupportedFunction();

llvm::Function* exec_eval_expr_func = CreateFunction<ExecEvalExprFn>(
Expand All @@ -147,10 +82,6 @@ bool ExecEvalExprCodegen::GenerateExecEvalExpr(
llvm::Value* llvm_isnull_arg = ArgumentByPosition(exec_eval_expr_func, 2);
llvm::Value* llvm_isDone_arg = ArgumentByPosition(exec_eval_expr_func, 3);

// External functions
llvm::Function* llvm_slot_getattr =
codegen_utils->RegisterExternalFunction(slot_getattr);

// BasicBlock of function entry.
llvm::BasicBlock* llvm_entry_block = codegen_utils->CreateBasicBlock(
"entry", exec_eval_expr_func);
Expand All @@ -161,14 +92,14 @@ bool ExecEvalExprCodegen::GenerateExecEvalExpr(

irb->SetInsertPoint(llvm_entry_block);

elogwrapper.CreateElog(
codegen_utils->CreateElog(
DEBUG1,
"Calling codegen'ed expression evaluation");

// Check if we can codegen. If so create ExprTreeGenerator
std::unique_ptr<ExprTreeGenerator> expr_tree_generator(nullptr);
bool can_generate = ExprTreeGenerator::VerifyAndCreateExprTree(
exprstate_, econtext_, expr_tree_generator);
exprstate_, econtext_, &expr_tree_generator);
if (!can_generate ||
expr_tree_generator.get() == nullptr) {
return false;
Expand All @@ -181,7 +112,7 @@ bool ExecEvalExprCodegen::GenerateExecEvalExpr(
exec_eval_expr_func,
llvm_error_block,
llvm_isnull_arg,
value);
&value);
if (!is_generated ||
nullptr == value) {
return false;
Expand All @@ -192,19 +123,17 @@ bool ExecEvalExprCodegen::GenerateExecEvalExpr(

irb->SetInsertPoint(llvm_error_block);
irb->CreateRet(codegen_utils->GetConstant<int64_t>(0));
exec_eval_expr_func->dump();
return true;
}


bool ExecEvalExprCodegen::GenerateCodeInternal(CodegenUtils* codegen_utils) {
bool ExecEvalExprCodegen::GenerateCodeInternal(GpCodegenUtils* codegen_utils) {
bool isGenerated = GenerateExecEvalExpr(codegen_utils);

if (isGenerated) {
elog(DEBUG1, "ExecEvalExpr was generated successfully!");
return true;
}
else {
} else {
elog(DEBUG1, "ExecEvalExpr generation failed!");
return false;
}
Expand Down
14 changes: 8 additions & 6 deletions src/backend/codegen/exec_variable_list_codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "llvm/Support/Casting.h"

extern "C" {
#include "postgres.h"
#include "postgres.h" // NOLINT(build/include)
#include "utils/elog.h"
#include "access/htup.h"
#include "nodes/execnodes.h"
Expand Down Expand Up @@ -133,14 +133,16 @@ bool ExecVariableListCodegen::GenerateExecVariableList(
"fallback", exec_variable_list_func);

// External functions
llvm::Function* llvm_memset = codegen_utils->GetOrRegisterExternalFunction(memset);
llvm::Function* llvm_memset = codegen_utils->GetOrRegisterExternalFunction(
memset);

// Generation-time constants
llvm::Value* llvm_max_attr = codegen_utils->GetConstant(max_attr);
llvm::Value* llvm_slot = codegen_utils->GetConstant(slot_);

// Function arguments to ExecVariableList
llvm::Value* llvm_projInfo_arg = ArgumentByPosition(exec_variable_list_func, 0);
llvm::Value* llvm_projInfo_arg = ArgumentByPosition(exec_variable_list_func,
0);
llvm::Value* llvm_values_arg = ArgumentByPosition(exec_variable_list_func, 1);
llvm::Value* llvm_isnull_arg = ArgumentByPosition(exec_variable_list_func, 2);

Expand Down Expand Up @@ -404,7 +406,6 @@ bool ExecVariableListCodegen::GenerateExecVariableList(
irb->SetInsertPoint(is_not_null_block);
} // End of if ( !thisatt->attnotnull )

// TODO : hardikar & nikos verify compatibility with 8.3 merge
off = att_align_nominal(off, thisatt->attalign);

// values[attnum] = fetchatt(thisatt, tp + off) {{{
Expand Down Expand Up @@ -487,8 +488,9 @@ bool ExecVariableListCodegen::GenerateExecVariableList(
llvm::Value* llvm_slot_PRIVATE_tts_off_ptr /* long* */ =
codegen_utils->GetPointerToMember(
llvm_slot, &TupleTableSlot::PRIVATE_tts_off);
irb->CreateStore(codegen_utils->GetConstant<long>(off),
llvm_slot_PRIVATE_tts_off_ptr);
irb->CreateStore(
codegen_utils->GetConstant<long>(off), // NOLINT(runtime/int)
llvm_slot_PRIVATE_tts_off_ptr);

// slot->PRIVATE_tts_nvalid = attnum;
irb->CreateStore(llvm_max_attr, llvm_slot_PRIVATE_tts_nvalid_ptr);
Expand Down
36 changes: 18 additions & 18 deletions src/backend/codegen/expr_tree_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (C) 2016 Pivotal Software, Inc.
//
// @filename:
// base_codegen.h
// expr_tree_generator.cc
//
// @doc:
// Base class for expression tree to generate code
Expand All @@ -18,7 +18,7 @@
#include "llvm/IR/Value.h"

extern "C" {
#include "postgres.h"
#include "postgres.h" // NOLINT(build/include)
#include "utils/elog.h"
#include "nodes/execnodes.h"
}
Expand All @@ -28,35 +28,35 @@ using gpcodegen::ExprTreeGenerator;
bool ExprTreeGenerator::VerifyAndCreateExprTree(
ExprState* expr_state,
ExprContext* econtext,
std::unique_ptr<ExprTreeGenerator>& expr_tree) {
assert(nullptr != expr_state && nullptr != expr_state->expr);
expr_tree.reset(nullptr);
std::unique_ptr<ExprTreeGenerator>* expr_tree) {
assert(nullptr != expr_state &&
nullptr != expr_state->expr &&
nullptr != expr_tree);
expr_tree->reset(nullptr);
bool supported_expr_tree = false;
switch(nodeTag(expr_state->expr)) {
switch (nodeTag(expr_state->expr)) {
case T_OpExpr: {
supported_expr_tree = OpExprTreeGenerator::VerifyAndCreateExprTree(expr_state,
econtext,
expr_tree);
supported_expr_tree = OpExprTreeGenerator::VerifyAndCreateExprTree(
expr_state, econtext, expr_tree);
break;
}
case T_Var: {
supported_expr_tree = VarExprTreeGenerator::VerifyAndCreateExprTree(expr_state,
econtext,
expr_tree);
supported_expr_tree = VarExprTreeGenerator::VerifyAndCreateExprTree(
expr_state, econtext, expr_tree);
break;
}
case T_Const: {
supported_expr_tree = ConstExprTreeGenerator::VerifyAndCreateExprTree(expr_state,
econtext,
expr_tree);
supported_expr_tree = ConstExprTreeGenerator::VerifyAndCreateExprTree(
expr_state, econtext, expr_tree);
break;
}
default : {
supported_expr_tree = false;
elog(DEBUG1, "Unsupported expression tree %d found", nodeTag(expr_state->expr));
elog(DEBUG1, "Unsupported expression tree %d found",
nodeTag(expr_state->expr));
}
}
assert((!supported_expr_tree && nullptr == expr_tree.get()) ||
(supported_expr_tree && nullptr != expr_tree.get()));
assert((!supported_expr_tree && nullptr == expr_tree->get()) ||
(supported_expr_tree && nullptr != expr_tree->get()));
return supported_expr_tree;
}
3 changes: 2 additions & 1 deletion src/backend/codegen/include/codegen/base_codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ class BaseCodegen: public CodegenInterface {
* @param codegen_utils Utility to ease the code generation process.
* @return true on successful generation.
**/
virtual bool GenerateCodeInternal(gpcodegen::GpCodegenUtils* codegen_utils) = 0;
virtual bool GenerateCodeInternal(
gpcodegen::GpCodegenUtils* codegen_utils) = 0;

/**
* @brief Create llvm Function for given type and store the function pointer
Expand Down
Loading

0 comments on commit 45f2aa9

Please sign in to comment.