Skip to content

Commit 30a9c8e

Browse files
jeanPeriermemfrob
authored andcommitted
[flang][NFC] Add source line to lowering TODO messages
- Add a fatal error handler that can print a message with source location before aborting. - Update TODO macro to take an mlir location argument and to use the newly introduced fatal error handler. - Introduce TODO_NOLOC for the few places where no source location is easily accessible. Reviewed By: schweitz Differential Revision: https://reviews.llvm.org/D97190
1 parent e637384 commit 30a9c8e

File tree

4 files changed

+105
-25
lines changed

4 files changed

+105
-25
lines changed

flang/include/flang/Lower/Todo.h

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef FORTRAN_LOWER_TODO_H
1414
#define FORTRAN_LOWER_TODO_H
1515

16+
#include "flang/Optimizer/Support/FatalError.h"
1617
#include "llvm/Support/ErrorHandling.h"
1718
#include "llvm/Support/raw_ostream.h"
1819
#include <cstdlib>
@@ -21,29 +22,55 @@
2122
// developed.
2223

2324
#undef TODO
25+
// Use TODO_NOLOC if no mlir location is available to indicate the line in
26+
// Fortran source file that requires an unimplemented feature.
27+
#undef TODO_NOLOC
28+
29+
#undef TODOQUOTE
30+
#define TODOQUOTE(X) #X
2431

2532
#ifdef NDEBUG
2633

2734
// In a release build, just give a message and exit.
28-
#define TODO(ToDoMsg) \
35+
#define TODO_NOLOC(ToDoMsg) \
2936
do { \
3037
llvm::errs() << __FILE__ << ':' << __LINE__ << ": not yet implemented " \
3138
<< ToDoMsg << '\n'; \
3239
std::exit(1); \
3340
} while (false)
3441

35-
#else
42+
#undef TODO_DEFN
43+
#define TODO_DEFN(MlirLoc, ToDoMsg, ToDoFile, ToDoLine) \
44+
do { \
45+
mlir::emitError(MlirLoc, ToDoFile \
46+
":" TODOQUOTE(ToDoLine) ": not yet implemented " ToDoMsg); \
47+
std::exit(1); \
48+
} while (false)
3649

37-
#undef TODOQUOTE
38-
#define TODOQUOTE(X) #X
50+
#define TODO(MlirLoc, ToDoMsg) TODO_DEFN(MlirLoc, ToDoMsg, __FILE__, __LINE__)
51+
52+
#else
3953

4054
// In a developer build, print a message and give a backtrace.
41-
#define TODO(ToDoMsg) \
55+
#undef TODO_NOLOCDEFN
56+
#define TODO_NOLOCDEFN(ToDoMsg, ToDoFile, ToDoLine) \
4257
do { \
4358
llvm::report_fatal_error( \
4459
__FILE__ ":" TODOQUOTE(__LINE__) ": not yet implemented " ToDoMsg); \
4560
} while (false)
4661

62+
#define TODO_NOLOC(ToDoMsg) TODO_NOLOCDEFN(ToDoMsg, __FILE__, __LINE__)
63+
64+
#undef TODO_DEFN
65+
#define TODO_DEFN(MlirLoc, ToDoMsg, ToDoFile, ToDoLine) \
66+
do { \
67+
fir::emitFatalError( \
68+
MlirLoc, \
69+
ToDoFile ":" TODOQUOTE(ToDoLine) ": not yet implemented " ToDoMsg); \
70+
} while (false)
71+
72+
#define TODO(MlirLoc, ToDoMsg) TODO_DEFN(MlirLoc, ToDoMsg, __FILE__, __LINE__)
73+
4774
#endif
4875

4976
#endif // FORTRAN_LOWER_TODO_H
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===-- Optimizer/Support/FatalError.h --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef FORTRAN_OPTIMIZER_SUPPORT_FATALERROR_H
14+
#define FORTRAN_OPTIMIZER_SUPPORT_FATALERROR_H
15+
16+
#include "mlir/IR/Diagnostics.h"
17+
#include "llvm/Support/ErrorHandling.h"
18+
19+
namespace fir {
20+
21+
/// Fatal error reporting helper. Report a fatal error with a source location
22+
/// and immediately abort flang.
23+
LLVM_ATTRIBUTE_NORETURN inline void emitFatalError(mlir::Location loc,
24+
const llvm::Twine &message) {
25+
mlir::emitError(loc, message);
26+
llvm::report_fatal_error("aborting");
27+
}
28+
29+
/// Fatal error reporting helper. Report a fatal error without a source location
30+
/// and immediately abort flang.
31+
LLVM_ATTRIBUTE_NORETURN inline void emitFatalError(const llvm::Twine &message) {
32+
llvm::report_fatal_error(message);
33+
}
34+
35+
} // namespace fir
36+
37+
#endif // FORTRAN_OPTIMIZER_SUPPORT_FATALERROR_H

flang/lib/Lower/OpenACC.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ genACC(Fortran::lower::AbstractConverter &converter,
910910
} else if (standaloneDirective.v == llvm::acc::Directive::ACCD_shutdown) {
911911
genACCInitShutdownOp<mlir::acc::ShutdownOp>(converter, accClauseList);
912912
} else if (standaloneDirective.v == llvm::acc::Directive::ACCD_set) {
913-
TODO("OpenACC set directive not lowered yet!");
913+
TODO(converter.genLocation(), "OpenACC set directive not lowered yet!");
914914
} else if (standaloneDirective.v == llvm::acc::Directive::ACCD_update) {
915915
genACCUpdateOp(converter, accClauseList);
916916
}
@@ -1003,7 +1003,8 @@ void Fortran::lower::genOpenACCConstruct(
10031003
},
10041004
[&](const Fortran::parser::OpenACCCombinedConstruct
10051005
&combinedConstruct) {
1006-
TODO("OpenACC Combined construct not lowered yet!");
1006+
TODO(converter.genLocation(),
1007+
"OpenACC Combined construct not lowered yet!");
10071008
},
10081009
[&](const Fortran::parser::OpenACCLoopConstruct &loopConstruct) {
10091010
genACC(converter, eval, loopConstruct);
@@ -1014,16 +1015,19 @@ void Fortran::lower::genOpenACCConstruct(
10141015
},
10151016
[&](const Fortran::parser::OpenACCRoutineConstruct
10161017
&routineConstruct) {
1017-
TODO("OpenACC Routine construct not lowered yet!");
1018+
TODO(converter.genLocation(),
1019+
"OpenACC Routine construct not lowered yet!");
10181020
},
10191021
[&](const Fortran::parser::OpenACCCacheConstruct &cacheConstruct) {
1020-
TODO("OpenACC Cache construct not lowered yet!");
1022+
TODO(converter.genLocation(),
1023+
"OpenACC Cache construct not lowered yet!");
10211024
},
10221025
[&](const Fortran::parser::OpenACCWaitConstruct &waitConstruct) {
10231026
genACC(converter, eval, waitConstruct);
10241027
},
10251028
[&](const Fortran::parser::OpenACCAtomicConstruct &atomicConstruct) {
1026-
TODO("OpenACC Atomic construct not lowered yet!");
1029+
TODO(converter.genLocation(),
1030+
"OpenACC Atomic construct not lowered yet!");
10271031
},
10281032
},
10291033
accConstruct.u);

flang/lib/Lower/OpenMP.cpp

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
8383
converter.getCurrentLocation());
8484
break;
8585
case llvm::omp::Directive::OMPD_target_enter_data:
86-
TODO("");
86+
TODO(converter.getCurrentLocation(), "OMPD_target_enter_data");
8787
case llvm::omp::Directive::OMPD_target_exit_data:
88-
TODO("");
88+
TODO(converter.getCurrentLocation(), "OMPD_target_exit_data");
8989
case llvm::omp::Directive::OMPD_target_update:
90-
TODO("");
90+
TODO(converter.getCurrentLocation(), "OMPD_target_update");
9191
case llvm::omp::Directive::OMPD_ordered:
92-
TODO("");
92+
TODO(converter.getCurrentLocation(), "OMPD_ordered");
9393
}
9494
}
9595

@@ -112,15 +112,18 @@ genOMP(Fortran::lower::AbstractConverter &converter,
112112
if (std::get<std::optional<
113113
std::list<Fortran::parser::OmpMemoryOrderClause>>>(
114114
flushConstruct.t))
115-
TODO("Handle OmpMemoryOrderClause");
115+
TODO(converter.getCurrentLocation(),
116+
"Handle OmpMemoryOrderClause");
116117
converter.getFirOpBuilder().create<mlir::omp::FlushOp>(
117118
converter.getCurrentLocation(), operandRange);
118119
},
119120
[&](const Fortran::parser::OpenMPCancelConstruct &cancelConstruct) {
120-
TODO("");
121+
TODO(converter.getCurrentLocation(), "OpenMPCancelConstruct");
121122
},
122123
[&](const Fortran::parser::OpenMPCancellationPointConstruct
123-
&cancellationPointConstruct) { TODO(""); },
124+
&cancellationPointConstruct) {
125+
TODO(converter.getCurrentLocation(), "OpenMPCancelConstruct");
126+
},
124127
},
125128
standaloneConstruct.u);
126129
}
@@ -256,28 +259,37 @@ void Fortran::lower::genOpenMPConstruct(
256259
genOMP(converter, eval, standaloneConstruct);
257260
},
258261
[&](const Fortran::parser::OpenMPSectionsConstruct
259-
&sectionsConstruct) { TODO(""); },
262+
&sectionsConstruct) {
263+
TODO(converter.getCurrentLocation(), "OpenMPSectionsConstruct");
264+
},
260265
[&](const Fortran::parser::OpenMPLoopConstruct &loopConstruct) {
261-
TODO("");
266+
TODO(converter.getCurrentLocation(), "OpenMPLoopConstruct");
262267
},
263268
[&](const Fortran::parser::OpenMPDeclarativeAllocate
264-
&execAllocConstruct) { TODO(""); },
269+
&execAllocConstruct) {
270+
TODO(converter.getCurrentLocation(), "OpenMPDeclarativeAllocate");
271+
},
265272
[&](const Fortran::parser::OpenMPExecutableAllocate
266-
&execAllocConstruct) { TODO(""); },
273+
&execAllocConstruct) {
274+
TODO(converter.getCurrentLocation(), "OpenMPExecutableAllocate");
275+
},
267276
[&](const Fortran::parser::OpenMPBlockConstruct &blockConstruct) {
268277
genOMP(converter, eval, blockConstruct);
269278
},
270279
[&](const Fortran::parser::OpenMPAtomicConstruct &atomicConstruct) {
271-
TODO("");
280+
TODO(converter.getCurrentLocation(), "OpenMPAtomicConstruct");
272281
},
273282
[&](const Fortran::parser::OpenMPCriticalConstruct
274-
&criticalConstruct) { TODO(""); },
283+
&criticalConstruct) {
284+
TODO(converter.getCurrentLocation(), "OpenMPCriticalConstruct");
285+
},
275286
},
276287
ompConstruct.u);
277288
}
278289

279290
void Fortran::lower::genOpenMPEndLoop(
280-
Fortran::lower::AbstractConverter &, Fortran::lower::pft::Evaluation &,
291+
Fortran::lower::AbstractConverter &converter,
292+
Fortran::lower::pft::Evaluation &,
281293
const Fortran::parser::OmpEndLoopDirective &) {
282-
TODO("");
294+
TODO(converter.getCurrentLocation(), "OmpEndLoopDirective");
283295
}

0 commit comments

Comments
 (0)