Skip to content

Commit 9efce0b

Browse files
committed
[clang] Run LLVM Verifier in modes without CodeGen too
Previously, the Backend_Emit{Nothing,BC,LL} modes did not run the LLVM verifier since it is usually added via the TargetMachine::addPassesToEmitFile method according to the DisableVerify parameter. This is called from EmitAssemblyHelper::AddEmitPasses, which is only relevant for BackendAction-s that require CodeGen. Note: * In these particular situations the verifier is added to the optimization pipeline rather than the codegen pipeline so that it runs prior to the BC/LL emission pass. * This change applies to both the old and the new PMs. * Because the clang tests use -emit-llvm ubiquitously, this change will enable the verifier for them. * A small bug is fixed in emitIFuncDefinition so that the clang/test/CodeGen/ifunc.c test would pass: the emitIFuncDefinition incorrectly passed the GlobalDecl of the IFunc itself to the call to GetOrCreateLLVMFunction for creating the resolver. Signed-off-by: Itay Bookstein <[email protected]> Reviewed By: rjmccall Differential Revision: https://reviews.llvm.org/D113352
1 parent 3b1fd19 commit 9efce0b

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,11 @@ static CodeGenFileType getCodeGenFileType(BackendAction Action) {
487487
}
488488
}
489489

490+
static bool actionRequiresCodeGen(BackendAction Action) {
491+
return Action != Backend_EmitNothing && Action != Backend_EmitBC &&
492+
Action != Backend_EmitLL;
493+
}
494+
490495
static bool initTargetOptions(DiagnosticsEngine &Diags,
491496
llvm::TargetOptions &Options,
492497
const CodeGenOptions &CodeGenOpts,
@@ -977,9 +982,7 @@ void EmitAssemblyHelper::EmitAssemblyWithLegacyPassManager(
977982

978983
setCommandLineOpts(CodeGenOpts);
979984

980-
bool UsesCodeGen = (Action != Backend_EmitNothing &&
981-
Action != Backend_EmitBC &&
982-
Action != Backend_EmitLL);
985+
bool UsesCodeGen = actionRequiresCodeGen(Action);
983986
CreateTargetMachine(UsesCodeGen);
984987

985988
if (UsesCodeGen && !TM)
@@ -1006,6 +1009,12 @@ void EmitAssemblyHelper::EmitAssemblyWithLegacyPassManager(
10061009

10071010
CreatePasses(PerModulePasses, PerFunctionPasses);
10081011

1012+
// Add a verifier pass if requested. We don't have to do this if the action
1013+
// requires code generation because there will already be a verifier pass in
1014+
// the code-generation pipeline.
1015+
if (!UsesCodeGen && CodeGenOpts.VerifyModule)
1016+
PerModulePasses.add(createVerifierPass());
1017+
10091018
legacy::PassManager CodeGenPasses;
10101019
CodeGenPasses.add(
10111020
createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
@@ -1425,6 +1434,13 @@ void EmitAssemblyHelper::RunOptimizationPipeline(
14251434
MPM.addPass(ModuleMemProfilerPass());
14261435
}
14271436
}
1437+
1438+
// Add a verifier pass if requested. We don't have to do this if the action
1439+
// requires code generation because there will already be a verifier pass in
1440+
// the code-generation pipeline.
1441+
if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
1442+
MPM.addPass(VerifierPass());
1443+
14281444
switch (Action) {
14291445
case Backend_EmitBC:
14301446
if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
@@ -1514,8 +1530,7 @@ void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
15141530
TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr);
15151531
setCommandLineOpts(CodeGenOpts);
15161532

1517-
bool RequiresCodeGen = (Action != Backend_EmitNothing &&
1518-
Action != Backend_EmitBC && Action != Backend_EmitLL);
1533+
bool RequiresCodeGen = actionRequiresCodeGen(Action);
15191534
CreateTargetMachine(RequiresCodeGen);
15201535

15211536
if (RequiresCodeGen && !TM)

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5034,7 +5034,7 @@ void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
50345034
llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
50355035
llvm::Type *ResolverTy = llvm::GlobalIFunc::getResolverFunctionType(DeclTy);
50365036
llvm::Constant *Resolver =
5037-
GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, GD,
5037+
GetOrCreateLLVMFunction(IFA->getResolver(), ResolverTy, {},
50385038
/*ForVTable=*/false);
50395039
llvm::GlobalIFunc *GIF =
50405040
llvm::GlobalIFunc::create(DeclTy, 0, llvm::Function::ExternalLinkage,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
// CHECK-FULL-O0-NEXT: Running pass: CoroCleanupPass
3333
// CHECK-FULL-O0-NEXT: Running pass: CanonicalizeAliasesPass
3434
// CHECK-FULL-O0-NEXT: Running pass: NameAnonGlobalPass
35+
// CHECK-FULL-O0-NEXT: Running pass: VerifierPass
36+
// CHECK-FULL-O0-NEXT: Running analysis: VerifierAnalysis
3537
// CHECK-FULL-O0-NEXT: Running pass: BitcodeWriterPass
3638

3739
// CHECK-THIN-O0: Running pass: AlwaysInlinerPass
@@ -40,6 +42,8 @@
4042
// CHECK-THIN-O0: Running pass: CoroCleanupPass
4143
// CHECK-THIN-O0-NEXT: Running pass: CanonicalizeAliasesPass
4244
// CHECK-THIN-O0-NEXT: Running pass: NameAnonGlobalPass
45+
// CHECK-THIN-O0-NEXT: Running pass: VerifierPass
46+
// CHECK-THIN-O0-NEXT: Running analysis: VerifierAnalysis
4347
// CHECK-THIN-O0-NEXT: Running pass: ThinLTOBitcodeWriterPass
4448

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

0 commit comments

Comments
 (0)