Skip to content

Commit 17c81f7

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.5
1 parent ca8b064 commit 17c81f7

File tree

16 files changed

+462
-1
lines changed

16 files changed

+462
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===- CIRGenerator.h - CIR Generation from Clang AST ---------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares a simple interface to perform CIR generation from Clang
10+
// AST
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef CLANG_CIRGENERATOR_H_
15+
#define CLANG_CIRGENERATOR_H_
16+
17+
#include "clang/AST/ASTConsumer.h"
18+
#include "clang/AST/DeclGroup.h"
19+
#include "clang/Basic/CodeGenOptions.h"
20+
#include "clang/Basic/Diagnostic.h"
21+
22+
#include "llvm/ADT/IntrusiveRefCntPtr.h"
23+
#include "llvm/Support/VirtualFileSystem.h"
24+
25+
#include <memory>
26+
27+
namespace mlir {
28+
class MLIRContext;
29+
} // namespace mlir
30+
namespace cir {
31+
class CIRGenModule;
32+
33+
class CIRGenerator : public clang::ASTConsumer {
34+
virtual void anchor();
35+
clang::DiagnosticsEngine &Diags;
36+
clang::ASTContext *astCtx;
37+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
38+
fs; // Only used for debug info.
39+
40+
const clang::CodeGenOptions codeGenOpts; // Intentionally copied in.
41+
42+
[[maybe_unused]] unsigned HandlingTopLevelDecls;
43+
44+
protected:
45+
std::unique_ptr<mlir::MLIRContext> mlirCtx;
46+
std::unique_ptr<CIRGenModule> CGM;
47+
48+
public:
49+
CIRGenerator(clang::DiagnosticsEngine &diags,
50+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
51+
const clang::CodeGenOptions &CGO);
52+
~CIRGenerator();
53+
void Initialize(clang::ASTContext &Context) override;
54+
bool HandleTopLevelDecl(clang::DeclGroupRef D) override;
55+
};
56+
57+
} // namespace cir
58+
59+
#endif // CLANG_CIRGENERATOR_H_
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===---- CIRGenAction.h - CIR Code Generation Frontend Action -*- C++ -*--===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_CLANG_CIR_CIRGENACTION_H
10+
#define LLVM_CLANG_CIR_CIRGENACTION_H
11+
12+
#include "clang/Frontend/FrontendAction.h"
13+
14+
#include "mlir/IR/BuiltinOps.h"
15+
#include "mlir/IR/OwningOpRef.h"
16+
17+
namespace mlir {
18+
class MLIRContext;
19+
class ModuleOp;
20+
} // namespace mlir
21+
22+
namespace cir {
23+
class CIRGenConsumer;
24+
25+
class CIRGenAction : public clang::ASTFrontendAction {
26+
public:
27+
enum class OutputType {
28+
EmitCIR,
29+
};
30+
31+
private:
32+
friend class CIRGenConsumer;
33+
34+
mlir::OwningOpRef<mlir::ModuleOp> mlirModule;
35+
36+
mlir::MLIRContext *mlirContext;
37+
38+
protected:
39+
CIRGenAction(OutputType action, mlir::MLIRContext *mlirContext = nullptr);
40+
41+
std::unique_ptr<clang::ASTConsumer>
42+
CreateASTConsumer(clang::CompilerInstance &CI,
43+
llvm::StringRef InFile) override;
44+
45+
public:
46+
~CIRGenAction() override;
47+
48+
CIRGenConsumer *cgConsumer;
49+
OutputType action;
50+
};
51+
52+
class EmitCIRAction : public CIRGenAction {
53+
virtual void anchor();
54+
55+
public:
56+
EmitCIRAction(mlir::MLIRContext *mlirCtx = nullptr);
57+
};
58+
59+
} // namespace cir
60+
61+
#endif

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2900,7 +2900,7 @@ defm clangir : BoolFOption<"clangir",
29002900
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Use the ClangIR pipeline to compile">,
29012901
NegFlag<SetFalse, [], [ClangOption, CC1Option], "Use the AST -> LLVM pipeline to compile">,
29022902
BothFlags<[], [ClangOption, CC1Option], "">>;
2903-
def emit_cir : Flag<["-"], "emit-cir">, Visibility<[CC1Option]>,
2903+
def emit_cir : Flag<["-"], "emit-cir">, Visibility<[ClangOption, CC1Option]>,
29042904
Group<Action_Group>, HelpText<"Build ASTs and then lower to ClangIR">;
29052905
/// ClangIR-specific options - END
29062906

clang/lib/CIR/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ include_directories(${LLVM_MAIN_SRC_DIR}/../mlir/include)
22
include_directories(${CMAKE_BINARY_DIR}/tools/mlir/include)
33

44
add_subdirectory(Dialect)
5+
add_subdirectory(CodeGen)
6+
add_subdirectory(FrontendAction)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===- CIRGenModule.cpp - Per-Module state for CIR generation -------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This is the internal per-translation-unit state used for CIR translation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "CIRGenModule.h"
14+
15+
#include "clang/AST/DeclBase.h"
16+
17+
#include "llvm/Support/Debug.h"
18+
19+
#include "mlir/IR/BuiltinOps.h"
20+
#include "mlir/IR/Location.h"
21+
#include "mlir/IR/MLIRContext.h"
22+
23+
using namespace cir;
24+
CIRGenModule::CIRGenModule(mlir::MLIRContext &context,
25+
clang::ASTContext &astctx,
26+
const clang::CodeGenOptions &CGO,
27+
DiagnosticsEngine &Diags)
28+
: astCtx(astctx), langOpts(astctx.getLangOpts()), codeGenOpts(CGO),
29+
theModule{mlir::ModuleOp::create(mlir::UnknownLoc())}, Diags(Diags),
30+
target(astCtx.getTargetInfo()) {}
31+
32+
CIRGenModule::~CIRGenModule() {}
33+
34+
// Emit code for a single top level declaration.
35+
void CIRGenModule::buildTopLevelDecl(Decl *decl) {}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===--- CIRGenModule.h - Per-Module state for CIR gen ----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This is the internal per-translation-unit state used for CIR translation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H
14+
#define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H
15+
16+
#include "CIRGenTypeCache.h"
17+
18+
#include "clang/AST/ASTContext.h"
19+
#include "clang/Basic/CodeGenOptions.h"
20+
#include "clang/Basic/Diagnostic.h"
21+
22+
#include "mlir/IR/BuiltinOps.h"
23+
#include "mlir/IR/MLIRContext.h"
24+
25+
using namespace clang;
26+
namespace cir {
27+
28+
/// This class organizes the cross-function state that is used while generating
29+
/// CIR code.
30+
class CIRGenModule : public CIRGenTypeCache {
31+
CIRGenModule(CIRGenModule &) = delete;
32+
CIRGenModule &operator=(CIRGenModule &) = delete;
33+
34+
public:
35+
CIRGenModule(mlir::MLIRContext &context, clang::ASTContext &astctx,
36+
const clang::CodeGenOptions &CGO,
37+
clang::DiagnosticsEngine &Diags);
38+
39+
~CIRGenModule();
40+
41+
private:
42+
/// Hold Clang AST information.
43+
clang::ASTContext &astCtx;
44+
45+
const clang::LangOptions &langOpts;
46+
47+
[[maybe_unused]] const clang::CodeGenOptions &codeGenOpts;
48+
49+
/// A "module" matches a c/cpp source file: containing a list of functions.
50+
mlir::ModuleOp theModule;
51+
52+
[[maybe_unused]] clang::DiagnosticsEngine &Diags;
53+
54+
const clang::TargetInfo &target;
55+
56+
public:
57+
void buildTopLevelDecl(clang::Decl *decl);
58+
};
59+
} // namespace cir
60+
61+
#endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===--- CIRGenTypeCache.h - Commonly used LLVM types and info -*- C++ --*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This structure provides a set of common types useful during CIR emission.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_CLANG_LIB_CIR_CIRGENTYPECACHE_H
14+
#define LLVM_CLANG_LIB_CIR_CIRGENTYPECACHE_H
15+
16+
namespace cir {
17+
18+
/// This structure provides a set of types that are commonly used
19+
/// during IR emission. It's initialized once in CodeGenModule's
20+
/// constructor and then copied around into new CIRGenFunction's.
21+
struct CIRGenTypeCache {
22+
CIRGenTypeCache() {}
23+
};
24+
25+
} // namespace cir
26+
27+
#endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENTYPECACHE_H
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===--- CIRGenerator.cpp - Emit CIR from ASTs ----------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This builds an AST and converts it to CIR.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "CIRGenModule.h"
14+
15+
#include "clang/AST/DeclGroup.h"
16+
#include "clang/CIR/CIRGenerator.h"
17+
18+
using namespace cir;
19+
using namespace clang;
20+
21+
void CIRGenerator::anchor() {}
22+
23+
CIRGenerator::CIRGenerator(clang::DiagnosticsEngine &diags,
24+
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs,
25+
const CodeGenOptions &CGO)
26+
: Diags(diags), fs(std::move(vfs)), codeGenOpts{CGO},
27+
HandlingTopLevelDecls(0) {}
28+
CIRGenerator::~CIRGenerator() {}
29+
30+
void CIRGenerator::Initialize(ASTContext &astCtx) {
31+
using namespace llvm;
32+
33+
this->astCtx = &astCtx;
34+
35+
CGM = std::make_unique<CIRGenModule>(*mlirCtx.get(), astCtx, codeGenOpts,
36+
Diags);
37+
}
38+
39+
bool CIRGenerator::HandleTopLevelDecl(DeclGroupRef D) {
40+
41+
for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) {
42+
CGM->buildTopLevelDecl(*I);
43+
}
44+
45+
return true;
46+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
set(
2+
LLVM_LINK_COMPONENTS
3+
Core
4+
Support
5+
)
6+
7+
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
8+
9+
add_clang_library(clangCIR
10+
CIRGenerator.cpp
11+
CIRGenModule.cpp
12+
13+
DEPENDS
14+
MLIRCIR
15+
${dialect_libs}
16+
17+
LINK_LIBS
18+
clangAST
19+
clangBasic
20+
clangLex
21+
${dialect_libs}
22+
MLIRCIR
23+
)

0 commit comments

Comments
 (0)