Skip to content

Commit e6d0f0e

Browse files
committed
[CIR] Make AST Function attributes constrained.
1 parent d32cd9c commit e6d0f0e

File tree

6 files changed

+48
-4
lines changed

6 files changed

+48
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ def FuncOp : CIR_Op<"func", [
15901590
OptionalAttr<DictArrayAttr>:$arg_attrs,
15911591
OptionalAttr<DictArrayAttr>:$res_attrs,
15921592
OptionalAttr<FlatSymbolRefAttr>:$aliasee,
1593-
OptionalAttr<AnyAttr>:$ast);
1593+
OptionalAttr<AnyASTFunctionDeclAttr>:$ast);
15941594
let regions = (region AnyRegion:$body);
15951595
let skipDefaultBuilders = 1;
15961596

clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ namespace cir {
2121
mlir::Attribute makeAstDeclAttr(const clang::Decl *decl,
2222
mlir::MLIRContext *ctx);
2323

24+
mlir::Attribute makeFuncDeclAttr(const clang::Decl *decl,
25+
mlir::MLIRContext *ctx);
26+
2427
} // namespace cir
2528
} // namespace mlir
2629

clang/include/clang/CIR/Interfaces/ASTAttrInterfaces.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ let cppNamespace = "::mlir::cir" in {
190190
AttrInterface<"ASTClassTemplatePartialSpecializationDeclInterface",
191191
[ASTClassTemplateSpecializationDeclInterface]>;
192192

193+
194+
def AnyASTFunctionDeclAttr : Attr<
195+
CPred<"::mlir::isa<::mlir::cir::ASTFunctionDeclInterface>($_self)">,
196+
"AST Function attribute"> {
197+
let storageType = "::mlir::Attribute";
198+
let returnType = "::mlir::Attribute";
199+
let convertFromStorage = "$_self";
200+
let constBuilderCall = "$0";
201+
}
202+
193203
} // namespace mlir::cir
194204

195205
#endif // MLIR_CIR_INTERFACES_AST_ATTR_INTERFACES

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,7 @@ CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
18221822
f = builder.create<mlir::cir::FuncOp>(loc, name, Ty);
18231823

18241824
if (FD)
1825-
f.setAstAttr(makeAstDeclAttr(FD, builder.getContext()));
1825+
f.setAstAttr(makeFuncDeclAttr(FD, builder.getContext()));
18261826

18271827
if (FD && !FD->hasPrototype())
18281828
f.setNoProtoAttr(builder.getUnitAttr());

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,11 @@ using namespace mlir::cir;
4646
// CIR AST Attr helpers
4747
//===----------------------------------------------------------------------===//
4848

49-
mlir::Attribute mlir::cir::makeAstDeclAttr(const clang::Decl *decl,
50-
mlir::MLIRContext *ctx) {
49+
namespace mlir {
50+
namespace cir {
51+
52+
mlir::Attribute makeAstDeclAttr(const clang::Decl *decl,
53+
mlir::MLIRContext *ctx) {
5154
if (auto ast = clang::dyn_cast<clang::CXXConstructorDecl>(decl))
5255
return ASTCXXConstructorDeclAttr::get(ctx, ast);
5356
if (auto ast = clang::dyn_cast<clang::CXXConversionDecl>(decl))
@@ -78,6 +81,33 @@ mlir::Attribute mlir::cir::makeAstDeclAttr(const clang::Decl *decl,
7881
return ASTDeclAttr::get(ctx, decl);
7982
};
8083

84+
mlir::Attribute makeFuncDeclAttr(const clang::Decl *decl,
85+
mlir::MLIRContext *ctx) {
86+
return llvm::TypeSwitch<const clang::Decl *, mlir::Attribute>(decl)
87+
.Case([ctx](const clang::CXXConstructorDecl *ast) {
88+
return ASTCXXConstructorDeclAttr::get(ctx, ast);
89+
})
90+
.Case([ctx](const clang::CXXConversionDecl *ast) {
91+
return ASTCXXConversionDeclAttr::get(ctx, ast);
92+
})
93+
.Case([ctx](const clang::CXXDestructorDecl *ast) {
94+
return ASTCXXDestructorDeclAttr::get(ctx, ast);
95+
})
96+
.Case([ctx](const clang::CXXMethodDecl *ast) {
97+
return ASTCXXMethodDeclAttr::get(ctx, ast);
98+
})
99+
.Case([ctx](const clang::FunctionDecl *ast) {
100+
return ASTFunctionDeclAttr::get(ctx, ast);
101+
})
102+
.Default([](auto) {
103+
llvm_unreachable("unexpected Decl kind");
104+
return mlir::Attribute();
105+
});
106+
}
107+
108+
} // namespace cir
109+
} // namespace mlir
110+
81111
//===----------------------------------------------------------------------===//
82112
// General CIR parsing / printing
83113
//===----------------------------------------------------------------------===//

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ using namespace mlir::cir;
4242
#include "clang/CIR/Dialect/IR/CIROpsStructs.cpp.inc"
4343

4444
#include "clang/CIR/Dialect/IR/CIROpsDialect.cpp.inc"
45+
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
4546

4647
//===----------------------------------------------------------------------===//
4748
// CIR Dialect

0 commit comments

Comments
 (0)