Author: Jez Ng Date: 2021-01-20T14:21:32-05:00 New Revision: 697f4e429b900d2d3d8a03713c7d6cd562a5bd35
URL: https://github.com/llvm/llvm-project/commit/697f4e429b900d2d3d8a03713c7d6cd562a5bd35 DIFF: https://github.com/llvm/llvm-project/commit/697f4e429b900d2d3d8a03713c7d6cd562a5bd35.diff LOG: [lld-macho] Run ObjCContractPass during LTO Run the ObjCARCContractPass during LTO. The legacy LTO backend (under LTO/ThinLTOCodeGenerator.cpp) already does this; this diff just adds that behavior to the new LTO backend. Without that pass, the objc.clang.arc.use intrinsic will get passed to the instruction selector, which doesn't know how to handle it. In order to test both the new and old pass managers, I've also added support for the `--[no-]lto-legacy-pass-manager` flags. P.S. Not sure if the ordering of the pass within the pipeline matters... Reviewed By: fhahn Differential Revision: https://reviews.llvm.org/D94547 Added: lld/test/MachO/objc-arc-contract.ll Modified: lld/MachO/Config.h lld/MachO/Driver.cpp lld/MachO/LTO.cpp lld/MachO/Options.td llvm/include/llvm/LTO/Config.h llvm/lib/LTO/LTOBackend.cpp Removed: ################################################################################ diff --git a/lld/MachO/Config.h b/lld/MachO/Config.h index 4f27ec2db45f..f6e1f134d974 100644 --- a/lld/MachO/Config.h +++ b/lld/MachO/Config.h @@ -47,6 +47,7 @@ struct Configuration { bool implicitDylibs = false; bool isPic = false; bool headerPadMaxInstallNames = false; + bool ltoNewPassManager = LLVM_ENABLE_NEW_PASS_MANAGER; bool printEachFile = false; bool printWhyLoad = false; bool searchDylibsFirst = false; diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp index 857c9991a8e6..1b337f38f7ba 100644 --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -733,6 +733,9 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly, config->printWhyLoad = args.hasArg(OPT_why_load); config->outputType = getOutputType(args); config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto); + config->ltoNewPassManager = + args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager, + LLVM_ENABLE_NEW_PASS_MANAGER); config->runtimePaths = args::getStrings(args, OPT_rpath); config->allLoad = args.hasArg(OPT_all_load); config->forceLoadObjC = args.hasArg(OPT_ObjC); diff --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp index 7554693f15e4..f48bc24df3d7 100644 --- a/lld/MachO/LTO.cpp +++ b/lld/MachO/LTO.cpp @@ -18,6 +18,7 @@ #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Transforms/ObjCARC.h" using namespace lld; using namespace lld::macho; @@ -30,6 +31,10 @@ static lto::Config createConfig() { c.CodeModel = getCodeModelFromCMModel(); c.CPU = getCPUStr(); c.MAttrs = getMAttrs(); + c.UseNewPM = config->ltoNewPassManager; + c.PreCodeGenPassesHook = [](legacy::PassManager &pm) { + pm.add(createObjCARCContractPass()); + }; return c; } diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td index af497884eb9e..89473acebdb2 100644 --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -29,6 +29,12 @@ def reproduce_eq: Joined<["--"], "reproduce=">, def version: Flag<["--"], "version">, HelpText<"Display the version number and exit">, Group<grp_lld>; +def lto_legacy_pass_manager: Flag<["--"], "lto-legacy-pass-manager">, + HelpText<"Use the legacy pass manager in LLVM">, + Group<grp_lld>; +def no_lto_legacy_pass_manager : Flag<["--"], "no-lto-legacy-pass-manager">, + HelpText<"Use the new pass manager in LLVM">, + Group<grp_lld>; // This is a complete Options.td compiled from Apple's ld(1) manpage diff --git a/lld/test/MachO/objc-arc-contract.ll b/lld/test/MachO/objc-arc-contract.ll new file mode 100644 index 000000000000..66f30dc60c49 --- /dev/null +++ b/lld/test/MachO/objc-arc-contract.ll @@ -0,0 +1,30 @@ +; REQUIRES: x86 + +;; Verify that we run the ObjCARCContractPass during LTO. Without that, the +;; objc.clang.arc.use intrinsic will get passed to the instruction selector, +;; which doesn't know how to handle it. + +; RUN: llvm-as %s -o %t.o +; RUN: %lld -dylib -lSystem %t.o -o %t --lto-legacy-pass-manager +; RUN: llvm-objdump -d %t | FileCheck %s +; RUN: %lld -dylib -lSystem %t.o -o %t --no-lto-legacy-pass-manager +; RUN: llvm-objdump -d %t | FileCheck %s + +; RUN: opt -module-summary %s -o %t.o +; RUN: %lld -dylib -lSystem %t.o -o %t --lto-legacy-pass-manager +; RUN: llvm-objdump -d %t | FileCheck %s +; RUN: %lld -dylib -lSystem %t.o -o %t --no-lto-legacy-pass-manager +; RUN: llvm-objdump -d %t | FileCheck %s + +; CHECK: <_foo>: +; CHECK-NEXT: retq + +target triple = "x86_64-apple-darwin" +target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" + +define void @foo(i8* %a, i8* %b) { + call void (...) @llvm.objc.clang.arc.use(i8* %a, i8* %b) nounwind + ret void +} + +declare void @llvm.objc.clang.arc.use(...) nounwind diff --git a/llvm/include/llvm/LTO/Config.h b/llvm/include/llvm/LTO/Config.h index 1a7595d75404..88c1452e5aa9 100644 --- a/llvm/include/llvm/LTO/Config.h +++ b/llvm/include/llvm/LTO/Config.h @@ -19,6 +19,7 @@ #include "llvm/IR/DiagnosticInfo.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/LLVMContext.h" +#include "llvm/IR/LegacyPassManager.h" #include "llvm/Passes/PassBuilder.h" #include "llvm/Support/CodeGen.h" #include "llvm/Target/TargetOptions.h" @@ -43,6 +44,8 @@ struct Config { TargetOptions Options; std::vector<std::string> MAttrs; std::vector<std::string> PassPlugins; + /// For adding passes that run right before codegen. + std::function<void(legacy::PassManager &)> PreCodeGenPassesHook; Optional<Reloc::Model> RelocModel = Reloc::PIC_; Optional<CodeModel::Model> CodeModel = None; CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default; diff --git a/llvm/lib/LTO/LTOBackend.cpp b/llvm/lib/LTO/LTOBackend.cpp index effbdf6cfd9e..83282249ef44 100644 --- a/llvm/lib/LTO/LTOBackend.cpp +++ b/llvm/lib/LTO/LTOBackend.cpp @@ -428,6 +428,8 @@ static void codegen(const Config &Conf, TargetMachine *TM, legacy::PassManager CodeGenPasses; CodeGenPasses.add( createImmutableModuleSummaryIndexWrapperPass(&CombinedIndex)); + if (Conf.PreCodeGenPassesHook) + Conf.PreCodeGenPassesHook(CodeGenPasses); if (TM->addPassesToEmitFile(CodeGenPasses, *Stream->OS, DwoOut ? &DwoOut->os() : nullptr, Conf.CGFileType)) _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits