-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[CIR] Build out AST consumer patterns to reach the entry point into CIRGen #91007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
17c81f7
32c56f9
f323648
4b14997
6e6014d
fe5818c
d275d17
6f9b985
9ec0d9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
lanza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include "clang/Basic/CodeGenOptions.h" | ||
#include "clang/Basic/Diagnostic.h" | ||
lanza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The mixed naming conventions in the header should be fixed (preference is to follow LLVM style if we're changing code around, but if the local preference is for MLIR, that's fine so long as it's consistent). |
||
fs; // Only used for debug info. | ||
|
||
const clang::CodeGenOptions codeGenOpts; // Intentionally copied in. | ||
lanza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[[maybe_unused]] unsigned HandlingTopLevelDecls; | ||
lanza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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_ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//===---- CIRGenAction.h - CIR Code Generation Frontend Action -*- C++ -*--===// | ||
lanza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// 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, | ||
erichkeane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @erichkeane @AaronBallman @MaskRay I had to re-add the ClangOption here. Notably, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The description of #91140 might be useful. You need
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It would be incorrect to only use
That's an inaccurate comparison. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MaskRay are you okay with this approach? Also, if this is exposed as a driver flag, should we have a test in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fangrui is out for a few weeks: #104899 (review) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To restate the argument here in favor of using a
|
||
Group<Action_Group>, HelpText<"Build ASTs and then lower to ClangIR">; | ||
/// ClangIR-specific options - END | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
lanza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
lanza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#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; | ||
lanza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
[[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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, would be nice to only have to bring these in when necessary. |
||
|
||
const clang::TargetInfo ⌖ | ||
|
||
public: | ||
void buildTopLevelDecl(clang::Decl *decl); | ||
}; | ||
} // namespace cir | ||
|
||
#endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() {} | ||
lanza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
} // namespace cir | ||
|
||
#endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENTYPECACHE_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
CGM->buildTopLevelDecl(*I); | ||
} | ||
|
||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
Uh oh!
There was an error while loading. Please reload this page.