Skip to content

Commit

Permalink
Resolve dependency conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuanqiXu9 committed Sep 13, 2024
1 parent 470e8b8 commit 75edb18
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 107 deletions.
4 changes: 0 additions & 4 deletions clang/include/clang/CIRFrontendAction/CIRGenAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ class EmitObjAction : public CIRGenAction {
public:
EmitObjAction(mlir::MLIRContext *mlirCtx = nullptr);
};

std::unique_ptr<clang::ASTConsumer>
createCIRAnalysisOnlyConsumer(clang::CompilerInstance &);

} // namespace cir

#endif
102 changes: 102 additions & 0 deletions clang/include/clang/CIRFrontendAction/CIRGenConsumer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//===---- CIRGenConsumer.h - CIR Code Generation Consumer ------*- C++ -*--===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_CIR_CIRGENCONSUMER_H
#define LLVM_CLANG_CIR_CIRGENCONSUMER_H

#include "clang/AST/ASTConsumer.h"
#include "clang/CIR/CIRGenerator.h"
#include "clang/CIRFrontendAction/CIRGenAction.h"
#include "clang/Frontend/CompilerInstance.h"

namespace llvm {
class raw_pwrite_stream;
}

namespace clang {
class DiagnosticsEngine;
class HeaderSearchOptions;
class CodeGenOptions;
class TargetOptions;
class LangOptions;
class FrontendOptions;
} // namespace clang

namespace cir {

class CIRGenConsumer : public clang::ASTConsumer {
protected:
virtual void anchor();

CIRGenAction::OutputType action;

clang::DiagnosticsEngine &diagnosticsEngine;
const clang::HeaderSearchOptions &headerSearchOptions;
const clang::CodeGenOptions &codeGenOptions;
const clang::TargetOptions &targetOptions;
const clang::LangOptions &langOptions;
const clang::FrontendOptions &feOptions;

std::unique_ptr<llvm::raw_pwrite_stream> outputStream;

clang::ASTContext *astContext{nullptr};
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
std::unique_ptr<CIRGenerator> gen;

public:
CIRGenConsumer(CIRGenAction::OutputType action,
clang::DiagnosticsEngine &diagnosticsEngine,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
const clang::HeaderSearchOptions &headerSearchOptions,
const clang::CodeGenOptions &codeGenOptions,
const clang::TargetOptions &targetOptions,
const clang::LangOptions &langOptions,
const clang::FrontendOptions &feOptions,
std::unique_ptr<llvm::raw_pwrite_stream> os)
: action(action), diagnosticsEngine(diagnosticsEngine),
headerSearchOptions(headerSearchOptions),
codeGenOptions(codeGenOptions), targetOptions(targetOptions),
langOptions(langOptions), feOptions(feOptions),
outputStream(std::move(os)), FS(VFS),
gen(std::make_unique<CIRGenerator>(diagnosticsEngine, std::move(VFS),
codeGenOptions)) {}

void Initialize(clang::ASTContext &ctx) override;

bool HandleTopLevelDecl(clang::DeclGroupRef D) override;

void HandleCXXStaticMemberVarInstantiation(clang::VarDecl *VD) override;
void HandleInlineFunctionDefinition(clang::FunctionDecl *D) override;

void HandleInterestingDecl(clang::DeclGroupRef D) override;

void HandleTranslationUnit(clang::ASTContext &C) override {}

void HandleTagDeclDefinition(clang::TagDecl *D) override;

void HandleTagDeclRequiredDefinition(const clang::TagDecl *D) override;

void CompleteTentativeDefinition(clang::VarDecl *D) override;

void CompleteExternalDeclaration(clang::VarDecl *D) override;

void AssignInheritanceModel(clang::CXXRecordDecl *RD) override;

void HandleVTable(clang::CXXRecordDecl *RD) override;
};

inline std::unique_ptr<clang::ASTConsumer>
createCIRAnalysisOnlyConsumer(clang::CompilerInstance &ci) {
return std::make_unique<cir::CIRGenConsumer>(
CIRGenAction::OutputType::None, ci.getDiagnostics(),
&ci.getVirtualFileSystem(), ci.getHeaderSearchOpts(), ci.getCodeGenOpts(),
ci.getTargetOpts(), ci.getLangOpts(), ci.getFrontendOpts(), nullptr);
}
} // namespace cir

#endif // LLVM_CLANG_CIR_CIRGENCONSUMER_H
67 changes: 67 additions & 0 deletions clang/lib/CIR/CodeGen/CIRGenConsumer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//===--- CIRGenConsumer.cpp -ClangIR Generator Consumer -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/CIRFrontendAction/CIRGenConsumer.h"

using namespace cir;
using namespace clang;

void CIRGenConsumer::Initialize(ASTContext &ctx) {
assert(!astContext && "initialized multiple times");

astContext = &ctx;

gen->Initialize(ctx);
}

bool CIRGenConsumer::HandleTopLevelDecl(DeclGroupRef D) {
PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
astContext->getSourceManager(),
"LLVM IR generation of declaration");
gen->HandleTopLevelDecl(D);
return true;
}

void CIRGenConsumer::HandleCXXStaticMemberVarInstantiation(clang::VarDecl *VD) {
gen->HandleCXXStaticMemberVarInstantiation(VD);
}

void CIRGenConsumer::HandleInlineFunctionDefinition(FunctionDecl *D) {
gen->HandleInlineFunctionDefinition(D);
}

void CIRGenConsumer::HandleInterestingDecl(DeclGroupRef D) {
llvm_unreachable("NYI");
}

void CIRGenConsumer::HandleTagDeclDefinition(TagDecl *D) {
PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
astContext->getSourceManager(),
"CIR generation of declaration");
gen->HandleTagDeclDefinition(D);
}

void CIRGenConsumer::HandleTagDeclRequiredDefinition(const TagDecl *D) {
gen->HandleTagDeclRequiredDefinition(D);
}

void CIRGenConsumer::CompleteTentativeDefinition(VarDecl *D) {
gen->CompleteTentativeDefinition(D);
}

void CIRGenConsumer::CompleteExternalDeclaration(VarDecl *D) {
llvm_unreachable("NYI");
}

void CIRGenConsumer::AssignInheritanceModel(CXXRecordDecl *RD) {
llvm_unreachable("NYI");
}

void CIRGenConsumer::HandleVTable(CXXRecordDecl *RD) { gen->HandleVTable(RD); }

void CIRGenConsumer::anchor() {}
1 change: 1 addition & 0 deletions clang/lib/CIR/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_clang_library(clangCIR
CIRGenClass.cpp
CIRGenCleanup.cpp
CIRGenCoroutine.cpp
CIRGenConsumer.cpp
CIRGenDecl.cpp
CIRGenDeclCXX.cpp
CIRGenException.cpp
Expand Down
119 changes: 18 additions & 101 deletions clang/lib/CIR/FrontendAction/CIRGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/CIR/CIRGenerator.h"
#include "clang/CIRFrontendAction/CIRGenConsumer.h"
#include "clang/CIR/CIRToCIRPasses.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/LowerToLLVM.h"
Expand Down Expand Up @@ -72,8 +73,6 @@ static std::string sanitizePassOptions(llvm::StringRef o) {
return llvm::StringRef(opts).trim('"').str();
}

namespace cir {

static BackendAction
getBackendActionFromOutputType(CIRGenAction::OutputType action) {
switch (action) {
Expand Down Expand Up @@ -102,70 +101,23 @@ lowerFromCIRToLLVMIR(const clang::FrontendOptions &feOptions,
return lowerFromCIRToMLIRToLLVMIR(mlirMod, std::move(mlirCtx), llvmCtx);
}

class CIRGenConsumer : public clang::ASTConsumer {

virtual void anchor();

CIRGenAction::OutputType action;

DiagnosticsEngine &diagnosticsEngine;
const HeaderSearchOptions &headerSearchOptions;
const CodeGenOptions &codeGenOptions;
const TargetOptions &targetOptions;
const LangOptions &langOptions;
const FrontendOptions &feOptions;

std::unique_ptr<raw_pwrite_stream> outputStream;

ASTContext *astContext{nullptr};
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
std::unique_ptr<CIRGenerator> gen;
namespace {
class CIRLoweringConsumer : public cir::CIRGenConsumer {
using base = cir::CIRGenConsumer;

public:
CIRGenConsumer(CIRGenAction::OutputType action,
DiagnosticsEngine &diagnosticsEngine,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
const HeaderSearchOptions &headerSearchOptions,
const CodeGenOptions &codeGenOptions,
const TargetOptions &targetOptions,
const LangOptions &langOptions,
const FrontendOptions &feOptions,
std::unique_ptr<raw_pwrite_stream> os)
: action(action), diagnosticsEngine(diagnosticsEngine),
headerSearchOptions(headerSearchOptions),
codeGenOptions(codeGenOptions), targetOptions(targetOptions),
langOptions(langOptions), feOptions(feOptions),
outputStream(std::move(os)), FS(VFS),
gen(std::make_unique<CIRGenerator>(diagnosticsEngine, std::move(VFS),
codeGenOptions)) {}

void Initialize(ASTContext &ctx) override {
assert(!astContext && "initialized multiple times");

astContext = &ctx;

gen->Initialize(ctx);
}

bool HandleTopLevelDecl(DeclGroupRef D) override {
PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(),
astContext->getSourceManager(),
"LLVM IR generation of declaration");
gen->HandleTopLevelDecl(D);
return true;
}

void HandleCXXStaticMemberVarInstantiation(clang::VarDecl *VD) override {
gen->HandleCXXStaticMemberVarInstantiation(VD);
}

void HandleInlineFunctionDefinition(FunctionDecl *D) override {
gen->HandleInlineFunctionDefinition(D);
}

void HandleInterestingDecl(DeclGroupRef D) override {
llvm_unreachable("NYI");
}
CIRLoweringConsumer(CIRGenAction::OutputType action,
clang::DiagnosticsEngine &diagnosticsEngine,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
const clang::HeaderSearchOptions &headerSearchOptions,
const clang::CodeGenOptions &codeGenOptions,
const clang::TargetOptions &targetOptions,
const clang::LangOptions &langOptions,
const clang::FrontendOptions &feOptions,
std::unique_ptr<llvm::raw_pwrite_stream> os)
: base(action, diagnosticsEngine, VFS, headerSearchOptions,
codeGenOptions, targetOptions, langOptions, feOptions,
std::move(os)) {}

void HandleTranslationUnit(ASTContext &C) override {
// Note that this method is called after `HandleTopLevelDecl` has already
Expand Down Expand Up @@ -297,35 +249,8 @@ class CIRGenConsumer : public clang::ASTConsumer {
break;
}
}

void HandleTagDeclDefinition(TagDecl *D) override {
PrettyStackTraceDecl CrashInfo(D, SourceLocation(),
astContext->getSourceManager(),
"CIR generation of declaration");
gen->HandleTagDeclDefinition(D);
}

void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
gen->HandleTagDeclRequiredDefinition(D);
}

void CompleteTentativeDefinition(VarDecl *D) override {
gen->CompleteTentativeDefinition(D);
}

void CompleteExternalDeclaration(VarDecl *D) override {
llvm_unreachable("NYI");
}

void AssignInheritanceModel(CXXRecordDecl *RD) override {
llvm_unreachable("NYI");
}

void HandleVTable(CXXRecordDecl *RD) override { gen->HandleVTable(RD); }
};
} // namespace cir

void CIRGenConsumer::anchor() {}
} // namespace

CIRGenAction::CIRGenAction(OutputType act, mlir::MLIRContext *_MLIRContext)
: mlirContext(_MLIRContext ? _MLIRContext : new mlir::MLIRContext),
Expand Down Expand Up @@ -373,7 +298,7 @@ CIRGenAction::CreateASTConsumer(CompilerInstance &ci, StringRef inputFile) {
if (!out)
out = getOutputStream(ci, inputFile, action);

auto Result = std::make_unique<cir::CIRGenConsumer>(
auto Result = std::make_unique<CIRLoweringConsumer>(
action, ci.getDiagnostics(), &ci.getVirtualFileSystem(),
ci.getHeaderSearchOpts(), ci.getCodeGenOpts(), ci.getTargetOpts(),
ci.getLangOpts(), ci.getFrontendOpts(), std::move(out));
Expand Down Expand Up @@ -470,11 +395,3 @@ EmitBCAction::EmitBCAction(mlir::MLIRContext *_MLIRContext)
void EmitObjAction::anchor() {}
EmitObjAction::EmitObjAction(mlir::MLIRContext *_MLIRContext)
: CIRGenAction(OutputType::EmitObj, _MLIRContext) {}

std::unique_ptr<clang::ASTConsumer>
cir::createCIRAnalysisOnlyConsumer(clang::CompilerInstance &ci) {
return std::make_unique<cir::CIRGenConsumer>(
CIRGenAction::OutputType::None, ci.getDiagnostics(),
&ci.getVirtualFileSystem(), ci.getHeaderSearchOpts(), ci.getCodeGenOpts(),
ci.getTargetOpts(), ci.getLangOpts(), ci.getFrontendOpts(), nullptr);
}
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ endif()
set(conditional_link_libs)
if(CLANG_ENABLE_CIR)
list(APPEND conditional_link_libs
clangCIRFrontendAction
clangCIR
)
endif()

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CodeGenAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "clang/Basic/TargetInfo.h"
#include "clang/Config/config.h"
#if CLANG_ENABLE_CIR
#include "clang/CIRFrontendAction/CIRGenAction.h"
#include "clang/CIRFrontendAction/CIRGenConsumer.h"
#endif
#include "clang/CodeGen/BackendUtil.h"
#include "clang/CodeGen/ModuleBuilder.h"
Expand Down

0 comments on commit 75edb18

Please sign in to comment.