[clang] [CIR] Build out AST consumer patterns to reach the entry point into CIRGen (PR #91007)
Nathan Lanza via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 10 16:21:31 PDT 2024
https://github.com/lanza updated https://github.com/llvm/llvm-project/pull/91007
>From 17c81f79ede403e63010a39622d61937fcf898b4 Mon Sep 17 00:00:00 2001
From: Nathan Lanza <nathanlanza at gmail.com>
Date: Fri, 3 May 2024 20:19:45 +0000
Subject: [PATCH 1/4] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
=?UTF-8?q?itial=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 | 59 +++++++++++++
.../clang/CIRFrontendAction/CIRGenAction.h | 61 +++++++++++++
clang/include/clang/Driver/Options.td | 2 +-
clang/lib/CIR/CMakeLists.txt | 2 +
clang/lib/CIR/CodeGen/CIRGenModule.cpp | 35 ++++++++
clang/lib/CIR/CodeGen/CIRGenModule.h | 61 +++++++++++++
clang/lib/CIR/CodeGen/CIRGenTypeCache.h | 27 ++++++
clang/lib/CIR/CodeGen/CIRGenerator.cpp | 46 ++++++++++
clang/lib/CIR/CodeGen/CMakeLists.txt | 23 +++++
clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 88 +++++++++++++++++++
clang/lib/CIR/FrontendAction/CMakeLists.txt | 17 ++++
clang/lib/Driver/ToolChains/Clang.cpp | 3 +
clang/lib/FrontendTool/CMakeLists.txt | 15 ++++
.../ExecuteCompilerInvocation.cpp | 18 ++++
clang/test/CIR/hello.c | 4 +
clang/test/CIR/lit.local.cfg | 2 +
16 files changed, 462 insertions(+), 1 deletion(-)
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
create mode 100644 clang/test/CIR/hello.c
create mode 100644 clang/test/CIR/lit.local.cfg
diff --git a/clang/include/clang/CIR/CIRGenerator.h b/clang/include/clang/CIR/CIRGenerator.h
new file mode 100644
index 0000000000000..c9505d2473b43
--- /dev/null
+++ b/clang/include/clang/CIR/CIRGenerator.h
@@ -0,0 +1,59 @@
+//===- 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"
+#include "clang/Basic/CodeGenOptions.h"
+#include "clang/Basic/Diagnostic.h"
+
+#include "llvm/ADT/IntrusiveRefCntPtr.h"
+#include "llvm/Support/VirtualFileSystem.h"
+
+#include <memory>
+
+namespace mlir {
+class MLIRContext;
+} // namespace mlir
+namespace cir {
+class CIRGenModule;
+
+class CIRGenerator : public clang::ASTConsumer {
+ virtual void anchor();
+ clang::DiagnosticsEngine &Diags;
+ clang::ASTContext *astCtx;
+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
+ fs; // Only used for debug info.
+
+ const clang::CodeGenOptions codeGenOpts; // Intentionally copied in.
+
+ [[maybe_unused]] unsigned HandlingTopLevelDecls;
+
+protected:
+ std::unique_ptr<mlir::MLIRContext> mlirCtx;
+ std::unique_ptr<CIRGenModule> CGM;
+
+public:
+ CIRGenerator(clang::DiagnosticsEngine &diags,
+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
+ const clang::CodeGenOptions &CGO);
+ ~CIRGenerator();
+ void Initialize(clang::ASTContext &Context) override;
+ 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 0000000000000..aaf73ec2bffc4
--- /dev/null
+++ b/clang/include/clang/CIRFrontendAction/CIRGenAction.h
@@ -0,0 +1,61 @@
+//===---- 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"
+
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/OwningOpRef.h"
+
+namespace mlir {
+class MLIRContext;
+class ModuleOp;
+} // namespace mlir
+
+namespace cir {
+class CIRGenConsumer;
+
+class CIRGenAction : public clang::ASTFrontendAction {
+public:
+ enum class OutputType {
+ EmitCIR,
+ };
+
+private:
+ friend class CIRGenConsumer;
+
+ mlir::OwningOpRef<mlir::ModuleOp> mlirModule;
+
+ mlir::MLIRContext *mlirContext;
+
+protected:
+ CIRGenAction(OutputType action, mlir::MLIRContext *mlirContext = nullptr);
+
+ std::unique_ptr<clang::ASTConsumer>
+ CreateASTConsumer(clang::CompilerInstance &CI,
+ llvm::StringRef InFile) override;
+
+public:
+ ~CIRGenAction() override;
+
+ CIRGenConsumer *cgConsumer;
+ OutputType action;
+};
+
+class EmitCIRAction : public CIRGenAction {
+ virtual void anchor();
+
+public:
+ EmitCIRAction(mlir::MLIRContext *mlirCtx = nullptr);
+};
+
+} // namespace cir
+
+#endif
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 938d5358eeda6..bd1200f77079a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2900,7 +2900,7 @@ defm clangir : BoolFOption<"clangir",
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Use the ClangIR pipeline to compile">,
NegFlag<SetFalse, [], [ClangOption, CC1Option], "Use the AST -> LLVM pipeline to compile">,
BothFlags<[], [ClangOption, CC1Option], "">>;
-def emit_cir : Flag<["-"], "emit-cir">, Visibility<[CC1Option]>,
+def emit_cir : Flag<["-"], "emit-cir">, Visibility<[ClangOption, CC1Option]>,
Group<Action_Group>, HelpText<"Build ASTs and then lower to ClangIR">;
/// ClangIR-specific options - END
diff --git a/clang/lib/CIR/CMakeLists.txt b/clang/lib/CIR/CMakeLists.txt
index d2ff200e0da5f..11cca734808df 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 0000000000000..244ab4117c859
--- /dev/null
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -0,0 +1,35 @@
+//===- 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"
+
+#include "llvm/Support/Debug.h"
+
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/Location.h"
+#include "mlir/IR/MLIRContext.h"
+
+using namespace cir;
+CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
+ clang::ASTContext &astctx,
+ const clang::CodeGenOptions &CGO,
+ DiagnosticsEngine &Diags)
+ : astCtx(astctx), langOpts(astctx.getLangOpts()), codeGenOpts(CGO),
+ theModule{mlir::ModuleOp::create(mlir::UnknownLoc())}, Diags(Diags),
+ target(astCtx.getTargetInfo()) {}
+
+CIRGenModule::~CIRGenModule() {}
+
+// 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 0000000000000..e7917be14cfd3
--- /dev/null
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.h
@@ -0,0 +1,61 @@
+//===--- 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_CIR_CODEGEN_CIRGENMODULE_H
+#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H
+
+#include "CIRGenTypeCache.h"
+
+#include "clang/AST/ASTContext.h"
+#include "clang/Basic/CodeGenOptions.h"
+#include "clang/Basic/Diagnostic.h"
+
+#include "mlir/IR/BuiltinOps.h"
+#include "mlir/IR/MLIRContext.h"
+
+using namespace clang;
+namespace cir {
+
+/// This class organizes the cross-function state that is used while generating
+/// CIR code.
+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();
+
+private:
+ /// Hold Clang AST information.
+ clang::ASTContext &astCtx;
+
+ const clang::LangOptions &langOpts;
+
+ [[maybe_unused]] const clang::CodeGenOptions &codeGenOpts;
+
+ /// A "module" matches a c/cpp source file: containing a list of functions.
+ mlir::ModuleOp theModule;
+
+ [[maybe_unused]] clang::DiagnosticsEngine &Diags;
+
+ const clang::TargetInfo ⌖
+
+public:
+ void buildTopLevelDecl(clang::Decl *decl);
+};
+} // namespace cir
+
+#endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H
diff --git a/clang/lib/CIR/CodeGen/CIRGenTypeCache.h b/clang/lib/CIR/CodeGen/CIRGenTypeCache.h
new file mode 100644
index 0000000000000..9b865aa20b4fd
--- /dev/null
+++ b/clang/lib/CIR/CodeGen/CIRGenTypeCache.h
@@ -0,0 +1,27 @@
+//===--- 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_CIRGENTYPECACHE_H
+#define LLVM_CLANG_LIB_CIR_CIRGENTYPECACHE_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 // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENTYPECACHE_H
diff --git a/clang/lib/CIR/CodeGen/CIRGenerator.cpp b/clang/lib/CIR/CodeGen/CIRGenerator.cpp
new file mode 100644
index 0000000000000..00f9e97ee1327
--- /dev/null
+++ b/clang/lib/CIR/CodeGen/CIRGenerator.cpp
@@ -0,0 +1,46 @@
+//===--- 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/AST/DeclGroup.h"
+#include "clang/CIR/CIRGenerator.h"
+
+using namespace cir;
+using namespace clang;
+
+void CIRGenerator::anchor() {}
+
+CIRGenerator::CIRGenerator(clang::DiagnosticsEngine &diags,
+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
+ const CodeGenOptions &CGO)
+ : Diags(diags), fs(std::move(vfs)), codeGenOpts{CGO},
+ HandlingTopLevelDecls(0) {}
+CIRGenerator::~CIRGenerator() {}
+
+void CIRGenerator::Initialize(ASTContext &astCtx) {
+ using namespace llvm;
+
+ this->astCtx = &astCtx;
+
+ CGM = std::make_unique<CIRGenModule>(*mlirCtx.get(), astCtx, codeGenOpts,
+ Diags);
+}
+
+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 0000000000000..17a3aabfbd7f0
--- /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 0000000000000..efdd109963a19
--- /dev/null
+++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
@@ -0,0 +1,88 @@
+//===--- 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/CIRFrontendAction/CIRGenAction.h"
+#include "clang/CIR/CIRGenerator.h"
+#include "clang/Frontend/CompilerInstance.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();
+
+ [[maybe_unused]] CIRGenAction::OutputType action;
+
+ [[maybe_unused]] DiagnosticsEngine &diagnosticsEngine;
+ [[maybe_unused]] const HeaderSearchOptions &headerSearchOptions;
+ [[maybe_unused]] const CodeGenOptions &codeGenOptions;
+ [[maybe_unused]] const TargetOptions &targetOptions;
+ [[maybe_unused]] const LangOptions &langOptions;
+ [[maybe_unused]] const FrontendOptions &feOptions;
+
+ std::unique_ptr<raw_pwrite_stream> outputStream;
+
+ [[maybe_unused]] ASTContext *astContext{nullptr};
+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
+ std::unique_ptr<CIRGenerator> gen;
+
+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)) {}
+
+ 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.release(); }
+
+std::unique_ptr<ASTConsumer>
+CIRGenAction::CreateASTConsumer(CompilerInstance &ci, StringRef inputFile) {
+ auto out = ci.takeOutputStream();
+
+ auto Result = std::make_unique<cir::CIRGenConsumer>(
+ action, ci.getDiagnostics(), &ci.getVirtualFileSystem(),
+ ci.getHeaderSearchOpts(), ci.getCodeGenOpts(), ci.getTargetOpts(),
+ ci.getLangOpts(), ci.getFrontendOpts(), std::move(out));
+ cgConsumer = Result.get();
+
+ return std::move(Result);
+}
+
+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 0000000000000..b0616ab5d64b0
--- /dev/null
+++ b/clang/lib/CIR/FrontendAction/CMakeLists.txt
@@ -0,0 +1,17 @@
+set(LLVM_LINK_COMPONENTS
+ Core
+ Support
+ )
+
+get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
+
+add_clang_library(clangCIRFrontendAction
+ CIRGenAction.cpp
+
+ LINK_LIBS
+ clangAST
+ clangFrontend
+ clangCIR
+ MLIRCIR
+ MLIRIR
+ )
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index f9ca76a5ac800..b04752f23ed63 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -4944,6 +4944,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
}
+ if (Args.hasArg(options::OPT_fclangir))
+ CmdArgs.push_back("-fclangir");
+
if (IsOpenMPDevice) {
// We have to pass the triple of the host if compiling for an OpenMP device.
std::string NormalizedTriple =
diff --git a/clang/lib/FrontendTool/CMakeLists.txt b/clang/lib/FrontendTool/CMakeLists.txt
index 51c379ade2704..bfc7652b4c118 100644
--- a/clang/lib/FrontendTool/CMakeLists.txt
+++ b/clang/lib/FrontendTool/CMakeLists.txt
@@ -12,6 +12,15 @@ set(link_libs
clangRewriteFrontend
)
+set(deps)
+
+if(CLANG_ENABLE_CIR)
+ list(APPEND link_libs
+ clangCIRFrontendAction
+ MLIRIR
+ )
+endif()
+
if(CLANG_ENABLE_ARCMT)
list(APPEND link_libs
clangARCMigrate
@@ -29,7 +38,13 @@ add_clang_library(clangFrontendTool
DEPENDS
ClangDriverOptions
+ ${deps}
LINK_LIBS
${link_libs}
)
+
+if(CLANG_ENABLE_CIR)
+ target_include_directories(clangFrontendTool PRIVATE ${LLVM_MAIN_SRC_DIR}/../mlir/include)
+ target_include_directories(clangFrontendTool PRIVATE ${CMAKE_BINARY_DIR}/tools/mlir/include)
+endif()
diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index 7476b1076d103..563e8473f4fce 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,13 @@ 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>();
diff --git a/clang/test/CIR/hello.c b/clang/test/CIR/hello.c
new file mode 100644
index 0000000000000..37c6ac9e65b3b
--- /dev/null
+++ b/clang/test/CIR/hello.c
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s
+
+// just confirm that we don't crash
+void foo() {}
diff --git a/clang/test/CIR/lit.local.cfg b/clang/test/CIR/lit.local.cfg
new file mode 100644
index 0000000000000..6e9e8b42c0088
--- /dev/null
+++ b/clang/test/CIR/lit.local.cfg
@@ -0,0 +1,2 @@
+if not config.root.clang_enable_cir:
+ clang.unsupported = True
>From f323648a2ee5167b2ee0d8f369bc9e70a452d445 Mon Sep 17 00:00:00 2001
From: Nathan Lanza <nathanlanza at gmail.com>
Date: Wed, 10 Jul 2024 23:15:09 +0000
Subject: [PATCH 2/4] few small fixes
Created using spr 1.3.5
---
clang/include/clang/CIR/.clang-tidy | 60 -------------------
clang/lib/CIR/.clang-tidy | 60 -------------------
clang/lib/CIR/FrontendAction/.clang-tidy | 1 -
.../ExecuteCompilerInvocation.cpp | 3 +-
4 files changed, 1 insertion(+), 123 deletions(-)
delete mode 100644 clang/include/clang/CIR/.clang-tidy
delete mode 100644 clang/lib/CIR/.clang-tidy
delete mode 100644 clang/lib/CIR/FrontendAction/.clang-tidy
diff --git a/clang/include/clang/CIR/.clang-tidy b/clang/include/clang/CIR/.clang-tidy
deleted file mode 100644
index eb8cbbeb9723c..0000000000000
--- a/clang/include/clang/CIR/.clang-tidy
+++ /dev/null
@@ -1,60 +0,0 @@
-InheritParentConfig: true
-Checks: >
- -misc-const-correctness,
- bugprone-argument-comment,
- bugprone-assert-side-effect,
- bugprone-branch-clone,
- bugprone-copy-constructor-init,
- bugprone-dangling-handle,
- bugprone-dynamic-static-initializers,
- bugprone-macro-parentheses,
- bugprone-macro-repeated-side-effects,
- bugprone-misplaced-widening-cast,
- bugprone-move-forwarding-reference,
- bugprone-multiple-statement-macro,
- bugprone-suspicious-semicolon,
- bugprone-swapped-arguments,
- bugprone-terminating-continue,
- bugprone-unused-raii,
- bugprone-unused-return-value,
- misc-redundant-expression,
- misc-static-assert,
- misc-unused-using-decls,
- modernize-use-bool-literals,
- modernize-loop-convert,
- modernize-make-unique,
- modernize-raw-string-literal,
- modernize-use-equals-default,
- modernize-use-default-member-init,
- modernize-use-emplace,
- modernize-use-nullptr,
- modernize-use-override,
- modernize-use-using,
- performance-for-range-copy,
- performance-implicit-conversion-in-loop,
- performance-inefficient-algorithm,
- performance-inefficient-vector-operation,
- performance-move-const-arg,
- performance-no-automatic-move,
- performance-trivially-destructible,
- performance-unnecessary-copy-initialization,
- performance-unnecessary-value-param,
- readability-avoid-const-params-in-decls,
- readability-const-return-type,
- readability-container-size-empty,
- readability-inconsistent-declaration-parameter-name,
- readability-misleading-indentation,
- readability-redundant-control-flow,
- readability-redundant-smartptr-get,
- readability-simplify-boolean-expr,
- readability-simplify-subscript-expr,
- readability-use-anyofallof
-
-
-CheckOptions:
- - key: readability-identifier-naming.MemberCase
- value: camelBack
- - key: readability-identifier-naming.ParameterCase
- value: camelBack
- - key: readability-identifier-naming.VariableCase
- value: camelBack
diff --git a/clang/lib/CIR/.clang-tidy b/clang/lib/CIR/.clang-tidy
deleted file mode 100644
index eb8cbbeb9723c..0000000000000
--- a/clang/lib/CIR/.clang-tidy
+++ /dev/null
@@ -1,60 +0,0 @@
-InheritParentConfig: true
-Checks: >
- -misc-const-correctness,
- bugprone-argument-comment,
- bugprone-assert-side-effect,
- bugprone-branch-clone,
- bugprone-copy-constructor-init,
- bugprone-dangling-handle,
- bugprone-dynamic-static-initializers,
- bugprone-macro-parentheses,
- bugprone-macro-repeated-side-effects,
- bugprone-misplaced-widening-cast,
- bugprone-move-forwarding-reference,
- bugprone-multiple-statement-macro,
- bugprone-suspicious-semicolon,
- bugprone-swapped-arguments,
- bugprone-terminating-continue,
- bugprone-unused-raii,
- bugprone-unused-return-value,
- misc-redundant-expression,
- misc-static-assert,
- misc-unused-using-decls,
- modernize-use-bool-literals,
- modernize-loop-convert,
- modernize-make-unique,
- modernize-raw-string-literal,
- modernize-use-equals-default,
- modernize-use-default-member-init,
- modernize-use-emplace,
- modernize-use-nullptr,
- modernize-use-override,
- modernize-use-using,
- performance-for-range-copy,
- performance-implicit-conversion-in-loop,
- performance-inefficient-algorithm,
- performance-inefficient-vector-operation,
- performance-move-const-arg,
- performance-no-automatic-move,
- performance-trivially-destructible,
- performance-unnecessary-copy-initialization,
- performance-unnecessary-value-param,
- readability-avoid-const-params-in-decls,
- readability-const-return-type,
- readability-container-size-empty,
- readability-inconsistent-declaration-parameter-name,
- readability-misleading-indentation,
- readability-redundant-control-flow,
- readability-redundant-smartptr-get,
- readability-simplify-boolean-expr,
- readability-simplify-subscript-expr,
- readability-use-anyofallof
-
-
-CheckOptions:
- - key: readability-identifier-naming.MemberCase
- value: camelBack
- - key: readability-identifier-naming.ParameterCase
- value: camelBack
- - key: readability-identifier-naming.VariableCase
- value: camelBack
diff --git a/clang/lib/CIR/FrontendAction/.clang-tidy b/clang/lib/CIR/FrontendAction/.clang-tidy
deleted file mode 100644
index 47bc73c13f192..0000000000000
--- a/clang/lib/CIR/FrontendAction/.clang-tidy
+++ /dev/null
@@ -1 +0,0 @@
-InheritParentConfig: true
diff --git a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index 68b96e035f18c..60fde03289cf3 100644
--- a/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ b/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -71,8 +71,7 @@ CreateFrontendBaseAction(CompilerInstance &CI) {
#else
llvm_unreachable("CIR suppport not built into clang");
#endif
- case EmitHTML:
- return std::make_unique<HTMLPrintAction>();
+ case EmitHTML: return std::make_unique<HTMLPrintAction>();
case EmitLLVM: return std::make_unique<EmitLLVMAction>();
case EmitLLVMOnly: return std::make_unique<EmitLLVMOnlyAction>();
case EmitCodeGenOnly: return std::make_unique<EmitCodeGenOnlyAction>();
>From 4b1499792075214fad63de0f968bade73d8a8703 Mon Sep 17 00:00:00 2001
From: Nathan Lanza <nathanlanza at gmail.com>
Date: Wed, 10 Jul 2024 23:19:09 +0000
Subject: [PATCH 3/4] few more
Created using spr 1.3.5
---
clang/lib/CIR/CodeGen/CIRGenModule.h | 15 ++++++++++-----
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h
index 7b16b56e7942d..20e43a9d8f236 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.h
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.h
@@ -15,13 +15,18 @@
#include "CIRGenTypeCache.h"
-#include "clang/AST/ASTContext.h"
-#include "clang/Basic/CodeGenOptions.h"
-#include "clang/Basic/Diagnostic.h"
-
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/MLIRContext.h"
+namespace clang {
+class ASTContext;
+class CodeGenOptions;
+class Decl;
+class DiagnosticsEngine;
+class LangOptions;
+class TargetInfo;
+} // namespace clang
+
using namespace clang;
namespace cir {
@@ -33,7 +38,7 @@ class CIRGenModule : public CIRGenTypeCache {
public:
CIRGenModule(mlir::MLIRContext &context, clang::ASTContext &astctx,
- const clang::CodeGenOptions &CGO,
+ const clang::CodeGenOptions &cgo,
clang::DiagnosticsEngine &Diags);
~CIRGenModule() = default;
>From 6e6014d59a1be9421ae50d07e91e4362d7f29ab1 Mon Sep 17 00:00:00 2001
From: Nathan Lanza <nathanlanza at gmail.com>
Date: Wed, 10 Jul 2024 23:21:21 +0000
Subject: [PATCH 4/4] more
Created using spr 1.3.5
---
clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 32 +++++++++----------
1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
index 2a6c18327f124..72b9fa0c13c59 100644
--- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
+++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
@@ -22,27 +22,27 @@ class CIRGenConsumer : public clang::ASTConsumer {
virtual void anchor();
- std::unique_ptr<raw_pwrite_stream> outputStream;
+ std::unique_ptr<raw_pwrite_stream> OutputStream;
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
- std::unique_ptr<CIRGenerator> gen;
+ std::unique_ptr<CIRGenerator> Gen;
public:
- CIRGenConsumer(CIRGenAction::OutputType action,
- DiagnosticsEngine &diagnosticsEngine,
+ 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)
- : outputStream(std::move(os)), FS(VFS),
- gen(std::make_unique<CIRGenerator>(diagnosticsEngine, std::move(VFS),
- codeGenOptions)) {}
+ const HeaderSearchOptions &HeaderSearchOptions,
+ const CodeGenOptions &CodeGenOptions,
+ const TargetOptions &TargetOptions,
+ const LangOptions &LangOptions,
+ const FrontendOptions &FEOptions,
+ std::unique_ptr<raw_pwrite_stream> OS)
+ : OutputStream(std::move(OS)), FS(VFS),
+ Gen(std::make_unique<CIRGenerator>(DiagnosticsEngine, std::move(VFS),
+ CodeGenOptions)) {}
bool HandleTopLevelDecl(DeclGroupRef D) override {
- gen->HandleTopLevelDecl(D);
+ Gen->HandleTopLevelDecl(D);
return true;
}
};
@@ -57,12 +57,12 @@ CIRGenAction::~CIRGenAction() { MLIRMod.release(); }
std::unique_ptr<ASTConsumer>
CIRGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
- std::unique_ptr<llvm::raw_pwrite_stream> out = CI.takeOutputStream();
+ std::unique_ptr<llvm::raw_pwrite_stream> Out = CI.takeOutputStream();
auto Result = std::make_unique<cir::CIRGenConsumer>(
Action, CI.getDiagnostics(), &CI.getVirtualFileSystem(),
CI.getHeaderSearchOpts(), CI.getCodeGenOpts(), CI.getTargetOpts(),
- CI.getLangOpts(), CI.getFrontendOpts(), std::move(out));
+ CI.getLangOpts(), CI.getFrontendOpts(), std::move(Out));
return Result;
}
More information about the cfe-commits
mailing list