Skip to content

Commit

Permalink
Merge pull request #1 from Robertorosmaninho/fix-llvm-10
Browse files Browse the repository at this point in the history
MLIR Support to LDC - Milestone 1
  • Loading branch information
Robertorosmaninho committed Feb 5, 2020
2 parents 2ff7d49 + 4248089 commit 2780246
Show file tree
Hide file tree
Showing 27 changed files with 1,221 additions and 20 deletions.
98 changes: 96 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ endif()
#
include(GetLinuxDistribution)


#
# Main configuration.
#
Expand Down Expand Up @@ -348,6 +347,89 @@ endif()
message(STATUS "LDC version identifier: ${LDC_VERSION}")
configure_file(driver/ldc-version.cpp.in driver/ldc-version.cpp)

#
# Setting MLIR headers
#

find_package(MLIR REQUIRED)
if(MLIR_FOUND)
message(STATUS "Buiding with MLIR support")
include_directories(${MLIR_INCLUDE_DIRS})
include_directories(${MLIR_BUILD_INCLUDE_DIRS})
include_directories(${MLIR_LIB_DIRS})
add_definitions("-DLDC_MLIR_ENABLED")

if(MSVC)
if(EXISTS "${LLVM_LIBRARY_DIRS}/libMLIR*.lib")
set(suffix "lib")
endif()
else()
if(EXISTS "${LLVM_LIBRARY_DIRS}/libMLIRIR.a")
set(suffix "a")
elseif(EXISTS "${LLVM_LIBRARY_DIRS}/libMLIRIR.so")
set(suffix "so")
elseif(EXISTS "${LLVM_LIBRARY_DIRS}/libMLIRIR.dylib")
set(suffix "dylib")
endif()
endif() #if MSVC


set(MLIR_LIB
${LLVM_LIBRARY_DIRS}/libMLIRAffineOps.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRAnalysis.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRDialect.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIREDSC.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRExecutionEngine.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRFxpMathOps.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRGPU.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRGPUtoNVVMTransforms.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRGPUtoROCDLTransforms.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRGPUtoSPIRVTransforms.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRIR.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRJitRunner.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRLinalg.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRLLVMIR.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRLoopOps.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRLoopsToGPU.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRLoopToStandard.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRMlirOptLib.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRNVVMIR.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIROptMain.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRParser.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRPass.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRQuantizerFxpMathConfig.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRQuantizerSupport.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRQuantizerTransforms.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRQuantOps.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRROCDLIR.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRSDBM.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRSPIRV.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRSPIRVConversion.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRSPIRVSerialization.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRSPIRVTransforms.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRStandardOps.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRStandardToLLVM.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRSupport.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTargetLLVMIR.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTargetLLVMIRModuleTranslation.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTargetNVVMIR.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTargetROCDLIR.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTestDialect.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTestIR.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTestPass.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTestTransforms.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTransforms.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTransformUtils.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTranslateClParser.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRTranslation.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRVectorOps.${suffix}
${LLVM_LIBRARY_DIRS}/libMLIRVectorToLLVM.${suffix}
)

set(LLVM_LIBRARIES "${MLIR_LIB}" ${LLVM_LIBRARIES})

endif() #if MLIR_FOUND

# Also add the header files to the build so that they are available in IDE
# project files generated via CMake.
file(GLOB_RECURSE FE_SRC_D dmd/*.d)
Expand All @@ -360,6 +442,8 @@ file(GLOB_RECURSE DRV_SRC_D driver/*.d)
file(GLOB_RECURSE IR_SRC_D ir/*.d)
file(GLOB IR_SRC ir/*.cpp)
file(GLOB IR_HDR ir/*.h)
file(GLOB MLIR_HDR gen/MLIR/*.h)
file(GLOB MLIR_SRC gen/MLIR/*.cpp)
set(DRV_SRC
driver/args.cpp
driver/cache.cpp
Expand All @@ -373,6 +457,7 @@ 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
Expand All @@ -399,6 +484,7 @@ set(DRV_HDR
driver/linker.h
driver/plugins.h
driver/targetmachine.h
driver/tomlirfile.h
driver/toobj.h
driver/tool.h
)
Expand All @@ -410,6 +496,8 @@ set(LDC_CXX_SOURCE_FILES
${FE_HDR}
${GEN_SRC}
${GEN_HDR}
${MLIR_HDR}
${MLIR_SRC}
${IR_SRC}
${IR_HDR}
)
Expand All @@ -423,6 +511,10 @@ set(LDC_D_SOURCE_FILES
source_group("Source Files\\dmd" FILES ${FE_SRC_D})
source_group("Header Files\\dmd" FILES ${FE_HDR})
source_group("Source Files\\gen" FILES ${GEN_SRC} ${GEN_SRC_D})
if(MLIR_FOUND)
source_group("Header Files\\gen\\MLIR" FILES ${MLIR_HDR})
source_group("Source Files\\gen\\MLIR" FILES ${MLIR_SRC})
endif()
source_group("Header Files\\gen" FILES ${GEN_HDR})
source_group("Source Files\\ir" FILES ${IR_SRC} ${IR_SRC_D})
source_group("Header Files\\ir" FILES ${IR_HDR})
Expand Down Expand Up @@ -675,7 +767,9 @@ if (HAVE_SC_ARG_MAX)
append("-DHAVE_SC_ARG_MAX" CMAKE_CXX_FLAGS)
endif()

set_source_files_properties(driver/args.cpp driver/exe_path.cpp driver/ldmd.cpp driver/response.cpp PROPERTIES
set_source_files_properties( driver/args.cpp driver/exe_path.cpp driver/ldmd.cpp driver/response
.cpp
PROPERTIES
COMPILE_FLAGS "${LLVM_CXXFLAGS} ${LDC_CXXFLAGS}"
COMPILE_DEFINITIONS LDC_EXE_NAME="${LDC_EXE_NAME}"
)
Expand Down
41 changes: 41 additions & 0 deletions cmake/Modules/FindMLIR.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# - Try to find MLIR project at LLVM
#
# The following are set after configuration is done:
# MLIR_FOUND
# MLIR_ROOT_DIRS
# MLIR_INCLUDE_DIRS
# MLIR_BUILD_INCLUDE_DIR
project(ldc)

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

#Used to get the main header files
find_path(MLIR_INCLUDE_DIR NAMES "Parser.h" HINTS ${MLIR_ROOT_DIR}/include/mlir)

#Lib directories
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)

message(STATUS "MLIR Dir: ${MLIR_ROOT_DIR}")
message(STATUS "MLIR Include Dir: ${MLIR_INCLUDE_DIR}/..")
message(STATUS "MLIR Build Include Dir: ${MLIR_BUILD_INCLUDE_DIR}/..")
message(STATUS "MLIR Lib Dir: ${MLIR_LIB_DIR}")

set(MLIR_ROOT_DIRS ${MLIR_ROOT_DIR})
set(MLIR_INCLUDE_DIRS ${MLIR_INCLUDE_DIR}/..)
set(MLIR_BUILD_INCLUDE_DIRS ${MLIR_BUILD_INCLUDE_DIR}/..)
set(MLIR_LIB_DIRS ${MLIR_LIB_DIR})

# Handle the QUIETLY and REQUIRED arguments and set the MLIR_FOUND to TRUE
# if all listed variables are TRUE
if(EXISTS ${MLIR_ROOT_DIR})
include(FindPackageHandleStandardArgs)

find_package_handle_standard_args(MLIR DEFAULT_MSG MLIR_ROOT_DIRS MLIR_INCLUDE_DIRS
MLIR_BUILD_INCLUDE_DIRS MLIR_LIB_DIRS)

mark_as_advanced(MLIR_ROOT_DIRS MLIR_INCLUDE_DIRS MLIR_BUILD_INCLUDE_DIRS MLIR_LIB_DIRS)
endif()
2 changes: 2 additions & 0 deletions dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,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);
Expand Down
5 changes: 5 additions & 0 deletions dmd/globals.d
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ version (IN_LLVM)

// LDC stuff
OUTPUTFLAG output_ll;
OUTPUTFLAG output_mlir;
OUTPUTFLAG output_bc;
OUTPUTFLAG output_s;
OUTPUTFLAG output_o;
Expand Down Expand Up @@ -347,6 +348,7 @@ 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;
Expand Down Expand Up @@ -422,9 +424,11 @@ version (IN_LLVM)

extern (C++) void _init()
{

static if (!IN_LLVM) _version = import("VERSION") ~ '\0';

version (MARS)

{
vendor = "Digital Mars D";
static if (TARGET.Windows)
Expand Down Expand Up @@ -502,6 +506,7 @@ version (IN_LLVM)
vendor = "LDC";
obj_ext = "o";
ll_ext = "ll";
mlir_ext = "mlir";
bc_ext = "bc";
s_ext = "s";

Expand Down
4 changes: 4 additions & 0 deletions dmd/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ struct Param

// LDC stuff
OUTPUTFLAG output_ll;
OUTPUTFLAG output_mlir;
OUTPUTFLAG output_bc;
OUTPUTFLAG output_s;
OUTPUTFLAG output_o;
Expand Down Expand Up @@ -292,12 +293,15 @@ struct Global
const DString mars_ext;
DString obj_ext;
#if IN_LLVM

DString ll_ext;
DString mlir_ext; //MLIR code
DString bc_ext;
DString s_ext;
DString ldc_version;
DString llvm_version;


bool gaggedForInlining; // Set for functionSemantic3 for external inlining candidates
#endif
DString lib_ext;
Expand Down
3 changes: 3 additions & 0 deletions driver/cl_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ cl::opt<bool> output_bc("output-bc", cl::desc("Write LLVM bitcode"),

cl::opt<bool> output_ll("output-ll", cl::desc("Write LLVM IR"), cl::ZeroOrMore);

cl::opt<bool> output_mlir("output-mlir", cl::desc("Write MLIR"),
cl::ZeroOrMore);

cl::opt<bool> output_s("output-s", cl::desc("Write native assembly"),
cl::ZeroOrMore);

Expand Down
1 change: 1 addition & 0 deletions driver/cl_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extern cl::opt<std::string> objectDir;
extern cl::opt<std::string> soname;
extern cl::opt<bool> output_bc;
extern cl::opt<bool> output_ll;
extern cl::opt<bool> output_mlir;
extern cl::opt<bool> output_s;
extern cl::opt<cl::boolOrDefault> output_o;
extern cl::opt<std::string> ddocDir;
Expand Down
28 changes: 25 additions & 3 deletions driver/codegenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
#include "driver/cl_options_instrumentation.h"
#include "driver/linker.h"
#include "driver/toobj.h"
#if LDC_MLIR_ENABLED
#include "driver/tomlirfile.h"
#endif
#include "gen/dynamiccompile.h"
#include "gen/logger.h"
#include "gen/modules.h"
Expand All @@ -36,6 +39,12 @@ namespace llvm {
}
#endif

#if LDC_LLVM_VER >= 1000
using std::make_unique;
#else
using llvm::make_unique;
#endif

namespace {

std::unique_ptr<llvm::ToolOutputFile>
Expand Down Expand Up @@ -69,7 +78,7 @@ createAndSetDiagnosticsOutputFile(IRState &irs, llvm::LLVMContext &ctx,
diagnosticsOutputFile = std::move(*remarksFileOrError);
#else
std::error_code EC;
diagnosticsOutputFile = llvm::make_unique<llvm::ToolOutputFile>(
diagnosticsOutputFile = make_unique<llvm::ToolOutputFile>(
diagnosticsFilename, EC, llvm::sys::fs::F_None);
if (EC) {
irs.dmodule->error("Could not create file %s: %s",
Expand All @@ -78,9 +87,11 @@ createAndSetDiagnosticsOutputFile(IRState &irs, llvm::LLVMContext &ctx,
}

ctx.setDiagnosticsOutputFile(

llvm::make_unique<llvm::yaml::Output>(diagnosticsOutputFile->os()));

if (withHotness) {

#if LDC_LLVM_VER >= 500
ctx.setDiagnosticsHotnessRequested(true);
#else
Expand Down Expand Up @@ -187,8 +198,16 @@ 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), moduleCount_(0), singleObj_(singleObj), ir_(nullptr)
#if LDC_MLIR_ENABLED
, mlirContext_(mlirContext)
#endif
{
// Set the context to discard value names when not generating textual IR.
if (!global.params.output_ll) {
context_.setDiscardValueNames(true);
Expand Down Expand Up @@ -243,6 +262,9 @@ void CodeGenerator::finishLLModule(Module *m) {
insertBitcodeFiles(ir_->module, ir_->context(), global.params.bitcodeFiles);
}

#if LDC_MLIR_ENABLED
writeMLIRModule(m, mlirContext_, m->objfile.toChars(), ir_);
#endif
writeAndFreeLLModule(m->objfile.toChars());
}

Expand Down
12 changes: 11 additions & 1 deletion driver/codegenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@
#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, bool singleObj);
CodeGenerator(llvm::LLVMContext &context,
#if LDC_MLIR_ENABLED
mlir::MLIRContext &mlirContext,
#endif
bool singleObj);
~CodeGenerator();
void emit(Module *m);

Expand All @@ -35,6 +42,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_;
Expand Down
Loading

0 comments on commit 2780246

Please sign in to comment.