Skip to content

Commit 1ab68d7

Browse files
authored
[clang][codegen] Add a verifier IR pass before any further passes. (#68015)
This helps check clang generated good IR in the first place, as otherwise this can cause UB in subsequent passes, with the final verification pass not catching any issues. This for example would have helped catch #67937 earlier.
1 parent 42de2b7 commit 1ab68d7

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,9 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
923923
PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
924924

925925
ModulePassManager MPM;
926+
// Add a verifier pass, before any other passes, to catch CodeGen issues.
927+
if (CodeGenOpts.VerifyModule)
928+
MPM.addPass(VerifierPass());
926929

927930
if (!CodeGenOpts.DisableLLVMPasses) {
928931
// Map our optimization levels into one of the distinct levels used to
@@ -1020,21 +1023,23 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
10201023
}
10211024

10221025
if (CodeGenOpts.FatLTO) {
1023-
MPM = PB.buildFatLTODefaultPipeline(Level, PrepareForThinLTO,
1024-
PrepareForThinLTO ||
1025-
shouldEmitRegularLTOSummary());
1026+
MPM.addPass(PB.buildFatLTODefaultPipeline(
1027+
Level, PrepareForThinLTO,
1028+
PrepareForThinLTO || shouldEmitRegularLTOSummary()));
10261029
} else if (PrepareForThinLTO) {
1027-
MPM = PB.buildThinLTOPreLinkDefaultPipeline(Level);
1030+
MPM.addPass(PB.buildThinLTOPreLinkDefaultPipeline(Level));
10281031
} else if (PrepareForLTO) {
1029-
MPM = PB.buildLTOPreLinkDefaultPipeline(Level);
1032+
MPM.addPass(PB.buildLTOPreLinkDefaultPipeline(Level));
10301033
} else {
1031-
MPM = PB.buildPerModuleDefaultPipeline(Level);
1034+
MPM.addPass(PB.buildPerModuleDefaultPipeline(Level));
10321035
}
10331036
}
10341037

10351038
// Add a verifier pass if requested. We don't have to do this if the action
10361039
// requires code generation because there will already be a verifier pass in
10371040
// the code-generation pipeline.
1041+
// Since we already added a verifier pass above, this
1042+
// might even not run the analysis, if previous passes caused no changes.
10381043
if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
10391044
MPM.addPass(VerifierPass());
10401045

clang/test/CodeGen/code-coverage.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// RUN: %clang_cc1 -emit-llvm-bc -o /dev/null -fdebug-pass-manager -coverage-data-file=/dev/null %s 2>&1 | FileCheck --check-prefix=NEWPM %s
1616
// RUN: %clang_cc1 -emit-llvm-bc -o /dev/null -fdebug-pass-manager -coverage-data-file=/dev/null -O3 %s 2>&1 | FileCheck --check-prefix=NEWPM-O3 %s
1717

18-
// NEWPM-NOT: Running pass
18+
// NEWPM: Running pass: VerifierPass
1919
// NEWPM: Running pass: GCOVProfilerPass
2020

21-
// NEWPM-O3-NOT: Running pass
21+
// NEWPM-O3: Running pass: VerifierPass
2222
// NEWPM-O3: Running pass: Annotation2MetadataPass
2323
// NEWPM-O3: Running pass: ForceFunctionAttrsPass
2424
// NEWPM-O3: Running pass: GCOVProfilerPass

clang/test/CodeGen/lto-newpm-pipeline.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm-bc -o /dev/null -mllvm -verify-analysis-invalidation=0 -fdebug-pass-manager -flto=thin -Oz %s 2>&1 | FileCheck %s \
2626
// RUN: -check-prefix=CHECK-THIN-OPTIMIZED
2727

28-
// CHECK-FULL-O0: Running pass: AlwaysInlinerPass
28+
// CHECK-FULL-O0: Running pass: VerifierPass
29+
// CHECK-FULL-O0-NEXT: Running analysis: VerifierAnalysis
30+
// CHECK-FULL-O0-NEXT: Running pass: AlwaysInlinerPass
2931
// CHECK-FULL-O0-NEXT: Running analysis: InnerAnalysisManagerProxy
3032
// CHECK-FULL-O0-NEXT: Running analysis: ProfileSummaryAnalysis
3133
// CHECK-FULL-O0-NEXT: Running pass: CoroConditionalWrapper
@@ -34,10 +36,11 @@
3436
// CHECK-FULL-O0-NEXT: Running pass: AnnotationRemarksPass
3537
// CHECK-FULL-O0-NEXT: Running analysis: TargetLibraryAnalysis
3638
// CHECK-FULL-O0-NEXT: Running pass: VerifierPass
37-
// CHECK-FULL-O0-NEXT: Running analysis: VerifierAnalysis
3839
// CHECK-FULL-O0-NEXT: Running pass: BitcodeWriterPass
3940

40-
// CHECK-THIN-O0: Running pass: AlwaysInlinerPass
41+
// CHECK-THIN-O0: Running pass: VerifierPass
42+
// CHECK-THIN-O0-NEXT: Running analysis: VerifierAnalysis
43+
// CHECK-THIN-O0-NEXT: Running pass: AlwaysInlinerPass
4144
// CHECK-THIN-O0-NEXT: Running analysis: InnerAnalysisManagerProxy
4245
// CHECK-THIN-O0-NEXT: Running analysis: ProfileSummaryAnalysis
4346
// CHECK-THIN-O0-NEXT: Running pass: CoroConditionalWrapper
@@ -46,7 +49,6 @@
4649
// CHECK-THIN-O0-NEXT: Running pass: AnnotationRemarksPass
4750
// CHECK-THIN-O0-NEXT: Running analysis: TargetLibraryAnalysis
4851
// CHECK-THIN-O0-NEXT: Running pass: VerifierPass
49-
// CHECK-THIN-O0-NEXT: Running analysis: VerifierAnalysis
5052
// CHECK-THIN-O0-NEXT: Running pass: ThinLTOBitcodeWriterPass
5153

5254
// TODO: The LTO pre-link pipeline currently invokes

0 commit comments

Comments
 (0)