Skip to content

Commit

Permalink
[llc] New diagnostic handler
Browse files Browse the repository at this point in the history
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269655 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rengolin committed May 16, 2016
1 parent 5c1f236 commit 3b0d377
Show file tree
Hide file tree
Showing 31 changed files with 69 additions and 34 deletions.
2 changes: 1 addition & 1 deletion include/llvm/IR/DiagnosticInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Module;
class SMDiagnostic;

/// \brief Defines the different supported severity of a diagnostic.
enum DiagnosticSeverity {
enum DiagnosticSeverity : char {
DS_Error,
DS_Warning,
DS_Remark,
Expand Down
5 changes: 5 additions & 0 deletions include/llvm/IR/LLVMContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class MDString;
class DICompositeType;
class SMDiagnostic;
class DiagnosticInfo;
enum DiagnosticSeverity : char;
template <typename T> class SmallVectorImpl;
class Function;
class DebugLoc;
Expand Down Expand Up @@ -172,6 +173,10 @@ class LLVMContext {
/// setDiagnosticContext.
void *getDiagnosticContext() const;

/// \brief Get the prefix that should be printed in front of a diagnostic of
/// the given \p Severity
static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity);

/// \brief Report a message to the currently installed diagnostic handler.
///
/// This function returns, in particular in the case of error reporting
Expand Down
3 changes: 2 additions & 1 deletion lib/IR/LLVMContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ static bool isDiagnosticEnabled(const DiagnosticInfo &DI) {
return true;
}

static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
const char *
LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) {
switch (Severity) {
case DS_Error:
return "error";
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/AArch64/arm64-inline-asm-error-I.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=arm64 < %s 2> %t
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s

; Check for at least one invalid constant.
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/AArch64/arm64-inline-asm-error-J.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=arm64 < %s 2> %t
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s

; Check for at least one invalid constant.
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/AArch64/arm64-inline-asm-error-K.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=arm64 < %s 2> %t
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s

; Check for at least one invalid constant.
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/AArch64/arm64-inline-asm-error-L.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=arm64 < %s 2> %t
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s

; Check for at least one invalid constant.
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/AArch64/arm64-inline-asm-error-M.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=arm64 < %s 2> %t
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s

; Check for at least one invalid constant.
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/AArch64/arm64-inline-asm-error-N.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=arm64 < %s 2> %t
; RUN: not llc -march=arm64 -exit-on-error < %s 2> %t
; RUN: FileCheck --check-prefix=CHECK-ERRORS < %t %s

; Check for at least one invalid constant.
Expand Down
6 changes: 3 additions & 3 deletions test/CodeGen/AMDGPU/call.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: not llc -march=amdgcn -mcpu=SI -verify-machineinstrs< %s 2>&1 | FileCheck %s
; RUN: not llc -march=amdgcn -mcpu=tonga -verify-machineinstrs< %s 2>&1 | FileCheck %s
; RUN: not llc -march=r600 -mcpu=cypress < %s 2>&1 | FileCheck %s
; RUN: not llc -march=amdgcn -mcpu=SI -verify-machineinstrs -exit-on-error < %s 2>&1 | FileCheck %s
; RUN: not llc -march=amdgcn -mcpu=tonga -verify-machineinstrs -exit-on-error < %s 2>&1 | FileCheck %s
; RUN: not llc -march=r600 -mcpu=cypress -exit-on-error < %s 2>&1 | FileCheck %s

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

Expand Down
6 changes: 3 additions & 3 deletions test/CodeGen/AMDGPU/dynamic_stackalloc.ll
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
; RUN: not llc -march=amdgcn -mcpu=tahiti -mattr=+promote-alloca -verify-machineinstrs < %s 2>&1 | FileCheck %s
; RUN: not llc -march=amdgcn -mcpu=tahiti -mattr=-promote-alloca -verify-machineinstrs < %s 2>&1 | FileCheck %s
; RUN: not llc -march=r600 -mcpu=cypress < %s 2>&1 | FileCheck %s
; RUN: not llc -march=amdgcn -mcpu=tahiti -mattr=+promote-alloca -verify-machineinstrs -exit-on-error < %s 2>&1 | FileCheck %s
; RUN: not llc -march=amdgcn -mcpu=tahiti -mattr=-promote-alloca -verify-machineinstrs -exit-on-error < %s 2>&1 | FileCheck %s
; RUN: not llc -march=r600 -mcpu=cypress -exit-on-error < %s 2>&1 | FileCheck %s

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

Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/AMDGPU/no-hsa-graphics-shaders.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=amdgcn -mtriple=amdgcn-unknown-amdhsa < %s 2>&1 | FileCheck %s
; RUN: not llc -march=amdgcn -mtriple=amdgcn-unknown-amdhsa -exit-on-error < %s 2>&1 | FileCheck %s

; CHECK: in function pixel_s{{.*}}: unsupported non-compute shaders with HSA
define amdgpu_ps void @pixel_shader() #0 {
Expand Down
4 changes: 2 additions & 2 deletions test/CodeGen/AMDGPU/private-memory-broken.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; RUN: not llc -verify-machineinstrs -march=amdgcn -mcpu=SI %s -o /dev/null 2>&1 | FileCheck %s
; RUN: not llc -verify-machineinstrs -march=amdgcn -mcpu=tonga %s -o /dev/null 2>&1 | FileCheck %s
; RUN: not llc -verify-machineinstrs -march=amdgcn -mcpu=SI -exit-on-error %s -o /dev/null 2>&1 | FileCheck %s
; RUN: not llc -verify-machineinstrs -march=amdgcn -mcpu=tonga -exit-on-error %s -o /dev/null 2>&1 | FileCheck %s

; Make sure promote alloca pass doesn't crash

Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/AMDGPU/promote-alloca-bitcast-function.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=amdgcn < %s 2>&1 | FileCheck %s
; RUN: not llc -march=amdgcn -exit-on-error < %s 2>&1 | FileCheck %s

; Make sure that AMDGPUPromoteAlloca doesn't crash if the called
; function is a constantexpr cast of a function.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -mtriple=arm-eabi -mcpu=cortex-a8 %s -o - 2>&1 | FileCheck %s
; RUN: not llc -mtriple=arm-eabi -mcpu=cortex-a8 -exit-on-error %s -o - 2>&1 | FileCheck %s

; Check for error message:
; CHECK: scalar-to-vector conversion failed, possible invalid constraint for vector type
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/BPF/many_args1.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=bpf < %s 2> %t1
; RUN: not llc -march=bpf -exit-on-error < %s 2> %t1
; RUN: FileCheck %s < %t1
; CHECK: too many args

Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/BPF/many_args2.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=bpf < %s 2> %t1
; RUN: not llc -march=bpf -exit-on-error < %s 2> %t1
; RUN: FileCheck %s < %t1
; CHECK: too many args

Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/BPF/struct_ret1.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=bpf < %s 2> %t1
; RUN: not llc -march=bpf -exit-on-error < %s 2> %t1
; RUN: FileCheck %s < %t1
; CHECK: only integer returns

Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/BPF/struct_ret2.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -march=bpf < %s 2> %t1
; RUN: not llc -march=bpf -exit-on-error < %s 2> %t1
; RUN: FileCheck %s < %t1
; CHECK: only small returns

Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/MIR/Generic/invalid-jump-table-kind.mir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
# RUN: not llc -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s

--- |

Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/MIR/Generic/llvm-ir-error-reported.mir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
# RUN: not llc -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
# This test ensures an error is reported if the embedded LLVM IR contains an
# error.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
# RUN: not llc -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
# This test ensures that an error is reported when the mir file has LLVM IR and
# one of the machine functions has a name that doesn't match any function in
# the LLVM IR.
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/MIR/Generic/machine-function-missing-name.mir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
# RUN: not llc -start-after branch-folder -stop-after branch-folder -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
# This test ensures that an error is reported when a machine function doesn't
# have a name attribute.

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

---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
# 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

--- |

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
# 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

--- |

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: not llc -march=x86-64 -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s
# 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
--- |

define i32 @test(i32 %a) {
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/PowerPC/crbit-asm-disabled.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -mcpu=pwr7 -o /dev/null %s 2>&1 | FileCheck %s
; RUN: not llc -mcpu=pwr7 -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
target datalayout = "E-m:e-i64:64-n32:64"
target triple = "powerpc64-unknown-linux-gnu"

Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/PowerPC/vec-asm-disabled.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -mcpu=pwr7 -o /dev/null %s 2>&1 | FileCheck %s
; RUN: not llc -mcpu=pwr7 -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
target datalayout = "E-m:e-i64:64-n32:64"
target triple = "powerpc64-unknown-linux-gnu"

Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/X86/asm-reject-reg-type-mismatch.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: not llc -o /dev/null %s 2>&1 | FileCheck %s
; RUN: not llc -exit-on-error -o /dev/null %s 2>&1 | FileCheck %s
target triple = "x86_64--"

; CHECK: error: couldn't allocate output register for constraint '{ax}'
Expand Down
29 changes: 29 additions & 0 deletions tools/llc/llc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
Expand Down Expand Up @@ -111,6 +113,11 @@ static cl::opt<bool> DiscardValueNames(
cl::desc("Discard names from Value (other than GlobalValue)."),
cl::init(false), cl::Hidden);

static cl::opt<bool> ExitOnError(
"exit-on-error",
cl::desc("Exit as soon as an error is encountered."),
cl::init(false), cl::Hidden);

static int compileModule(char **, LLVMContext &);

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

static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context) {
bool *HasError = static_cast<bool *>(Context);
if (DI.getSeverity() == DS_Error)
*HasError = true;

DiagnosticPrinterRawOStream DP(errs());
errs() << LLVMContext::getDiagnosticMessagePrefix(DI.getSeverity()) << ": ";
DI.print(DP);
errs() << "\n";
}

// main - Entry point for the llc compiler.
//
int main(int argc, char **argv) {
Expand Down Expand Up @@ -215,6 +233,11 @@ int main(int argc, char **argv) {

Context.setDiscardValueNames(DiscardValueNames);

// Set a diagnostic handler that doesn't exit on the first error
bool HasError = false;
if (!ExitOnError)
Context.setDiagnosticHandler(DiagnosticHandler, &HasError);

// Compile the module TimeCompilations times to give better compile time
// metrics.
for (unsigned I = TimeCompilations; I; --I)
Expand Down Expand Up @@ -441,6 +464,12 @@ static int compileModule(char **argv, LLVMContext &Context) {

PM.run(*M);

if (!ExitOnError) {
auto HasError = *static_cast<bool *>(Context.getDiagnosticContext());
if (HasError)
return 1;
}

// Compare the two outputs and make sure they're the same
if (CompileTwice) {
if (Buffer.size() != CompileTwiceBuffer.size() ||
Expand Down

0 comments on commit 3b0d377

Please sign in to comment.