[PATCH] Driver: use new gold/ld64 plugin flags with LTO-requiring passes.

Duncan P. N. Exon Smith dexonsmith at apple.com
Tue Mar 17 17:51:00 PDT 2015


> On 2015-Mar-17, at 15:41, Peter Collingbourne <peter at pcc.me.uk> wrote:
> 
> Hi rafael,
> 
> http://reviews.llvm.org/D8402
> 
> Files:
>  lib/Driver/Tools.cpp
>  test/Driver/fsanitize.c
> 
> Index: lib/Driver/Tools.cpp
> ===================================================================
> --- lib/Driver/Tools.cpp
> +++ lib/Driver/Tools.cpp
> @@ -1560,6 +1560,15 @@
>   std::string CPU = getCPUName(Args, ToolChain.getTriple());
>   if (!CPU.empty())
>     CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=mcpu=") + CPU));
> +
> +  // If a sanitizer needs LTO, turn on the relevant passes.
> +  if (ToolChain.getSanitizerArgs().needsLTO()) {
> +    CmdArgs.push_back("-plugin-opt=lowerbitsets");
> +
> +    // Disable most optimizations unless LTO was specifically requested.
> +    if (!Args.hasFlag(options::OPT_flto, options::OPT_fno_lto, false))
> +      CmdArgs.push_back("-plugin-opt=disable-opt");
> +  }
> }
> 
> static void getX86TargetFeatures(const Driver & D,
> @@ -5850,6 +5859,18 @@
>     CmdArgs.push_back(TmpPath);
>   }
> 
> +  // If a sanitizer needs LTO, turn on the relevant passes.
> +  if (getToolChain().getSanitizerArgs().needsLTO()) {
> +    CmdArgs.push_back("-mllvm");
> +    CmdArgs.push_back("-lowerbitsets");
> +
> +    // Disable most optimizations unless LTO was specifically requested.
> +    if (!Args.hasFlag(options::OPT_flto, options::OPT_fno_lto, false)) {
> +      CmdArgs.push_back("-mllvm");
> +      CmdArgs.push_back("-disable-opt");

Do I understand correctly that you're turning off optimizations for the
following flow?

    $ clang -flto -O3 -c a.c
    $ clang -flto -O3 -c b.c
    $ clang a.o b.o

Can you clarify?

Also, the Darwin linker `-mllvm` option is a debugging hack.  Module flags
[1] are the correct way to pass options libLTO.dylib.

I think what you want here is:

    Module &M;
    if (sanitizeCFI())
      M.addModuleFlag(Module::Error, "CFI Sanitizer", /* Non-zero value */ 1)

Then if any module has this flag, the merged module will have it.  On the
other side, you want something similar to this to check the flag:

    bool usesSanitizeCFI(const Module &M) {
      if (auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("CFI Sanitizer")))
        return cast<ConstantInt>(Val->getValue())->getZExtValue();
      return false;
    }

[1]: http://llvm.org/docs/LangRef.html#module-flags-metadata

> +    }
> +  }
> +
>   // Derived from the "link" spec.
>   Args.AddAllArgs(CmdArgs, options::OPT_static);
>   if (!Args.hasArg(options::OPT_static))
> Index: test/Driver/fsanitize.c
> ===================================================================
> --- test/Driver/fsanitize.c
> +++ test/Driver/fsanitize.c
> @@ -209,6 +209,15 @@
> // CHECK-CFI-UCAST: -emit-llvm-bc{{.*}}-fsanitize=cfi-unrelated-cast
> // CHECK-CFI-VPTR: -emit-llvm-bc{{.*}}-fsanitize=cfi-vptr
> 
> +// RUN: %clang -target x86_64-linux-gnu -fsanitize=cfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI-LINK-LINUX
> +// RUN: %clang -target x86_64-apple-darwin10 -fsanitize=cfi %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI-LINK-DARWIN
> +// RUN: %clang -target x86_64-linux-gnu -fsanitize=cfi -flto %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI-LINK-LINUX-LTO
> +// RUN: %clang -target x86_64-apple-darwin10 -fsanitize=cfi -flto %s -### 2>&1 | FileCheck %s --check-prefix=CHECK-CFI-LINK-DARWIN-LTO
> +// CHECK-CFI-LINK-LINUX: "-plugin-opt=lowerbitsets" "-plugin-opt=disable-opt"
> +// CHECK-CFI-LINK-DARWIN: "-mllvm" "-lowerbitsets" "-mllvm" "-disable-opt"
> +// CHECK-CFI-LINK-LINUX-LTO-NOT: "-plugin-opt=disable-opt"
> +// CHECK-CFI-LINK-DARWIN-LTO-NOT: "-mllvm" "-disable-opt"
> +
> // RUN: %clang_cl -fsanitize=address -c -MDd -### -- %s 2>&1 | FileCheck %s -check-prefix=CHECK-ASAN-DEBUGRTL
> // RUN: %clang_cl -fsanitize=address -c -MTd -### -- %s 2>&1 | FileCheck %s -check-prefix=CHECK-ASAN-DEBUGRTL
> // RUN: %clang_cl -fsanitize=address -c -LDd -### -- %s 2>&1 | FileCheck %s -check-prefix=CHECK-ASAN-DEBUGRTL
> 
> EMAIL PREFERENCES
>  http://reviews.llvm.org/settings/panel/emailpreferences/
> <D8402.22140.patch>_______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits





More information about the cfe-commits mailing list