Skip to content

Commit 723cd2e

Browse files
committed
Fix potential crash in Flang generateLLVMIR() when MLIR fails to translate to LLVM
This is pure code motion, to ensure that the check if we have a valid llvmModule comes before trying to set option on this module. Differential Revision: https://reviews.llvm.org/D144342
1 parent cf83e79 commit 723cd2e

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,13 @@ void CodeGenAction::generateLLVMIR() {
560560
llvmModule = mlir::translateModuleToLLVMIR(
561561
*mlirModule, *llvmCtx, moduleName ? *moduleName : "FIRModule");
562562

563+
if (!llvmModule) {
564+
unsigned diagID = ci.getDiagnostics().getCustomDiagID(
565+
clang::DiagnosticsEngine::Error, "failed to create the LLVM module");
566+
ci.getDiagnostics().Report(diagID);
567+
return;
568+
}
569+
563570
// Set PIC/PIE level LLVM module flags.
564571
if (opts.PICLevel > 0) {
565572
llvmModule->setPICLevel(static_cast<llvm::PICLevel::Level>(opts.PICLevel));
@@ -568,12 +575,6 @@ void CodeGenAction::generateLLVMIR() {
568575
static_cast<llvm::PIELevel::Level>(opts.PICLevel));
569576
}
570577

571-
if (!llvmModule) {
572-
unsigned diagID = ci.getDiagnostics().getCustomDiagID(
573-
clang::DiagnosticsEngine::Error, "failed to create the LLVM module");
574-
ci.getDiagnostics().Report(diagID);
575-
return;
576-
}
577578
}
578579

579580
void CodeGenAction::setUpTargetMachine() {

0 commit comments

Comments
 (0)