-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
codegen: Initial framework changes to emit hl-mlir and llvm ir
- Loading branch information
Showing
29 changed files
with
1,190 additions
and
441 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (c) 2024, Trail of Bits, Inc. | ||
* | ||
* This source code is licensed in accordance with the terms specified in | ||
* the LICENSE file found in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "patchestry/Util/Options.hpp" | ||
#include <mlir/IR/MLIRContext.h> | ||
#include <unordered_map> | ||
|
||
#include <clang/AST/ASTContext.h> | ||
#include <clang/Frontend/CompilerInstance.h> | ||
#include <mlir/Pass/PassRegistry.h> | ||
|
||
#include <vast/Frontend/FrontendAction.hpp> | ||
#include <vast/Frontend/Options.hpp> | ||
#include <vast/Frontend/Targets.hpp> | ||
|
||
namespace llvm { | ||
class raw_fd_ostream; | ||
} // namespace llvm | ||
|
||
namespace patchestry::codegen { | ||
|
||
using LocationMap = std::unordered_map< void *, std::string >; | ||
|
||
class MLIRRegistryInitializer | ||
{ | ||
public: | ||
explicit MLIRRegistryInitializer(mlir::DialectRegistry ®istry); | ||
}; | ||
|
||
class CodegenInitializer | ||
{ | ||
public: | ||
// Delete copy and move constructors and assignment operators | ||
CodegenInitializer(const CodegenInitializer &) = delete; | ||
CodegenInitializer &operator=(const CodegenInitializer &) = delete; | ||
CodegenInitializer(CodegenInitializer &&) noexcept = delete; | ||
CodegenInitializer &operator=(CodegenInitializer &&) noexcept = delete; | ||
|
||
// Public static method to access the singleton instance | ||
static CodegenInitializer &getInstance() { | ||
static CodegenInitializer instance(0); | ||
return instance; | ||
} | ||
|
||
inline mlir::MLIRContext &context() const noexcept { return ctx; } | ||
|
||
~CodegenInitializer(); | ||
|
||
private: | ||
explicit CodegenInitializer(int /*unused*/); | ||
|
||
// Members | ||
mlir::DialectRegistry registry; | ||
MLIRRegistryInitializer registry_initializer; | ||
mutable mlir::MLIRContext ctx; | ||
}; | ||
|
||
class CodeGenerator | ||
|
||
{ | ||
public: | ||
explicit CodeGenerator(clang::CompilerInstance &ci) : opts(vast::cc::options(ci)) {} | ||
|
||
CodeGenerator(const CodeGenerator &) = delete; | ||
CodeGenerator &operator=(const CodeGenerator &) = delete; | ||
CodeGenerator(CodeGenerator &&) noexcept = delete; | ||
CodeGenerator &operator=(CodeGenerator &&) noexcept = delete; | ||
|
||
virtual ~CodeGenerator() = default; | ||
|
||
void emit_tower( | ||
clang::ASTContext &actx, const LocationMap &locations, | ||
const patchestry::Options &options | ||
); | ||
|
||
void emit_source_ir( | ||
clang::ASTContext &actx, const LocationMap &locations, | ||
const patchestry::Options &options | ||
); | ||
|
||
private: | ||
void process_mlir_module( | ||
clang::ASTContext &actx, vast::cc::target_dialect target, vast::mlir_module mod | ||
); | ||
|
||
void emit_mlir_after_pipeline( | ||
clang::ASTContext &actx, vast::mlir_module mod, const patchestry::Options &options | ||
); | ||
|
||
void emit_llvmir( | ||
clang::ASTContext &actx, vast::mlir_module mod, const patchestry::Options &options | ||
); | ||
|
||
void emit_asm( | ||
clang::ASTContext &actx, vast::mlir_module mod, const patchestry::Options &options | ||
); | ||
|
||
std::optional< vast::owning_mlir_module_ref > | ||
emit_mlir(clang::ASTContext &ctx, const LocationMap &locations); | ||
|
||
vast::cc::action_options opts; | ||
}; | ||
|
||
} // namespace patchestry::codegen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2024, Trail of Bits, Inc. | ||
* | ||
* This source code is licensed in accordance with the terms specified in | ||
* the LICENSE file found in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <clang/AST/ASTContext.h> | ||
#include <mlir/IR/MLIRContext.h> | ||
|
||
#define GAP_ENABLE_COROUTINES | ||
#include <vast/CodeGen/CodeGenMetaGenerator.hpp> | ||
|
||
#include <patchestry/Codegen/Codegen.hpp> | ||
|
||
namespace patchestry::codegen { | ||
|
||
struct MetaGen final : vast::cg::meta_generator | ||
{ | ||
MetaGen(clang::ASTContext *actx, mlir::MLIRContext *mctx, const LocationMap &locs); | ||
|
||
void *raw_pointer(const clang::Decl *decl) const; | ||
|
||
void *raw_pointer(const clang::Stmt *stmt) const; | ||
|
||
void *raw_pointer(const clang::Expr *expr) const; | ||
|
||
mlir::Location location(const clang::Decl *decl) const override; | ||
|
||
mlir::Location location(const clang::Stmt *stmt) const override; | ||
|
||
mlir::Location location(const clang::Expr *expr) const override; | ||
|
||
private: | ||
uint64_t address_from_location(const std::string &str, char delimiter) const; | ||
|
||
mlir::Location location(void *data, const clang::SourceLocation &loc) const; | ||
|
||
clang::ASTContext *actx; | ||
|
||
mlir::MLIRContext *mctx; | ||
const LocationMap &locations; | ||
}; | ||
|
||
} // namespace patchestry::codegen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright (c) 2024, Trail of Bits, Inc. | ||
* | ||
* This source code is licensed in accordance with the terms specified in | ||
* the LICENSE file found in the root directory of this source tree. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <mlir/InitAllPasses.h> | ||
#include <mlir/Pass/Pass.h> | ||
#include <mlir/Pass/PassManager.h> | ||
#include <mlir/Pass/PassRegistry.h> | ||
|
||
namespace patchestry::codegen { | ||
|
||
class PassInstrumentation : public mlir::PassInstrumentation | ||
{ | ||
public: | ||
explicit PassInstrumentation(bool enable_location_transform = false) | ||
: location_transform(enable_location_transform) {} | ||
|
||
void runAfterPass(mlir::Pass *pass, mlir::Operation *op) override; | ||
|
||
void runBeforePass(mlir::Pass *pass, mlir::Operation *op) override; | ||
|
||
private: | ||
bool location_transform; | ||
}; | ||
|
||
} // namespace patchestry::codegen |
Oops, something went wrong.