https://github.com/lanza created https://github.com/llvm/llvm-project/pull/90831
None >From e57548737a72c5308e23fb442774b9bd9eca0bfa Mon Sep 17 00:00:00 2001 From: Nathan Lanza <nathanla...@gmail.com> Date: Thu, 2 May 2024 07:55:36 +0000 Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?= =?UTF-8?q?l=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Created using spr 1.3.5 --- clang/include/clang/CIR/CIRGenerator.h | 28 +++++++++ .../clang/CIRFrontendAction/CIRGenAction.h | 62 +++++++++++++++++++ clang/lib/CIR/CMakeLists.txt | 2 + clang/lib/CIR/CodeGen/CIRGenModule.cpp | 21 +++++++ clang/lib/CIR/CodeGen/CIRGenModule.h | 42 +++++++++++++ clang/lib/CIR/CodeGen/CIRGenTypeCache.h | 28 +++++++++ clang/lib/CIR/CodeGen/CIRGenerator.cpp | 26 ++++++++ clang/lib/CIR/CodeGen/CMakeLists.txt | 23 +++++++ clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 46 ++++++++++++++ clang/lib/CIR/FrontendAction/CMakeLists.txt | 12 ++++ clang/lib/FrontendTool/CMakeLists.txt | 9 +++ .../ExecuteCompilerInvocation.cpp | 17 +++++ 12 files changed, 316 insertions(+) create mode 100644 clang/include/clang/CIR/CIRGenerator.h create mode 100644 clang/include/clang/CIRFrontendAction/CIRGenAction.h create mode 100644 clang/lib/CIR/CodeGen/CIRGenModule.cpp create mode 100644 clang/lib/CIR/CodeGen/CIRGenModule.h create mode 100644 clang/lib/CIR/CodeGen/CIRGenTypeCache.h create mode 100644 clang/lib/CIR/CodeGen/CIRGenerator.cpp create mode 100644 clang/lib/CIR/CodeGen/CMakeLists.txt create mode 100644 clang/lib/CIR/FrontendAction/CIRGenAction.cpp create mode 100644 clang/lib/CIR/FrontendAction/CMakeLists.txt diff --git a/clang/include/clang/CIR/CIRGenerator.h b/clang/include/clang/CIR/CIRGenerator.h new file mode 100644 index 00000000000000..c910f02698f41f --- /dev/null +++ b/clang/include/clang/CIR/CIRGenerator.h @@ -0,0 +1,28 @@ +//===- CIRGenerator.h - CIR Generation from Clang AST ---------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file declares a simple interface to perform CIR generation from Clang +// AST +// +//===----------------------------------------------------------------------===// + +#ifndef CLANG_CIRGENERATOR_H_ +#define CLANG_CIRGENERATOR_H_ + +#include "clang/AST/ASTConsumer.h" +#include "clang/AST/DeclGroup.h" + +namespace cir { +class CIRGenerator : public clang::ASTConsumer { +public: + bool HandleTopLevelDecl(clang::DeclGroupRef D) override; +}; + +} // namespace cir + +#endif // CLANG_CIRGENERATOR_H_ diff --git a/clang/include/clang/CIRFrontendAction/CIRGenAction.h b/clang/include/clang/CIRFrontendAction/CIRGenAction.h new file mode 100644 index 00000000000000..02c906b173f52b --- /dev/null +++ b/clang/include/clang/CIRFrontendAction/CIRGenAction.h @@ -0,0 +1,62 @@ +//===---- CIRGenAction.h - CIR Code Generation Frontend Action -*- 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_CIRGENACTION_H +#define LLVM_CLANG_CIR_CIRGENACTION_H + +#include "clang/Frontend/FrontendAction.h" + +namespace mlir { +class MLIRContext; +class ModuleOp; +template <typename T> class OwningOpRef; +} // namespace mlir + +namespace cir { + +class CIRGenAction : public clang::ASTFrontendAction { +public: + enum class OutputType { + EmitCIR, + }; + +private: + friend class CIRGenConsumer; + + // TODO: this is redundant but just using the OwningModuleRef requires more of + // clang against MLIR. Hide this somewhere else. + std::unique_ptr<mlir::OwningOpRef<mlir::ModuleOp>> mlirModule; + + mlir::MLIRContext *mlirContext; + +protected: + CIRGenAction(OutputType action, mlir::MLIRContext *mlirContext = nullptr); + + void foo() { + + } + + std::unique_ptr<clang::ASTConsumer> + CreateASTConsumer(clang::CompilerInstance &CI, + llvm::StringRef InFile) override; + +public: + ~CIRGenAction() override; + OutputType action; +}; + +class EmitCIRAction : public CIRGenAction { + virtual void anchor(); + +public: + EmitCIRAction(mlir::MLIRContext *mlirCtx = nullptr); +}; + +} // namespace cir + +#endif diff --git a/clang/lib/CIR/CMakeLists.txt b/clang/lib/CIR/CMakeLists.txt index d2ff200e0da5f5..11cca734808dfa 100644 --- a/clang/lib/CIR/CMakeLists.txt +++ b/clang/lib/CIR/CMakeLists.txt @@ -2,3 +2,5 @@ include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include) include_directories(${CMAKE_BINARY_DIR}/tools/mlir/include) add_subdirectory(Dialect) +add_subdirectory(CodeGen) +add_subdirectory(FrontendAction) diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp new file mode 100644 index 00000000000000..c9ed81a749cd2b --- /dev/null +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -0,0 +1,21 @@ +//===- CIRGenModule.cpp - Per-Module state for CIR generation -------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This is the internal per-translation-unit state used for CIR translation. +// +//===----------------------------------------------------------------------===// + +#include "CIRGenModule.h" + +#include "clang/AST/DeclBase.h" + +using namespace cir; + +// Emit code for a single top level declaration. +void CIRGenModule::buildTopLevelDecl(Decl *decl) { +} diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h new file mode 100644 index 00000000000000..4fbd441b7a6196 --- /dev/null +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -0,0 +1,42 @@ +//===--- CIRGenModule.h - Per-Module state for CIR gen ----------*- 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 +// +//===----------------------------------------------------------------------===// +// +// This is the internal per-translation-unit state used for CIR translation. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_LIB_CODEGEN_CIRGENMODULE_H +#define LLVM_CLANG_LIB_CODEGEN_CIRGENMODULE_H + +#include "CIRGenTypeCache.h" + +#include "clang/AST/ASTContext.h" +#include "clang/Basic/CodeGenOptions.h" +#include "clang/Basic/Diagnostic.h" +#include "mlir/IR/MLIRContext.h" + +using namespace clang; +namespace cir { +/// Implementation of a CIR/MLIR emission from Clang AST. +/// +/// This will emit operations that are specific to C(++)/ObjC(++) language, +/// preserving the semantics of the language and (hopefully) allow to perform +/// accurate analysis and transformation based on these high level semantics. +class CIRGenModule : public CIRGenTypeCache { + CIRGenModule(CIRGenModule &) = delete; + CIRGenModule &operator=(CIRGenModule &) = delete; + +public: + CIRGenModule(mlir::MLIRContext &context, clang::ASTContext &astctx, + const clang::CodeGenOptions &CGO, + clang::DiagnosticsEngine &Diags); + + ~CIRGenModule(); + void buildTopLevelDecl(clang::Decl *decl); +}; +#endif // LLVM_CLANG_LIB_CODEGEN_CIRGENMODULE_H diff --git a/clang/lib/CIR/CodeGen/CIRGenTypeCache.h b/clang/lib/CIR/CodeGen/CIRGenTypeCache.h new file mode 100644 index 00000000000000..e23c6b0531e3c7 --- /dev/null +++ b/clang/lib/CIR/CodeGen/CIRGenTypeCache.h @@ -0,0 +1,28 @@ +//===--- CIRGenTypeCache.h - Commonly used LLVM types and info -*- 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 +// +//===----------------------------------------------------------------------===// +// +// This structure provides a set of common types useful during CIR emission. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_LIB_CIR_CODEGENTYPECACHE_H +#define LLVM_CLANG_LIB_CIR_CODEGENTYPECACHE_H + +namespace cir { + +/// This structure provides a set of types that are commonly used +/// during IR emission. It's initialized once in CodeGenModule's +/// constructor and then copied around into new CIRGenFunction's. +struct CIRGenTypeCache { + CIRGenTypeCache() {} + +}; + +} // namespace cir + +#endif diff --git a/clang/lib/CIR/CodeGen/CIRGenerator.cpp b/clang/lib/CIR/CodeGen/CIRGenerator.cpp new file mode 100644 index 00000000000000..a35a5c1372eedd --- /dev/null +++ b/clang/lib/CIR/CodeGen/CIRGenerator.cpp @@ -0,0 +1,26 @@ +//===--- CIRGenerator.cpp - Emit CIR from ASTs ----------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This builds an AST and converts it to CIR. +// +//===----------------------------------------------------------------------===// + +#include "CIRGenModule.h" + +#include "clang/CIR/CIRGenerator.h" + +using namespace cir; +using namespace clang; + +bool CIRGenerator::HandleTopLevelDecl(DeclGroupRef D) { + for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) { + CGM->buildTopLevelDecl(*I); + } + + return true; +} diff --git a/clang/lib/CIR/CodeGen/CMakeLists.txt b/clang/lib/CIR/CodeGen/CMakeLists.txt new file mode 100644 index 00000000000000..17a3aabfbd7f0e --- /dev/null +++ b/clang/lib/CIR/CodeGen/CMakeLists.txt @@ -0,0 +1,23 @@ +set( + LLVM_LINK_COMPONENTS + Core + Support +) + +get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) + +add_clang_library(clangCIR + CIRGenerator.cpp + CIRGenModule.cpp + + DEPENDS + MLIRCIR + ${dialect_libs} + + LINK_LIBS + clangAST + clangBasic + clangLex + ${dialect_libs} + MLIRCIR +) diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp new file mode 100644 index 00000000000000..5d75ad1c15a66f --- /dev/null +++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp @@ -0,0 +1,46 @@ +//===--- CIRGenAction.cpp - LLVM Code generation Frontend Action ---------===// +// +// 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/CIR/CIRGenerator.h" +#include "clang/CIRFrontendAction/CIRGenAction.h" + +#include "mlir/IR/BuiltinOps.h" +#include "mlir/IR/MLIRContext.h" +#include "mlir/IR/OwningOpRef.h" + +using namespace cir; +using namespace clang; + +namespace cir { + +class CIRGenConsumer : public clang::ASTConsumer { + + virtual void anchor(); + + std::unique_ptr<CIRGenerator> gen; + bool HandleTopLevelDecl(DeclGroupRef D) override { + gen->HandleTopLevelDecl(D); + return true; + } +}; +} // namespace cir + +void CIRGenConsumer::anchor() {} + +CIRGenAction::CIRGenAction(OutputType act, mlir::MLIRContext *mlirContext) + : mlirContext(mlirContext ? mlirContext : new mlir::MLIRContext), + action(act) {} + +CIRGenAction::~CIRGenAction() { mlirModule.reset(); } + +std::unique_ptr<ASTConsumer> +CIRGenAction::CreateASTConsumer(CompilerInstance &ci, StringRef inputFile) {} + +void EmitCIRAction::anchor() {} +EmitCIRAction::EmitCIRAction(mlir::MLIRContext *_MLIRContext) + : CIRGenAction(OutputType::EmitCIR, _MLIRContext) {} diff --git a/clang/lib/CIR/FrontendAction/CMakeLists.txt b/clang/lib/CIR/FrontendAction/CMakeLists.txt new file mode 100644 index 00000000000000..164d9e259fb09c --- /dev/null +++ b/clang/lib/CIR/FrontendAction/CMakeLists.txt @@ -0,0 +1,12 @@ +set(LLVM_LINK_COMPONENTS + ) + +get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) + +add_clang_library(clangCIRFrontendAction + CIRGenAction.cpp + + LINK_LIBS + MLIRCIR + MLIRIR + ) diff --git a/clang/lib/FrontendTool/CMakeLists.txt b/clang/lib/FrontendTool/CMakeLists.txt index 51c379ade2704c..6c6e6fd61e643a 100644 --- a/clang/lib/FrontendTool/CMakeLists.txt +++ b/clang/lib/FrontendTool/CMakeLists.txt @@ -12,6 +12,14 @@ set(link_libs clangRewriteFrontend ) +set(deps) + +if(CLANG_ENABLE_CIR) + list(APPEND link_libs + clangCIRFrontendAction + ) +endif() + if(CLANG_ENABLE_ARCMT) list(APPEND link_libs clangARCMigrate @@ -29,6 +37,7 @@ add_clang_library(clangFrontendTool DEPENDS ClangDriverOptions + ${deps} LINK_LIBS ${link_libs} diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp index 7476b1076d1038..ff1216992cded5 100644 --- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp +++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp @@ -31,6 +31,11 @@ #include "llvm/Support/BuryPointer.h" #include "llvm/Support/DynamicLibrary.h" #include "llvm/Support/ErrorHandling.h" + +#if CLANG_ENABLE_CIR +#include "clang/CIRFrontendAction/CIRGenAction.h" +#endif + using namespace clang; using namespace llvm::opt; @@ -42,6 +47,14 @@ CreateFrontendBaseAction(CompilerInstance &CI) { StringRef Action("unknown"); (void)Action; + auto UseCIR = CI.getFrontendOpts().UseClangIRPipeline; + auto Act = CI.getFrontendOpts().ProgramAction; + auto EmitsCIR = Act == EmitCIR; + + if (!UseCIR && EmitsCIR) + llvm::report_fatal_error( + "-emit-cir and -emit-cir-only only valid when using -fclangir"); + switch (CI.getFrontendOpts().ProgramAction) { case ASTDeclList: return std::make_unique<ASTDeclListAction>(); case ASTDump: return std::make_unique<ASTDumpAction>(); @@ -53,8 +66,12 @@ CreateFrontendBaseAction(CompilerInstance &CI) { case DumpTokens: return std::make_unique<DumpTokensAction>(); case EmitAssembly: return std::make_unique<EmitAssemblyAction>(); case EmitBC: return std::make_unique<EmitBCAction>(); +#if CLANG_ENABLE_CIR + case EmitCIR: return std::make_unique<::cir::EmitCIRAction>(); +#else case EmitCIR: llvm_unreachable("CIR suppport not built into clang"); +#endif case EmitHTML: return std::make_unique<HTMLPrintAction>(); case EmitLLVM: return std::make_unique<EmitLLVMAction>(); case EmitLLVMOnly: return std::make_unique<EmitLLVMOnlyAction>(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits