Skip to content

Commit

Permalink
Bump to latest VAST. Use the same MLIRContext from the library. Work …
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Goodman authored Jun 19, 2024
1 parent 52e28a6 commit 9932378
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
90 changes: 49 additions & 41 deletions bin/Index/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ VAST_UNRELAX_WARNINGS
#include "TypeMapper.h"
#include "Util.h"

#include "../../lib/IR/SourceIR.h"

namespace indexer {
namespace {

Expand Down Expand Up @@ -292,6 +294,21 @@ class MXPreprocessingVisitorProxy : public vast::cg::fallthrough_list_node {
return next->visit(compress(type), scope);
}

vast::operation visit(const vast::cg::clang_decl *decl,
vast::cg::scope_context &scope) override {

// NOTE(pag): Workaround until VAST fixes Issue #625.
//
// XREF: https://github.com/trailofbits/vast/issues/625
if (auto func = clang::dyn_cast<clang::FunctionDecl>(decl)) {
if (auto def = func->getDefinition()) {
return next->visit(def, scope);
}
}

return next->visit(decl, scope);
}

private:

vast::cg::clang_qual_type compress(const vast::cg::clang_type *type) {
Expand All @@ -309,12 +326,8 @@ class MXPreprocessingVisitorProxy : public vast::cg::fallthrough_list_node {
// VAST Driver Setup Functions
//

std::unique_ptr<mlir::MLIRContext> MakeMLIRContext() {
return vast::cg::mk_mcontext();
}

std::unique_ptr<vast::cg::codegen_builder>
MakeCodeGenBuilder(mlir::MLIRContext *mctx) {
MakeCodeGenBuilder(mlir::MLIRContext &mctx) {
return vast::cg::mk_codegen_builder(mctx);
}

Expand All @@ -331,9 +344,9 @@ MakeSymbolGenerator(const EntityMapper &em) {
std::unique_ptr<vast::cg::driver> MakeCodeGenDriver(const pasta::AST &ast,
const EntityMapper &em) {
auto &actx = ast.UnderlyingAST();
auto mctx = MakeMLIRContext();
auto bld = MakeCodeGenBuilder(mctx.get());
auto mg = MakeMetaGenerator(*mctx, em);
auto &mctx = mx::ir::GlobalMLIRContext();
auto bld = MakeCodeGenBuilder(mctx);
auto mg = MakeMetaGenerator(mctx, em);
auto sg = MakeSymbolGenerator(em);

using vast::cg::as_node;
Expand All @@ -345,35 +358,24 @@ std::unique_ptr<vast::cg::driver> MakeCodeGenDriver(const pasta::AST &ast,
as_node_with_list_ref<vast::cg::attr_visitor_proxy>() |
as_node<vast::cg::type_caching_proxy>() |
as_node_with_list_ref<vast::cg::default_visitor>(
*mctx, *bld, mg, sg,
mctx, *bld, mg, sg,
/* strict return = */ false,
vast::cg::missing_return_policy::emit_trap) |
as_node<MXErrorReportVisitorProxy>(ast) |
as_node_with_list_ref<vast::cg::unsup_visitor>(*mctx, *bld) |
as_node_with_list_ref<vast::cg::unsup_visitor>(mctx, *bld) |
as_node<vast::cg::fallthrough_visitor>();

auto drv = std::make_unique<vast::cg::driver>(
actx, std::move(mctx), std::move(bld), visitors);
actx, mctx, std::move(bld), visitors);

drv->enable_verifier(true);
return drv;
}

} // namespace

class CodeGeneratorImpl {
public:

std::optional<vast::owning_module_ref> Process(const pasta::AST &ast);
std::optional<std::string> DumpToString(vast::owning_module_ref &mod);

bool enabled = true;
std::unique_ptr<vast::cg::driver> driver;
};

std::optional<vast::owning_module_ref> CodeGeneratorImpl::Process(
const pasta::AST &ast) {
static std::optional<vast::owning_module_ref> CreateModule(
const pasta::AST &ast, const EntityMapper &em) {
try {
auto driver = MakeCodeGenDriver(ast, em);
auto decls = GenerateDeclarationsInDeclContext(ast.TranslationUnit());

for (const auto &decl : decls) {
Expand All @@ -392,6 +394,26 @@ std::optional<vast::owning_module_ref> CodeGeneratorImpl::Process(
}
}

static std::optional<std::string> DumpToString(
vast::owning_module_ref &mlir_module) {
std::string result;
llvm::raw_string_ostream os(result);
mlir::BytecodeWriterConfig config("MX");

if (mlir::failed(mlir::writeBytecodeToFile(mlir_module.get(), os, config))) {
return std::nullopt;
}

return std::make_optional(result);
}

} // namespace

class CodeGeneratorImpl {
public:
bool enabled = true;
};

CodeGenerator::CodeGenerator(void)
: impl(std::make_unique<CodeGeneratorImpl>()) {}

Expand All @@ -405,29 +427,15 @@ CodeGenerator::~CodeGenerator(void) {
}
}

std::optional<std::string> CodeGeneratorImpl::DumpToString(
vast::owning_module_ref &mlir_module) {
std::string result;
llvm::raw_string_ostream os(result);
mlir::BytecodeWriterConfig config("MX");

if (mlir::failed(mlir::writeBytecodeToFile(mlir_module.get(), os, config))) {
return std::nullopt;
}

return std::make_optional(result);
}

std::string CodeGenerator::GenerateSourceIR(const pasta::AST &ast,
const EntityMapper &em,
const NameMangler &) {
if (!IsEnabled()) {
return "";
}

impl->driver = MakeCodeGenDriver(ast, em);
if (auto mod = impl->Process(ast)) {
if (auto result = impl->DumpToString(mod.value())) {
if (auto mod = CreateModule(ast, em)) {
if (auto result = DumpToString(mod.value())) {
return result.value();
}

Expand Down
5 changes: 5 additions & 0 deletions lib/IR/SourceIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -576,5 +576,10 @@ gap::generator<Operation> Operation::all_from(const ::mx::Stmt &that) {
that.id().Pack());
}

// Return a reference to the global MLIR context.
mlir::MLIRContext &GlobalMLIRContext(void) {
return kMLIR.Context();
}

} // namespace ir
} // namespace mx
3 changes: 3 additions & 0 deletions lib/IR/SourceIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@ bool BlocksMatch(
const std::shared_ptr<const SourceIRImpl> &a_mod, mlir::Block *a,
const std::shared_ptr<const SourceIRImpl> &b_mod, mlir::Block *b);

// Return a reference to the global MLIR context.
mlir::MLIRContext &GlobalMLIRContext(void);

} // namespace ir
} // namespace mx
2 changes: 1 addition & 1 deletion vendor/vast/src
Submodule src updated 57 files
+194 −0 docs/GettingStarted/integration.md
+4 −3 include/vast/ABI/ABI.hpp
+130 −314 include/vast/ABI/Classify.hpp
+7 −2 include/vast/ABI/Driver.hpp
+245 −0 include/vast/ABI/MLIRTypeInfo.hpp
+7 −1 include/vast/CodeGen/CodeGenBuilder.hpp
+9 −9 include/vast/CodeGen/CodeGenDriver.hpp
+1 −1 include/vast/CodeGen/DefaultDeclVisitor.hpp
+6 −6 include/vast/CodeGen/DefaultStmtVisitor.hpp
+3 −0 include/vast/CodeGen/DefaultTypeVisitor.hpp
+8 −9 include/vast/CodeGen/UnsupportedVisitor.hpp
+156 −144 include/vast/Conversion/ABI/AggregateTypes.hpp
+1 −2 include/vast/Conversion/ABI/Common.hpp
+17 −1 include/vast/Dialect/HighLevel/HighLevelTypes.td
+15 −7 include/vast/Frontend/Action.hpp
+11 −4 include/vast/Frontend/Consumer.hpp
+14 −1 include/vast/repl/cli.hpp
+1 −1 include/vast/repl/codegen.hpp
+43 −21 include/vast/repl/command.hpp
+23 −0 include/vast/repl/command_base.hpp
+6 −0 include/vast/repl/common.hpp
+43 −0 include/vast/repl/config.hpp
+18 −0 include/vast/repl/pipeline.hpp
+26 −0 include/vast/repl/state.hpp
+9 −18 lib/vast/CodeGen/CodeGenDriver.cpp
+6 −6 lib/vast/CodeGen/DefaultDeclVisitor.cpp
+39 −34 lib/vast/CodeGen/DefaultStmtVisitor.cpp
+16 −3 lib/vast/CodeGen/DefaultTypeVisitor.cpp
+85 −20 lib/vast/Conversion/ToLLVM/IRsToLLVM.cpp
+4 −0 lib/vast/Dialect/HighLevel/HighLevelTypes.cpp
+26 −18 lib/vast/Frontend/Action.cpp
+1 −1 lib/vast/Frontend/Consumer.cpp
+0 −1 test/repl/raise.c
+0 −6 test/vast/Compile/ObjectFiles/call-array-a.c
+18 −0 test/vast/Compile/ObjectFiles/x86_64/call-array-a.c
+3 −0 test/vast/Compile/ObjectFiles/x86_64/call-array-a.c.driver
+28 −0 test/vast/Compile/ObjectFiles/x86_64/call-array-b.c
+29 −0 test/vast/Compile/ObjectFiles/x86_64/call-array-b.c.driver
+31 −0 test/vast/Compile/ObjectFiles/x86_64/call-array-c.c
+29 −0 test/vast/Compile/ObjectFiles/x86_64/call-array-c.c.driver
+34 −0 test/vast/Compile/ObjectFiles/x86_64/call-array-d.c
+29 −0 test/vast/Compile/ObjectFiles/x86_64/call-array-d.c.driver
+26 −0 test/vast/Compile/ObjectFiles/x86_64/one-element-array-a.c
+27 −0 test/vast/Compile/ObjectFiles/x86_64/one-element-array-a.c.driver
+26 −0 test/vast/Compile/ObjectFiles/x86_64/one-element-array-b.c
+27 −0 test/vast/Compile/ObjectFiles/x86_64/one-element-array-b.c.driver
+23 −0 test/vast/Dialect/HighLevel/atomic-a.c
+20 −0 test/vast/Dialect/HighLevel/atomic-b.c
+41 −0 test/vast/Dialect/HighLevel/stdatomic-a.c
+16 −11 tools/vast-front/compiler_invocation.cpp
+2 −0 tools/vast-repl/CMakeLists.txt
+35 −0 tools/vast-repl/cli.cpp
+3 −3 tools/vast-repl/codegen.cpp
+38 −7 tools/vast-repl/command.cpp
+110 −0 tools/vast-repl/config.cpp
+1 −0 tools/vast-repl/vast-repl.cpp
+1 −0 www/mkdocs.yml

0 comments on commit 9932378

Please sign in to comment.