Skip to content

Commit 4b9c0d4

Browse files
committedMay 16, 2016
[llc] New diagnostic handler
Without a diagnostic handler installed, llc's behaviour is to exit on the first error that it encounters. This is very different from the behaviour of clang and other front ends, which try to gather as many errors as possible before exiting. This commit adds a diagnostic handler to llc, allowing it to find and report more than one error. The old behaviour is preserved under a flag (-exit-on-error). Some of the tests fail with the new diagnostic handler, so they have to use the new flag in order to run under the previous behaviour. Some of these are known bugs, others need further investigation. Ideally, we should fix the tests and remove the flag at some point in the future. Reapplied after fixing the LLDB build that was broken due to the new DiagnosticSeverity in LLVMContext.h, and fixed an UB in the new change. Patch by Diana Picus. llvm-svn: 269655
1 parent a250dc9 commit 4b9c0d4

31 files changed

+69
-34
lines changed
 

‎llvm/include/llvm/IR/DiagnosticInfo.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Module;
3434
class SMDiagnostic;
3535

3636
/// \brief Defines the different supported severity of a diagnostic.
37-
enum DiagnosticSeverity {
37+
enum DiagnosticSeverity : char {
3838
DS_Error,
3939
DS_Warning,
4040
DS_Remark,

‎llvm/include/llvm/IR/LLVMContext.h

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class MDString;
2929
class DICompositeType;
3030
class SMDiagnostic;
3131
class DiagnosticInfo;
32+
enum DiagnosticSeverity : char;
3233
template <typename T> class SmallVectorImpl;
3334
class Function;
3435
class DebugLoc;
@@ -172,6 +173,10 @@ class LLVMContext {
172173
/// setDiagnosticContext.
173174
void *getDiagnosticContext() const;
174175

176+
/// \brief Get the prefix that should be printed in front of a diagnostic of
177+
/// the given \p Severity
178+
static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity);
179+
175180
/// \brief Report a message to the currently installed diagnostic handler.
176181
///
177182
/// This function returns, in particular in the case of error reporting

‎llvm/lib/IR/LLVMContext.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
231231
return true;
232232
}
233233

234-
static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
234+
const char *
235+
LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
235236
switch (Severity) {
236237
case DS_Error:
237238
return "error";

‎llvm/test/CodeGen/AArch64/arm64-inline-asm-error-I.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=arm64 < %s 2> %t
1+
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
22
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s
33

44
; Check for at least one invalid constant.

‎llvm/test/CodeGen/AArch64/arm64-inline-asm-error-J.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=arm64 < %s 2> %t
1+
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
22
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s
33

44
; Check for at least one invalid constant.

‎llvm/test/CodeGen/AArch64/arm64-inline-asm-error-K.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=arm64 < %s 2> %t
1+
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
22
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s
33

44
; Check for at least one invalid constant.

‎llvm/test/CodeGen/AArch64/arm64-inline-asm-error-L.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=arm64 < %s 2> %t
1+
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
22
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s
33

44
; Check for at least one invalid constant.

‎llvm/test/CodeGen/AArch64/arm64-inline-asm-error-M.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=arm64 < %s 2> %t
1+
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
22
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s
33

44
; Check for at least one invalid constant.

‎llvm/test/CodeGen/AArch64/arm64-inline-asm-error-N.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=arm64 < %s 2> %t
1+
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
22
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s
33

44
; Check for at least one invalid constant.

‎llvm/test/CodeGen/AMDGPU/call.ll

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
; RUN: not llc -march=amdgcn -mcpu=SI -verify-machineinstrs< %s 2>&1 | FileCheck %s
2-
; RUN: not llc -march=amdgcn -mcpu=tonga -verify-machineinstrs< %s 2>&1 | FileCheck %s
3-
; RUN: not llc -march=r600 -mcpu=cypress < %s 2>&1 | FileCheck %s
1+
; RUN: not llc -march=amdgcn -mcpu=SI -verify-machineinstrs -exit-on-error < %s 2>&1 | FileCheck %s
2+
; RUN: not llc -march=amdgcn -mcpu=tonga -verify-machineinstrs -exit-on-error < %s 2>&1 | FileCheck %s
3+
; RUN: not llc -march=r600 -mcpu=cypress -exit-on-error < %s 2>&1 | FileCheck %s
44

55
; CHECK: in function test_call_external{{.*}}: unsupported call to function external_function
66

‎llvm/test/CodeGen/AMDGPU/dynamic_stackalloc.ll

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
; RUN: not llc -march=amdgcn -mcpu=tahiti -mattr=+promote-alloca -verify-machineinstrs < %s 2>&1 | FileCheck %s
2-
; RUN: not llc -march=amdgcn -mcpu=tahiti -mattr=-promote-alloca -verify-machineinstrs < %s 2>&1 | FileCheck %s
3-
; RUN: not llc -march=r600 -mcpu=cypress < %s 2>&1 | FileCheck %s
1+
; RUN: not llc -march=amdgcn -mcpu=tahiti -mattr=+promote-alloca -verify-machineinstrs -exit-on-error < %s 2>&1 | FileCheck %s
2+
; RUN: not llc -march=amdgcn -mcpu=tahiti -mattr=-promote-alloca -verify-machineinstrs -exit-on-error < %s 2>&1 | FileCheck %s
3+
; RUN: not llc -march=r600 -mcpu=cypress -exit-on-error < %s 2>&1 | FileCheck %s
44

55
; CHECK: in function test_dynamic_stackalloc{{.*}}: unsupported dynamic alloca
66

‎llvm/test/CodeGen/AMDGPU/no-hsa-graphics-shaders.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=amdgcn -mtriple=amdgcn-unknown-amdhsa < %s 2>&1 | FileCheck %s
1+
; RUN: not llc -march=amdgcn -mtriple=amdgcn-unknown-amdhsa -exit-on-error < %s 2>&1 | FileCheck %s
22

33
; CHECK: in function pixel_s{{.*}}: unsupported non-compute shaders with HSA
44
define amdgpu_ps void @pixel_shader() #0 {

‎llvm/test/CodeGen/AMDGPU/private-memory-broken.ll

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
; RUN: not llc -verify-machineinstrs -march=amdgcn -mcpu=SI %s -o /dev/null 2>&1 | FileCheck %s
2-
; RUN: not llc -verify-machineinstrs -march=amdgcn -mcpu=tonga %s -o /dev/null 2>&1 | FileCheck %s
1+
; RUN: not llc -verify-machineinstrs -march=amdgcn -mcpu=SI -exit-on-error %s -o /dev/null 2>&1 | FileCheck %s
2+
; RUN: not llc -verify-machineinstrs -march=amdgcn -mcpu=tonga -exit-on-error %s -o /dev/null 2>&1 | FileCheck %s
33

44
; Make sure promote alloca pass doesn't crash
55

‎llvm/test/CodeGen/AMDGPU/promote-alloca-bitcast-function.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=amdgcn < %s 2>&1 | FileCheck %s
1+
; RUN: not llc -march=amdgcn -exit-on-error < %s 2>&1 | FileCheck %s
22

33
; Make sure that AMDGPUPromoteAlloca doesn't crash if the called
44
; function is a constantexpr cast of a function.

‎llvm/test/CodeGen/ARM/2012-09-25-InlineAsmScalarToVectorConv2.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -mtriple=arm-eabi -mcpu=cortex-a8 %s -o - 2>&1 | FileCheck %s
1+
; RUN: not llc -mtriple=arm-eabi -mcpu=cortex-a8 -exit-on-error %s -o - 2>&1 | FileCheck %s
22

33
; Check for error message:
44
; CHECK: scalar-to-vector conversion failed, possible invalid constraint for vector type

‎llvm/test/CodeGen/BPF/many_args1.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=bpf < %s 2> %t1
1+
; RUN: not llc -march=bpf -exit-on-error < %s 2> %t1
22
; RUN: FileCheck %s < %t1
33
; CHECK: too many args
44

‎llvm/test/CodeGen/BPF/many_args2.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=bpf < %s 2> %t1
1+
; RUN: not llc -march=bpf -exit-on-error < %s 2> %t1
22
; RUN: FileCheck %s < %t1
33
; CHECK: too many args
44

‎llvm/test/CodeGen/BPF/struct_ret1.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=bpf < %s 2> %t1
1+
; RUN: not llc -march=bpf -exit-on-error < %s 2> %t1
22
; RUN: FileCheck %s < %t1
33
; CHECK: only integer returns
44

‎llvm/test/CodeGen/BPF/struct_ret2.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -march=bpf < %s 2> %t1
1+
; RUN: not llc -march=bpf -exit-on-error < %s 2> %t1
22
; RUN: FileCheck %s < %t1
33
; CHECK: only small returns
44

‎llvm/test/CodeGen/MIR/Generic/invalid-jump-table-kind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1+
# RUN: not llc -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22

33
--- |
44

‎llvm/test/CodeGen/MIR/Generic/llvm-ir-error-reported.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1+
# RUN: not llc -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22
# This test ensures an error is reported if the embedded LLVM IR contains an
33
# error.
44

‎llvm/test/CodeGen/MIR/Generic/machine-function-missing-function.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1+
# RUN: not llc -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22
# This test ensures that an error is reported when the mir file has LLVM IR and
33
# one of the machine functions has a name that doesn't match any function in
44
# the LLVM IR.

‎llvm/test/CodeGen/MIR/Generic/machine-function-missing-name.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1+
# RUN: not llc -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22
# This test ensures that an error is reported when a machine function doesn't
33
# have a name attribute.
44

‎llvm/test/CodeGen/MIR/Generic/machine-function-redefinition-error.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1+
# RUN: not llc -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22
# This test ensures that the machine function errors are reported correctly.
33

44
---

‎llvm/test/CodeGen/MIR/X86/spill-slot-fixed-stack-object-aliased.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1+
# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22

33
--- |
44

‎llvm/test/CodeGen/MIR/X86/spill-slot-fixed-stack-object-immutable.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1+
# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22

33
--- |
44

‎llvm/test/CodeGen/MIR/X86/variable-sized-stack-object-size-error.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
1+
# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22
--- |
33

44
define i32 @test(i32 %a) {

‎llvm/test/CodeGen/PowerPC/crbit-asm-disabled.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -mcpu=pwr7 -o /dev/null %s 2>&1 | FileCheck %s
1+
; RUN: not llc -mcpu=pwr7 -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22
target datalayout = "E-m:e-i64:64-n32:64"
33
target triple = "powerpc64-unknown-linux-gnu"
44

‎llvm/test/CodeGen/PowerPC/vec-asm-disabled.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -mcpu=pwr7 -o /dev/null %s 2>&1 | FileCheck %s
1+
; RUN: not llc -mcpu=pwr7 -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22
target datalayout = "E-m:e-i64:64-n32:64"
33
target triple = "powerpc64-unknown-linux-gnu"
44

‎llvm/test/CodeGen/X86/asm-reject-reg-type-mismatch.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not llc -o /dev/null %s 2>&1 | FileCheck %s
1+
; RUN: not llc -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
22
target triple = "x86_64--"
33

44
; CHECK: error: couldn't allocate output register for constraint '{ax}'

‎llvm/tools/llc/llc.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "llvm/CodeGen/MachineModuleInfo.h"
2626
#include "llvm/CodeGen/TargetPassConfig.h"
2727
#include "llvm/IR/DataLayout.h"
28+
#include "llvm/IR/DiagnosticInfo.h"
29+
#include "llvm/IR/DiagnosticPrinter.h"
2830
#include "llvm/IR/IRPrintingPasses.h"
2931
#include "llvm/IR/LLVMContext.h"
3032
#include "llvm/IR/LegacyPassManager.h"
@@ -111,6 +113,11 @@ static cl::opt<bool> DiscardValueNames(
111113
cl::desc("Discard names from Value (other than GlobalValue)."),
112114
cl::init(false), cl::Hidden);
113115

116+
static cl::opt<bool> ExitOnError(
117+
"exit-on-error",
118+
cl::desc("Exit as soon as an error is encountered."),
119+
cl::init(false), cl::Hidden);
120+
114121
static int compileModule(char **, LLVMContext &);
115122

116123
static std::unique_ptr<tool_output_file>
@@ -181,6 +188,17 @@ GetOutputStream(const char *TargetName, Triple::OSType OS,
181188
return FDOut;
182189
}
183190

191+
static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context) {
192+
bool *HasError = static_cast<bool *>(Context);
193+
if (DI.getSeverity() == DS_Error)
194+
*HasError = true;
195+
196+
DiagnosticPrinterRawOStream DP(errs());
197+
errs() << LLVMContext::getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
198+
DI.print(DP);
199+
errs() << "\n";
200+
}
201+
184202
// main - Entry point for the llc compiler.
185203
//
186204
int main(int argc, char **argv) {
@@ -215,6 +233,11 @@ int main(int argc, char **argv) {
215233

216234
Context.setDiscardValueNames(DiscardValueNames);
217235

236+
// Set a diagnostic handler that doesn't exit on the first error
237+
bool HasError = false;
238+
if (!ExitOnError)
239+
Context.setDiagnosticHandler(DiagnosticHandler, &HasError);
240+
218241
// Compile the module TimeCompilations times to give better compile time
219242
// metrics.
220243
for (unsigned I = TimeCompilations; I; --I)
@@ -441,6 +464,12 @@ static int compileModule(char **argv, LLVMContext &Context) {
441464

442465
PM.run(*M);
443466

467+
if (!ExitOnError) {
468+
auto HasError = *static_cast<bool *>(Context.getDiagnosticContext());
469+
if (HasError)
470+
return 1;
471+
}
472+
444473
// Compare the two outputs and make sure they're the same
445474
if (CompileTwice) {
446475
if (Buffer.size() != CompileTwiceBuffer.size() ||

0 commit comments

Comments
 (0)
Please sign in to comment.