59 lines
2.0 KiB
C++
59 lines
2.0 KiB
C++
/* -*- C++ -*-
|
|
* Serene Programming Language
|
|
*
|
|
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, version 2.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "serene/slir/slir.h"
|
|
|
|
namespace serene {
|
|
namespace slir {
|
|
|
|
std::unique_ptr<llvm::Module> compileToLLVMIR(serene::SereneContext &ctx,
|
|
mlir::ModuleOp &module) {
|
|
// Register the translation to LLVM IR with the MLIR context.
|
|
mlir::registerLLVMDialectTranslation(ctx.mlirContext);
|
|
|
|
// Convert the module to LLVM IR in a new LLVM IR context.
|
|
auto llvmModule = mlir::translateModuleToLLVMIR(module, ctx.llvmContext);
|
|
if (!llvmModule) {
|
|
// TODO: Return a Result type instead
|
|
llvm::errs() << "Failed to emit LLVM IR\n";
|
|
throw std::runtime_error("Failed to emit LLVM IR\n");
|
|
}
|
|
|
|
// Initialize LLVM targets.
|
|
llvm::InitializeNativeTarget();
|
|
llvm::InitializeNativeTargetAsmPrinter();
|
|
|
|
// TODO: replace this call with our own version of setupTargetTriple
|
|
mlir::ExecutionEngine::setupTargetTriple(llvmModule.get());
|
|
|
|
/// Optionally run an optimization pipeline over the llvm module.
|
|
auto optPipeline = mlir::makeOptimizingTransformer(
|
|
/*optLevel=*/ctx.getOptimizatioLevel(), /*sizeLevel=*/0,
|
|
/*targetMachine=*/nullptr);
|
|
if (auto err = optPipeline(llvmModule.get())) {
|
|
llvm::errs() << "Failed to optimize LLVM IR " << err << "\n";
|
|
throw std::runtime_error("Failed to optimize LLVM IR");
|
|
}
|
|
|
|
return llvmModule;
|
|
};
|
|
|
|
} // namespace slir
|
|
|
|
} // namespace serene
|