Skip to content

Commit 8f98574

Browse files
committed
[CIR] Start printing/parsing func 'attributes'
This patch adds a print and parse ability for the func to have MLIR-standard 'attributes' printed along side the standard function. This patch also seeds the initial "disallowed" list so that we don't print things that we have custom printing for, AND will disallow them from being parsed. I believe this list to be complete, and it passes all tests. This printing of attributes is necessary for testing some OpenACC things that putting into the normal func-printing seems unnecessary.
1 parent de4e128 commit 8f98574

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,25 @@ def CIR_FuncType : CIR_Type<"Func", "func"> {
509509
/// Returns a clone of this function type with the given argument
510510
/// and result types.
511511
FuncType clone(mlir::TypeRange inputs, mlir::TypeRange results) const;
512+
513+
/// A list of mlir attributes that shouldn't appear in the generic
514+
/// 'attributes' list, and instead are handled via other syntax.
515+
static constexpr llvm::StringRef disallowedFromAttrList[] = {
516+
"alias",
517+
"builtin",
518+
"comdat",
519+
"coroutine",
520+
"cxx_special_member",
521+
"dso_local",
522+
"function_type",
523+
"global_ctor_priority",
524+
"global_dtor_priority",
525+
"global_visibility",
526+
"inline_kind",
527+
"lambda",
528+
"linkage",
529+
"no_proto",
530+
"sym_visibility"};
512531
}];
513532
}
514533

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,20 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
18241824
return failure();
18251825
}
18261826

1827+
// Parse the rest of the attributes.
1828+
NamedAttrList parsedAttrs;
1829+
if (parser.parseOptionalAttrDictWithKeyword(parsedAttrs))
1830+
return failure();
1831+
1832+
for (StringRef disallowed : cir::FuncType::disallowedFromAttrList) {
1833+
if (parsedAttrs.get(disallowed))
1834+
return parser.emitError(loc, "attribute '")
1835+
<< disallowed
1836+
<< "' should not be specified in the explicit attribute list";
1837+
}
1838+
1839+
state.attributes.append(parsedAttrs);
1840+
18271841
// Parse the optional function body.
18281842
auto *body = state.addRegion();
18291843
OptionalParseResult parseResult = parser.parseOptionalRegion(
@@ -1977,6 +1991,9 @@ void cir::FuncOp::print(OpAsmPrinter &p) {
19771991
p << " inline(" << cir::stringifyInlineKind(inlineAttr.getValue()) << ")";
19781992
}
19791993

1994+
function_interface_impl::printFunctionAttributes(
1995+
p, *this, cir::FuncType::disallowedFromAttrList);
1996+
19801997
// Print the body if this is not an external function.
19811998
Region &body = getOperation()->getRegion(0);
19821999
if (!body.empty()) {

clang/test/CIR/IR/func.cir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,11 @@ cir.func @Foo_move_assign() special_member<#cir.cxx_assign<!rec_Foo, move>> {
186186
// CHECK: cir.func @Foo_move_assign() special_member<#cir.cxx_assign<!rec_Foo, move>> {
187187
// CHECK: cir.return
188188
// CHECK: }
189+
190+
cir.func @has_attrs() attributes {foo, baz = 5, floof = "flop"} {
191+
cir.return
192+
}
193+
194+
// CHECK: cir.func @has_attrs() attributes {bz = 5, floof = "flop", foo} {
195+
// CHECK: cir.return
196+
// CHECK: }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: cir-opt %s -verify-diagnostics
2+
3+
module {
4+
cir.func @l0() {
5+
cir.return
6+
}
7+
8+
cir.func @disallowedAttr() attributes {comdat} { // expected-error{{custom op 'cir.func' attribute 'comdat' should not be specified in the explicit attribute list}}
9+
cir.return
10+
}
11+
}

0 commit comments

Comments
 (0)