From 83ebc265b368f405097ba4196d8f402ac9b52cc9 Mon Sep 17 00:00:00 2001 From: Jakob Stoklund Olesen Date: Sat, 6 Feb 2010 01:16:28 +0000 Subject: [PATCH] Reintroduce the InlineHint function attribute. This time it's for real! I am going to hook this up in the frontends as well. The inliner has some experimental heuristics for dealing with the inline hint. When given a -respect-inlinehint option, functions marked with the inline keyword are given a threshold just above the default for -O3. We need some experiments to determine if that is the right thing to do. llvm-svn: 95466 --- bindings/ocaml/llvm/llvm.ml | 1 + bindings/ocaml/llvm/llvm.mli | 1 + docs/LangRef.html | 5 ++++ include/llvm-c/Core.h | 3 ++- include/llvm/Attributes.h | 4 ++- include/llvm/Transforms/IPO/InlinerPass.h | 7 ++--- lib/AsmParser/LLLexer.cpp | 1 + lib/AsmParser/LLParser.cpp | 1 + lib/AsmParser/LLToken.h | 1 + lib/Target/CppBackend/CPPBackend.cpp | 1 + lib/Transforms/IPO/Inliner.cpp | 31 ++++++++++++++++++----- lib/VMCore/Attributes.cpp | 2 ++ utils/llvm.grm | 1 + utils/vim/llvm.vim | 2 +- 14 files changed, 48 insertions(+), 13 deletions(-) diff --git a/bindings/ocaml/llvm/llvm.ml b/bindings/ocaml/llvm/llvm.ml index 4539098aa07..7e4acbff476 100644 --- a/bindings/ocaml/llvm/llvm.ml +++ b/bindings/ocaml/llvm/llvm.ml @@ -93,6 +93,7 @@ module Attribute = struct | Noredzone | Noimplicitfloat | Naked + | Inlinehint end module Icmp = struct diff --git a/bindings/ocaml/llvm/llvm.mli b/bindings/ocaml/llvm/llvm.mli index 719df430fab..bcdcb2ce110 100644 --- a/bindings/ocaml/llvm/llvm.mli +++ b/bindings/ocaml/llvm/llvm.mli @@ -143,6 +143,7 @@ module Attribute : sig | Noredzone | Noimplicitfloat | Naked + | Inlinehint end (** The predicate for an integer comparison ([icmp]) instruction. diff --git a/docs/LangRef.html b/docs/LangRef.html index c028f6bfe52..fed2f80696c 100644 --- a/docs/LangRef.html +++ b/docs/LangRef.html @@ -1083,6 +1083,11 @@ define void @f() optsize { ... } function into callers whenever possible, ignoring any active inlining size threshold for this caller. +
inlinehint
+
This attribute indicates that the source code contained a hint that inlining + this function is desirable (such as the "inline" keyword in C/C++). It + is just a hint; it imposes no requirements on the inliner.
+
noinline
This attribute indicates that the inliner should never inline this function in any situation. This attribute may not be used together with diff --git a/include/llvm-c/Core.h b/include/llvm-c/Core.h index 674dde509d5..98358fe3807 100644 --- a/include/llvm-c/Core.h +++ b/include/llvm-c/Core.h @@ -118,7 +118,8 @@ typedef enum { LLVMNoCaptureAttribute = 1<<21, LLVMNoRedZoneAttribute = 1<<22, LLVMNoImplicitFloatAttribute = 1<<23, - LLVMNakedAttribute = 1<<24 + LLVMNakedAttribute = 1<<24, + LLVMInlineHintAttribute = 1<<25 } LLVMAttribute; typedef enum { diff --git a/include/llvm/Attributes.h b/include/llvm/Attributes.h index 7fa5d4ae6bd..068f81fc7de 100644 --- a/include/llvm/Attributes.h +++ b/include/llvm/Attributes.h @@ -58,6 +58,8 @@ const Attributes NoRedZone = 1<<22; /// disable redzone const Attributes NoImplicitFloat = 1<<23; /// disable implicit floating point /// instructions. const Attributes Naked = 1<<24; ///< Naked function +const Attributes InlineHint = 1<<25; ///< source said inlining was + ///desirable /// @brief Attributes that only apply to function parameters. const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture; @@ -66,7 +68,7 @@ const Attributes ParameterOnly = ByVal | Nest | StructRet | NoCapture; /// be used on return values or function parameters. const Attributes FunctionOnly = NoReturn | NoUnwind | ReadNone | ReadOnly | NoInline | AlwaysInline | OptimizeForSize | StackProtect | StackProtectReq | - NoRedZone | NoImplicitFloat | Naked; + NoRedZone | NoImplicitFloat | Naked | InlineHint; /// @brief Parameter attributes that do not apply to vararg call arguments. const Attributes VarArgsIncompatible = StructRet; diff --git a/include/llvm/Transforms/IPO/InlinerPass.h b/include/llvm/Transforms/IPO/InlinerPass.h index dc5e644c7fe..30ece0eb422 100644 --- a/include/llvm/Transforms/IPO/InlinerPass.h +++ b/include/llvm/Transforms/IPO/InlinerPass.h @@ -52,10 +52,11 @@ struct Inliner : public CallGraphSCCPass { unsigned getInlineThreshold() const { return InlineThreshold; } /// Calculate the inline threshold for given Caller. This threshold is lower - /// if Caller is marked with OptimizeForSize and -inline-threshold is not - /// given on the comand line. + /// if the caller is marked with OptimizeForSize and -inline-threshold is not + /// given on the comand line. It is higher if the callee is marked with the + /// inlinehint attribute. /// - unsigned getInlineThreshold(Function* Caller) const; + unsigned getInlineThreshold(CallSite CS) const; /// getInlineCost - This method must be implemented by the subclass to /// determine the cost of inlining the specified call site. If the cost diff --git a/lib/AsmParser/LLLexer.cpp b/lib/AsmParser/LLLexer.cpp index 2a926d2e5e8..8ad658d858d 100644 --- a/lib/AsmParser/LLLexer.cpp +++ b/lib/AsmParser/LLLexer.cpp @@ -558,6 +558,7 @@ lltok::Kind LLLexer::LexIdentifier() { KEYWORD(readnone); KEYWORD(readonly); + KEYWORD(inlinehint); KEYWORD(noinline); KEYWORD(alwaysinline); KEYWORD(optsize); diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index ed743016245..5dd65691a3d 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -947,6 +947,7 @@ bool LLParser::ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind) { case lltok::kw_noinline: Attrs |= Attribute::NoInline; break; case lltok::kw_readnone: Attrs |= Attribute::ReadNone; break; case lltok::kw_readonly: Attrs |= Attribute::ReadOnly; break; + case lltok::kw_inlinehint: Attrs |= Attribute::InlineHint; break; case lltok::kw_alwaysinline: Attrs |= Attribute::AlwaysInline; break; case lltok::kw_optsize: Attrs |= Attribute::OptimizeForSize; break; case lltok::kw_ssp: Attrs |= Attribute::StackProtect; break; diff --git a/lib/AsmParser/LLToken.h b/lib/AsmParser/LLToken.h index 80eb1947744..7f1807c7d0d 100644 --- a/lib/AsmParser/LLToken.h +++ b/lib/AsmParser/LLToken.h @@ -85,6 +85,7 @@ namespace lltok { kw_readnone, kw_readonly, + kw_inlinehint, kw_noinline, kw_alwaysinline, kw_optsize, diff --git a/lib/Target/CppBackend/CPPBackend.cpp b/lib/Target/CppBackend/CPPBackend.cpp index 4f3f0047b36..3dd8ca7c71e 100644 --- a/lib/Target/CppBackend/CPPBackend.cpp +++ b/lib/Target/CppBackend/CPPBackend.cpp @@ -470,6 +470,7 @@ namespace { HANDLE_ATTR(Nest); HANDLE_ATTR(ReadNone); HANDLE_ATTR(ReadOnly); + HANDLE_ATTR(InlineHint); HANDLE_ATTR(NoInline); HANDLE_ATTR(AlwaysInline); HANDLE_ATTR(OptimizeForSize); diff --git a/lib/Transforms/IPO/Inliner.cpp b/lib/Transforms/IPO/Inliner.cpp index 55cc536717c..97e2f063946 100644 --- a/lib/Transforms/IPO/Inliner.cpp +++ b/lib/Transforms/IPO/Inliner.cpp @@ -41,6 +41,16 @@ static cl::opt InlineLimit("inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore, cl::desc("Control the amount of inlining to perform (default = 225)")); +static cl::opt +RespectHint("respect-inlinehint", cl::Hidden, + cl::desc("Respect the inlinehint attribute")); + +// Threshold to use when inlinehint is given. +const int HintThreshold = 300; + +// Threshold to use when optsize is specified (and there is no -inline-limit). +const int OptSizeThreshold = 75; + Inliner::Inliner(void *ID) : CallGraphSCCPass(ID), InlineThreshold(InlineLimit) {} @@ -172,13 +182,21 @@ static bool InlineCallIfPossible(CallSite CS, CallGraph &CG, return true; } -unsigned Inliner::getInlineThreshold(Function* Caller) const { +unsigned Inliner::getInlineThreshold(CallSite CS) const { + // Listen to inlinehint when -respect-inlinehint is given. + Function *Callee = CS.getCalledFunction(); + if (RespectHint && Callee && !Callee->isDeclaration() && + Callee->hasFnAttr(Attribute::InlineHint)) + return HintThreshold; + + // Listen to optsize when -inline-limit is not given. + Function *Caller = CS.getCaller(); if (Caller && !Caller->isDeclaration() && Caller->hasFnAttr(Attribute::OptimizeForSize) && InlineLimit.getNumOccurrences() == 0) - return 75; - else - return InlineThreshold; + return OptSizeThreshold; + + return InlineThreshold; } /// shouldInline - Return true if the inliner should attempt to inline @@ -200,7 +218,7 @@ bool Inliner::shouldInline(CallSite CS) { int Cost = IC.getValue(); Function *Caller = CS.getCaller(); - int CurrentThreshold = getInlineThreshold(Caller); + int CurrentThreshold = getInlineThreshold(CS); float FudgeFactor = getInlineFudgeFactor(CS); if (Cost >= (int)(CurrentThreshold * FudgeFactor)) { DEBUG(dbgs() << " NOT Inlining: cost=" << Cost @@ -236,8 +254,7 @@ bool Inliner::shouldInline(CallSite CS) { outerCallsFound = true; int Cost2 = IC2.getValue(); - Function *Caller2 = CS2.getCaller(); - int CurrentThreshold2 = getInlineThreshold(Caller2); + int CurrentThreshold2 = getInlineThreshold(CS2); float FudgeFactor2 = getInlineFudgeFactor(CS2); if (Cost2 >= (int)(CurrentThreshold2 * FudgeFactor2)) diff --git a/lib/VMCore/Attributes.cpp b/lib/VMCore/Attributes.cpp index 65155f1d489..a371c6f92eb 100644 --- a/lib/VMCore/Attributes.cpp +++ b/lib/VMCore/Attributes.cpp @@ -56,6 +56,8 @@ std::string Attribute::getAsString(Attributes Attrs) { Result += "optsize "; if (Attrs & Attribute::NoInline) Result += "noinline "; + if (Attrs & Attribute::InlineHint) + Result += "inlinehint "; if (Attrs & Attribute::AlwaysInline) Result += "alwaysinline "; if (Attrs & Attribute::StackProtect) diff --git a/utils/llvm.grm b/utils/llvm.grm index 4499d4b35a9..86a707a925d 100644 --- a/utils/llvm.grm +++ b/utils/llvm.grm @@ -161,6 +161,7 @@ FuncAttr ::= noreturn | signext | readnone | readonly + | inlinehint | noinline | alwaysinline | optsize diff --git a/utils/vim/llvm.vim b/utils/vim/llvm.vim index 48a4c68aefa..6e4a207b68e 100644 --- a/utils/vim/llvm.vim +++ b/utils/vim/llvm.vim @@ -51,7 +51,7 @@ syn keyword llvmKeyword volatile fastcc coldcc cc ccc syn keyword llvmKeyword x86_stdcallcc x86_fastcallcc syn keyword llvmKeyword signext zeroext inreg sret nounwind noreturn syn keyword llvmKeyword nocapture byval nest readnone readonly noalias -syn keyword llvmKeyword noinline alwaysinline optsize ssp sspreq +syn keyword llvmKeyword inlinehint noinline alwaysinline optsize ssp sspreq syn keyword llvmKeyword noredzone noimplicitfloat naked syn keyword llvmKeyword module asm align tail to syn keyword llvmKeyword addrspace section alias sideeffect c gc