diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -284,6 +284,7 @@ "default visibility for types [-ftype-visibility]") LANGOPT(SetVisibilityForExternDecls, 1, 0, "apply global symbol visibility to external declarations without an explicit visibility") +BENIGN_LANGOPT(SemanticInterposition , 1, 0, "semantic interposition") ENUM_LANGOPT(StackProtector, StackProtectorMode, 2, SSPOff, "stack protector mode") ENUM_LANGOPT(TrivialAutoVarInit, TrivialAutoVarInitKind, 2, TrivialAutoVarInitKind::Uninitialized, diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3262,7 +3262,8 @@ defm ipa_cp : BooleanFFlag<"ipa-cp">, Group; defm ivopts : BooleanFFlag<"ivopts">, Group; -def : Flag<["-"], "fno-semantic-interposition">, Group; +def fsemantic_interposition : Flag<["-"], "fsemantic-interposition">, Group, Flags<[CC1Option]>; +def fno_semantic_interposition: Flag<["-"], "fno-semantic-interposition">, Group, Flags<[CC1Option]>; defm non_call_exceptions : BooleanFFlag<"non-call-exceptions">, Group; defm peel_loops : BooleanFFlag<"peel-loops">, Group; defm permissive : BooleanFFlag<"permissive">, Group; diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -483,6 +483,11 @@ getModule().addModuleFlag(llvm::Module::Max, "Dwarf Version", CodeGenOpts.DwarfVersion); } + + if (Context.getLangOpts().SemanticInterposition) + // Require various optimization to respect semantic interposition. + getModule().setSemanticInterposition(1); + if (CodeGenOpts.EmitCodeView) { // Indicate that we want CodeView in the metadata. getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1); @@ -856,7 +861,7 @@ if (isa(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static) return true; - // Otherwise don't assue it is local. + // Otherwise don't assume it is local. return false; } diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -5002,6 +5002,8 @@ Args.AddLastArg(CmdArgs, options::OPT_femulated_tls, options::OPT_fno_emulated_tls); Args.AddLastArg(CmdArgs, options::OPT_fkeep_static_consts); + Args.AddLastArg(CmdArgs, options::OPT_fsemantic_interposition, + options::OPT_fno_semantic_interposition); // AltiVec-like language extensions aren't relevant for assembling. if (!isa(JA) || Output.getType() != types::TY_PP_Asm) diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -2679,6 +2679,13 @@ Opts.setValueVisibilityMode(DefaultVisibility); } + // Semantic interposition mode defaults to "false". + if (Arg *siOpt = Args.getLastArg(OPT_fsemantic_interposition, + OPT_fno_semantic_interposition)) { + Opts.SemanticInterposition = + unsigned(siOpt->getOption().getID() == OPT_fsemantic_interposition); + } + // The type-visibility mode defaults to the value-visibility mode. if (Arg *typeVisOpt = Args.getLastArg(OPT_ftype_visibility)) { Opts.setTypeVisibilityMode(parseVisibility(typeVisOpt, Args, Diags)); diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h --- a/llvm/include/llvm/IR/GlobalValue.h +++ b/llvm/include/llvm/IR/GlobalValue.h @@ -284,6 +284,8 @@ return IsDSOLocal; } + bool isDSOPreemptable() const { return !IsDSOLocal; } + bool hasPartition() const { return HasPartition; } @@ -426,7 +428,7 @@ /// *arbitrary* definition at link time. We cannot do any IPO or inlinining /// across interposable call edges, since the callee can be replaced with /// something arbitrary at link time. - bool isInterposable() const { return isInterposableLinkage(getLinkage()); } + bool isInterposable() const; bool hasExternalLinkage() const { return isExternalLinkage(getLinkage()); } bool hasAvailableExternallyLinkage() const { diff --git a/llvm/include/llvm/IR/Module.h b/llvm/include/llvm/IR/Module.h --- a/llvm/include/llvm/IR/Module.h +++ b/llvm/include/llvm/IR/Module.h @@ -846,6 +846,17 @@ Metadata *getProfileSummary(bool IsCS); /// @} + /// @name Utility functions for querying and setting semantic interposition + /// @{ + + /// Returns whether semantic interposition is to be respected. + bool getSemanticInterposition() const; + + /// Set whether semantic interposition is to be respected. + void setSemanticInterposition(bool); + + /// @} + /// Returns true if PLT should be avoided for RTLib calls. bool getRtLibUseGOT() const; diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp --- a/llvm/lib/Analysis/InlineCost.cpp +++ b/llvm/lib/Analysis/InlineCost.cpp @@ -1842,7 +1842,7 @@ } else if (Operator::getOpcode(V) == Instruction::BitCast) { V = cast(V)->getOperand(0); } else if (GlobalAlias *GA = dyn_cast(V)) { - if (GA->isInterposable()) + if (GA->isInterposable() || GA->isDSOPreemptable()) break; V = GA->getAliasee(); } else { diff --git a/llvm/lib/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp --- a/llvm/lib/IR/Globals.cpp +++ b/llvm/lib/IR/Globals.cpp @@ -94,6 +94,13 @@ llvm_unreachable("not a global"); } +bool GlobalValue::isInterposable() const { + if (getParent() && getParent()->getSemanticInterposition() && + isDSOPreemptable()) + return true; + return isInterposableLinkage(getLinkage()); +} + unsigned GlobalValue::getAlignment() const { if (auto *GA = dyn_cast(this)) { // In general we cannot compute this at the IR level, but we try. diff --git a/llvm/lib/IR/Module.cpp b/llvm/lib/IR/Module.cpp --- a/llvm/lib/IR/Module.cpp +++ b/llvm/lib/IR/Module.cpp @@ -559,6 +559,20 @@ : getModuleFlag("ProfileSummary")); } +bool Module::getSemanticInterposition() const { + auto *Val = + cast_or_null(getModuleFlag("SemanticInterposition")); + + if (!Val) + return false; + + return cast(Val->getValue())->getZExtValue(); +} + +void Module::setSemanticInterposition(bool SI) { + addModuleFlag(ModFlagBehavior::Error, "SemanticInterposition", SI); +} + void Module::setOwnedMemoryBuffer(std::unique_ptr MB) { OwnedMemoryBuffer = std::move(MB); }