-
Notifications
You must be signed in to change notification settings - Fork 168
[CIR][CIRGen] Implement CIRGenModule::shouldEmitFunction #984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3031,9 +3031,110 @@ void CIRGenModule::Release() { | |
// TODO: FINISH THE REST OF THIS | ||
} | ||
|
||
bool CIRGenModule::shouldEmitFunction(GlobalDecl GD) { | ||
// TODO: implement this -- requires defining linkage for CIR | ||
return true; | ||
namespace { | ||
// TODO(cir): This should be a common helper shared with CodeGen. | ||
struct FunctionIsDirectlyRecursive | ||
: public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> { | ||
const StringRef name; | ||
const Builtin::Context &builtinCtx; | ||
FunctionIsDirectlyRecursive(StringRef name, | ||
const Builtin::Context &builtinCtx) | ||
: name(name), builtinCtx(builtinCtx) {} | ||
|
||
bool VisitCallExpr(const CallExpr *expr) { | ||
const FunctionDecl *func = expr->getDirectCallee(); | ||
if (!func) | ||
return false; | ||
AsmLabelAttr *attr = func->getAttr<AsmLabelAttr>(); | ||
if (attr && name == attr->getLabel()) | ||
return true; | ||
unsigned builtinId = func->getBuiltinID(); | ||
if (!builtinId || !builtinCtx.isLibFunction(builtinId)) | ||
return false; | ||
StringRef builtinName = builtinCtx.getName(builtinId); | ||
if (builtinName.starts_with("__builtin_") && | ||
name == builtinName.slice(strlen("__builtin_"), StringRef::npos)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool VisitStmt(const Stmt *stmt) { | ||
for (const Stmt *child : stmt->children()) | ||
if (child && this->Visit(child)) | ||
return true; | ||
return false; | ||
} | ||
}; | ||
} // namespace | ||
|
||
// isTriviallyRecursive - Check if this function calls another | ||
// decl that, because of the asm attribute or the other decl being a builtin, | ||
// ends up pointing to itself. | ||
// TODO(cir): This should be a common helper shared with CodeGen. | ||
bool CIRGenModule::isTriviallyRecursive(const FunctionDecl *func) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
StringRef name; | ||
if (getCXXABI().getMangleContext().shouldMangleDeclName(func)) { | ||
// asm labels are a special kind of mangling we have to support. | ||
AsmLabelAttr *attr = func->getAttr<AsmLabelAttr>(); | ||
if (!attr) | ||
return false; | ||
name = attr->getLabel(); | ||
} else { | ||
name = func->getName(); | ||
} | ||
|
||
FunctionIsDirectlyRecursive walker(name, astCtx.BuiltinInfo); | ||
const Stmt *body = func->getBody(); | ||
return body ? walker.Visit(body) : false; | ||
} | ||
|
||
// TODO(cir): This should be a common helper shared with CodeGen. | ||
bool CIRGenModule::shouldEmitFunction(GlobalDecl globalDecl) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (getFunctionLinkage(globalDecl) != | ||
GlobalLinkageKind::AvailableExternallyLinkage) | ||
return true; | ||
|
||
const auto *func = cast<FunctionDecl>(globalDecl.getDecl()); | ||
// Inline builtins declaration must be emitted. They often are fortified | ||
// functions. | ||
if (func->isInlineBuiltinDeclaration()) | ||
return true; | ||
|
||
if (codeGenOpts.OptimizationLevel == 0 && !func->hasAttr<AlwaysInlineAttr>()) | ||
return false; | ||
|
||
// We don't import function bodies from other named module units since that | ||
// behavior may break ABI compatibility of the current unit. | ||
if (const Module *mod = func->getOwningModule(); | ||
mod && mod->getTopLevelModule()->isNamedModule() && | ||
astCtx.getCurrentNamedModule() != mod->getTopLevelModule()) { | ||
// There are practices to mark template member function as always-inline | ||
// and mark the template as extern explicit instantiation but not give | ||
// the definition for member function. So we have to emit the function | ||
// from explicitly instantiation with always-inline. | ||
// | ||
// See https://github.com/llvm/llvm-project/issues/86893 for details. | ||
// | ||
// TODO: Maybe it is better to give it a warning if we call a non-inline | ||
// function from other module units which is marked as always-inline. | ||
if (!func->isTemplateInstantiation() || !func->hasAttr<AlwaysInlineAttr>()) | ||
return false; | ||
} | ||
|
||
if (func->hasAttr<NoInlineAttr>()) | ||
return false; | ||
|
||
if (func->hasAttr<DLLImportAttr>() && !func->hasAttr<AlwaysInlineAttr>()) | ||
assert(!MissingFeatures::setDLLImportDLLExport() && | ||
"shouldEmitFunction for dllimport is NYI"); | ||
|
||
// PR9614. Avoid cases where the source code is lying to us. An available | ||
// externally function should have an equivalent function somewhere else, | ||
// but a function that calls itself through asm label/`__builtin_` trickery is | ||
// clearly not equivalent to the real implementation. | ||
// This happens in glibc's btowc and in some configure checks. | ||
return !isTriviallyRecursive(func); | ||
} | ||
|
||
bool CIRGenModule::supportsCOMDAT() const { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/llvm/llvm-project/blob/eca3206d29e7ce97dd6336deaa3da96be37f8277/clang/lib/CodeGen/CodeGenModule.cpp#L3964-L3995