Skip to content

Commit

Permalink
[CIR][NFC] Use an attribute alias for extra_func_attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardosolopes committed Feb 17, 2024
1 parent b9cd201 commit c2049ba
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
23 changes: 14 additions & 9 deletions clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ struct CIROpAsmDialectInterface : public OpAsmDialectInterface {
os << "bfi_" << bitfield.getName().str();
return AliasResult::FinalAlias;
}
if (auto extraFuncAttr =
attr.dyn_cast<mlir::cir::ExtraFuncAttributesAttr>()) {
os << "fn_attr";
return AliasResult::FinalAlias;
}

return AliasResult::NoAlias;
}
Expand Down Expand Up @@ -1717,20 +1722,20 @@ ParseResult cir::FuncOp::parse(OpAsmParser &parser, OperationState &state) {
hasAlias = true;
}

// If extra func attributes are present, parse them.
NamedAttrList extraAttrs;
Attribute extraAttrs;
if (::mlir::succeeded(parser.parseOptionalKeyword("extra"))) {
if (parser.parseLParen().failed())
return failure();
if (parser.parseOptionalAttrDict(extraAttrs).failed())
if (parser.parseAttribute(extraAttrs).failed())
return failure();
if (parser.parseRParen().failed())
return failure();
} else {
NamedAttrList empty;
extraAttrs = mlir::cir::ExtraFuncAttributesAttr::get(
builder.getContext(), empty.getDictionary(builder.getContext()));
}
state.addAttribute(getExtraAttrsAttrName(state.name),
mlir::cir::ExtraFuncAttributesAttr::get(
builder.getContext(),
extraAttrs.getDictionary(builder.getContext())));
state.addAttribute(getExtraAttrsAttrName(state.name), extraAttrs);

// Parse the optional function body.
auto *body = state.addRegion();
Expand Down Expand Up @@ -1821,8 +1826,8 @@ void cir::FuncOp::print(OpAsmPrinter &p) {

if (!getExtraAttrs().getElements().empty()) {
p << " extra(";
p.printOptionalAttrDict(getExtraAttrs().getElements().getValue());
p << " )";
p.printAttributeWithoutType(getExtraAttrs());
p << ")";
}

// Print the body if this is not an external function.
Expand Down
9 changes: 6 additions & 3 deletions clang/test/CIR/CodeGen/function-attrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ int s3(int a, int b) {
return x;
}

// CIR: #fn_attr = #cir<extra({inline = #cir.inline<hint>, nothrow = #cir.nothrow})>
// CIR: #fn_attr1 = #cir<extra({inline = #cir.inline<no>, nothrow = #cir.nothrow})>
// CIR: #fn_attr2 = #cir<extra({inline = #cir.inline<always>, nothrow = #cir.nothrow})>

// CIR: cir.func linkonce_odr @_Z2s0ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} extra( {inline = #cir.inline<hint>, nothrow = #cir.nothrow} )
// CIR: cir.func @_Z2s1ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} extra( {inline = #cir.inline<no>, nothrow = #cir.nothrow} )
// CIR: cir.func @_Z2s2ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} extra( {inline = #cir.inline<always>, nothrow = #cir.nothrow} )
// CIR: cir.func linkonce_odr @_Z2s0ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} extra(#fn_attr)
// CIR: cir.func @_Z2s1ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} extra(#fn_attr1)
// CIR: cir.func @_Z2s2ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} extra(#fn_attr2)
// CIR: cir.func @_Z2s3ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} {

// LLVM: define i32 @_Z2s1ii(i32 %0, i32 %1) {{.*}} #[[#ATTR1:]]
Expand Down
6 changes: 4 additions & 2 deletions clang/test/CIR/CodeGen/optnone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ int s0(int a, int b) {
return x;
}

// CIR-O0: cir.func @_Z2s0ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} extra( {inline = #cir.inline<no>, nothrow = #cir.nothrow, optnone = #cir.optnone} )
// CIR-O2-NOT: cir.func @_Z2s0ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} optnone
// CIR-O0: #fn_attr = #cir<extra({inline = #cir.inline<no>, nothrow = #cir.nothrow, optnone = #cir.optnone})>
// CIR-O0: cir.func @_Z2s0ii(%arg0:{{.*}}, %arg1:{{.*}} -> {{.*}} extra(#fn_attr)

// CIR-O2-NOT: #fn_attr ={{.*}} optnone

// LLVM-O0: define i32 @_Z2s0ii(i32 %0, i32 %1) #[[#ATTR:]]
// LLVM-O0: attributes #[[#ATTR]] = { noinline nounwind optnone }
Expand Down
10 changes: 7 additions & 3 deletions clang/test/CIR/IR/inlineAttr.cir
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// RUN: cir-opt %s | FileCheck %s -check-prefix=CIR
// RUN: cir-opt %s -cir-to-llvm -o - | FileCheck %s -check-prefix=MLIR

#fn_attr = #cir<extra({inline = #cir.inline<no>})>

module {
cir.func @l0() extra( {cir.inline = #cir.inline<no>} ) {
cir.func @l0() extra(#fn_attr) {
cir.return
}
}

// CIR: cir.func @l0() extra( {cir.inline = #cir.inline<no>} ) {
// MLIR: llvm.func @l0() attributes {cir.extra_attrs = #cir<extra({cir.inline = #cir.inline<no>})>}
// CIR: #fn_attr = #cir<extra({inline = #cir.inline<no>})>
// CIR: cir.func @l0() extra(#fn_attr) {

// MLIR: llvm.func @l0() attributes {cir.extra_attrs = #fn_attr}
2 changes: 1 addition & 1 deletion clang/test/CIR/Lowering/alloca.cir
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module {
}

// MLIR: module {
// MLIR-NEXT: llvm.func @foo(%arg0: i32) attributes {cir.extra_attrs = #cir<extra({})>} {
// MLIR-NEXT: llvm.func @foo(%arg0: i32) attributes {cir.extra_attrs = #fn_attr} {
// MLIR-NEXT: %0 = llvm.alloca %arg0 x i32 {alignment = 16 : i64} : (i32) -> !llvm.ptr
// MLIR-NEXT: llvm.return
// MLIR-NEXT: }
Expand Down

0 comments on commit c2049ba

Please sign in to comment.