Skip to content

Commit 1aaa05f

Browse files
committed
Reapply "Run ObjCContractPass in Default Codegen Pipeline (llvm#92331)"
This reverts commit 1579e9c.
1 parent bb4aeb6 commit 1aaa05f

File tree

21 files changed

+119
-37
lines changed

21 files changed

+119
-37
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,6 @@ bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
588588
// this also adds codegenerator level optimization passes.
589589
CodeGenFileType CGFT = getCodeGenFileType(Action);
590590

591-
// Add ObjC ARC final-cleanup optimizations. This is done as part of the
592-
// "codegen" passes so that it isn't run multiple times when there is
593-
// inlining happening.
594-
if (CodeGenOpts.OptimizationLevel > 0)
595-
CodeGenPasses.add(createObjCARCContractPass());
596-
597591
if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
598592
/*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
599593
Diags.Report(diag::err_fe_unable_to_interface_with_target);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; RUN: opt -thinlto-bc -o %t.o %s
2+
3+
; RUN: llvm-lto2 run -thinlto-distributed-indexes %t.o \
4+
; RUN: -o %t2.index \
5+
; RUN: -r=%t.o,_use_arc,px
6+
7+
; RUN: %clang_cc1 -triple x86_64-apple-darwin \
8+
; RUN: -emit-obj -fthinlto-index=%t.o.thinlto.bc \
9+
; RUN: -o %t.native.o -x ir %t.o
10+
11+
target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
12+
target triple = "x86_64-apple-darwin"
13+
14+
define void @use_arc(ptr %a, ptr %b) {
15+
call void (...) @llvm.objc.clang.arc.use(ptr %a, ptr %b) nounwind
16+
ret void
17+
}
18+
19+
declare void @llvm.objc.clang.arc.use(...) nounwind

lld/MachO/LTO.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ static lto::Config createConfig() {
4848
c.CPU = getCPUStr();
4949
c.MAttrs = getMAttrs();
5050
c.DiagHandler = diagnosticHandler;
51-
c.PreCodeGenPassesHook = [](legacy::PassManager &pm) {
52-
pm.add(createObjCARCContractPass());
53-
};
5451

5552
c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
5653

llvm/include/llvm/Analysis/ObjCARCAnalysisUtils.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@ extern bool EnableARCOpts;
4141
/// Test if the given module looks interesting to run ARC optimization
4242
/// on.
4343
inline bool ModuleHasARC(const Module &M) {
44-
return
45-
M.getNamedValue("llvm.objc.retain") ||
46-
M.getNamedValue("llvm.objc.release") ||
47-
M.getNamedValue("llvm.objc.autorelease") ||
48-
M.getNamedValue("llvm.objc.retainAutoreleasedReturnValue") ||
49-
M.getNamedValue("llvm.objc.unsafeClaimAutoreleasedReturnValue") ||
50-
M.getNamedValue("llvm.objc.retainBlock") ||
51-
M.getNamedValue("llvm.objc.autoreleaseReturnValue") ||
52-
M.getNamedValue("llvm.objc.autoreleasePoolPush") ||
53-
M.getNamedValue("llvm.objc.loadWeakRetained") ||
54-
M.getNamedValue("llvm.objc.loadWeak") ||
55-
M.getNamedValue("llvm.objc.destroyWeak") ||
56-
M.getNamedValue("llvm.objc.storeWeak") ||
57-
M.getNamedValue("llvm.objc.initWeak") ||
58-
M.getNamedValue("llvm.objc.moveWeak") ||
59-
M.getNamedValue("llvm.objc.copyWeak") ||
60-
M.getNamedValue("llvm.objc.retainedObject") ||
61-
M.getNamedValue("llvm.objc.unretainedObject") ||
62-
M.getNamedValue("llvm.objc.unretainedPointer") ||
63-
M.getNamedValue("llvm.objc.clang.arc.use");
44+
return M.getNamedValue("llvm.objc.retain") ||
45+
M.getNamedValue("llvm.objc.release") ||
46+
M.getNamedValue("llvm.objc.autorelease") ||
47+
M.getNamedValue("llvm.objc.retainAutoreleasedReturnValue") ||
48+
M.getNamedValue("llvm.objc.unsafeClaimAutoreleasedReturnValue") ||
49+
M.getNamedValue("llvm.objc.retainBlock") ||
50+
M.getNamedValue("llvm.objc.autoreleaseReturnValue") ||
51+
M.getNamedValue("llvm.objc.autoreleasePoolPush") ||
52+
M.getNamedValue("llvm.objc.loadWeakRetained") ||
53+
M.getNamedValue("llvm.objc.loadWeak") ||
54+
M.getNamedValue("llvm.objc.destroyWeak") ||
55+
M.getNamedValue("llvm.objc.storeWeak") ||
56+
M.getNamedValue("llvm.objc.initWeak") ||
57+
M.getNamedValue("llvm.objc.moveWeak") ||
58+
M.getNamedValue("llvm.objc.copyWeak") ||
59+
M.getNamedValue("llvm.objc.retainedObject") ||
60+
M.getNamedValue("llvm.objc.unretainedObject") ||
61+
M.getNamedValue("llvm.objc.unretainedPointer") ||
62+
M.getNamedValue("llvm.objc.clang.arc.noop.use") ||
63+
M.getNamedValue("llvm.objc.clang.arc.use");
6464
}
6565

6666
/// This is a wrapper around getUnderlyingObject which also knows how to

llvm/lib/CodeGen/TargetPassConfig.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "llvm/Support/WithColor.h"
4747
#include "llvm/Target/CGPassBuilderOption.h"
4848
#include "llvm/Target/TargetMachine.h"
49+
#include "llvm/Transforms/ObjCARC.h"
4950
#include "llvm/Transforms/Scalar.h"
5051
#include "llvm/Transforms/Utils.h"
5152
#include <cassert>
@@ -949,6 +950,8 @@ void TargetPassConfig::addCodeGenPrepare() {
949950
void TargetPassConfig::addISelPrepare() {
950951
addPreISel();
951952

953+
addPass(createObjCARCContractPass());
954+
952955
// Force codegen to run according to the callgraph.
953956
if (requiresCodeGenSCCOrder())
954957
addPass(new DummyCGSCCPass);

llvm/lib/LTO/LTOCodeGenerator.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
136136

137137
Config.CodeModel = std::nullopt;
138138
Config.StatsFile = LTOStatsFile;
139-
Config.PreCodeGenPassesHook = [](legacy::PassManager &PM) {
140-
PM.add(createObjCARCContractPass());
141-
};
142-
143139
Config.RunCSIRInstr = LTORunCSIRInstr;
144140
Config.CSIRProfile = LTOCSIRProfile;
145141
}

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,6 @@ std::unique_ptr<MemoryBuffer> codegenModule(Module &TheModule,
335335
raw_svector_ostream OS(OutputBuffer);
336336
legacy::PassManager PM;
337337

338-
// If the bitcode files contain ARC code and were compiled with optimization,
339-
// the ObjCARCContractPass must be run, so do it unconditionally here.
340-
PM.add(createObjCARCContractPass());
341-
342338
// Setup the codegen now.
343339
if (TM.addPassesToEmitFile(PM, OS, nullptr, CodeGenFileType::ObjectFile,
344340
/* DisableVerify */ true))

llvm/lib/Transforms/ObjCARC/ObjCARCContract.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class ObjCARCContract {
7171
ARCRuntimeEntryPoints EP;
7272
BundledRetainClaimRVs *BundledInsts = nullptr;
7373

74+
/// A flag indicating whether this optimization pass should run.
75+
bool Run;
76+
7477
/// The inline asm string to insert between calls and RetainRV calls to make
7578
/// the optimization work on targets which need it.
7679
const MDString *RVInstMarker;
@@ -527,6 +530,10 @@ bool ObjCARCContract::tryToPeepholeInstruction(
527530
//===----------------------------------------------------------------------===//
528531

529532
bool ObjCARCContract::init(Module &M) {
533+
Run = ModuleHasARC(M);
534+
if (!Run)
535+
return false;
536+
530537
EP.init(&M);
531538

532539
// Initialize RVInstMarker.
@@ -539,6 +546,9 @@ bool ObjCARCContract::run(Function &F, AAResults *A, DominatorTree *D) {
539546
if (!EnableARCOpts)
540547
return false;
541548

549+
if (!Run)
550+
return false;
551+
542552
Changed = CFGChanged = false;
543553
AA = A;
544554
DT = D;

llvm/test/CodeGen/AArch64/O0-pipeline.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
; CHECK-NEXT: AArch64 Stack Tagging
3333
; CHECK-NEXT: SME ABI Pass
3434
; CHECK-NEXT: Exception handling preparation
35+
; CHECK-NEXT: Dominator Tree Construction
36+
; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
37+
; CHECK-NEXT: Function Alias Analysis Results
38+
; CHECK-NEXT: ObjC ARC contraction
3539
; CHECK-NEXT: Prepare callbr
3640
; CHECK-NEXT: Safe Stack instrumentation pass
3741
; CHECK-NEXT: Insert stack protectors

llvm/test/CodeGen/AArch64/O3-pipeline.ll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@
104104
; CHECK-NEXT: Dominator Tree Construction
105105
; CHECK-NEXT: FunctionPass Manager
106106
; CHECK-NEXT: Merge internal globals
107+
; CHECK-NEXT: Dominator Tree Construction
108+
; CHECK-NEXT: Basic Alias Analysis (stateless AA impl)
109+
; CHECK-NEXT: Function Alias Analysis Results
110+
; CHECK-NEXT: ObjC ARC contraction
107111
; CHECK-NEXT: Prepare callbr
108112
; CHECK-NEXT: Safe Stack instrumentation pass
109113
; CHECK-NEXT: Insert stack protectors

0 commit comments

Comments
 (0)