Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fsaintjacques committed Dec 15, 2019
1 parent 9cbf398 commit 9a2c272
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 32 deletions.
16 changes: 15 additions & 1 deletion include/jitmap/query/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Module;
namespace jitmap {
namespace query {

class CompilerException : public util::Exception {
class CompilerException : public Exception {
public:
using Exception::Exception;
};
Expand All @@ -37,8 +37,22 @@ class QueryCompiler {
public:
QueryCompiler(const std::string& module_name, CompilerOptions options = {});

// Compile a query expression in an llvm::Function.
//
// - The query's name must be unique within the QueryCompiler object. It
// always returns the first inserted function irregardless of the
// expression.
// - The generated function is not executable as-is, it is only the LLVM IR
// representation.
//
// \param[in] query, the query to compile into LLVM IR.
//
// \return an llvm function object
//
// \throws CompilerExpception if any errors is encountered.
llvm::Function* Compile(const Query& query);

// Return the LLVM IR representation of the module and all the compiled queries.
std::string DumpIR() const;

~QueryCompiler();
Expand Down
2 changes: 1 addition & 1 deletion include/jitmap/query/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace jitmap {
namespace query {

class ParserException : public util::Exception {
class ParserException : public Exception {
public:
using Exception::Exception;
};
Expand Down
4 changes: 1 addition & 3 deletions include/jitmap/util/exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
#include <jitmap/util/fmt.h>

namespace jitmap {
namespace util {

class Exception {
public:
explicit Exception(std::string message) : message_(std::move(message)) {}

template <typename... Args>
Exception(Args&&... args) : Exception(StaticFmt(std::forward<Args>(args)...)) {}
Exception(Args&&... args) : Exception(util::StaticFmt(std::forward<Args>(args)...)) {}

const std::string& message() const { return message_; }

protected:
std::string message_;
};

} // namespace util
} // namespace jitmap
32 changes: 16 additions & 16 deletions src/jitmap/query/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ InputsOutputArguments PartitionFunctionArguments(llvm::Function* fn);
// Generate the hot section of the loop. Takes an expression and reduce it to a
// single (scalar or vector) value.
struct ExprCodeGenVisitor {
public:
std::unordered_map<std::string, llvm::Value*>& bitmaps;
llvm::IRBuilder<>& builder;
llvm::Type* vector_type;

llvm::Value* operator()(const VariableExpr& e) { return FindBitmapByName(e.value()); }

llvm::Value* operator()(const EmptyBitmapExpr& e) {
Expand All @@ -40,10 +45,6 @@ struct ExprCodeGenVisitor {
return builder.CreateNot(operand);
}

std::pair<llvm::Value*, llvm::Value*> VisitBinary(const BinaryOpExpr& e) {
return {e.left_operand()->Visit(*this), e.right_operand()->Visit(*this)};
}

llvm::Value* operator()(const AndOpExpr& e) {
auto [lhs, rhs] = VisitBinary(e);
return builder.CreateAnd(lhs, rhs);
Expand All @@ -59,16 +60,17 @@ struct ExprCodeGenVisitor {
return builder.CreateXor(lhs, rhs);
}

private:
llvm::Value* FindBitmapByName(const std::string& name) {
auto result = bitmaps.find(name);
if (result == bitmaps.end())
throw CompilerException("Referenced bitmap '", name, "' not found.");
return result->second;
}

std::unordered_map<std::string, llvm::Value*>& bitmaps;
llvm::IRBuilder<>& builder;
llvm::Type* vector_type;
std::pair<llvm::Value*, llvm::Value*> VisitBinary(const BinaryOpExpr& e) {
return {e.left_operand()->Visit(*this), e.right_operand()->Visit(*this)};
}
};

class QueryCompiler::Impl {
Expand Down Expand Up @@ -130,26 +132,24 @@ class QueryCompiler::Impl {
auto namify = [&i](std::string key) { return key + "_" + std::to_string(i); };
// Compute the address to load
auto gep = builder_.CreateInBoundsGEP(bitmap_addr, {loop_idx}, namify("gep"));
// Cast previous address as a vector-type.
// Cast previous address as a vector-type
auto bitcast = builder_.CreateBitCast(gep, VectorPtrType(), namify("bitcast"));
// Load in a register
return builder_.CreateLoad(bitcast, namify("load"));
};

std::vector<llvm::Value*> bitmaps;
for (size_t i = 0; i < inputs.size(); i++) {
bitmaps.push_back(load_vector_inst(inputs[i], i));
}

std::unordered_map<std::string, llvm::Value*> keyed_bitmaps;
// Bind the variable bitmaps by name to inputs of the function
const auto& parameters = query.parameters();
for (size_t i = 0; i < bitmaps.size(); i++) {
keyed_bitmaps.emplace(parameters[i], bitmaps[i]);
std::unordered_map<std::string, llvm::Value*> keyed_bitmaps;
for (size_t i = 0; i < inputs.size(); i++) {
keyed_bitmaps.emplace(parameters[i], load_vector_inst(inputs[i], i));
}

// Execute the expression tree on the input
ExprCodeGenVisitor visitor{keyed_bitmaps, builder_, VectorType()};
auto result = query.expr().Visit(visitor);

// Store the result in the output bitmap.
auto gep = builder_.CreateInBoundsGEP(output, {loop_idx}, "gep_output");
auto bitcast = builder_.CreateBitCast(gep, VectorPtrType(), "bitcast_output");
builder_.CreateStore(result, bitcast);
Expand Down
8 changes: 4 additions & 4 deletions src/jitmap/query/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Token Token::XorOp(std::string_view t) { return Token(Token::XOR_OPERATOR, t); }
Token Token::EoS(std::string_view t) { return Token(Token::END_OF_STREAM, t); }

constexpr char kEoFCharacter = '\0';
constexpr char kIndefRefCharacter = '$';
constexpr char kLiteralPrefixCharacter = '$';

static bool IsSpace(char c) { return std::isspace(c); }
static bool IsVariable(char c) { return std::isalnum(c) || c == '_'; }
Expand Down Expand Up @@ -95,7 +95,7 @@ char Lexer::Consume(char expected) {

Token Lexer::ConsumeLiteral() {
// Pop '$'.
Consume(kIndefRefCharacter);
Consume(kLiteralPrefixCharacter);

size_t start = position_;
if (Peek() == '0') {
Expand All @@ -106,7 +106,7 @@ Token Lexer::ConsumeLiteral() {
return Token::Full(query_.substr(start, 0));
}

throw ParserException("Index reference expects at least one digit");
throw ParserException("Invalid literal character ", Peek());
}

Token Lexer::ConsumeVariable() {
Expand Down Expand Up @@ -150,7 +150,7 @@ Token Lexer::Next() {
char next = Peek();
if (next == kEoFCharacter)
return Token::EoS();
else if (next == kIndefRefCharacter)
else if (next == kLiteralPrefixCharacter)
return ConsumeLiteral();
else if (IsVariable(next))
return ConsumeVariable();
Expand Down
20 changes: 13 additions & 7 deletions tools/jitmap_ir.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <memory>
#include <iostream>
#include <memory>

#include <jitmap/query/compiler.h>
#include <jitmap/query/parser.h>
Expand All @@ -12,12 +12,18 @@ int main(int argc, char** argv) {
return 1;
}

ExprBuilder builder;
auto expr = Parse(argv[1], &builder);
auto query = Query::Make("query", expr);
auto compiler = QueryCompiler("jitmap-ir-module", {});
compiler.Compile(*query);
std::cout << compiler.DumpIR() << "\n";
try {
ExprBuilder builder;
auto expr = Parse(argv[1], &builder);
auto query = Query::Make("query", expr);
auto compiler = QueryCompiler("jitmap-ir-module", {});
compiler.Compile(*query);
std::cout << compiler.DumpIR() << "\n";
} catch (jitmap::query::ParserException& e) {
std::cerr << "Problem parsing '" << argv[1] << "' :\n";
std::cerr << "\t" << e.message() << "\n";
return 1;
}

return 0;
}

0 comments on commit 9a2c272

Please sign in to comment.