Skip to content

Commit bde154c

Browse files
authored
[CIR][CodeGen] Refactor setExtraAttributesForFunc to better align with OG (#830)
Previously the body of `setExtraAttributesForFunc` corresponds to `SetLLVMFunctionAttributesForDefinition`, but the callsite of it does not reside at the right position. This PR rename it and adjust the calls to it following OG CodeGen. To be specific, `setExtraAttributesForFunc` is called right after the initialization of `FuncOp`. But in OG CodeGen, the list of attributes is constructed by several more functions: `SetLLVMFunctionAttributes` and `SetLLVMFunctionAttributesForDefinition`. This results in diff in attributes of function declarations, which is reflected by the changes of test files. Apart from them, there is no functional change. In other words, the two code path calling `setCIRFunctionAttributesForDefinition` are tested by existing tests: * Caller `buildGlobalFunctionDefinition`: tested by `CIR/CodeGen/function-attrs.cpp`, ... * Caller `codegenCXXStructor`: tested by `CIR/CodeGen/delegating-ctor.cpp`, `defined-pure-virtual-func.cpp`, ...
1 parent a061729 commit bde154c

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

clang/lib/CIR/CodeGen/CIRGenCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ mlir::cir::FuncOp CIRGenModule::codegenCXXStructor(GlobalDecl GD) {
287287
CurCGF = nullptr;
288288

289289
setNonAliasAttributes(GD, Fn);
290-
// TODO: SetLLVMFunctionAttributesForDefinition
290+
setCIRFunctionAttributesForDefinition(cast<CXXMethodDecl>(GD.getDecl()), Fn);
291291
return Fn;
292292
}
293293

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ void CIRGenModule::buildGlobalFunctionDefinition(GlobalDecl GD,
587587
CurCGF = nullptr;
588588

589589
setNonAliasAttributes(GD, Op);
590-
// TODO: SetLLVMFunctionAttributesForDeclaration
590+
setCIRFunctionAttributesForDefinition(D, Fn);
591591

592592
if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
593593
AddGlobalCtor(Fn, CA->getPriority());
@@ -2264,7 +2264,9 @@ CIRGenModule::createCIRFunction(mlir::Location loc, StringRef name,
22642264
mlir::SymbolTable::setSymbolVisibility(
22652265
f, mlir::SymbolTable::Visibility::Private);
22662266

2267-
setExtraAttributesForFunc(f, FD);
2267+
// Initialize with empty dict of extra attributes.
2268+
f.setExtraAttrsAttr(mlir::cir::ExtraFuncAttributesAttr::get(
2269+
builder.getContext(), builder.getDictionaryAttr({})));
22682270

22692271
if (!curCGF)
22702272
theModule.push_back(f);
@@ -2333,16 +2335,16 @@ static bool hasUnwindExceptions(const LangOptions &LangOpts) {
23332335
return true;
23342336
}
23352337

2336-
void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
2337-
const clang::FunctionDecl *FD) {
2338-
mlir::NamedAttrList attrs;
2338+
void CIRGenModule::setCIRFunctionAttributesForDefinition(const Decl *decl,
2339+
FuncOp f) {
2340+
mlir::NamedAttrList attrs{f.getExtraAttrs().getElements().getValue()};
23392341

23402342
if (!hasUnwindExceptions(getLangOpts())) {
23412343
auto attr = mlir::cir::NoThrowAttr::get(builder.getContext());
23422344
attrs.set(attr.getMnemonic(), attr);
23432345
}
23442346

2345-
if (!FD) {
2347+
if (!decl) {
23462348
// If we don't have a declaration to control inlining, the function isn't
23472349
// explicitly marked as alwaysinline for semantic reasons, and inlining is
23482350
// disabled, mark the function as noinline.
@@ -2351,12 +2353,12 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
23512353
builder.getContext(), mlir::cir::InlineKind::AlwaysInline);
23522354
attrs.set(attr.getMnemonic(), attr);
23532355
}
2354-
} else if (FD->hasAttr<NoInlineAttr>()) {
2356+
} else if (decl->hasAttr<NoInlineAttr>()) {
23552357
// Add noinline if the function isn't always_inline.
23562358
auto attr = mlir::cir::InlineAttr::get(builder.getContext(),
23572359
mlir::cir::InlineKind::NoInline);
23582360
attrs.set(attr.getMnemonic(), attr);
2359-
} else if (FD->hasAttr<AlwaysInlineAttr>()) {
2361+
} else if (decl->hasAttr<AlwaysInlineAttr>()) {
23602362
// (noinline wins over always_inline, and we can't specify both in IR)
23612363
auto attr = mlir::cir::InlineAttr::get(builder.getContext(),
23622364
mlir::cir::InlineKind::AlwaysInline);
@@ -2371,18 +2373,18 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
23712373
// Otherwise, propagate the inline hint attribute and potentially use its
23722374
// absence to mark things as noinline.
23732375
// Search function and template pattern redeclarations for inline.
2374-
auto CheckForInline = [](const FunctionDecl *FD) {
2376+
auto CheckForInline = [](const FunctionDecl *decl) {
23752377
auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
23762378
return Redecl->isInlineSpecified();
23772379
};
2378-
if (any_of(FD->redecls(), CheckRedeclForInline))
2380+
if (any_of(decl->redecls(), CheckRedeclForInline))
23792381
return true;
2380-
const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
2382+
const FunctionDecl *Pattern = decl->getTemplateInstantiationPattern();
23812383
if (!Pattern)
23822384
return false;
23832385
return any_of(Pattern->redecls(), CheckRedeclForInline);
23842386
};
2385-
if (CheckForInline(FD)) {
2387+
if (CheckForInline(cast<FunctionDecl>(decl))) {
23862388
auto attr = mlir::cir::InlineAttr::get(builder.getContext(),
23872389
mlir::cir::InlineKind::InlineHint);
23882390
attrs.set(attr.getMnemonic(), attr);
@@ -2397,10 +2399,10 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
23972399
// starting with the default for this optimization level.
23982400
bool ShouldAddOptNone =
23992401
!codeGenOpts.DisableO0ImplyOptNone && codeGenOpts.OptimizationLevel == 0;
2400-
if (FD) {
2401-
ShouldAddOptNone &= !FD->hasAttr<MinSizeAttr>();
2402-
ShouldAddOptNone &= !FD->hasAttr<AlwaysInlineAttr>();
2403-
ShouldAddOptNone |= FD->hasAttr<OptimizeNoneAttr>();
2402+
if (decl) {
2403+
ShouldAddOptNone &= !decl->hasAttr<MinSizeAttr>();
2404+
ShouldAddOptNone &= !decl->hasAttr<AlwaysInlineAttr>();
2405+
ShouldAddOptNone |= decl->hasAttr<OptimizeNoneAttr>();
24042406
}
24052407

24062408
if (ShouldAddOptNone) {

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,12 @@ class CIRGenModule : public CIRGenTypeCache {
573573

574574
/// Set the CIR function attributes (sext, zext, etc).
575575
void setCIRFunctionAttributes(GlobalDecl GD, const CIRGenFunctionInfo &info,
576-
mlir::cir::FuncOp func, bool isThunk);
576+
mlir::cir::FuncOp func, bool isThunk);
577+
578+
/// Set the CIR function attributes which only apply to a function
579+
/// definition.
580+
void setCIRFunctionAttributesForDefinition(const Decl *decl,
581+
mlir::cir::FuncOp func);
577582

578583
void buildGlobalDefinition(clang::GlobalDecl D,
579584
mlir::Operation *Op = nullptr);
@@ -666,9 +671,6 @@ class CIRGenModule : public CIRGenTypeCache {
666671
void ReplaceUsesOfNonProtoTypeWithRealFunction(mlir::Operation *Old,
667672
mlir::cir::FuncOp NewFn);
668673

669-
void setExtraAttributesForFunc(mlir::cir::FuncOp f,
670-
const clang::FunctionDecl *FD);
671-
672674
// TODO: CodeGen also passes an AttributeList here. We'll have to match that
673675
// in CIR
674676
mlir::cir::FuncOp

clang/test/CIR/CodeGen/attributes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int __attribute__((section(".shared"))) glob = 42;
1414

1515

1616
void __attribute__((__visibility__("hidden"))) foo();
17-
// CIR: cir.func no_proto private hidden @foo(...) extra(#fn_attr)
17+
// CIR: cir.func no_proto private hidden @foo(...)
1818
int bah()
1919
{
2020
foo();

clang/test/CIR/CodeGen/visibility-attribute.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ int call_glob()
1919
}
2020

2121
void foo_default();
22-
// CIR: cir.func no_proto private @foo_default(...) extra(#fn_attr)
22+
// CIR: cir.func no_proto private @foo_default(...)
2323
// LLVM: declare {{.*}} void @foo_default(...)
2424

2525
void __attribute__((__visibility__("hidden"))) foo_hidden();
26-
// CIR: cir.func no_proto private hidden @foo_hidden(...) extra(#fn_attr)
26+
// CIR: cir.func no_proto private hidden @foo_hidden(...)
2727
// LLVM: declare {{.*}} hidden void @foo_hidden(...)
2828

2929
void __attribute__((__visibility__("protected"))) foo_protected();
30-
// CIR: cir.func no_proto private protected @foo_protected(...) extra(#fn_attr)
30+
// CIR: cir.func no_proto private protected @foo_protected(...)
3131
// LLVM: declare {{.*}} protected void @foo_protected(...)
3232

3333
void call_foo()

0 commit comments

Comments
 (0)