[PATCH] D18843: Always have clang pass -pie-level and -pic-level values to the code generator

Sriraman Tallam via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 6 15:12:38 PDT 2016

tmsriram created this revision.
tmsriram added reviewers: rnk, davidxl.
tmsriram added a subscriber: cfe-commits.

clang does not pass pie-level and pic-level option values to the code generator with "-x ir" due to the following code in CompilerInvocation.cpp:

   if (DashX == IK_AST || DashX == IK_LLVM_IR) {
    } else {
       ParseLangArgs(*Res.getLangOpts(), Args, DashX, Res.getTargetOpts(), Diags);
pie-level and pic-level are LangArgs and are not parsed with "-x ir". They define macros __PIC__ and __PIE___ when set, see lib/Frontend/InitPreprocessor.cpp

Not setting LangOpts.PIELevel means that Options.PositionIndependentExecutable is always false even when -fPIE is used.  This patch fixes that.

I considered an alternative of making pie-level and pic-level CodeGen Options.  However, since these options are used to set macros __PIE__, __PIC__, etc. this looked better as LangOpts.  Please let me know what you think.

For a broader context, I am working on optimizing accesses to global variables  with -fPIE on x86-64 and this patch is the first step to get there.  In particular, I noticed the following code generated with clang:


int a = 0;

int main() {
  return a;

$ clang -O2 -fPIE hello.cc -S
$ cat hello.s

main:                                   # @main
	movq	a at GOTPCREL(%rip), %rax
	movl	(%rax), %eax

Creating a GOT entry for global 'a' is unnecessary as 'a' is defined in hello.cpp which will be linked into a position independent executable (fPIE).  Hence, the definition of 'a' cannot be overridden and we can remove a load. The efficient access is this:

main:                                   # @main
	movl	a(%rip), %eax

I plan to address this in a separate patch.



Index: lib/Frontend/CompilerInvocation.cpp
--- lib/Frontend/CompilerInvocation.cpp
+++ lib/Frontend/CompilerInvocation.cpp
@@ -2142,6 +2142,12 @@
     // what the input type is.
     if (Args.hasArg(OPT_fobjc_arc))
       Res.getLangOpts()->ObjCAutoRefCount = 1;
+    // PIClevel and PIELevel are needed during code generation and this should be
+    // set regardless of the input type.
+    Res.getLangOpts()->PICLevel = getLastArgIntValue(Args, OPT_pic_level,
+                                                     0, Diags);
+    Res.getLangOpts()->PIELevel = getLastArgIntValue(Args, OPT_pie_level,
+                                                     0, Diags);
     parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
                         Diags, Res.getLangOpts()->Sanitize);
   } else {

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18843.52853.patch
Type: text/x-patch
Size: 890 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160406/2d8c8eb0/attachment-0001.bin>

More information about the cfe-commits mailing list