Skip to content

Commit 38eff93

Browse files
bcardosolopeslanza
authored andcommitted
[CIR] Hook the first AST node into CIR: FunctionDecl
- Add the attribute that wraps the AST node and a class helper. - Do proper includes. - Optionally attachable to cir.func - Add generic verifier. Next we should teach codegen to pass those in.
1 parent e50e325 commit 38eff93

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
// CIR Dialect Attrs
2424
//===----------------------------------------------------------------------===//
2525

26+
namespace clang {
27+
class FunctionDecl;
28+
}
29+
2630
#define GET_ATTRDEF_CLASSES
2731
#include "clang/CIR/Dialect/IR/CIROpsAttributes.h.inc"
2832

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class CIR_Attr<string name, string attrMnemonic, list<Trait> traits = []>
2727
let mnemonic = attrMnemonic;
2828
}
2929

30+
//===----------------------------------------------------------------------===//
31+
// NullAttr
32+
//===----------------------------------------------------------------------===//
33+
3034
def NullAttr : CIR_Attr<"Null", "null", [TypedAttrInterface]> {
3135
let summary = "A simple attr to represent nullptr";
3236
let description = [{
@@ -38,6 +42,10 @@ def NullAttr : CIR_Attr<"Null", "null", [TypedAttrInterface]> {
3842
let assemblyFormat = [{}];
3943
}
4044

45+
//===----------------------------------------------------------------------===//
46+
// CstArrayAttr
47+
//===----------------------------------------------------------------------===//
48+
4149
def CstArrayAttr : CIR_Attr<"CstArray", "cst_array", [TypedAttrInterface]> {
4250
let summary = "A constant array from ArrayAttr or StringRefAttr";
4351
let description = [{
@@ -71,4 +79,31 @@ def CstArrayAttr : CIR_Attr<"CstArray", "cst_array", [TypedAttrInterface]> {
7179
let genVerifyDecl = 1;
7280
}
7381

82+
//===----------------------------------------------------------------------===//
83+
// AST Wrappers
84+
//===----------------------------------------------------------------------===//
85+
86+
class ASTDecl<string name, list<Trait> traits = []>
87+
: CIR_Attr<!strconcat("AST", name), "ast", traits> {
88+
string clang_name = !strconcat("clang::", name);
89+
90+
let summary = !strconcat("Wraps a ", clang_name, " AST node.");
91+
let description = [{
92+
Operations optionally refer to this node, they could be available depending
93+
on the CIR lowering stage. Whether it's attached to the appropriated
94+
CIR operation is delegated to the operation verifier.
95+
96+
This always implies a non-null AST reference (verified).
97+
}];
98+
let parameters = (ins !strconcat(clang_name, " *"):$astDecl);
99+
100+
// Printing and parsing available in CIRDialect.cpp
101+
let hasCustomAssemblyFormat = 1;
102+
103+
// Enable verifier.
104+
let genVerifyDecl = 1;
105+
}
106+
107+
def ASTFunctionDeclAttr : ASTDecl<"FunctionDecl">;
108+
74109
#endif // MLIR_CIR_DIALECT_CIR_ATTRS

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,8 @@ def FuncOp : CIR_Op<"func", [
11491149
"GlobalLinkageKind::ExternalLinkage">:$linkage,
11501150
OptionalAttr<StrAttr>:$sym_visibility,
11511151
OptionalAttr<DictArrayAttr>:$arg_attrs,
1152-
OptionalAttr<DictArrayAttr>:$res_attrs);
1152+
OptionalAttr<DictArrayAttr>:$res_attrs,
1153+
OptionalAttr<ASTFunctionDeclAttr>:$ast);
11531154
let regions = (region AnyRegion:$body);
11541155
let skipDefaultBuilders = 1;
11551156

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include "llvm/ADT/STLExtras.h"
2424
#include "llvm/ADT/TypeSwitch.h"
2525

26+
// ClangIR holds back AST references when available.
27+
#include "clang/AST/Decl.h"
28+
2629
#define GET_ATTRDEF_CLASSES
2730
#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
2831

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,27 @@ void CstArrayAttr::print(::mlir::AsmPrinter &printer) const {
14201420
printer << ">";
14211421
}
14221422

1423+
::mlir::Attribute ASTFunctionDeclAttr::parse(::mlir::AsmParser &parser,
1424+
::mlir::Type type) {
1425+
// We cannot really parse anything AST related at this point
1426+
// since we have no serialization/JSON story.
1427+
return mlir::Attribute();
1428+
}
1429+
1430+
void ASTFunctionDeclAttr::print(::mlir::AsmPrinter &printer) const {
1431+
// Nothing to print besides the mnemonics.
1432+
}
1433+
1434+
LogicalResult ASTFunctionDeclAttr::verify(
1435+
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
1436+
::clang::FunctionDecl *decl) {
1437+
if (!decl) {
1438+
emitError() << "expected non-null AST declaration";
1439+
return failure();
1440+
}
1441+
return success();
1442+
}
1443+
14231444
//===----------------------------------------------------------------------===//
14241445
// TableGen'd op method definitions
14251446
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)