Skip to content

Commit

Permalink
[CIR] Add option to emit MLIR in LLVM dialect.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jezurko committed Feb 7, 2025
1 parent 5373f42 commit 3af1d49
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 18 deletions.
8 changes: 8 additions & 0 deletions clang/include/clang/CIR/FrontendAction/CIRGenAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CIRGenAction : public clang::ASTFrontendAction {
EmitLLVM,
EmitBC,
EmitMLIR,
EmitMLIRLLVM,
EmitObj,
None
};
Expand Down Expand Up @@ -101,6 +102,13 @@ class EmitMLIRAction : public CIRGenAction {
EmitMLIRAction(mlir::MLIRContext *mlirCtx = nullptr);
};

class EmitMLIRLLVMAction : public CIRGenAction {
virtual void anchor();

public:
EmitMLIRLLVMAction(mlir::MLIRContext *mlirCtx = nullptr);
};

class EmitLLVMAction : public CIRGenAction {
virtual void anchor();

Expand Down
10 changes: 8 additions & 2 deletions clang/include/clang/CIR/LowerToLLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ class ModuleOp;
namespace cir {

namespace direct {
mlir::ModuleOp lowerDirectlyFromCIRToLLVMDialect(mlir::ModuleOp theModule,
bool disableVerifier = false,
bool disableCCLowering = false,
bool disableDebugInfo = false);

// Lower directly from pristine CIR to LLVMIR.
std::unique_ptr<llvm::Module> lowerDirectlyFromCIRToLLVMIR(
mlir::ModuleOp theModule, llvm::LLVMContext &llvmCtx,
bool disableVerifier = false, bool disableCCLowering = false,
bool disableDebugInfo = false);
}

// Lower directly from pristine CIR to LLVMIR.
} // namespace direct

std::unique_ptr<llvm::Module>
lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
std::unique_ptr<mlir::MLIRContext> mlirCtx,
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/CIR/LowerToMLIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@
#ifndef CLANG_CIR_LOWERTOMLIR_H
#define CLANG_CIR_LOWERTOMLIR_H

#include "mlir/Transforms/DialectConversion.h"

namespace cir {

void populateCIRLoopToSCFConversionPatterns(mlir::RewritePatternSet &patterns,
mlir::TypeConverter &converter);

mlir::ModuleOp
lowerFromCIRToMLIRToLLVMDialect(mlir::ModuleOp theModule,
mlir::MLIRContext *mlirCtx = nullptr);
} // namespace cir

#endif // CLANG_CIR_LOWERTOMLIR_H_
2 changes: 2 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -3121,6 +3121,8 @@ def emit_cir_flat : Flag<["-"], "emit-cir-flat">, Visibility<[ClangOption, CC1Op
Group<Action_Group>, HelpText<"Similar to -emit-cir but also lowers structured CFG into basic blocks.">;
def emit_mlir : Flag<["-"], "emit-mlir">, Visibility<[CC1Option]>, Group<Action_Group>,
HelpText<"Build ASTs and then lower through ClangIR to MLIR, emit the .milr file">;
def emit_mlir_llvm : Flag<["-"], "emit-mlir-llvm">, Visibility<[CC1Option]>, Group<Action_Group>,
HelpText<"Build ASTs and then lower through ClangIR to MLIR LLVM Dialect, emit the .mlir file">;
/// ClangIR-specific options - END

def flto : Flag<["-"], "flto">,
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ enum ActionKind {
/// Emit a .mlir file
EmitMLIR,

/// Emit an .mlir file with LLVM dialect
EmitMLIRLLVM,

/// Emit a .ll file.
EmitLLVM,

Expand Down
29 changes: 24 additions & 5 deletions clang/lib/CIR/FrontendAction/CIRGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "clang/CIR/CIRToCIRPasses.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/LowerToLLVM.h"
#include "clang/CIR/LowerToMLIR.h"
#include "clang/CIR/Passes.h"
#include "clang/CodeGen/BackendUtil.h"
#include "clang/CodeGen/ModuleBuilder.h"
Expand Down Expand Up @@ -260,6 +261,14 @@ class CIRGenConsumer : public clang::ASTConsumer {
}
}

auto printMlirModule = [&](mlir::ModuleOp root) {
assert(outputStream && "Why are we here without an output stream?");
// FIXME: we cannot roundtrip prettyForm=true right now.
mlir::OpPrintingFlags flags;
flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false);
root->print(*outputStream, flags);
};

switch (action) {
case CIRGenAction::OutputType::EmitCIR:
case CIRGenAction::OutputType::EmitCIRFlat:
Expand All @@ -274,11 +283,15 @@ class CIRGenConsumer : public clang::ASTConsumer {
break;
case CIRGenAction::OutputType::EmitMLIR: {
auto loweredMlirModule = lowerFromCIRToMLIR(mlirMod, mlirCtx.get());
assert(outputStream && "Why are we here without an output stream?");
// FIXME: we cannot roundtrip prettyForm=true right now.
mlir::OpPrintingFlags flags;
flags.enableDebugInfo(/*enable=*/true, /*prettyForm=*/false);
loweredMlirModule->print(*outputStream, flags);
printMlirModule(loweredMlirModule);
break;
}
case CIRGenAction::OutputType::EmitMLIRLLVM: {
auto loweredMlirModule =
feOptions.ClangIRDirectLowering
? direct::lowerDirectlyFromCIRToLLVMDialect(mlirMod)
: lowerFromCIRToMLIRToLLVMDialect(mlirMod);
printMlirModule(loweredMlirModule);
break;
}
case CIRGenAction::OutputType::EmitLLVM:
Expand Down Expand Up @@ -362,6 +375,8 @@ getOutputStream(CompilerInstance &ci, StringRef inFile,
return ci.createDefaultOutputFile(false, inFile, "cir");
case CIRGenAction::OutputType::EmitMLIR:
return ci.createDefaultOutputFile(false, inFile, "mlir");
case CIRGenAction::OutputType::EmitMLIRLLVM:
return ci.createDefaultOutputFile(false, inFile, "mlir");
case CIRGenAction::OutputType::EmitLLVM:
return ci.createDefaultOutputFile(false, inFile, "ll");
case CIRGenAction::OutputType::EmitBC:
Expand Down Expand Up @@ -472,6 +487,10 @@ void EmitMLIRAction::anchor() {}
EmitMLIRAction::EmitMLIRAction(mlir::MLIRContext *_MLIRContext)
: CIRGenAction(OutputType::EmitMLIR, _MLIRContext) {}

void EmitMLIRLLVMAction::anchor() {}
EmitMLIRLLVMAction::EmitMLIRLLVMAction(mlir::MLIRContext *_MLIRContext)
: CIRGenAction(OutputType::EmitMLIRLLVM, _MLIRContext) {}

void EmitLLVMAction::anchor() {}
EmitLLVMAction::EmitLLVMAction(mlir::MLIRContext *_MLIRContext)
: CIRGenAction(OutputType::EmitLLVM, _MLIRContext) {}
Expand Down
24 changes: 19 additions & 5 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4689,11 +4689,11 @@ void populateCIRToLLVMPasses(mlir::OpPassManager &pm, bool useCCLowering) {

extern void registerCIRDialectTranslation(mlir::MLIRContext &context);

std::unique_ptr<llvm::Module>
lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
bool disableVerifier, bool disableCCLowering,
bool disableDebugInfo) {
llvm::TimeTraceScope scope("lower from CIR to LLVM directly");
mlir::ModuleOp lowerDirectlyFromCIRToLLVMDialect(mlir::ModuleOp theModule,
bool disableVerifier,
bool disableCCLowering,
bool disableDebugInfo) {
llvm::TimeTraceScope scope("lower from CIR to LLVM Dialect");

mlir::MLIRContext *mlirCtx = theModule.getContext();
mlir::PassManager pm(mlirCtx);
Expand Down Expand Up @@ -4722,6 +4722,20 @@ lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
if (theModule.verify().failed())
report_fatal_error("Verification of the final LLVMIR dialect failed!");

return theModule;
}

std::unique_ptr<llvm::Module>
lowerDirectlyFromCIRToLLVMIR(mlir::ModuleOp theModule, LLVMContext &llvmCtx,
bool disableVerifier, bool disableCCLowering,
bool disableDebugInfo) {
llvm::TimeTraceScope scope("lower from CIR to LLVM directly");

lowerDirectlyFromCIRToLLVMDialect(theModule, disableVerifier,
disableCCLowering, disableDebugInfo);

mlir::MLIRContext *mlirCtx = theModule.getContext();

mlir::registerBuiltinDialectTranslation(*mlirCtx);
mlir::registerLLVMDialectTranslation(*mlirCtx);
mlir::registerOpenMPDialectTranslation(*mlirCtx);
Expand Down
25 changes: 19 additions & 6 deletions clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "mlir/Transforms/DialectConversion.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/IR/CIRTypes.h"
#include "clang/CIR/LowerToLLVM.h"
#include "clang/CIR/LowerToMLIR.h"
#include "clang/CIR/LoweringHelpers.h"
#include "clang/CIR/Passes.h"
Expand Down Expand Up @@ -1458,13 +1459,14 @@ void ConvertCIRToMLIRPass::runOnOperation() {
signalPassFailure();
}

std::unique_ptr<llvm::Module>
lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
std::unique_ptr<mlir::MLIRContext> mlirCtx,
LLVMContext &llvmCtx) {
llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM");
mlir::ModuleOp lowerFromCIRToMLIRToLLVMDialect(mlir::ModuleOp theModule,
mlir::MLIRContext *mlirCtx) {
llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM Dialect");
if (!mlirCtx) {
mlirCtx = theModule.getContext();
}

mlir::PassManager pm(mlirCtx.get());
mlir::PassManager pm(mlirCtx);

pm.addPass(createConvertCIRToMLIRPass());
pm.addPass(createConvertMLIRToLLVMPass());
Expand All @@ -1478,6 +1480,17 @@ lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
if (theModule.verify().failed())
report_fatal_error("Verification of the final LLVMIR dialect failed!");

return theModule;
}

std::unique_ptr<llvm::Module>
lowerFromCIRToMLIRToLLVMIR(mlir::ModuleOp theModule,
std::unique_ptr<mlir::MLIRContext> mlirCtx,
LLVMContext &llvmCtx) {
llvm::TimeTraceScope scope("Lower from CIR to MLIR To LLVM");

lowerFromCIRToMLIRToLLVMDialect(theModule, mlirCtx.get());

mlir::registerBuiltinDialectTranslation(*mlirCtx);
mlir::registerLLVMDialectTranslation(*mlirCtx);
mlir::registerOpenMPDialectTranslation(*mlirCtx);
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2738,6 +2738,7 @@ static const auto &getFrontendActionTable() {
{frontend::EmitCIRFlat, OPT_emit_cir_flat},
{frontend::EmitCIROnly, OPT_emit_cir_only},
{frontend::EmitMLIR, OPT_emit_mlir},
{frontend::EmitMLIRLLVM, OPT_emit_mlir_llvm},
{frontend::EmitHTML, OPT_emit_html},
{frontend::EmitLLVM, OPT_emit_llvm},
{frontend::EmitLLVMOnly, OPT_emit_llvm_only},
Expand Down Expand Up @@ -4669,6 +4670,7 @@ static bool isStrictlyPreprocessorAction(frontend::ActionKind Action) {
case frontend::EmitCIRFlat:
case frontend::EmitCIROnly:
case frontend::EmitMLIR:
case frontend::EmitMLIRLLVM:
case frontend::EmitHTML:
case frontend::EmitLLVM:
case frontend::EmitLLVMOnly:
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
return std::make_unique<cir::EmitCIROnlyAction>();
case EmitMLIR:
return std::make_unique<cir::EmitMLIRAction>();
case EmitMLIRLLVM:
return std::make_unique<cir::EmitMLIRLLVMAction>();
#else
case EmitCIR:
case EmitCIRFlat:
Expand Down

0 comments on commit 3af1d49

Please sign in to comment.