[clang] fd09f12 - Implement -fsemantic-interposition
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 31 05:02:59 PST 2020
Author: serge-sans-paille
Date: 2020-01-31T14:02:33+01:00
New Revision: fd09f12f32f57564d619a28b8826d33ade47b12e
URL: https://github.com/llvm/llvm-project/commit/fd09f12f32f57564d619a28b8826d33ade47b12e
DIFF: https://github.com/llvm/llvm-project/commit/fd09f12f32f57564d619a28b8826d33ade47b12e.diff
LOG: Implement -fsemantic-interposition
First attempt at implementing -fsemantic-interposition.
Rely on GlobalValue::isInterposable that already captures most of the expected
behavior.
Rely on a ModuleFlag to state whether we should respect SemanticInterposition or
not. The default remains no.
So this should be a no-op if -fsemantic-interposition isn't used, and if it is,
isInterposable being already used in most optimisation, they should honor it
properly.
Note that it only impacts architecture compiled with -fPIC and no pie.
Differential Revision: https://reviews.llvm.org/D72829
Added:
clang/test/CodeGen/semantic-interposition.c
llvm/test/Transforms/Inline/inline-semantic-interposition.ll
llvm/test/Verifier/module-flags-semantic-interposition.ll
Modified:
clang/docs/ClangCommandLineReference.rst
clang/include/clang/Basic/LangOptions.def
clang/include/clang/Driver/Options.td
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Frontend/CompilerInvocation.cpp
clang/test/Driver/clang_f_opts.c
llvm/include/llvm/IR/GlobalValue.h
llvm/include/llvm/IR/Module.h
llvm/lib/IR/Globals.cpp
llvm/lib/IR/Module.cpp
llvm/lib/IR/Verifier.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangCommandLineReference.rst b/clang/docs/ClangCommandLineReference.rst
index eb0519988fa2..24c8ee2bc9ef 100644
--- a/clang/docs/ClangCommandLineReference.rst
+++ b/clang/docs/ClangCommandLineReference.rst
@@ -904,6 +904,12 @@ Strip (or keep only, if negative) a given number of path components when emittin
Turn on runtime checks for various forms of undefined or suspicious behavior. See user manual for available checks
+.. option:: -fno-semantic-interposition, -fsemantic-interposition
+
+Enable semantic interposition. Semantic interposition allows for the
+interposition of a symbol by another at runtime, thus preventing a range of
+inter-procedural optimisation.
+
.. option:: -moutline, -mno-outline
Enable function outlining (AArch64 only)
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index f7fa0151d397..4c7f7dde1f7d 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -284,6 +284,7 @@ ENUM_LANGOPT(TypeVisibilityMode, Visibility, 3, DefaultVisibility,
"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
index 4d5806154968..dcec7e6fde1e 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -3283,7 +3283,8 @@ defm inline_small_functions : BooleanFFlag<"inline-small-functions">,
defm ipa_cp : BooleanFFlag<"ipa-cp">,
Group<clang_ignored_gcc_optimization_f_Group>;
defm ivopts : BooleanFFlag<"ivopts">, Group<clang_ignored_gcc_optimization_f_Group>;
-def : Flag<["-"], "fno-semantic-interposition">, Group<clang_ignored_f_Group>;
+def fsemantic_interposition : Flag<["-"], "fsemantic-interposition">, Group<f_Group>, Flags<[CC1Option]>;
+def fno_semantic_interposition: Flag<["-"], "fno-semantic-interposition">, Group<f_Group>;
defm non_call_exceptions : BooleanFFlag<"non-call-exceptions">, Group<clang_ignored_f_Group>;
defm peel_loops : BooleanFFlag<"peel-loops">, Group<clang_ignored_gcc_optimization_f_Group>;
defm permissive : BooleanFFlag<"permissive">, Group<clang_ignored_f_Group>;
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 03c3fec5ebec..6a43b6bba627 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -483,6 +483,11 @@ void CodeGenModule::Release() {
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);
@@ -872,7 +877,7 @@ static bool shouldAssumeDSOLocal(const CodeGenModule &CGM,
if (isa<llvm::Function>(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
index fdd0610f464c..510dc19f7a90 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5037,6 +5037,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
options::OPT_fno_emulated_tls);
Args.AddLastArg(CmdArgs, options::OPT_fkeep_static_consts);
+ if (Args.hasFlag(options::OPT_fsemantic_interposition,
+ options::OPT_fno_semantic_interposition, false))
+ CmdArgs.push_back("-fsemantic-interposition");
+
// AltiVec-like language extensions aren't relevant for assembling.
if (!isa<PreprocessJobAction>(JA) || Output.getType() != types::TY_PP_Asm)
Args.AddLastArg(CmdArgs, options::OPT_fzvector);
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index a8dfd8a97c8c..335f8cd0e323 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -3036,6 +3036,10 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
Opts.setDefaultCallingConv(DefaultCC);
}
+ // -fsemantic-interposition
+ Opts.SemanticInterposition =
+ Args.hasArg(OPT_fsemantic_interposition) && Opts.PICLevel && !Opts.PIE;
+
// -mrtd option
if (Arg *A = Args.getLastArg(OPT_mrtd)) {
if (Opts.getDefaultCallingConv() != LangOptions::DCC_None)
diff --git a/clang/test/CodeGen/semantic-interposition.c b/clang/test/CodeGen/semantic-interposition.c
new file mode 100644
index 000000000000..77df61566e07
--- /dev/null
+++ b/clang/test/CodeGen/semantic-interposition.c
@@ -0,0 +1,14 @@
+// Semantic Interposition is active if
+// -fsemantic-interposition is set,
+// - pic-level > 0
+// - pic-is-pie is not set
+
+// RUN: %clang_cc1 -emit-llvm -fsemantic-interposition -pic-level 0 %s -o - | FileCheck %s -check-prefix=CHECK-NO-INTERPOSITION
+// RUN: %clang_cc1 -emit-llvm -fsemantic-interposition -pic-level 1 %s -o - | FileCheck %s -check-prefix=CHECK-INTERPOSITION
+// RUN: %clang_cc1 -emit-llvm -fsemantic-interposition -pic-level 2 %s -o - | FileCheck %s -check-prefix=CHECK-INTERPOSITION
+// RUN: %clang_cc1 -emit-llvm -fsemantic-interposition -pic-level 0 %s -o - | FileCheck %s -check-prefix=CHECK-NO-INTERPOSITION
+// RUN: %clang_cc1 -emit-llvm -fsemantic-interposition -pic-level 1 -pic-is-pie %s -o - | FileCheck %s -check-prefix=CHECK-NO-INTERPOSITION
+// RUN: %clang_cc1 -emit-llvm -fsemantic-interposition -pic-level 2 -pic-is-pie %s -o - | FileCheck %s -check-prefix=CHECK-NO-INTERPOSITION
+
+// CHECK-NO-INTERPOSITION-NOT: "SemanticInterposition"
+// CHECK-INTERPOSITION: !{{[0-9]+}} = !{i32 1, !"SemanticInterposition", i32 1}
diff --git a/clang/test/Driver/clang_f_opts.c b/clang/test/Driver/clang_f_opts.c
index 7038a2b52f66..e3fe373d7198 100644
--- a/clang/test/Driver/clang_f_opts.c
+++ b/clang/test/Driver/clang_f_opts.c
@@ -252,6 +252,7 @@
// RUN: -fivopts -fno-ivopts \
// RUN: -fnon-call-exceptions -fno-non-call-exceptions \
// RUN: -fno-semantic-interposition \
+// RUN: -fsemantic-interposition \
// RUN: -fpermissive -fno-permissive \
// RUN: -fdefer-pop -fno-defer-pop \
// RUN: -fprefetch-loop-arrays -fno-prefetch-loop-arrays \
diff --git a/llvm/include/llvm/IR/GlobalValue.h b/llvm/include/llvm/IR/GlobalValue.h
index 0171356914d6..89adf5e5cee4 100644
--- a/llvm/include/llvm/IR/GlobalValue.h
+++ b/llvm/include/llvm/IR/GlobalValue.h
@@ -423,10 +423,10 @@ class GlobalValue : public Constant {
}
/// Return true if this global's definition can be substituted with an
- /// *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()); }
+ /// *arbitrary* definition at link time or load time. We cannot do any IPO or
+ /// inlining across interposable call edges, since the callee can be
+ /// replaced with something arbitrary.
+ 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
index 71e67b4bedc6..c65113a6f3e9 100644
--- a/llvm/include/llvm/IR/Module.h
+++ b/llvm/include/llvm/IR/Module.h
@@ -848,6 +848,12 @@ class Module {
Metadata *getProfileSummary(bool IsCS);
/// @}
+ /// 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/IR/Globals.cpp b/llvm/lib/IR/Globals.cpp
index 9a07a689e886..344fda36ea69 100644
--- a/llvm/lib/IR/Globals.cpp
+++ b/llvm/lib/IR/Globals.cpp
@@ -94,6 +94,13 @@ void GlobalValue::eraseFromParent() {
llvm_unreachable("not a global");
}
+bool GlobalValue::isInterposable() const {
+ if (isInterposableLinkage(getLinkage()))
+ return true;
+ return getParent() && getParent()->getSemanticInterposition() &&
+ !isDSOLocal();
+}
+
unsigned GlobalValue::getAlignment() const {
if (auto *GA = dyn_cast<GlobalAlias>(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
index 3b3517639007..c2083f5db897 100644
--- a/llvm/lib/IR/Module.cpp
+++ b/llvm/lib/IR/Module.cpp
@@ -555,6 +555,20 @@ Metadata *Module::getProfileSummary(bool IsCS) {
: getModuleFlag("ProfileSummary"));
}
+bool Module::getSemanticInterposition() const {
+ Metadata *MF = getModuleFlag("SemanticInterposition");
+
+ auto *Val = cast_or_null<ConstantAsMetadata>(MF);
+ if (!Val)
+ return false;
+
+ return cast<ConstantInt>(Val->getValue())->getZExtValue();
+}
+
+void Module::setSemanticInterposition(bool SI) {
+ addModuleFlag(ModFlagBehavior::Error, "SemanticInterposition", SI);
+}
+
void Module::setOwnedMemoryBuffer(std::unique_ptr<MemoryBuffer> MB) {
OwnedMemoryBuffer = std::move(MB);
}
diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 4eef66ffe367..6698a6860b39 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -1476,6 +1476,13 @@ Verifier::visitModuleFlag(const MDNode *Op,
"'Linker Options' named metadata no longer supported");
}
+ if (ID->getString() == "SemanticInterposition") {
+ ConstantInt *Value =
+ mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(2));
+ Assert(Value,
+ "SemanticInterposition metadata requires constant integer argument");
+ }
+
if (ID->getString() == "CG Profile") {
for (const MDOperand &MDO : cast<MDNode>(Op->getOperand(2))->operands())
visitModuleFlagCGProfileEntry(MDO);
diff --git a/llvm/test/Transforms/Inline/inline-semantic-interposition.ll b/llvm/test/Transforms/Inline/inline-semantic-interposition.ll
new file mode 100644
index 000000000000..234136e7ca37
--- /dev/null
+++ b/llvm/test/Transforms/Inline/inline-semantic-interposition.ll
@@ -0,0 +1,26 @@
+; Check that @callee1 gets inlined while @callee2 is not, because of
+; SemanticInterposition.
+
+; RUN: opt < %s -inline -S | FileCheck %s
+
+define internal i32 @callee1(i32 %A) {
+ ret i32 %A
+}
+
+define i32 @callee2(i32 %A) {
+ ret i32 %A
+}
+
+; CHECK-LABEL: @caller
+define i32 @caller(i32 %A) {
+; CHECK-NOT: call i32 @callee1(i32 %A)
+ %A1 = call i32 @callee1(i32 %A)
+; CHECK: %A2 = call i32 @callee2(i32 %A)
+ %A2 = call i32 @callee2(i32 %A)
+; CHECK: add i32 %A, %A2
+ %R = add i32 %A1, %A2
+ ret i32 %R
+}
+
+!llvm.module.flags = !{!0}
+!0 = !{i32 1, !"SemanticInterposition", i32 1}
diff --git a/llvm/test/Verifier/module-flags-semantic-interposition.ll b/llvm/test/Verifier/module-flags-semantic-interposition.ll
new file mode 100644
index 000000000000..2c209e5c58ca
--- /dev/null
+++ b/llvm/test/Verifier/module-flags-semantic-interposition.ll
@@ -0,0 +1,12 @@
+; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at foo = dso_local global i32 1, align 4
+
+!llvm.module.flags = !{!0}
+
+!0 = !{i32 1, !"SemanticInterposition", float 1.}
+
+; CHECK: SemanticInterposition metadata requires constant integer argument
More information about the cfe-commits
mailing list