Skip to content

Commit 309c397

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 5c2f3e6 commit 309c397

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
@@ -21,6 +21,10 @@
2121
// CIR Dialect Attrs
2222
//===----------------------------------------------------------------------===//
2323

24+
namespace clang {
25+
class FunctionDecl;
26+
}
27+
2428
#define GET_ATTRDEF_CLASSES
2529
#include "clang/CIR/Dialect/IR/CIROpsAttributes.h.inc"
2630

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
@@ -1151,7 +1151,8 @@ def FuncOp : CIR_Op<"func", [
11511151
TypeAttrOf<FunctionType>:$function_type,
11521152
DefaultValuedAttr<GlobalLinkageKind,
11531153
"GlobalLinkageKind::ExternalLinkage">:$linkage,
1154-
OptionalAttr<StrAttr>:$sym_visibility);
1154+
OptionalAttr<StrAttr>:$sym_visibility,
1155+
OptionalAttr<ASTFunctionDeclAttr>:$ast);
11551156
let regions = (region AnyRegion:$body);
11561157
let skipDefaultBuilders = 1;
11571158

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

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

24+
// ClangIR holds back AST references when available.
25+
#include "clang/AST/Decl.h"
26+
2427
#define GET_ATTRDEF_CLASSES
2528
#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
2629

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

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

1405+
::mlir::Attribute ASTFunctionDeclAttr::parse(::mlir::AsmParser &parser,
1406+
::mlir::Type type) {
1407+
// We cannot really parse anything AST related at this point
1408+
// since we have no serialization/JSON story.
1409+
return mlir::Attribute();
1410+
}
1411+
1412+
void ASTFunctionDeclAttr::print(::mlir::AsmPrinter &printer) const {
1413+
// Nothing to print besides the mnemonics.
1414+
}
1415+
1416+
LogicalResult ASTFunctionDeclAttr::verify(
1417+
::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
1418+
::clang::FunctionDecl *decl) {
1419+
if (!decl) {
1420+
emitError() << "expected non-null AST declaration";
1421+
return failure();
1422+
}
1423+
return success();
1424+
}
1425+
14051426
//===----------------------------------------------------------------------===//
14061427
// TableGen'd op method definitions
14071428
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)