Skip to content

Commit

Permalink
[CIR][CodeGen] Refactor setExtraAttributesForFunc to better align w…
Browse files Browse the repository at this point in the history
…ith 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`, ...
  • Loading branch information
seven-mile authored and lanza committed Nov 3, 2024
1 parent 821d378 commit 2e4d4d0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion clang/lib/CIR/CodeGen/CIRGenCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ mlir::cir::FuncOp CIRGenModule::codegenCXXStructor(GlobalDecl GD) {
CurCGF = nullptr;

setNonAliasAttributes(GD, Fn);
// TODO: SetLLVMFunctionAttributesForDefinition
setCIRFunctionAttributesForDefinition(cast<CXXMethodDecl>(GD.getDecl()), Fn);
return Fn;
}

Expand Down
34 changes: 18 additions & 16 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ void CIRGenModule::buildGlobalFunctionDefinition(GlobalDecl GD,
CurCGF = nullptr;

setNonAliasAttributes(GD, Op);
// TODO: SetLLVMFunctionAttributesForDeclaration
setCIRFunctionAttributesForDefinition(D, Fn);

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

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

if (!curCGF)
theModule.push_back(f);
Expand Down Expand Up @@ -2334,16 +2336,16 @@ static bool hasUnwindExceptions(const LangOptions &LangOpts) {
return true;
}

void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
const clang::FunctionDecl *FD) {
mlir::NamedAttrList attrs;
void CIRGenModule::setCIRFunctionAttributesForDefinition(const Decl *decl,
FuncOp f) {
mlir::NamedAttrList attrs{f.getExtraAttrs().getElements().getValue()};

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

if (!FD) {
if (!decl) {
// If we don't have a declaration to control inlining, the function isn't
// explicitly marked as alwaysinline for semantic reasons, and inlining is
// disabled, mark the function as noinline.
Expand All @@ -2352,12 +2354,12 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
builder.getContext(), mlir::cir::InlineKind::AlwaysInline);
attrs.set(attr.getMnemonic(), attr);
}
} else if (FD->hasAttr<NoInlineAttr>()) {
} else if (decl->hasAttr<NoInlineAttr>()) {
// Add noinline if the function isn't always_inline.
auto attr = mlir::cir::InlineAttr::get(builder.getContext(),
mlir::cir::InlineKind::NoInline);
attrs.set(attr.getMnemonic(), attr);
} else if (FD->hasAttr<AlwaysInlineAttr>()) {
} else if (decl->hasAttr<AlwaysInlineAttr>()) {
// (noinline wins over always_inline, and we can't specify both in IR)
auto attr = mlir::cir::InlineAttr::get(builder.getContext(),
mlir::cir::InlineKind::AlwaysInline);
Expand All @@ -2372,18 +2374,18 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
// Otherwise, propagate the inline hint attribute and potentially use its
// absence to mark things as noinline.
// Search function and template pattern redeclarations for inline.
auto CheckForInline = [](const FunctionDecl *FD) {
auto CheckForInline = [](const FunctionDecl *decl) {
auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
return Redecl->isInlineSpecified();
};
if (any_of(FD->redecls(), CheckRedeclForInline))
if (any_of(decl->redecls(), CheckRedeclForInline))
return true;
const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
const FunctionDecl *Pattern = decl->getTemplateInstantiationPattern();
if (!Pattern)
return false;
return any_of(Pattern->redecls(), CheckRedeclForInline);
};
if (CheckForInline(FD)) {
if (CheckForInline(cast<FunctionDecl>(decl))) {
auto attr = mlir::cir::InlineAttr::get(builder.getContext(),
mlir::cir::InlineKind::InlineHint);
attrs.set(attr.getMnemonic(), attr);
Expand All @@ -2398,10 +2400,10 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
// starting with the default for this optimization level.
bool ShouldAddOptNone =
!codeGenOpts.DisableO0ImplyOptNone && codeGenOpts.OptimizationLevel == 0;
if (FD) {
ShouldAddOptNone &= !FD->hasAttr<MinSizeAttr>();
ShouldAddOptNone &= !FD->hasAttr<AlwaysInlineAttr>();
ShouldAddOptNone |= FD->hasAttr<OptimizeNoneAttr>();
if (decl) {
ShouldAddOptNone &= !decl->hasAttr<MinSizeAttr>();
ShouldAddOptNone &= !decl->hasAttr<AlwaysInlineAttr>();
ShouldAddOptNone |= decl->hasAttr<OptimizeNoneAttr>();
}

if (ShouldAddOptNone) {
Expand Down
10 changes: 6 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,12 @@ class CIRGenModule : public CIRGenTypeCache {

/// Set the CIR function attributes (sext, zext, etc).
void setCIRFunctionAttributes(GlobalDecl GD, const CIRGenFunctionInfo &info,
mlir::cir::FuncOp func, bool isThunk);
mlir::cir::FuncOp func, bool isThunk);

/// Set the CIR function attributes which only apply to a function
/// definition.
void setCIRFunctionAttributesForDefinition(const Decl *decl,
mlir::cir::FuncOp func);

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

void setExtraAttributesForFunc(mlir::cir::FuncOp f,
const clang::FunctionDecl *FD);

// TODO: CodeGen also passes an AttributeList here. We'll have to match that
// in CIR
mlir::cir::FuncOp
Expand Down
2 changes: 1 addition & 1 deletion clang/test/CIR/CodeGen/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int __attribute__((section(".shared"))) glob = 42;


void __attribute__((__visibility__("hidden"))) foo();
// CIR: cir.func no_proto private hidden @foo(...) extra(#fn_attr)
// CIR: cir.func no_proto private hidden @foo(...)
int bah()
{
foo();
Expand Down
6 changes: 3 additions & 3 deletions clang/test/CIR/CodeGen/visibility-attribute.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ int call_glob()
}

void foo_default();
// CIR: cir.func no_proto private @foo_default(...) extra(#fn_attr)
// CIR: cir.func no_proto private @foo_default(...)
// LLVM: declare {{.*}} void @foo_default(...)

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

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

void call_foo()
Expand Down

0 comments on commit 2e4d4d0

Please sign in to comment.