From 4f0e16c7e8061c487173550f524449e7e0070a6b Mon Sep 17 00:00:00 2001 From: chh Date: Mon, 27 Nov 2023 13:34:40 +0000 Subject: [PATCH] [example] fix toy dsl --- examples/ToyDSL/CMakeLists.txt | 19 +++++++------------ examples/ToyDSL/main.cpp | 22 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/examples/ToyDSL/CMakeLists.txt b/examples/ToyDSL/CMakeLists.txt index 5e87c2b0c6..fc0a6a9177 100644 --- a/examples/ToyDSL/CMakeLists.txt +++ b/examples/ToyDSL/CMakeLists.txt @@ -46,19 +46,23 @@ 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 @@ -66,15 +70,6 @@ target_link_libraries(buddy-toy-dsl MLIRSideEffectInterfaces MLIRTargetLLVMIRExport MLIRTransforms - MLIRSupport - MLIRLLVMCommonConversion - MLIRLLVMToLLVMIRTranslation - MLIRTargetLLVMIRExport - MLIRControlFlowToLLVM - MLIRFuncToLLVM - MLIRMemRefToLLVM - MLIRSCFToControlFlow - MLIRAffineToStandard antlr4_static ${dialect_libs} ${conversion_libs}) diff --git a/examples/ToyDSL/main.cpp b/examples/ToyDSL/main.cpp index f5355e739b..4898c4d939 100644 --- a/examples/ToyDSL/main.cpp +++ b/examples/ToyDSL/main.cpp @@ -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" @@ -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" @@ -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" @@ -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(); + optPM.addPass(mlir::createCanonicalizerPass()); optPM.addPass(mlir::toy::createShapeInferencePass()); optPM.addPass(mlir::createCanonicalizerPass()); optPM.addPass(mlir::createCSEPass()); @@ -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::createDIScopeForLLVMFuncOpPass()); } if (mlir::failed(pm.run(*module))) @@ -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. @@ -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()); @@ -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();