Skip to content

Commit

Permalink
[example] fix toy dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
CBalaa committed Nov 27, 2023
1 parent ce466ba commit 4f0e16c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
19 changes: 7 additions & 12 deletions examples/ToyDSL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,30 @@ llvm_update_compile_flags(buddy-toy-dsl)

get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS)

# Add link libraries.
target_link_libraries(buddy-toy-dsl
PRIVATE
Threads::Threads
${dialect_libs}
${conversion_libs}
${extension_libs}
MLIRAnalysis
MLIRBuiltinToLLVMIRTranslation
MLIRCallInterfaces
MLIRCastInterfaces
MLIRExecutionEngine
MLIRFunctionInterfaces
MLIRIR
MLIRAffineDialect
MLIRFuncDialect
MLIRSCFDialect
MLIRLLVMCommonConversion
MLIRLLVMToLLVMIRTranslation
MLIRMemRefDialect
MLIRParser
MLIRPass
MLIRSideEffectInterfaces
MLIRTargetLLVMIRExport
MLIRTransforms
MLIRSupport
MLIRLLVMCommonConversion
MLIRLLVMToLLVMIRTranslation
MLIRTargetLLVMIRExport
MLIRControlFlowToLLVM
MLIRFuncToLLVM
MLIRMemRefToLLVM
MLIRSCFToControlFlow
MLIRAffineToStandard
antlr4_static
${dialect_libs}
${conversion_libs})
22 changes: 21 additions & 1 deletion examples/ToyDSL/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Func/Extensions/AllExtensions.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Support/LogicalResult.h"

#include "MLIRToyVisitor.h"
#include "ToyLexer.h"
Expand All @@ -35,6 +38,7 @@
#include "antlr4-common.h"

#include "mlir/Dialect/Affine/Passes.h"
#include "mlir/Dialect/LLVMIR/Transforms/Passes.h"
#include "mlir/ExecutionEngine/ExecutionEngine.h"
#include "mlir/ExecutionEngine/OptUtils.h"
#include "mlir/IR/AsmState.h"
Expand All @@ -54,6 +58,7 @@
#include "toy/Passes.h"

#include "llvm/ADT/StringRef.h"
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
Expand Down Expand Up @@ -136,6 +141,7 @@ int loadAndProcessMLIR(mlir::MLIRContext &context,
// Now that there is only one function, we can infer the shapes of each of
// the operations.
mlir::OpPassManager &optPM = pm.nest<mlir::toy::FuncOp>();
optPM.addPass(mlir::createCanonicalizerPass());
optPM.addPass(mlir::toy::createShapeInferencePass());
optPM.addPass(mlir::createCanonicalizerPass());
optPM.addPass(mlir::createCSEPass());
Expand All @@ -160,6 +166,11 @@ int loadAndProcessMLIR(mlir::MLIRContext &context,
if (isLoweringToLLVM) {
// Finish lowering the toy IR to the LLVM dialect.
pm.addPass(mlir::toy::createLowerToLLVMPass());
// This is necessary to have line tables emitted and basic
// debugger working. In the future we will add proper debug information
// emission directly from our frontend.
pm.addNestedPass<mlir::LLVM::LLVMFuncOp>(
mlir::LLVM::createDIScopeForLLVMFuncOpPass());
}

if (mlir::failed(pm.run(*module)))
Expand All @@ -178,6 +189,7 @@ int dumpAST(ToyParser::ModuleContext *moduleAST) {

int dumpLLVMIR(mlir::ModuleOp module) {
// Register the translation to LLVM IR with the MLIR context.
mlir::registerBuiltinDialectTranslation(*module->getContext());
mlir::registerLLVMDialectTranslation(*module->getContext());

// Convert the module to LLVM IR in a new LLVM IR context.
Expand All @@ -199,6 +211,10 @@ int dumpLLVMIR(mlir::ModuleOp module) {
}

auto tmOrError = tmBuilderOrError->createTargetMachine();
if (!tmOrError) {
llvm::errs() << "Could not create TargetMachine\n";
return -1;
}
mlir::ExecutionEngine::setupTargetTripleAndDataLayout(llvmModule.get(),
tmOrError.get().get());

Expand Down Expand Up @@ -267,7 +283,11 @@ int main(int argc, char *argv[]) {
if (emitAction == Action::DumpAST)
return dumpAST(moduleAST);

mlir::MLIRContext context;
// If we aren't dumping the AST, then we are compiling with/to MLIR.
mlir::DialectRegistry registry;
mlir::func::registerAllExtensions(registry);

mlir::MLIRContext context(registry);
// Load our Dialect in this MLIR Context.
context.getOrLoadDialect<mlir::toy::ToyDialect>();

Expand Down

0 comments on commit 4f0e16c

Please sign in to comment.