Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -452,6 +452,9 @@ def fprofile_instr_use_EQ : Joined<["-"], "fprofile-instr-use=">, Group, Flags<[CC1Option]>, HelpText<"Use instrumentation data for profile-guided optimization">; +def fprofile_ir_instr : Flag<["-"], "fprofile-ir-instr">, Group, + Flags<[CC1Option]>, + HelpText<"Use IR level instrumentation rather the FE implementation">; def fcoverage_mapping : Flag<["-"], "fcoverage-mapping">, Group, Flags<[CC1Option]>, HelpText<"Generate coverage mapping to enable code coverage analysis">; Index: include/clang/Frontend/CodeGenOptions.def =================================================================== --- include/clang/Frontend/CodeGenOptions.def +++ include/clang/Frontend/CodeGenOptions.def @@ -105,6 +105,7 @@ CODEGENOPT(ProfileInstrGenerate , 1, 0) ///< Instrument code to generate ///< execution counts to use with PGO. +CODEGENOPT(ProfileIRInstr, 1, 0) ///< IR level code PGO instrumentation and use. CODEGENOPT(CoverageMapping , 1, 0) ///< Generate coverage mapping regions to ///< enable code coverage analysis. CODEGENOPT(DumpCoverageMapping , 1, 0) ///< Dump the generated coverage mapping Index: lib/CodeGen/BackendUtil.cpp =================================================================== --- lib/CodeGen/BackendUtil.cpp +++ lib/CodeGen/BackendUtil.cpp @@ -431,6 +431,18 @@ MPM->add(createInstrProfilingPass(Options)); } + if (CodeGenOpts.ProfileIRInstr) { + // Should not have ProfileInstrGenerate set -- it is for clang + // instrumentation only. + assert (!CodeGenOpts.ProfileInstrGenerate); + if (!CodeGenOpts.InstrProfileInput.empty()) + PMBuilder.PGOInstrUse = CodeGenOpts.InstrProfileInput; + else if (!CodeGenOpts.InstrProfileOutput.empty()) + PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput; + else + PMBuilder.PGOInstrGen = "default.profraw"; + } + if (!CodeGenOpts.SampleProfileFile.empty()) MPM->add(createSampleProfileLoaderPass(CodeGenOpts.SampleProfileFile)); Index: lib/CodeGen/CodeGenModule.cpp =================================================================== --- lib/CodeGen/CodeGenModule.cpp +++ lib/CodeGen/CodeGenModule.cpp @@ -147,7 +147,7 @@ if (C.getLangOpts().ObjC1) ObjCData = new ObjCEntrypoints(); - if (!CodeGenOpts.InstrProfileInput.empty()) { + if (!CodeGenOpts.ProfileIRInstr && !CodeGenOpts.InstrProfileInput.empty()) { auto ReaderOrErr = llvm::IndexedInstrProfReader::create(CodeGenOpts.InstrProfileInput); if (std::error_code EC = ReaderOrErr.getError()) { Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -3223,6 +3223,8 @@ CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); } } + + Args.AddAllArgs(CmdArgs, options::OPT_fprofile_ir_instr); } static void addPS4ProfileRTArgs(const ToolChain &TC, const ArgList &Args, Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -449,8 +449,16 @@ Opts.DisableIntegratedAS = Args.hasArg(OPT_fno_integrated_as); Opts.Autolink = !Args.hasArg(OPT_fno_autolink); Opts.SampleProfileFile = Args.getLastArgValue(OPT_fprofile_sample_use_EQ); - Opts.ProfileInstrGenerate = Args.hasArg(OPT_fprofile_instr_generate) || - Args.hasArg(OPT_fprofile_instr_generate_EQ); + // Only set ProfileIRInstr when there are profile generate or use options. + Opts.ProfileIRInstr = Args.hasArg(OPT_fprofile_ir_instr) && + (Args.hasArg(OPT_fprofile_instr_generate) || + Args.hasArg(OPT_fprofile_instr_generate_EQ) || + Args.hasArg(OPT_fprofile_instr_use_EQ)); + // Set Opts.ProfileInstrGenerate when Opts.ProfileIRInstr is false. + // Opts.ProfileInstrGenerate will be used for Clang instrumentation only. + if (!Opts.ProfileIRInstr) + Opts.ProfileInstrGenerate = Args.hasArg(OPT_fprofile_instr_generate) || + Args.hasArg(OPT_fprofile_instr_generate_EQ); Opts.InstrProfileOutput = Args.getLastArgValue(OPT_fprofile_instr_generate_EQ); Opts.InstrProfileInput = Args.getLastArgValue(OPT_fprofile_instr_use_EQ); Opts.CoverageMapping = Index: test/Driver/instrprof-ld.c =================================================================== --- test/Driver/instrprof-ld.c +++ test/Driver/instrprof-ld.c @@ -105,3 +105,23 @@ // // CHECK-WATCHOS-ARMV7: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}" // CHECK-WATCHOS-ARMV7: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}darwin{{/|\\\\}}libclang_rt.profile_watchos.a" +// +// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ +// RUN: -shared \ +// RUN: -target x86_64-unknown-linux -fprofile-ir-instr -fprofile-instr-generate \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_linux_tree \ +// RUN: | FileCheck --check-prefix=CHECK-LINUX-X86-64-SHARED-IR-Level %s +// +// CHECK-LINUX-X86-64-SHARED-IR-Level: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}" +// CHECK-LINUX-X86-64-SHARED-IR-Level: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}libclang_rt.profile-x86_64.a" {{.*}} "-lc" +// +// RUN: %clang -no-canonical-prefixes %s -### -o %t.o 2>&1 \ +// RUN: -shared \ +// RUN: -target x86_64-unknown-linux -fprofile-ir-instr \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: --sysroot=%S/Inputs/basic_linux_tree \ +// RUN: | FileCheck --check-prefix=CHECK-LINUX-X86-64-SHARED-IR-Level-2 %s +// +// CHECK-LINUX-X86-64-SHARED-IR-Level-2: "{{(.*[^-.0-9A-Z_a-z])?}}ld{{(.exe)?}}" +// CHECK-LINUX-X86-64-SHARED-IR-Level-2-NOT: "{{.*}}/Inputs/resource_dir{{/|\\\\}}lib{{/|\\\\}}linux{{/|\\\\}}libclang_rt.profile-x86_64.a" {{.*}} "-lc"