[clang] [CIR] Build out AST consumer patterns to reach the entry point into CIRGen (PR #91007)

Chuanqi Xu via cfe-commits cfe-commits at lists.llvm.org
Mon May 27 22:49:01 PDT 2024


================
@@ -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();
----------------
ChuanqiXu9 wrote:

If I read correctly, `cgConsumer` is only used here? I guess it is needed in following patches. But slightly odd.

https://github.com/llvm/llvm-project/pull/91007


More information about the cfe-commits mailing list