Skip to content

Commit 4d28f0a

Browse files
committed
[llc] Add reportError helper and canonicalize error messages
1 parent 2f72147 commit 4d28f0a

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

llvm/test/CodeGen/AMDGPU/invalid-alloca.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
; RUN: not llvm-as -data-layout=A5 < %s 2>&1 | FileCheck -check-prefixes=COMMON,AS %s
22
; RUN: not llc -mtriple amdgcn-amd-amdhsa < %s 2>&1 | FileCheck -check-prefixes=COMMON,LLC %s
33
; RUN: llvm-as < %s | not llc -mtriple amdgcn-amd-amdhsa 2>&1 | FileCheck -check-prefixes=MISMATCH %s
4-
; RUN: not opt -data-layout=A5 -S < %s 2>&1 | FileCheck -check-prefixes=COMMON,LLC %s
4+
; RUN: not opt -data-layout=A5 -S < %s 2>&1 | FileCheck -check-prefixes=COMMON,OPT %s
55
; RUN: llvm-as < %s | not opt -data-layout=A5 2>&1 | FileCheck -check-prefixes=MISMATCH %s
66

77
; AS: assembly parsed, but does not verify as correct!
88
; COMMON: Allocation instruction pointer not in the stack address space!
99
; COMMON: %tmp = alloca i32
1010
; MISMATCH: Explicit load/store type does not match pointee type of pointer operand
11-
; LLC: error: input module is broken!
11+
; LLC: error: {{.*}}input module cannot be verified
12+
; OPT: error: input module is broken!
1213

1314
define amdgpu_kernel void @test() {
1415
%tmp = alloca i32

llvm/test/tools/llc/aix-pic-setting.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
; RUN: not llc -mtriple=powerpc64-ibm-aix --relocation-model=ropi-rwpi < %s 2>&1 | FileCheck --check-prefix=CHECK-NON-PIC %s
77

88
; CHECK-NOT: {{.}}
9-
; CHECK-NON-PIC: invalid relocation model, AIX only supports PIC.
9+
; CHECK-NON-PIC: error: '<stdin>': invalid relocation model, AIX only supports PIC

llvm/tools/llc/llc.cpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,25 @@ static cl::opt<RunPassOption, true, cl::parser<std::string>> RunPass(
200200

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

203+
LLVM_ATTRIBUTE_NORETURN static void reportError(Twine Msg,
204+
StringRef Filename = "") {
205+
SmallString<256> Prefix;
206+
if (!Filename.empty()) {
207+
if (Filename == "-")
208+
Filename = "<stdin>";
209+
("'" + Twine(Filename) + "': ").toStringRef(Prefix);
210+
}
211+
WithColor::error(errs(), "llc") << Prefix << Msg << "\n";
212+
exit(1);
213+
}
214+
215+
LLVM_ATTRIBUTE_NORETURN static void reportError(Error Err, StringRef Filename) {
216+
assert(Err);
217+
handleAllErrors(createFileError(Filename, std::move(Err)),
218+
[&](const ErrorInfoBase &EI) { reportError(EI.message()); });
219+
llvm_unreachable("reportError() should not return");
220+
}
221+
203222
static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
204223
Triple::OSType OS,
205224
const char *ProgName) {
@@ -260,7 +279,7 @@ static std::unique_ptr<ToolOutputFile> GetOutputStream(const char *TargetName,
260279
OpenFlags |= sys::fs::OF_Text;
261280
auto FDOut = std::make_unique<ToolOutputFile>(OutputFilename, EC, OpenFlags);
262281
if (EC) {
263-
WithColor::error() << EC.message() << '\n';
282+
reportError(EC.message());
264283
return nullptr;
265284
}
266285

@@ -353,18 +372,12 @@ int main(int argc, char **argv) {
353372
setupLLVMOptimizationRemarks(Context, RemarksFilename, RemarksPasses,
354373
RemarksFormat, RemarksWithHotness,
355374
RemarksHotnessThreshold);
356-
if (Error E = RemarksFileOrErr.takeError()) {
357-
WithColor::error(errs(), argv[0]) << toString(std::move(E)) << '\n';
358-
return 1;
359-
}
375+
if (Error E = RemarksFileOrErr.takeError())
376+
reportError(std::move(E), RemarksFilename);
360377
std::unique_ptr<ToolOutputFile> RemarksFile = std::move(*RemarksFileOrErr);
361378

362-
if (InputLanguage != "" && InputLanguage != "ir" &&
363-
InputLanguage != "mir") {
364-
WithColor::error(errs(), argv[0])
365-
<< "input language must be '', 'IR' or 'MIR'\n";
366-
return 1;
367-
}
379+
if (InputLanguage != "" && InputLanguage != "ir" && InputLanguage != "mir")
380+
reportError("input language must be '', 'IR' or 'MIR'");
368381

369382
// Compile the module TimeCompilations times to give better compile time
370383
// metrics.
@@ -490,11 +503,9 @@ static int compileModule(char **argv, LLVMContext &Context) {
490503

491504
// On AIX, setting the relocation model to anything other than PIC is
492505
// considered a user error.
493-
if (TheTriple.isOSAIX() && RM.hasValue() && *RM != Reloc::PIC_) {
494-
WithColor::error(errs(), argv[0])
495-
<< "invalid relocation model, AIX only supports PIC.\n";
496-
exit(1);
497-
}
506+
if (TheTriple.isOSAIX() && RM.hasValue() && *RM != Reloc::PIC_)
507+
reportError("invalid relocation model, AIX only supports PIC",
508+
InputFilename);
498509

499510
InitializeOptions(TheTriple);
500511
Target = std::unique_ptr<TargetMachine>(TheTarget->createTargetMachine(
@@ -567,10 +578,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
567578
std::error_code EC;
568579
DwoOut = std::make_unique<ToolOutputFile>(SplitDwarfOutputFile, EC,
569580
sys::fs::OF_None);
570-
if (EC) {
571-
WithColor::error(errs(), argv[0]) << EC.message() << '\n';
572-
return 1;
573-
}
581+
if (EC)
582+
reportError(EC.message(), SplitDwarfOutputFile);
574583
}
575584

576585
// Build up all of the passes that we want to do to the module.
@@ -586,12 +595,8 @@ static int compileModule(char **argv, LLVMContext &Context) {
586595

587596
// Verify module immediately to catch problems before doInitialization() is
588597
// called on any passes.
589-
if (!NoVerify && verifyModule(*M, &errs())) {
590-
std::string Prefix =
591-
(Twine(argv[0]) + Twine(": ") + Twine(InputFilename)).str();
592-
WithColor::error(errs(), Prefix) << "input module is broken!\n";
593-
return 1;
594-
}
598+
if (!NoVerify && verifyModule(*M, &errs()))
599+
reportError("input module cannot be verified", InputFilename);
595600

596601
// Override function attributes based on CPUStr, FeaturesStr, and command line
597602
// flags.
@@ -650,10 +655,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
650655
} else if (Target->addPassesToEmitFile(
651656
PM, *OS, DwoOut ? &DwoOut->os() : nullptr,
652657
codegen::getFileType(), NoVerify, MMIWP)) {
653-
WithColor::warning(errs(), argv[0])
654-
<< "target does not support generation of this"
655-
<< " file type!\n";
656-
return 1;
658+
reportError("target does not support generation of this file type");
657659
}
658660

659661
const_cast<TargetLoweringObjectFile *>(LLVMTM.getObjFileLowering())

0 commit comments

Comments
 (0)