Skip to content

Commit 2e4d4d0

Browse files
seven-milelanza
authored andcommitted
[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 821d378 commit 2e4d4d0

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
@@ -588,7 +588,7 @@ void CIRGenModule::buildGlobalFunctionDefinition(GlobalDecl GD,
588588
CurCGF = nullptr;
589589

590590
setNonAliasAttributes(GD, Op);
591-
// TODO: SetLLVMFunctionAttributesForDeclaration
591+
setCIRFunctionAttributesForDefinition(D, Fn);
592592

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

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

22702272
if (!curCGF)
22712273
theModule.push_back(f);
@@ -2334,16 +2336,16 @@ static bool hasUnwindExceptions(const LangOptions &LangOpts) {
23342336
return true;
23352337
}
23362338

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

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

2346-
if (!FD) {
2348+
if (!decl) {
23472349
// If we don't have a declaration to control inlining, the function isn't
23482350
// explicitly marked as alwaysinline for semantic reasons, and inlining is
23492351
// disabled, mark the function as noinline.
@@ -2352,12 +2354,12 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
23522354
builder.getContext(), mlir::cir::InlineKind::AlwaysInline);
23532355
attrs.set(attr.getMnemonic(), attr);
23542356
}
2355-
} else if (FD->hasAttr<NoInlineAttr>()) {
2357+
} else if (decl->hasAttr<NoInlineAttr>()) {
23562358
// Add noinline if the function isn't always_inline.
23572359
auto attr = mlir::cir::InlineAttr::get(builder.getContext(),
23582360
mlir::cir::InlineKind::NoInline);
23592361
attrs.set(attr.getMnemonic(), attr);
2360-
} else if (FD->hasAttr<AlwaysInlineAttr>()) {
2362+
} else if (decl->hasAttr<AlwaysInlineAttr>()) {
23612363
// (noinline wins over always_inline, and we can't specify both in IR)
23622364
auto attr = mlir::cir::InlineAttr::get(builder.getContext(),
23632365
mlir::cir::InlineKind::AlwaysInline);
@@ -2372,18 +2374,18 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
23722374
// Otherwise, propagate the inline hint attribute and potentially use its
23732375
// absence to mark things as noinline.
23742376
// Search function and template pattern redeclarations for inline.
2375-
auto CheckForInline = [](const FunctionDecl *FD) {
2377+
auto CheckForInline = [](const FunctionDecl *decl) {
23762378
auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
23772379
return Redecl->isInlineSpecified();
23782380
};
2379-
if (any_of(FD->redecls(), CheckRedeclForInline))
2381+
if (any_of(decl->redecls(), CheckRedeclForInline))
23802382
return true;
2381-
const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
2383+
const FunctionDecl *Pattern = decl->getTemplateInstantiationPattern();
23822384
if (!Pattern)
23832385
return false;
23842386
return any_of(Pattern->redecls(), CheckRedeclForInline);
23852387
};
2386-
if (CheckForInline(FD)) {
2388+
if (CheckForInline(cast<FunctionDecl>(decl))) {
23872389
auto attr = mlir::cir::InlineAttr::get(builder.getContext(),
23882390
mlir::cir::InlineKind::InlineHint);
23892391
attrs.set(attr.getMnemonic(), attr);
@@ -2398,10 +2400,10 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
23982400
// starting with the default for this optimization level.
23992401
bool ShouldAddOptNone =
24002402
!codeGenOpts.DisableO0ImplyOptNone && codeGenOpts.OptimizationLevel == 0;
2401-
if (FD) {
2402-
ShouldAddOptNone &= !FD->hasAttr<MinSizeAttr>();
2403-
ShouldAddOptNone &= !FD->hasAttr<AlwaysInlineAttr>();
2404-
ShouldAddOptNone |= FD->hasAttr<OptimizeNoneAttr>();
2403+
if (decl) {
2404+
ShouldAddOptNone &= !decl->hasAttr<MinSizeAttr>();
2405+
ShouldAddOptNone &= !decl->hasAttr<AlwaysInlineAttr>();
2406+
ShouldAddOptNone |= decl->hasAttr<OptimizeNoneAttr>();
24052407
}
24062408

24072409
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)