Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Standard Translation #2

Merged
merged 3 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmake/Modules/FindMLIR.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# MLIR_BUILD_INCLUDE_DIR
project(ldc)

find_path(MLIR_ROOT_DIR NAMES "CONTRIBUTING.md" HINTS ${LLVM_ROOT_DIR}/../llvm/projects/mlir)
find_path(MLIR_ROOT_DIR NAMES "LICENSE.TXT" HINTS ${LLVM_ROOT_DIR}/../mlir)

#Used to get the main header files
find_path(MLIR_INCLUDE_DIR NAMES "Parser.h" HINTS ${MLIR_ROOT_DIR}/include/mlir)
Expand All @@ -17,7 +17,7 @@ find_path(MLIR_LIB_DIR NAMES "CMakeLists.txt" HINTS ${MLIR_ROOT_DIR}/lib/IR)

#Used to get StandardOps.h.inc
find_path(MLIR_BUILD_INCLUDE_DIR NAMES "cmake_install.cmake"
HINTS ${LLVM_ROOT_DIR}/projects/mlir/include/mlir)
HINTS ${LLVM_ROOT_DIR}/tools/mlir/include/mlir)

message(STATUS "MLIR Dir: ${MLIR_ROOT_DIR}")
message(STATUS "MLIR Include Dir: ${MLIR_INCLUDE_DIR}/..")
Expand Down
2 changes: 2 additions & 0 deletions driver/codegenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "driver/toobj.h"
#if LDC_MLIR_ENABLED
#include "driver/tomlirfile.h"
#include "include/mlir/Pass/PassManager.h"
#endif
#include "gen/dynamiccompile.h"
#include "gen/logger.h"
Expand Down Expand Up @@ -263,6 +264,7 @@ void CodeGenerator::finishLLModule(Module *m) {
}

#if LDC_MLIR_ENABLED
mlir::registerPassManagerCLOptions();
writeMLIRModule(m, mlirContext_, m->objfile.toChars(), ir_);
#endif
writeAndFreeLLModule(m->objfile.toChars());
Expand Down
7 changes: 4 additions & 3 deletions driver/codegenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ class CodeGenerator {
void writeAndFreeLLModule(const char *filename);

llvm::LLVMContext &context_;
#if LDC_MLIR_ENABLED
mlir::MLIRContext &mlirContext_;
#endif
int moduleCount_;
bool const singleObj_;
IRState *ir_;
#if LDC_MLIR_ENABLED
mlir::MLIRContext &mlirContext_;
#endif

};
}
34 changes: 33 additions & 1 deletion driver/tomlirfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ namespace llvm {
#include "dmd/globals.h"
#include "gen/MLIR/MLIRGen.h"
#include "dmd/expression.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Transforms/Passes.h"
#include "gen/MLIR/Dialect.h"
#include "gen/MLIR/Passes.h"

void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext,
const char *filename, IRState *irs){
Expand All @@ -83,13 +88,40 @@ void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext,
const auto llpath = replaceExtensionWith(global.mlir_ext);
Logger::println("Writting MLIR to %s\n", llpath.c_str());
std::error_code errinfo;
llvm::raw_fd_ostream aos(llpath.c_str(), errinfo, llvm::sys::fs::F_None);
llvm::raw_fd_ostream aos(llpath, errinfo, llvm::sys::fs::F_None);
if(aos.has_error()){
error(Loc(), "Cannot write MLIR file '%s':%s", llpath.c_str(),
errinfo.message().c_str());
fatal();
}
mlir::OwningModuleRef module = ldc_mlir::mlirGen(mlirContext, m, irs);

mlir::PassManager pm(&mlirContext);

// Apply any generic pass manager command line options and run the pipeline.
mlir::applyPassManagerCLOptions(pm);

//TODO:Needs to set a flag to lowering D->MLIR->Affine+std
bool isLoweringToAffine = true;
if(isLoweringToAffine){
pm.addPass(mlir::D::createLowerToStandardPass());
mlir::OpPassManager &optPM = pm.nest<mlir::FuncOp>();
optPM.addPass(mlir::createCanonicalizerPass());
optPM.addPass(mlir::createCSEPass());

//TODO: Needs to set a flag to enaple opt
bool enableOpt = 1;
if (enableOpt) {
optPM.addPass(mlir::createLoopFusionPass());
optPM.addPass(mlir::createMemRefDataFlowOptPass());
}


if(mlir::failed(pm.run(*module))){
IF_LOG Logger::println("Failed on running passes!");
return;
}
}
if(!module){
IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str());
fatal();
Expand Down
Loading