From c92d161b17b9d41db7a9f2a2a9bd7dac31bb074d Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Fri, 7 Feb 2020 23:36:50 -0300 Subject: [PATCH 01/29] MLIR: #1 Command Line Flags - Introduces MLIR in the CL options --- dmd/globals.h | 2 ++ driver/cl_options.h | 1 + driver/main.cpp | 8 +++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/dmd/globals.h b/dmd/globals.h index 0d5ca7754ab..4e3928b3cb9 100644 --- a/dmd/globals.h +++ b/dmd/globals.h @@ -257,6 +257,7 @@ struct Param // LDC stuff OUTPUTFLAG output_ll; + OUTPUTFLAG output_mlir; OUTPUTFLAG output_bc; OUTPUTFLAG output_s; OUTPUTFLAG output_o; @@ -293,6 +294,7 @@ struct Global DString obj_ext; #if IN_LLVM DString ll_ext; + DString mlir_ext; //MLIR code DString bc_ext; DString s_ext; DString ldc_version; diff --git a/driver/cl_options.h b/driver/cl_options.h index bd4f05d7536..7b743409f03 100644 --- a/driver/cl_options.h +++ b/driver/cl_options.h @@ -52,6 +52,7 @@ extern cl::opt objectDir; extern cl::opt soname; extern cl::opt output_bc; extern cl::opt output_ll; +extern cl::opt output_mlir; extern cl::opt output_s; extern cl::opt output_o; extern cl::opt ddocDir; diff --git a/driver/main.cpp b/driver/main.cpp index f5fa53db4ca..9c500578898 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -433,11 +433,13 @@ void parseCommandLine(Strings &sourceFiles) { global.params.output_o = (opts::output_o == cl::BOU_UNSET && - !(opts::output_bc || opts::output_ll || opts::output_s)) + !(opts::output_bc || opts::output_ll || opts::output_s || + opts::output_mlir)) ? OUTPUTFLAGdefault : opts::output_o == cl::BOU_TRUE ? OUTPUTFLAGset : OUTPUTFLAGno; global.params.output_bc = opts::output_bc ? OUTPUTFLAGset : OUTPUTFLAGno; global.params.output_ll = opts::output_ll ? OUTPUTFLAGset : OUTPUTFLAGno; + global.params.output_mlir = opts::output_mlir ? OUTPUTFLAGset : OUTPUTFLAGno; global.params.output_s = opts::output_s ? OUTPUTFLAGset : OUTPUTFLAGno; global.params.cov = (global.params.covPercent <= 100); @@ -503,6 +505,10 @@ void parseCommandLine(Strings &sourceFiles) { strcmp(ext, global.s_ext.ptr) == 0) { global.params.output_s = OUTPUTFLAGset; global.params.output_o = OUTPUTFLAGno; + }else if(opts::output_mlir.getNumOccurrences() == 0 && + strcmp(ext, global.mlir_ext.ptr) == 0) { + global.params.output_mlir = OUTPUTFLAGset; + global.params.output_o = OUTPUTFLAGno; } } From 954304b3078021aedabfe287fd3725ddd9f5f625 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Fri, 7 Feb 2020 23:42:27 -0300 Subject: [PATCH 02/29] MLIR: #2 Command Line Flags - Add MLIR output in the global params Also add description for CL "output-mlir" --- driver/cl_options.cpp | 3 +++ driver/toobj.cpp | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/driver/cl_options.cpp b/driver/cl_options.cpp index 04b42fa0b66..a1f3f0e8eeb 100644 --- a/driver/cl_options.cpp +++ b/driver/cl_options.cpp @@ -192,6 +192,9 @@ cl::opt output_bc("output-bc", cl::desc("Write LLVM bitcode"), cl::opt output_ll("output-ll", cl::desc("Write LLVM IR"), cl::ZeroOrMore); +cl::opt output_mlir("output-mlir", cl::desc("Write MLIR"), + cl::ZeroOrMore); + cl::opt output_s("output-s", cl::desc("Write native assembly"), cl::ZeroOrMore); diff --git a/driver/toobj.cpp b/driver/toobj.cpp index 494d7785526..9d3d6bce4f8 100644 --- a/driver/toobj.cpp +++ b/driver/toobj.cpp @@ -426,6 +426,21 @@ void writeModule(llvm::Module *m, const char *filename) { m->print(aos, &annotator); } + //Write MLIR + if(global.params.output_mlir) { + 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); + if(aos.has_error()){ + error(Loc(), "Cannot write MLIR file '%s':%s", llpath.c_str(), + errinfo.message().c_str()); + fatal(); + } + AssemblyAnnotator annotator(m->getDataLayout()); + m->print(aos, &annotator); + } + const bool writeObj = outputObj && !emitBitcodeAsObjectFile; // write native assembly if (global.params.output_s || assembleExternally) { From d4e404a36b9ee77510cbfb9bce158387155bd72f Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Fri, 7 Feb 2020 23:52:09 -0300 Subject: [PATCH 03/29] MLIR: #3 Command Line Flags - Add output MILR on globals.d --- dmd/dmodule.d | 2 ++ dmd/globals.d | 2 ++ 2 files changed, 4 insertions(+) diff --git a/dmd/dmodule.d b/dmd/dmodule.d index b9a9fc23be6..5e919b7d9f0 100644 --- a/dmd/dmodule.d +++ b/dmd/dmodule.d @@ -548,6 +548,8 @@ version (IN_LLVM) objExt = global.ll_ext; else if (global.params.output_s) objExt = global.s_ext; + else if (global.params.output_mlir) + objExt = global.mlir_ext; if (objExt) objfile = setOutfilename(global.params.objname, global.params.objdir, filename, objExt); diff --git a/dmd/globals.d b/dmd/globals.d index e16ea7ae840..0248472397c 100644 --- a/dmd/globals.d +++ b/dmd/globals.d @@ -336,6 +336,7 @@ extern (C++) struct Global version (IN_LLVM) { const(char)[] ll_ext; + const(char)[] mlir_ext; const(char)[] bc_ext; const(char)[] s_ext; const(char)[] ldc_version; @@ -500,6 +501,7 @@ else vendor = "LDC"; obj_ext = "o"; ll_ext = "ll"; + mlir_ext = "mlir"; bc_ext = "bc"; s_ext = "s"; From 63e65ca60e71b354bfa48a6c77aefcaa71034798 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Sat, 8 Feb 2020 00:11:36 -0300 Subject: [PATCH 04/29] MLIR: #4 Command Line Flags - Add function to generate MLIR file --- CMakeLists.txt | 2 + dmd/globals.d | 1 + driver/codegenerator.cpp | 1 + driver/codegenerator.h | 4 +- driver/main.cpp | 1 + driver/tomlir.cpp | 93 ++++++++++++++++++++++++++++++++++++++++ driver/tomlir.h | 14 ++++++ driver/toobj.cpp | 18 +------- 8 files changed, 117 insertions(+), 17 deletions(-) create mode 100644 driver/tomlir.cpp create mode 100644 driver/tomlir.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2730ba2ba8a..aad933cfe75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -373,6 +373,7 @@ set(DRV_SRC driver/exe_path.cpp driver/targetmachine.cpp driver/toobj.cpp + driver/tomlir.cpp driver/tool.cpp driver/archiver.cpp driver/linker.cpp @@ -399,6 +400,7 @@ set(DRV_HDR driver/linker.h driver/plugins.h driver/targetmachine.h + driver/tomlir.cpp driver/toobj.h driver/tool.h ) diff --git a/dmd/globals.d b/dmd/globals.d index 0248472397c..20e4e630f86 100644 --- a/dmd/globals.d +++ b/dmd/globals.d @@ -297,6 +297,7 @@ version (IN_LLVM) // LDC stuff OUTPUTFLAG output_ll; + OUTPUTFLAG output_mlir; OUTPUTFLAG output_bc; OUTPUTFLAG output_s; OUTPUTFLAG output_o; diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index c1510876f7e..ce85699bbb5 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -18,6 +18,7 @@ #include "driver/cl_options_instrumentation.h" #include "driver/linker.h" #include "driver/toobj.h" +#include "driver/tomlir.h" #include "gen/dynamiccompile.h" #include "gen/logger.h" #include "gen/modules.h" diff --git a/driver/codegenerator.h b/driver/codegenerator.h index 5ced3e206b0..bb0156f216d 100644 --- a/driver/codegenerator.h +++ b/driver/codegenerator.h @@ -25,7 +25,8 @@ namespace ldc { class CodeGenerator { public: - CodeGenerator(llvm::LLVMContext &context, bool singleObj); + CodeGenerator(llvm::LLVMContext &context, mlir::MLIRContext &mlirContext, + bool singleObj); ~CodeGenerator(); void emit(Module *m); @@ -35,6 +36,7 @@ class CodeGenerator { void writeAndFreeLLModule(const char *filename); llvm::LLVMContext &context_; + mlir::MLIRContext &mlirContext_; int moduleCount_; bool const singleObj_; IRState *ir_; diff --git a/driver/main.cpp b/driver/main.cpp index 9c500578898..f6a585332de 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -85,6 +85,7 @@ void gendocfile(Module *m); void generateJson(Modules *modules); using namespace opts; +using namespace ldc_mlir; static StringsAdapter impPathsStore("I", global.params.imppath); static cl::list diff --git a/driver/tomlir.cpp b/driver/tomlir.cpp new file mode 100644 index 00000000000..c549cf78f56 --- /dev/null +++ b/driver/tomlir.cpp @@ -0,0 +1,93 @@ +// +// Created by Roberto Rosmaninho on 09/10/19. +// + +#include "tomlir.h" + +#include "dmd/errors.h" +#include "driver/cl_options.h" +#include "driver/cache.h" +#include "driver/targetmachine.h" +#include "driver/tool.h" +#include "gen/irstate.h" +#include "gen/logger.h" +#include "gen/optimizer.h" +#include "llvm/IR/AssemblyAnnotationWriter.h" +#include "llvm/IR/Verifier.h" +#include "llvm/Analysis/ModuleSummaryAnalysis.h" +#if LDC_LLVM_VER >= 400 +#include "llvm/Analysis/ProfileSummaryInfo.h" +#include "llvm/Bitcode/BitcodeWriter.h" +#else +#include "llvm/Bitcode/ReaderWriter.h" +#endif +#include "llvm/IR/LegacyPassManager.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/FormattedStream.h" +#include "llvm/Support/Program.h" +#include "llvm/Support/Path.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Analysis/TargetTransformInfo.h" +#if LDC_LLVM_VER >= 600 +#include "llvm/CodeGen/TargetSubtargetInfo.h" +#else +#include "llvm/Target/TargetSubtargetInfo.h" +#endif +#include "llvm/Transforms/Utils/Cloning.h" +#include "llvm/IR/Module.h" +#include +#include + +#ifdef LDC_LLVM_SUPPORTED_TARGET_SPIRV +namespace llvm { + ModulePass *createSPIRVWriterPass(llvm::raw_ostream &Str); +} +#endif + +#include "gen/logger.h" +#include "dmd/globals.h" +#include "gen/MLIR/MLIRGen.h" + +void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, + const char *filename){ + const auto outputFlags = {global.params.output_o, global.params.output_bc, + global.params.output_ll, global.params.output_s, + global.params.output_mlir}; + const auto numOutputFiles = + std::count_if(outputFlags.begin(), outputFlags.end(), + [](OUTPUTFLAG flag) { return flag != 0; }); + + const auto replaceExtensionWith = + [=](const DArray &ext) -> std::string { + if (numOutputFiles == 1) + return filename; + llvm::SmallString<128> buffer(filename); + llvm::sys::path::replace_extension(buffer, + llvm::StringRef(ext.ptr, ext.length)); + return buffer.str(); + }; + + //Write MLIR + if(global.params.output_mlir) { + 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); + 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); + if(!module){ + IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str()); + fatal(); + } + module->print(aos); + //AssemblyAnnotator annotator(m->getDataLayout()); + //m->print(aos, &annotator); + } + + +} diff --git a/driver/tomlir.h b/driver/tomlir.h new file mode 100644 index 00000000000..c6db74be481 --- /dev/null +++ b/driver/tomlir.h @@ -0,0 +1,14 @@ +// +// Created by Roberto Rosmaninho on 09/10/19. +// + +#ifndef LDC_TOMLIR_H +#define LDC_TOMLIR_H + +#include "dmd/module.h" +#include "mlir/IR/MLIRContext.h" + +void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, + const char *filename); + +#endif // LDC_TOMLIR_H diff --git a/driver/toobj.cpp b/driver/toobj.cpp index 9d3d6bce4f8..0a67e25c1c8 100644 --- a/driver/toobj.cpp +++ b/driver/toobj.cpp @@ -350,7 +350,8 @@ void writeModule(llvm::Module *m, const char *filename) { } const auto outputFlags = {global.params.output_o, global.params.output_bc, - global.params.output_ll, global.params.output_s}; + global.params.output_ll, global.params.output_s, + global.params.output_mlir}; const auto numOutputFiles = std::count_if(outputFlags.begin(), outputFlags.end(), [](OUTPUTFLAG flag) { return flag != 0; }); @@ -426,21 +427,6 @@ void writeModule(llvm::Module *m, const char *filename) { m->print(aos, &annotator); } - //Write MLIR - if(global.params.output_mlir) { - 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); - if(aos.has_error()){ - error(Loc(), "Cannot write MLIR file '%s':%s", llpath.c_str(), - errinfo.message().c_str()); - fatal(); - } - AssemblyAnnotator annotator(m->getDataLayout()); - m->print(aos, &annotator); - } - const bool writeObj = outputObj && !emitBitcodeAsObjectFile; // write native assembly if (global.params.output_s || assembleExternally) { From 98df97660a0b3ed7698ca5371a69b196bc7cd4df Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Sat, 8 Feb 2020 00:16:05 -0300 Subject: [PATCH 05/29] MLIR: #5 Command Line Flags - Rename files --- CMakeLists.txt | 4 ++-- driver/codegenerator.cpp | 3 ++- driver/codegenerator.h | 1 + driver/{tomlir.cpp => tomlirfile.cpp} | 15 +++++++-------- driver/{tomlir.h => tomlirfile.h} | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) rename driver/{tomlir.cpp => tomlirfile.cpp} (93%) rename driver/{tomlir.h => tomlirfile.h} (76%) diff --git a/CMakeLists.txt b/CMakeLists.txt index aad933cfe75..1d5ad86c47a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -373,7 +373,7 @@ set(DRV_SRC driver/exe_path.cpp driver/targetmachine.cpp driver/toobj.cpp - driver/tomlir.cpp + driver/tomlirfile.cpp driver/tool.cpp driver/archiver.cpp driver/linker.cpp @@ -400,7 +400,7 @@ set(DRV_HDR driver/linker.h driver/plugins.h driver/targetmachine.h - driver/tomlir.cpp + driver/tomlirfile.h driver/toobj.h driver/tool.h ) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index ce85699bbb5..1947521aa92 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -18,7 +18,7 @@ #include "driver/cl_options_instrumentation.h" #include "driver/linker.h" #include "driver/toobj.h" -#include "driver/tomlir.h" +#include "driver/tomlirfile.h" #include "gen/dynamiccompile.h" #include "gen/logger.h" #include "gen/modules.h" @@ -256,6 +256,7 @@ void CodeGenerator::finishLLModule(Module *m) { insertBitcodeFiles(ir_->module, ir_->context(), global.params.bitcodeFiles); } + writeMLIRModule(m, mlirContext_, m->objfile.toChars(), ir_); writeAndFreeLLModule(m->objfile.toChars()); } diff --git a/driver/codegenerator.h b/driver/codegenerator.h index bb0156f216d..19f1b479797 100644 --- a/driver/codegenerator.h +++ b/driver/codegenerator.h @@ -20,6 +20,7 @@ #pragma once #include "gen/irstate.h" +#include "mlir/IR/MLIRContext.h" namespace ldc { diff --git a/driver/tomlir.cpp b/driver/tomlirfile.cpp similarity index 93% rename from driver/tomlir.cpp rename to driver/tomlirfile.cpp index c549cf78f56..d10721d248d 100644 --- a/driver/tomlir.cpp +++ b/driver/tomlirfile.cpp @@ -2,7 +2,7 @@ // Created by Roberto Rosmaninho on 09/10/19. // -#include "tomlir.h" +#include "tomlirfile.h" #include "dmd/errors.h" #include "driver/cl_options.h" @@ -38,6 +38,7 @@ #include "llvm/IR/Module.h" #include #include +#include #ifdef LDC_LLVM_SUPPORTED_TARGET_SPIRV namespace llvm { @@ -46,11 +47,13 @@ namespace llvm { #endif #include "gen/logger.h" +#include "mlir/IR/Module.h" #include "dmd/globals.h" #include "gen/MLIR/MLIRGen.h" +#include "dmd/expression.h" void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, - const char *filename){ + const char *filename, IRState *irs){ const auto outputFlags = {global.params.output_o, global.params.output_bc, global.params.output_ll, global.params.output_s, global.params.output_mlir}; @@ -79,15 +82,11 @@ void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, errinfo.message().c_str()); fatal(); } - mlir::OwningModuleRef module = ldc_mlir::mlirGen(mlirContext, m); + mlir::OwningModuleRef module = ldc_mlir::mlirGen(mlirContext, m, irs); if(!module){ IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str()); fatal(); } module->print(aos); - //AssemblyAnnotator annotator(m->getDataLayout()); - //m->print(aos, &annotator); } - - -} +} \ No newline at end of file diff --git a/driver/tomlir.h b/driver/tomlirfile.h similarity index 76% rename from driver/tomlir.h rename to driver/tomlirfile.h index c6db74be481..a30ac190bd8 100644 --- a/driver/tomlir.h +++ b/driver/tomlirfile.h @@ -5,10 +5,10 @@ #ifndef LDC_TOMLIR_H #define LDC_TOMLIR_H +#include "gen/irstate.h" #include "dmd/module.h" #include "mlir/IR/MLIRContext.h" void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, - const char *filename); - + const char *filename, IRState *irs); #endif // LDC_TOMLIR_H From b4b0a6860440c997cad012d46e0c35d04e78a773 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Sat, 8 Feb 2020 11:45:01 -0300 Subject: [PATCH 06/29] MLIR: #6 Command Line Flags - Add Headers comments and #ifdef --- driver/main.cpp | 2 +- driver/tomlirfile.cpp | 16 +++++++++++++--- driver/tomlirfile.h | 17 +++++++++++++---- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/driver/main.cpp b/driver/main.cpp index f6a585332de..97952b1e228 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -506,7 +506,7 @@ void parseCommandLine(Strings &sourceFiles) { strcmp(ext, global.s_ext.ptr) == 0) { global.params.output_s = OUTPUTFLAGset; global.params.output_o = OUTPUTFLAGno; - }else if(opts::output_mlir.getNumOccurrences() == 0 && + } else if (opts::output_mlir.getNumOccurrences() == 0 && strcmp(ext, global.mlir_ext.ptr) == 0) { global.params.output_mlir = OUTPUTFLAGset; global.params.output_o = OUTPUTFLAGno; diff --git a/driver/tomlirfile.cpp b/driver/tomlirfile.cpp index d10721d248d..5fdb5528ed7 100644 --- a/driver/tomlirfile.cpp +++ b/driver/tomlirfile.cpp @@ -1,6 +1,13 @@ +//===-- tomlirfile.cpp-----------------------------------------------------===// // -// Created by Roberto Rosmaninho on 09/10/19. +// LDC – the LLVM D compiler // +// This file is distributed under the BSD-style LDC license. See the LICENSE +// file for details. +// +//===----------------------------------------------------------------------===// + +#if LDC_MLIR_ENABLED #include "tomlirfile.h" @@ -76,7 +83,7 @@ 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()); @@ -89,4 +96,7 @@ void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, } module->print(aos); } -} \ No newline at end of file +} + +#endif //LDC_MLIR_ENABLED + diff --git a/driver/tomlirfile.h b/driver/tomlirfile.h index a30ac190bd8..d82c6179617 100644 --- a/driver/tomlirfile.h +++ b/driver/tomlirfile.h @@ -1,9 +1,17 @@ +//===-- driver/tomlirfile.h - MLIR file emission ----------------*- C++ -*-===// // -// Created by Roberto Rosmaninho on 09/10/19. +// LDC – the LLVM D compiler // +// This file is distributed under the BSD-style LDC license. See the LICENSE +// file for details. +// +//===----------------------------------------------------------------------===// +// +// Handles emission of "finished" MLIR modules to on-disk object files. +// +//===----------------------------------------------------------------------===// -#ifndef LDC_TOMLIR_H -#define LDC_TOMLIR_H +#if LDC_MLIR_ENABLED #include "gen/irstate.h" #include "dmd/module.h" @@ -11,4 +19,5 @@ void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, const char *filename, IRState *irs); -#endif // LDC_TOMLIR_H +#endif // LDC_MLIR_ENABLED + From 10d760bd8308566b45cfd17bf7c4c7c4772cf3bb Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Wed, 12 Feb 2020 19:40:27 -0300 Subject: [PATCH 07/29] MLIR: #7 Command Line Flags - Fix typos, delete unnecessary includes --- driver/codegenerator.h | 12 ++++++++-- driver/main.cpp | 1 - driver/tomlirfile.cpp | 51 ++---------------------------------------- driver/tomlirfile.h | 2 +- 4 files changed, 13 insertions(+), 53 deletions(-) diff --git a/driver/codegenerator.h b/driver/codegenerator.h index 19f1b479797..861a45b1e62 100644 --- a/driver/codegenerator.h +++ b/driver/codegenerator.h @@ -20,14 +20,20 @@ #pragma once #include "gen/irstate.h" +#if LDC_MLIR_ENABLED #include "mlir/IR/MLIRContext.h" +#endif namespace ldc { class CodeGenerator { public: - CodeGenerator(llvm::LLVMContext &context, mlir::MLIRContext &mlirContext, - bool singleObj); + CodeGenerator(llvm::LLVMContext &context, +#if LDC_MLIR_ENABLED + mlir::MLIRContext &mlirContext, +#endif + bool singleObj); + ~CodeGenerator(); void emit(Module *m); @@ -37,7 +43,9 @@ 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_; diff --git a/driver/main.cpp b/driver/main.cpp index 97952b1e228..66d10f8d7e9 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -85,7 +85,6 @@ void gendocfile(Module *m); void generateJson(Modules *modules); using namespace opts; -using namespace ldc_mlir; static StringsAdapter impPathsStore("I", global.params.imppath); static cl::list diff --git a/driver/tomlirfile.cpp b/driver/tomlirfile.cpp index 5fdb5528ed7..53682a68436 100644 --- a/driver/tomlirfile.cpp +++ b/driver/tomlirfile.cpp @@ -10,54 +10,7 @@ #if LDC_MLIR_ENABLED #include "tomlirfile.h" - #include "dmd/errors.h" -#include "driver/cl_options.h" -#include "driver/cache.h" -#include "driver/targetmachine.h" -#include "driver/tool.h" -#include "gen/irstate.h" -#include "gen/logger.h" -#include "gen/optimizer.h" -#include "llvm/IR/AssemblyAnnotationWriter.h" -#include "llvm/IR/Verifier.h" -#include "llvm/Analysis/ModuleSummaryAnalysis.h" -#if LDC_LLVM_VER >= 400 -#include "llvm/Analysis/ProfileSummaryInfo.h" -#include "llvm/Bitcode/BitcodeWriter.h" -#else -#include "llvm/Bitcode/ReaderWriter.h" -#endif -#include "llvm/IR/LegacyPassManager.h" -#include "llvm/Support/CommandLine.h" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/FormattedStream.h" -#include "llvm/Support/Program.h" -#include "llvm/Support/Path.h" -#include "llvm/Target/TargetMachine.h" -#include "llvm/Analysis/TargetTransformInfo.h" -#if LDC_LLVM_VER >= 600 -#include "llvm/CodeGen/TargetSubtargetInfo.h" -#else -#include "llvm/Target/TargetSubtargetInfo.h" -#endif -#include "llvm/Transforms/Utils/Cloning.h" -#include "llvm/IR/Module.h" -#include -#include -#include - -#ifdef LDC_LLVM_SUPPORTED_TARGET_SPIRV -namespace llvm { - ModulePass *createSPIRVWriterPass(llvm::raw_ostream &Str); -} -#endif - -#include "gen/logger.h" -#include "mlir/IR/Module.h" -#include "dmd/globals.h" -#include "gen/MLIR/MLIRGen.h" -#include "dmd/expression.h" void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, const char *filename, IRState *irs){ @@ -81,11 +34,11 @@ void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, //Write MLIR if(global.params.output_mlir) { const auto llpath = replaceExtensionWith(global.mlir_ext); - Logger::println("Writting MLIR to %s\n", llpath.c_str()); + Logger::println("Writing MLIR to %s\n", llpath.c_str()); std::error_code errinfo; 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(), + error(Loc(), "Cannot write MLIR file '%s': %s", llpath.c_str(), errinfo.message().c_str()); fatal(); } diff --git a/driver/tomlirfile.h b/driver/tomlirfile.h index d82c6179617..98ce068ac09 100644 --- a/driver/tomlirfile.h +++ b/driver/tomlirfile.h @@ -7,7 +7,7 @@ // //===----------------------------------------------------------------------===// // -// Handles emission of "finished" MLIR modules to on-disk object files. +// Handles emission of MLIR modules to on-disk object files. // //===----------------------------------------------------------------------===// From 0b2d90d1a334c65956098d493d2e7eaceb191433 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Thu, 13 Feb 2020 00:13:44 -0300 Subject: [PATCH 08/29] set #ifdef around writeMLIRModule --- driver/codegenerator.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index 1947521aa92..68b28689ed4 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -255,8 +255,9 @@ void CodeGenerator::finishLLModule(Module *m) { if (moduleCount_ == 1) { insertBitcodeFiles(ir_->module, ir_->context(), global.params.bitcodeFiles); } - +#if LDC_MLIR_ENABLED writeMLIRModule(m, mlirContext_, m->objfile.toChars(), ir_); +#endif writeAndFreeLLModule(m->objfile.toChars()); } From 9c0a52f05d0a447769b05ea2c038d60b7cb56dc0 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Fri, 14 Feb 2020 22:13:24 -0300 Subject: [PATCH 09/29] Deleting file tomlirfile.* and writing it's function on toobj.* --- driver/tomlirfile.cpp | 55 ------------------------------------------- driver/tomlirfile.h | 23 ------------------ driver/toobj.cpp | 41 ++++++++++++++++++++++++++++++++ driver/toobj.h | 4 ++++ 4 files changed, 45 insertions(+), 78 deletions(-) delete mode 100644 driver/tomlirfile.cpp delete mode 100644 driver/tomlirfile.h diff --git a/driver/tomlirfile.cpp b/driver/tomlirfile.cpp deleted file mode 100644 index 53682a68436..00000000000 --- a/driver/tomlirfile.cpp +++ /dev/null @@ -1,55 +0,0 @@ -//===-- tomlirfile.cpp-----------------------------------------------------===// -// -// LDC – the LLVM D compiler -// -// This file is distributed under the BSD-style LDC license. See the LICENSE -// file for details. -// -//===----------------------------------------------------------------------===// - -#if LDC_MLIR_ENABLED - -#include "tomlirfile.h" -#include "dmd/errors.h" - -void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, - const char *filename, IRState *irs){ - const auto outputFlags = {global.params.output_o, global.params.output_bc, - global.params.output_ll, global.params.output_s, - global.params.output_mlir}; - const auto numOutputFiles = - std::count_if(outputFlags.begin(), outputFlags.end(), - [](OUTPUTFLAG flag) { return flag != 0; }); - - const auto replaceExtensionWith = - [=](const DArray &ext) -> std::string { - if (numOutputFiles == 1) - return filename; - llvm::SmallString<128> buffer(filename); - llvm::sys::path::replace_extension(buffer, - llvm::StringRef(ext.ptr, ext.length)); - return buffer.str(); - }; - - //Write MLIR - if(global.params.output_mlir) { - const auto llpath = replaceExtensionWith(global.mlir_ext); - Logger::println("Writing MLIR to %s\n", llpath.c_str()); - std::error_code errinfo; - 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); - if(!module){ - IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str()); - fatal(); - } - module->print(aos); - } -} - -#endif //LDC_MLIR_ENABLED - diff --git a/driver/tomlirfile.h b/driver/tomlirfile.h deleted file mode 100644 index 98ce068ac09..00000000000 --- a/driver/tomlirfile.h +++ /dev/null @@ -1,23 +0,0 @@ -//===-- driver/tomlirfile.h - MLIR file emission ----------------*- C++ -*-===// -// -// LDC – the LLVM D compiler -// -// This file is distributed under the BSD-style LDC license. See the LICENSE -// file for details. -// -//===----------------------------------------------------------------------===// -// -// Handles emission of MLIR modules to on-disk object files. -// -//===----------------------------------------------------------------------===// - -#if LDC_MLIR_ENABLED - -#include "gen/irstate.h" -#include "dmd/module.h" -#include "mlir/IR/MLIRContext.h" - -void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, - const char *filename, IRState *irs); -#endif // LDC_MLIR_ENABLED - diff --git a/driver/toobj.cpp b/driver/toobj.cpp index 0a67e25c1c8..32f43d28623 100644 --- a/driver/toobj.cpp +++ b/driver/toobj.cpp @@ -473,3 +473,44 @@ void writeModule(llvm::Module *m, const char *filename) { } } } + +#if LDC_MLIR_ENABLED +void writeMLIRModule(::Module *m, mlir::MLIRContext &mlirContext, + const char *filename, IRState *irs){ + const auto outputFlags = {global.params.output_o, global.params.output_bc, + global.params.output_ll, global.params.output_s, + global.params.output_mlir}; + const auto numOutputFiles = + std::count_if(outputFlags.begin(), outputFlags.end(), + [](OUTPUTFLAG flag) { return flag != 0; }); + + const auto replaceExtensionWith = + [=](const DArray &ext) -> std::string { + if (numOutputFiles == 1) + return filename; + llvm::SmallString<128> buffer(filename); + llvm::sys::path::replace_extension(buffer, + llvm::StringRef(ext.ptr, ext.length)); + return buffer.str(); + }; + + //Write MLIR + if(global.params.output_mlir) { + const auto llpath = replaceExtensionWith(global.mlir_ext); + Logger::println("Writing MLIR to %s\n", llpath.c_str()); + std::error_code errinfo; + 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); + if(!module){ + IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str()); + fatal(); + } + module->print(aos); + } +} +#endif diff --git a/driver/toobj.h b/driver/toobj.h index 9b13349ea32..5b3a77c6175 100644 --- a/driver/toobj.h +++ b/driver/toobj.h @@ -18,3 +18,7 @@ class Module; } void writeModule(llvm::Module *m, const char *filename); +#if LDC_MLIR_ENABLED +void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, + const char *filename, IRState *irs); +#endif From 18e71ff0e18580676db8e2c0cee15e8868ec69a7 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Fri, 14 Feb 2020 22:19:18 -0300 Subject: [PATCH 10/29] Deleting references to driver/tomlirfile.* --- CMakeLists.txt | 2 -- driver/codegenerator.cpp | 1 - 2 files changed, 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d5ad86c47a..2730ba2ba8a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -373,7 +373,6 @@ set(DRV_SRC driver/exe_path.cpp driver/targetmachine.cpp driver/toobj.cpp - driver/tomlirfile.cpp driver/tool.cpp driver/archiver.cpp driver/linker.cpp @@ -400,7 +399,6 @@ set(DRV_HDR driver/linker.h driver/plugins.h driver/targetmachine.h - driver/tomlirfile.h driver/toobj.h driver/tool.h ) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index 68b28689ed4..1601ecb2a7c 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -18,7 +18,6 @@ #include "driver/cl_options_instrumentation.h" #include "driver/linker.h" #include "driver/toobj.h" -#include "driver/tomlirfile.h" #include "gen/dynamiccompile.h" #include "gen/logger.h" #include "gen/modules.h" From c807832f22a09749575ab42d73daa26e1269f0a4 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Fri, 14 Feb 2020 22:24:42 -0300 Subject: [PATCH 11/29] Fixing alignment --- driver/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/driver/main.cpp b/driver/main.cpp index 66d10f8d7e9..5456ed965db 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -434,7 +434,7 @@ void parseCommandLine(Strings &sourceFiles) { global.params.output_o = (opts::output_o == cl::BOU_UNSET && !(opts::output_bc || opts::output_ll || opts::output_s || - opts::output_mlir)) + opts::output_mlir)) ? OUTPUTFLAGdefault : opts::output_o == cl::BOU_TRUE ? OUTPUTFLAGset : OUTPUTFLAGno; global.params.output_bc = opts::output_bc ? OUTPUTFLAGset : OUTPUTFLAGno; @@ -506,7 +506,7 @@ void parseCommandLine(Strings &sourceFiles) { global.params.output_s = OUTPUTFLAGset; global.params.output_o = OUTPUTFLAGno; } else if (opts::output_mlir.getNumOccurrences() == 0 && - strcmp(ext, global.mlir_ext.ptr) == 0) { + strcmp(ext, global.mlir_ext.ptr) == 0) { global.params.output_mlir = OUTPUTFLAGset; global.params.output_o = OUTPUTFLAGno; } From 9c074533f2ac5000945de9d20c5d13638a666e99 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Mon, 2 Mar 2020 02:15:24 -0300 Subject: [PATCH 12/29] Deleting WriteMLIRModule function and priting MLIR inside WriteModule --- driver/codegenerator.cpp | 21 ++++++++++--- driver/codegenerator.h | 2 +- driver/toobj.cpp | 65 ++++++++++++++-------------------------- driver/toobj.h | 11 +++++-- 4 files changed, 49 insertions(+), 50 deletions(-) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index 1601ecb2a7c..1593b0769d3 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -199,8 +199,17 @@ void emitLLVMUsedArray(IRState &irs) { } namespace ldc { -CodeGenerator::CodeGenerator(llvm::LLVMContext &context, bool singleObj) - : context_(context), moduleCount_(0), singleObj_(singleObj), ir_(nullptr) { +CodeGenerator::CodeGenerator(llvm::LLVMContext &context, +#if LDC_MLIR_ENABLED + mlir::MLIRContext &mlirContext, +#endif +bool singleObj) + : context_(context), +#if LDC_MLIR_ENABLED + mlirContext_(mlirContext), +#endif + moduleCount_(0), singleObj_(singleObj), ir_(nullptr) +{ // Set the context to discard value names when not generating textual IR. if (!global.params.output_ll) { context_.setDiscardValueNames(true); @@ -255,12 +264,12 @@ void CodeGenerator::finishLLModule(Module *m) { insertBitcodeFiles(ir_->module, ir_->context(), global.params.bitcodeFiles); } #if LDC_MLIR_ENABLED - writeMLIRModule(m, mlirContext_, m->objfile.toChars(), ir_); + writeAndFreeLLModule(m->objfile.toChars(), m); #endif writeAndFreeLLModule(m->objfile.toChars()); } -void CodeGenerator::writeAndFreeLLModule(const char *filename) { +void CodeGenerator::writeAndFreeLLModule(const char *filename, Module *m) { ir_->objc.finalize(); ir_->DBuilder.Finalize(); @@ -284,7 +293,11 @@ void CodeGenerator::writeAndFreeLLModule(const char *filename) { std::unique_ptr diagnosticsOutputFile = createAndSetDiagnosticsOutputFile(*ir_, context_, filename); +#if LDC_MLIR_ENABLED + writeModule(&ir_->module, filename, m, mlirContext_, ir_); +#else writeModule(&ir_->module, filename); +#endif if (diagnosticsOutputFile) diagnosticsOutputFile->keep(); diff --git a/driver/codegenerator.h b/driver/codegenerator.h index 861a45b1e62..80eba50c135 100644 --- a/driver/codegenerator.h +++ b/driver/codegenerator.h @@ -40,7 +40,7 @@ class CodeGenerator { private: void prepareLLModule(Module *m); void finishLLModule(Module *m); - void writeAndFreeLLModule(const char *filename); + void writeAndFreeLLModule(const char *filename, Module *m = nullptr); llvm::LLVMContext &context_; #if LDC_MLIR_ENABLED diff --git a/driver/toobj.cpp b/driver/toobj.cpp index 32f43d28623..e76295fb64d 100644 --- a/driver/toobj.cpp +++ b/driver/toobj.cpp @@ -10,6 +10,7 @@ #include "driver/toobj.h" #include "dmd/errors.h" +#include "dmd/module.h" #include "driver/cl_options.h" #include "driver/cache.h" #include "driver/targetmachine.h" @@ -17,6 +18,7 @@ #include "gen/irstate.h" #include "gen/logger.h" #include "gen/optimizer.h" +#include "gen/MLIR/MLIRGen.h" #include "llvm/IR/AssemblyAnnotationWriter.h" #include "llvm/IR/Verifier.h" #include "llvm/Analysis/ModuleSummaryAnalysis.h" @@ -309,7 +311,8 @@ bool shouldDoLTO(llvm::Module *m) { } } // end of anonymous namespace -void writeModule(llvm::Module *m, const char *filename) { +void writeModule(llvm::Module *m, const char *filename, ::Module *module, + mlir::MLIRContext &mlirContext, IRState *irs) { const bool doLTO = shouldDoLTO(m); const bool outputObj = shouldOutputObjectFile(); const bool assembleExternally = shouldAssembleExternally(); @@ -427,6 +430,25 @@ void writeModule(llvm::Module *m, const char *filename) { m->print(aos, &annotator); } + //Write MLIR + if(global.params.output_mlir && module != nullptr) { + const auto llpath = replaceExtensionWith(global.mlir_ext); + Logger::println("Writing MLIR to %s\n", llpath.c_str()); + std::error_code errinfo; + 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, module, irs); + if(!Module){ + IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str()); + fatal(); + } + Module->print(aos); + } + const bool writeObj = outputObj && !emitBitcodeAsObjectFile; // write native assembly if (global.params.output_s || assembleExternally) { @@ -473,44 +495,3 @@ void writeModule(llvm::Module *m, const char *filename) { } } } - -#if LDC_MLIR_ENABLED -void writeMLIRModule(::Module *m, mlir::MLIRContext &mlirContext, - const char *filename, IRState *irs){ - const auto outputFlags = {global.params.output_o, global.params.output_bc, - global.params.output_ll, global.params.output_s, - global.params.output_mlir}; - const auto numOutputFiles = - std::count_if(outputFlags.begin(), outputFlags.end(), - [](OUTPUTFLAG flag) { return flag != 0; }); - - const auto replaceExtensionWith = - [=](const DArray &ext) -> std::string { - if (numOutputFiles == 1) - return filename; - llvm::SmallString<128> buffer(filename); - llvm::sys::path::replace_extension(buffer, - llvm::StringRef(ext.ptr, ext.length)); - return buffer.str(); - }; - - //Write MLIR - if(global.params.output_mlir) { - const auto llpath = replaceExtensionWith(global.mlir_ext); - Logger::println("Writing MLIR to %s\n", llpath.c_str()); - std::error_code errinfo; - 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); - if(!module){ - IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str()); - fatal(); - } - module->print(aos); - } -} -#endif diff --git a/driver/toobj.h b/driver/toobj.h index 5b3a77c6175..c7715e9f4b0 100644 --- a/driver/toobj.h +++ b/driver/toobj.h @@ -12,13 +12,18 @@ //===----------------------------------------------------------------------===// #pragma once +#if LDC_MLIR_ENABLED +#include "mlir/IR/MLIRContext.h" +#include "gen/irstate.h" +#include "dmd/module.h" +#endif namespace llvm { class Module; } -void writeModule(llvm::Module *m, const char *filename); +void writeModule(llvm::Module *m, const char *filename #if LDC_MLIR_ENABLED -void writeMLIRModule(Module *m, mlir::MLIRContext &mlirContext, - const char *filename, IRState *irs); +, ::Module *module,mlir::MLIRContext &mlirContext, IRState *irs #endif +); From 6d7c63437be01afd8e976000350f3cc8e151dff9 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Tue, 5 May 2020 02:37:01 -0300 Subject: [PATCH 13/29] Creating emitMLIR to call MLIR CodeGenaration and WriteMLIRModule --- driver/codegenerator.cpp | 78 +++++++++++++++++++++++++++++++++++----- driver/codegenerator.h | 5 ++- driver/main.cpp | 13 ++++++- driver/toobj.cpp | 23 +----------- driver/toobj.h | 6 +--- 5 files changed, 87 insertions(+), 38 deletions(-) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index ccd81db29d1..ee6faad9d25 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -33,7 +33,7 @@ #if LDC_LLVM_VER < 600 namespace llvm { -using ToolOutputFile = tool_output_file; + using ToolOutputFile = tool_output_file; } #endif @@ -283,13 +283,10 @@ void CodeGenerator::finishLLModule(Module *m) { if (moduleCount_ == 1) { insertBitcodeFiles(ir_->module, ir_->context(), global.params.bitcodeFiles); } -#if LDC_MLIR_ENABLED - writeAndFreeLLModule(m->objfile.toChars(), m); -#endif writeAndFreeLLModule(m->objfile.toChars()); } -void CodeGenerator::writeAndFreeLLModule(const char *filename, Module *m) { +void CodeGenerator::writeAndFreeLLModule(const char *filename) { ir_->objc.finalize(); ir_->DBuilder.Finalize(); @@ -315,11 +312,7 @@ void CodeGenerator::writeAndFreeLLModule(const char *filename, Module *m) { std::unique_ptr diagnosticsOutputFile = createAndSetDiagnosticsOutputFile(*ir_, context_, filename); -#if LDC_MLIR_ENABLED - writeModule(&ir_->module, filename, m, mlirContext_, ir_); -#else writeModule(&ir_->module, filename); -#endif if (diagnosticsOutputFile) diagnosticsOutputFile->keep(); @@ -356,4 +349,71 @@ void CodeGenerator::emit(Module *m) { Logger::disable(); } } + +void CodeGenerator::emitMLIR(Module *m) { + bool const loggerWasEnabled = Logger::enabled(); + if (m->llvmForceLogging && !loggerWasEnabled) { + Logger::enable(); + } + + IF_LOG Logger::println("CodeGenerator::emitMLIR(%s)", m->toPrettyChars()); + LOG_SCOPE; + + if (global.params.verbose_cg) { + printf("codegen: %s (%s)\n", m->toPrettyChars(), m->srcfile.toChars()); + } + if (global.errors) { + Logger::println("Aborting because of errors"); + fatal(); + } + + mlir::OwningModuleRef module; + /*module = mlirGen(mlirContext, m, irs); + if(!module){ + IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str()); + fatal(); + }*/ + + writeMLIRModule(&module, m->objfile.toChars()); + + if (m->llvmForceLogging && !loggerWasEnabled) { + Logger::disable(); + } +} + +void CodeGenerator::writeMLIRModule(mlir::OwningModuleRef *module, + const char *filename) { + const auto outputFlags = {global.params.output_o, global.params.output_bc, + global.params.output_ll, global.params.output_s, + global.params.output_mlir}; + const auto numOutputFiles = + std::count_if(outputFlags.begin(), outputFlags.end(), + [](OUTPUTFLAG flag) { return flag != 0; }); + + const auto replaceExtensionWith = + [=](const DArray &ext) -> std::string { + if (numOutputFiles == 1) + return filename; + llvm::SmallString<128> buffer(filename); + llvm::sys::path::replace_extension(buffer, + llvm::StringRef(ext.ptr, ext.length)); + return buffer.c_str(); + }; + + //Write MLIR + if(global.params.output_mlir) { + 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, 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(); + } + + // module->print(aos); + } +} + } diff --git a/driver/codegenerator.h b/driver/codegenerator.h index 80eba50c135..5410efcda05 100644 --- a/driver/codegenerator.h +++ b/driver/codegenerator.h @@ -22,6 +22,7 @@ #include "gen/irstate.h" #if LDC_MLIR_ENABLED #include "mlir/IR/MLIRContext.h" +#include "mlir/IR/Module.h" #endif namespace ldc { @@ -36,11 +37,13 @@ class CodeGenerator { ~CodeGenerator(); void emit(Module *m); + void emitMLIR(Module *m); private: void prepareLLModule(Module *m); void finishLLModule(Module *m); - void writeAndFreeLLModule(const char *filename, Module *m = nullptr); + void writeAndFreeLLModule(const char *filename); + void writeMLIRModule(mlir::OwningModuleRef *module, const char *filename); llvm::LLVMContext &context_; #if LDC_MLIR_ENABLED diff --git a/driver/main.cpp b/driver/main.cpp index d76457de164..b5d0d8b7f0f 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -1094,9 +1094,17 @@ int cppmain() { } void codegenModules(Modules &modules) { + bool useMLIR = false; // Generate one or more object/IR/bitcode files/dcompute kernels. if (global.params.obj && !modules.empty()) { +#if LDC_MLIR_ENABLED + mlir::MLIRContext mlircontext; + useMLIR = true; + ldc::CodeGenerator cg(getGlobalContext(), mlircontext, + global.params.oneobj); +#else ldc::CodeGenerator cg(getGlobalContext(), global.params.oneobj); +#endif DComputeCodeGenManager dccg(getGlobalContext()); std::vector computeModules; // When inlining is enabled, we are calling semantic3 on function @@ -1119,7 +1127,10 @@ void codegenModules(Modules &modules) { const auto atCompute = hasComputeAttr(m); if (atCompute == DComputeCompileFor::hostOnly || atCompute == DComputeCompileFor::hostAndDevice) { - cg.emit(m); + if(useMLIR) + cg.emitMLIR(m); + else + cg.emit(m); } if (atCompute != DComputeCompileFor::hostOnly) { computeModules.push_back(m); diff --git a/driver/toobj.cpp b/driver/toobj.cpp index 1861c3a9cdb..c523b600dc5 100644 --- a/driver/toobj.cpp +++ b/driver/toobj.cpp @@ -18,7 +18,6 @@ #include "gen/irstate.h" #include "gen/logger.h" #include "gen/optimizer.h" -#include "gen/MLIR/MLIRGen.h" #include "llvm/IR/AssemblyAnnotationWriter.h" #include "llvm/IR/Verifier.h" #include "llvm/Analysis/ModuleSummaryAnalysis.h" @@ -311,8 +310,7 @@ bool shouldDoLTO(llvm::Module *m) { } } // end of anonymous namespace -void writeModule(llvm::Module *m, const char *filename, ::Module *module, - mlir::MLIRContext &mlirContext, IRState *irs) { +void writeModule(llvm::Module *m, const char *filename) { const bool doLTO = shouldDoLTO(m); const bool outputObj = shouldOutputObjectFile(); const bool assembleExternally = shouldAssembleExternally(); @@ -430,25 +428,6 @@ void writeModule(llvm::Module *m, const char *filename, ::Module *module, m->print(aos, &annotator); } - //Write MLIR - if(global.params.output_mlir && module != nullptr) { - const auto llpath = replaceExtensionWith(global.mlir_ext); - Logger::println("Writing MLIR to %s\n", llpath.c_str()); - std::error_code errinfo; - 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, module, irs); - if(!Module){ - IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str()); - fatal(); - } - Module->print(aos); - } - const bool writeObj = outputObj && !emitBitcodeAsObjectFile; // write native assembly if (global.params.output_s || assembleExternally) { diff --git a/driver/toobj.h b/driver/toobj.h index c7715e9f4b0..16ab6be4caf 100644 --- a/driver/toobj.h +++ b/driver/toobj.h @@ -22,8 +22,4 @@ namespace llvm { class Module; } -void writeModule(llvm::Module *m, const char *filename -#if LDC_MLIR_ENABLED -, ::Module *module,mlir::MLIRContext &mlirContext, IRState *irs -#endif -); +void writeModule(llvm::Module *m, const char *filename); From d173f40fabeb2c2ef8351cd15e45e64684b5d28b Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Tue, 5 May 2020 17:34:05 -0300 Subject: [PATCH 14/29] Add #if defines on codegenerator --- driver/codegenerator.cpp | 2 ++ driver/codegenerator.h | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index ee6faad9d25..1f6136c4a03 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -350,6 +350,7 @@ void CodeGenerator::emit(Module *m) { } } +#if LDC_MLIR_ENABLED void CodeGenerator::emitMLIR(Module *m) { bool const loggerWasEnabled = Logger::enabled(); if (m->llvmForceLogging && !loggerWasEnabled) { @@ -416,4 +417,5 @@ void CodeGenerator::writeMLIRModule(mlir::OwningModuleRef *module, } } +#endif } diff --git a/driver/codegenerator.h b/driver/codegenerator.h index 5410efcda05..986047581bc 100644 --- a/driver/codegenerator.h +++ b/driver/codegenerator.h @@ -37,13 +37,18 @@ class CodeGenerator { ~CodeGenerator(); void emit(Module *m); + +#if LDC_MLIR_ENABLED void emitMLIR(Module *m); +#endif private: void prepareLLModule(Module *m); void finishLLModule(Module *m); void writeAndFreeLLModule(const char *filename); +#if LDC_MLIR_ENABLED void writeMLIRModule(mlir::OwningModuleRef *module, const char *filename); +#endif llvm::LLVMContext &context_; #if LDC_MLIR_ENABLED From e00968c2fbe2a5555c998ecc44fead60badd8edc Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Tue, 5 May 2020 18:08:44 -0300 Subject: [PATCH 15/29] Adding CI --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index a2d8cc9e06b..7e050fe779d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -148,4 +148,4 @@ workflows: - Ubuntu-18.04-multilib-rtSanitizers - Ubuntu-19.10-multilib-sharedLibsOnly-gdmd - macOS-x64 - - macOS-x64-sharedLibsOnly + - macOS-x64-sharedLibsOnly \ No newline at end of file From 0496615d4514cb20e54b9616b2b4743e69f3e1ac Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Tue, 5 May 2020 18:35:27 -0300 Subject: [PATCH 16/29] Add MLIR Libraries --- cmake/Modules/FindMLIR.cmake | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/cmake/Modules/FindMLIR.cmake b/cmake/Modules/FindMLIR.cmake index 14ad214b6eb..c18172cca1f 100644 --- a/cmake/Modules/FindMLIR.cmake +++ b/cmake/Modules/FindMLIR.cmake @@ -28,7 +28,31 @@ else() set(MLIR_LIB_DIR ${MLIR_ROOT_DIR}/lib) # To be done: add the required MLIR libraries. Hopefully we don't have to manually list all MLIR libs. - set(MLIR_LIBRARIES "") + set(MLIR_LIBRARIES + libMLIRAffineOps.a + libMLIRAffineToStandard.a + libMLIRAnalysis.a + libMLIRDialect.a + libMLIRIR.a + libMLIRLLVMIR.a + libMLIRLoopOps.a + libMLIRLoopToStandard.a + libMLIRLoopsToGPU.a + libMLIRMlirOptLib.a + libMLIROptMain.a + libMLIRParser.a + libMLIRPass.a + libMLIRStandardOps.a + libMLIRStandardToLLVM.a + libMLIRStandardToSPIRVTransforms.a + libMLIRSupport.a + libMLIRTargetLLVMIR.a + libMLIRTargetLLVMIRModuleTranslation.a + libMLIRTransformUtils.a + libMLIRTransforms.a + libMLIRTranslateClParser.a + libMLIRTranslation.a + ) # XXX: This function is untested and will need adjustment. function(mlir_tablegen) From 6efcab3197ea592448d67bd0fed738bf4c78b307 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Tue, 5 May 2020 19:52:27 -0300 Subject: [PATCH 17/29] Add #if defines on codegenerator --- driver/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/driver/main.cpp b/driver/main.cpp index b5d0d8b7f0f..95c4e8f408a 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -1127,9 +1127,11 @@ void codegenModules(Modules &modules) { const auto atCompute = hasComputeAttr(m); if (atCompute == DComputeCompileFor::hostOnly || atCompute == DComputeCompileFor::hostAndDevice) { +#if LDC_MLIR_ENABLED if(useMLIR) cg.emitMLIR(m); else +#endif cg.emit(m); } if (atCompute != DComputeCompileFor::hostOnly) { From f142f4c22fc299f9c2d918856911e603a2fceb1c Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Mon, 11 May 2020 22:06:36 -0300 Subject: [PATCH 18/29] Add MLIR libs and condition to emit MLIR --- cmake/Modules/FindMLIR.cmake | 31 ++++++------------------------- driver/main.cpp | 2 +- 2 files changed, 7 insertions(+), 26 deletions(-) diff --git a/cmake/Modules/FindMLIR.cmake b/cmake/Modules/FindMLIR.cmake index c18172cca1f..d47bcec296b 100644 --- a/cmake/Modules/FindMLIR.cmake +++ b/cmake/Modules/FindMLIR.cmake @@ -28,31 +28,12 @@ else() set(MLIR_LIB_DIR ${MLIR_ROOT_DIR}/lib) # To be done: add the required MLIR libraries. Hopefully we don't have to manually list all MLIR libs. - set(MLIR_LIBRARIES - libMLIRAffineOps.a - libMLIRAffineToStandard.a - libMLIRAnalysis.a - libMLIRDialect.a - libMLIRIR.a - libMLIRLLVMIR.a - libMLIRLoopOps.a - libMLIRLoopToStandard.a - libMLIRLoopsToGPU.a - libMLIRMlirOptLib.a - libMLIROptMain.a - libMLIRParser.a - libMLIRPass.a - libMLIRStandardOps.a - libMLIRStandardToLLVM.a - libMLIRStandardToSPIRVTransforms.a - libMLIRSupport.a - libMLIRTargetLLVMIR.a - libMLIRTargetLLVMIRModuleTranslation.a - libMLIRTransformUtils.a - libMLIRTransforms.a - libMLIRTranslateClParser.a - libMLIRTranslation.a - ) + set(suffix "a") + if(EXISTS "libMLIRIR.lib") + set(suffix "lib") + endif() + + set(MLIR_LIBRARIES libMLIRIR.${suffix} libMLIRSupport.${suffix}) # XXX: This function is untested and will need adjustment. function(mlir_tablegen) diff --git a/driver/main.cpp b/driver/main.cpp index 95c4e8f408a..3ba3c3f1841 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -1128,7 +1128,7 @@ void codegenModules(Modules &modules) { if (atCompute == DComputeCompileFor::hostOnly || atCompute == DComputeCompileFor::hostAndDevice) { #if LDC_MLIR_ENABLED - if(useMLIR) + if(useMLIR && global.params.output_mlir == OUTPUTFLAGset) cg.emitMLIR(m); else #endif From 16ff665a86acb15c152405dfe4a6c6d57ab01444 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Mon, 11 May 2020 22:10:54 -0300 Subject: [PATCH 19/29] Update config.yml --- .circleci/config.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 7e050fe779d..410ea5fe00b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -148,4 +148,5 @@ workflows: - Ubuntu-18.04-multilib-rtSanitizers - Ubuntu-19.10-multilib-sharedLibsOnly-gdmd - macOS-x64 - - macOS-x64-sharedLibsOnly \ No newline at end of file + - macOS-x64-sharedLibsOnly + From bd6fafbe6508522b300dadba4d0a0b903ad3d242 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Mon, 11 May 2020 22:20:30 -0300 Subject: [PATCH 20/29] Fix windows support for MLIRlibs --- cmake/Modules/FindMLIR.cmake | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cmake/Modules/FindMLIR.cmake b/cmake/Modules/FindMLIR.cmake index d47bcec296b..81e2eb3b2b2 100644 --- a/cmake/Modules/FindMLIR.cmake +++ b/cmake/Modules/FindMLIR.cmake @@ -28,10 +28,12 @@ else() set(MLIR_LIB_DIR ${MLIR_ROOT_DIR}/lib) # To be done: add the required MLIR libraries. Hopefully we don't have to manually list all MLIR libs. - set(suffix "a") - if(EXISTS "libMLIRIR.lib") - set(suffix "lib") - endif() + + if(EXISTS "MLIRIR.lib") + set(MLIR_LIBRARIES MLIRIR.lib MLIRSupport.lib) + elseif(EXISTS "${MLIR_LIB_DIR}/libMLIRIR.a") + message("Found libMLIR.lib") + set(MLIR_LIBRARIES libMLIRIR.a libMLIRSupport.a) set(MLIR_LIBRARIES libMLIRIR.${suffix} libMLIRSupport.${suffix}) From bf16f0207ecdde685cfcdc2a7e497b5687b4b74d Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Mon, 11 May 2020 22:28:30 -0300 Subject: [PATCH 21/29] Update FindMLIR.cmake --- cmake/Modules/FindMLIR.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/cmake/Modules/FindMLIR.cmake b/cmake/Modules/FindMLIR.cmake index 81e2eb3b2b2..ab3e4d6f4d5 100644 --- a/cmake/Modules/FindMLIR.cmake +++ b/cmake/Modules/FindMLIR.cmake @@ -34,6 +34,7 @@ else() elseif(EXISTS "${MLIR_LIB_DIR}/libMLIRIR.a") message("Found libMLIR.lib") set(MLIR_LIBRARIES libMLIRIR.a libMLIRSupport.a) + endif() set(MLIR_LIBRARIES libMLIRIR.${suffix} libMLIRSupport.${suffix}) From 485a617b7e81984432d29819afdff244a45ac919 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Mon, 11 May 2020 22:35:34 -0300 Subject: [PATCH 22/29] Update FindMLIR.cmake --- cmake/Modules/FindMLIR.cmake | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cmake/Modules/FindMLIR.cmake b/cmake/Modules/FindMLIR.cmake index ab3e4d6f4d5..6804c96c9f8 100644 --- a/cmake/Modules/FindMLIR.cmake +++ b/cmake/Modules/FindMLIR.cmake @@ -29,11 +29,10 @@ else() # To be done: add the required MLIR libraries. Hopefully we don't have to manually list all MLIR libs. - if(EXISTS "MLIRIR.lib") - set(MLIR_LIBRARIES MLIRIR.lib MLIRSupport.lib) + if(EXISTS "${MLIR_LIB_DIR}/MLIRIR.lib") + set(MLIR_LIBRARIES ${MLIR_LIB_DIR}/MLIRIR.lib ${MLIR_LIB_DIR}/MLIRSupport.lib) elseif(EXISTS "${MLIR_LIB_DIR}/libMLIRIR.a") - message("Found libMLIR.lib") - set(MLIR_LIBRARIES libMLIRIR.a libMLIRSupport.a) + set(MLIR_LIBRARIES ${MLIR_LIB_DIR}/libMLIRIR.a ${MLIR_LIB_DIR}/libMLIRSupport.a) endif() set(MLIR_LIBRARIES libMLIRIR.${suffix} libMLIRSupport.${suffix}) From c94ab5d0969f5d4f83eb236027444b4057db8b1e Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Mon, 11 May 2020 22:53:50 -0300 Subject: [PATCH 23/29] Update FindMLIR.cmake --- cmake/Modules/FindMLIR.cmake | 2 -- 1 file changed, 2 deletions(-) diff --git a/cmake/Modules/FindMLIR.cmake b/cmake/Modules/FindMLIR.cmake index 6804c96c9f8..1609c2b8cc8 100644 --- a/cmake/Modules/FindMLIR.cmake +++ b/cmake/Modules/FindMLIR.cmake @@ -35,8 +35,6 @@ else() set(MLIR_LIBRARIES ${MLIR_LIB_DIR}/libMLIRIR.a ${MLIR_LIB_DIR}/libMLIRSupport.a) endif() - set(MLIR_LIBRARIES libMLIRIR.${suffix} libMLIRSupport.${suffix}) - # XXX: This function is untested and will need adjustment. function(mlir_tablegen) cmake_parse_arguments( From 1e1bd10e266c995d13478ffaba348fe9c42a835f Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Tue, 12 May 2020 18:11:54 -0300 Subject: [PATCH 24/29] Fix identation and deleting unused includes and variable --- driver/codegenerator.cpp | 2 +- driver/main.cpp | 4 +--- driver/toobj.cpp | 1 - driver/toobj.h | 5 ----- 4 files changed, 2 insertions(+), 10 deletions(-) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index 1f6136c4a03..153ea76b842 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -33,7 +33,7 @@ #if LDC_LLVM_VER < 600 namespace llvm { - using ToolOutputFile = tool_output_file; +using ToolOutputFile = tool_output_file; } #endif diff --git a/driver/main.cpp b/driver/main.cpp index 3ba3c3f1841..f54073c6e8c 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -1094,12 +1094,10 @@ int cppmain() { } void codegenModules(Modules &modules) { - bool useMLIR = false; // Generate one or more object/IR/bitcode files/dcompute kernels. if (global.params.obj && !modules.empty()) { #if LDC_MLIR_ENABLED mlir::MLIRContext mlircontext; - useMLIR = true; ldc::CodeGenerator cg(getGlobalContext(), mlircontext, global.params.oneobj); #else @@ -1128,7 +1126,7 @@ void codegenModules(Modules &modules) { if (atCompute == DComputeCompileFor::hostOnly || atCompute == DComputeCompileFor::hostAndDevice) { #if LDC_MLIR_ENABLED - if(useMLIR && global.params.output_mlir == OUTPUTFLAGset) + if(global.params.output_mlir == OUTPUTFLAGset) cg.emitMLIR(m); else #endif diff --git a/driver/toobj.cpp b/driver/toobj.cpp index c523b600dc5..28d615ceaf0 100644 --- a/driver/toobj.cpp +++ b/driver/toobj.cpp @@ -10,7 +10,6 @@ #include "driver/toobj.h" #include "dmd/errors.h" -#include "dmd/module.h" #include "driver/cl_options.h" #include "driver/cache.h" #include "driver/targetmachine.h" diff --git a/driver/toobj.h b/driver/toobj.h index 16ab6be4caf..9b13349ea32 100644 --- a/driver/toobj.h +++ b/driver/toobj.h @@ -12,11 +12,6 @@ //===----------------------------------------------------------------------===// #pragma once -#if LDC_MLIR_ENABLED -#include "mlir/IR/MLIRContext.h" -#include "gen/irstate.h" -#include "dmd/module.h" -#endif namespace llvm { class Module; From 618292b460495122b853f5e2b7193ae7dfdd91c2 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Tue, 12 May 2020 20:58:21 -0300 Subject: [PATCH 25/29] add if-statement checking if LDC was built with MLIR before create mlir file --- driver/main.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/driver/main.cpp b/driver/main.cpp index f54073c6e8c..a413b0b4e5c 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -518,6 +518,13 @@ void parseCommandLine(Strings &sourceFiles) { } } +#ifndef LDC_MLIR_ENABLED + if (global.params.output_mlir == OUTPUTFLAGset) { + error(Loc(), "MLIR output requested but this LDC was built without MLIR support"); + fatal(); + } +#endif + if (soname.getNumOccurrences() > 0 && !global.params.dll) { error(Loc(), "-soname can be used only when building a shared library"); } From 9fef2eb7e7734c9d85e7dce8d4c5ea674a65c554 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Thu, 14 May 2020 14:38:35 -0300 Subject: [PATCH 26/29] Refactoring replaceExtensionWith and fix clang-format --- driver/codegenerator.cpp | 45 ++++++++++++++-------------------------- driver/main.cpp | 2 +- driver/toobj.cpp | 40 +++++++++++++++++------------------ driver/toobj.h | 4 ++++ 4 files changed, 40 insertions(+), 51 deletions(-) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index 153ea76b842..fe8f68d376a 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -363,6 +363,7 @@ void CodeGenerator::emitMLIR(Module *m) { if (global.params.verbose_cg) { printf("codegen: %s (%s)\n", m->toPrettyChars(), m->srcfile.toChars()); } + if (global.errors) { Logger::println("Aborting because of errors"); fatal(); @@ -383,37 +384,21 @@ void CodeGenerator::emitMLIR(Module *m) { } void CodeGenerator::writeMLIRModule(mlir::OwningModuleRef *module, - const char *filename) { - const auto outputFlags = {global.params.output_o, global.params.output_bc, - global.params.output_ll, global.params.output_s, - global.params.output_mlir}; - const auto numOutputFiles = - std::count_if(outputFlags.begin(), outputFlags.end(), - [](OUTPUTFLAG flag) { return flag != 0; }); - - const auto replaceExtensionWith = - [=](const DArray &ext) -> std::string { - if (numOutputFiles == 1) - return filename; - llvm::SmallString<128> buffer(filename); - llvm::sys::path::replace_extension(buffer, - llvm::StringRef(ext.ptr, ext.length)); - return buffer.c_str(); - }; - - //Write MLIR - if(global.params.output_mlir) { - 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, 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(); - } + const char *filename) { + // Write MLIR + if (global.params.output_mlir) { + const auto llpath = replaceExtensionWith(global.mlir_ext, filename); + Logger::println("Writting MLIR to %s\n", llpath.c_str()); + std::error_code errinfo; + 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(); + } - // module->print(aos); + // module->print(aos); } } diff --git a/driver/main.cpp b/driver/main.cpp index a413b0b4e5c..7c815d6f47e 100644 --- a/driver/main.cpp +++ b/driver/main.cpp @@ -1133,7 +1133,7 @@ void codegenModules(Modules &modules) { if (atCompute == DComputeCompileFor::hostOnly || atCompute == DComputeCompileFor::hostAndDevice) { #if LDC_MLIR_ENABLED - if(global.params.output_mlir == OUTPUTFLAGset) + if (global.params.output_mlir == OUTPUTFLAGset) cg.emitMLIR(m); else #endif diff --git a/driver/toobj.cpp b/driver/toobj.cpp index 28d615ceaf0..d7d49d9c57c 100644 --- a/driver/toobj.cpp +++ b/driver/toobj.cpp @@ -309,6 +309,23 @@ bool shouldDoLTO(llvm::Module *m) { } } // end of anonymous namespace +std::string replaceExtensionWith(const DArray &ext, + const char *filename) { + const auto outputFlags = {global.params.output_o, global.params.output_bc, + global.params.output_ll, global.params.output_s, + global.params.output_mlir}; + const auto numOutputFiles = + std::count_if(outputFlags.begin(), outputFlags.end(), + [](OUTPUTFLAG flag) { return flag != 0; }); + + if (numOutputFiles == 1) + return filename; + llvm::SmallString<128> buffer(filename); + llvm::sys::path::replace_extension(buffer, + llvm::StringRef(ext.ptr, ext.length)); + return {buffer.data(), buffer.size()}; +} + void writeModule(llvm::Module *m, const char *filename) { const bool doLTO = shouldDoLTO(m); const bool outputObj = shouldOutputObjectFile(); @@ -349,30 +366,13 @@ void writeModule(llvm::Module *m, const char *filename) { } } - const auto outputFlags = {global.params.output_o, global.params.output_bc, - global.params.output_ll, global.params.output_s, - global.params.output_mlir}; - const auto numOutputFiles = - std::count_if(outputFlags.begin(), outputFlags.end(), - [](OUTPUTFLAG flag) { return flag != 0; }); - - const auto replaceExtensionWith = - [=](const DArray &ext) -> std::string { - if (numOutputFiles == 1) - return filename; - llvm::SmallString<128> buffer(filename); - llvm::sys::path::replace_extension(buffer, - llvm::StringRef(ext.ptr, ext.length)); - return {buffer.data(), buffer.size()}; - }; - // write LLVM bitcode const bool emitBitcodeAsObjectFile = doLTO && outputObj && !global.params.output_bc; if (global.params.output_bc || emitBitcodeAsObjectFile) { std::string bcpath = emitBitcodeAsObjectFile ? filename - : replaceExtensionWith(global.bc_ext); + : replaceExtensionWith(global.bc_ext, filename); Logger::println("Writing LLVM bitcode to: %s\n", bcpath.c_str()); std::error_code errinfo; llvm::raw_fd_ostream bos(bcpath.c_str(), errinfo, llvm::sys::fs::F_None); @@ -414,7 +414,7 @@ void writeModule(llvm::Module *m, const char *filename) { // write LLVM IR if (global.params.output_ll) { - const auto llpath = replaceExtensionWith(global.ll_ext); + const auto llpath = replaceExtensionWith(global.ll_ext, filename); Logger::println("Writing LLVM IR to: %s\n", llpath.c_str()); std::error_code errinfo; llvm::raw_fd_ostream aos(llpath.c_str(), errinfo, llvm::sys::fs::F_None); @@ -436,7 +436,7 @@ void writeModule(llvm::Module *m, const char *filename) { llvm::sys::fs::createUniqueFile("ldc-%%%%%%%.s", buffer); spath = {buffer.data(), buffer.size()}; } else { - spath = replaceExtensionWith(global.s_ext); + spath = replaceExtensionWith(global.s_ext, filename); } Logger::println("Writing asm to: %s\n", spath.c_str()); diff --git a/driver/toobj.h b/driver/toobj.h index 9b13349ea32..8986fe62836 100644 --- a/driver/toobj.h +++ b/driver/toobj.h @@ -12,9 +12,13 @@ //===----------------------------------------------------------------------===// #pragma once +#include +#include "dmd/root/dcompat.h" namespace llvm { class Module; } void writeModule(llvm::Module *m, const char *filename); +std::string replaceExtensionWith(const DArray &ext, + const char *filename); \ No newline at end of file From 724fcd1a4791fa5e7ae548716a68dd52ea12e0b1 Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Thu, 14 May 2020 18:40:22 -0300 Subject: [PATCH 27/29] Final modifications: clang-format, whitespaces and messages --- .circleci/config.yml | 1 - cmake/Modules/FindMLIR.cmake | 1 - driver/codegenerator.cpp | 13 ++++++------- driver/toobj.h | 1 + 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 410ea5fe00b..a2d8cc9e06b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -149,4 +149,3 @@ workflows: - Ubuntu-19.10-multilib-sharedLibsOnly-gdmd - macOS-x64 - macOS-x64-sharedLibsOnly - diff --git a/cmake/Modules/FindMLIR.cmake b/cmake/Modules/FindMLIR.cmake index 1609c2b8cc8..3713ebf7017 100644 --- a/cmake/Modules/FindMLIR.cmake +++ b/cmake/Modules/FindMLIR.cmake @@ -28,7 +28,6 @@ else() set(MLIR_LIB_DIR ${MLIR_ROOT_DIR}/lib) # To be done: add the required MLIR libraries. Hopefully we don't have to manually list all MLIR libs. - if(EXISTS "${MLIR_LIB_DIR}/MLIRIR.lib") set(MLIR_LIBRARIES ${MLIR_LIB_DIR}/MLIRIR.lib ${MLIR_LIB_DIR}/MLIRSupport.lib) elseif(EXISTS "${MLIR_LIB_DIR}/libMLIRIR.a") diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index fe8f68d376a..f8bb2e4ed3b 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -221,15 +221,14 @@ void inlineAsmDiagnosticHandler(const llvm::SMDiagnostic &d, void *context, namespace ldc { CodeGenerator::CodeGenerator(llvm::LLVMContext &context, #if LDC_MLIR_ENABLED - mlir::MLIRContext &mlirContext, + mlir::MLIRContext &mlirContext, #endif -bool singleObj) - : context_(context), + bool singleObj) + : context_(context), #if LDC_MLIR_ENABLED mlirContext_(mlirContext), #endif - moduleCount_(0), singleObj_(singleObj), ir_(nullptr) -{ + moduleCount_(0), singleObj_(singleObj), ir_(nullptr) { // Set the context to discard value names when not generating textual IR. if (!global.params.output_ll) { context_.setDiscardValueNames(true); @@ -372,7 +371,7 @@ void CodeGenerator::emitMLIR(Module *m) { mlir::OwningModuleRef module; /*module = mlirGen(mlirContext, m, irs); if(!module){ - IF_LOG Logger::println("Cannot write MLIR file to '%s'", llpath.c_str()); + IF_LOG Logger::println("Cannot write module to '%s'", llpath.c_str()); fatal(); }*/ @@ -393,7 +392,7 @@ void CodeGenerator::writeMLIRModule(mlir::OwningModuleRef *module, 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(), + error(Loc(), "Cannot write MLIR file '%s': %s", llpath.c_str(), errinfo.message().c_str()); fatal(); } diff --git a/driver/toobj.h b/driver/toobj.h index 8986fe62836..d59ecfda4fc 100644 --- a/driver/toobj.h +++ b/driver/toobj.h @@ -20,5 +20,6 @@ class Module; } void writeModule(llvm::Module *m, const char *filename); + std::string replaceExtensionWith(const DArray &ext, const char *filename); \ No newline at end of file From 85b4b331cba0689d6e055727b375e3b0a952747e Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Fri, 15 May 2020 13:41:52 -0300 Subject: [PATCH 28/29] Update toobj.h --- driver/toobj.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver/toobj.h b/driver/toobj.h index d59ecfda4fc..5914313c720 100644 --- a/driver/toobj.h +++ b/driver/toobj.h @@ -22,4 +22,4 @@ class Module; void writeModule(llvm::Module *m, const char *filename); std::string replaceExtensionWith(const DArray &ext, - const char *filename); \ No newline at end of file + const char *filename); From 4adf8653bd3d9c39c7f4c87caff17c51578347ad Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Fri, 15 May 2020 15:40:25 -0300 Subject: [PATCH 29/29] Update codegenerator.cpp --- driver/codegenerator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp index f8bb2e4ed3b..ec00cba4580 100644 --- a/driver/codegenerator.cpp +++ b/driver/codegenerator.cpp @@ -371,7 +371,7 @@ void CodeGenerator::emitMLIR(Module *m) { mlir::OwningModuleRef module; /*module = mlirGen(mlirContext, m, irs); if(!module){ - IF_LOG Logger::println("Cannot write module to '%s'", llpath.c_str()); + IF_LOG Logger::println("Error generating MLIR:'%s'", llpath.c_str()); fatal(); }*/