Skip to content

Commit 57214a6

Browse files
authored
Merge pull request llvm#389 from AMD-Lightning-Internal/amd/dev/juamarti/cache-cherry-picks-from-staging
[COMGR][Cache] Cherry-Pick patches from staging to mainline
2 parents 8a44444 + 68da27a commit 57214a6

File tree

8 files changed

+313
-108
lines changed

8 files changed

+313
-108
lines changed

amd/comgr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ set(SOURCES
7272
src/comgr-compiler.cpp
7373
src/comgr.cpp
7474
src/comgr-device-libs.cpp
75+
src/comgr-diagnostic-handler.cpp
7576
src/comgr-disassembly.cpp
7677
src/comgr-elfdump.cpp
7778
src/comgr-env.cpp

amd/comgr/src/comgr-compiler.cpp

Lines changed: 85 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "comgr-compiler.h"
4040
#include "comgr-device-libs.h"
41+
#include "comgr-diagnostic-handler.h"
4142
#include "comgr-env.h"
4243
#include "lld/Common/CommonLinkerContext.h"
4344
#include "lld/Common/Driver.h"
@@ -51,6 +52,7 @@
5152
#include "clang/Driver/Tool.h"
5253
#include "clang/Frontend/CompilerInstance.h"
5354
#include "clang/Frontend/FrontendDiagnostic.h"
55+
#include "clang/Frontend/TextDiagnosticPrinter.h"
5456
#include "clang/FrontendTool/Utils.h"
5557
#include "llvm/Bitcode/BitcodeWriter.h"
5658
#include "llvm/IR/LLVMContext.h"
@@ -640,6 +642,82 @@ void logArgv(raw_ostream &OS, StringRef ProgramName,
640642
OS << '\n';
641643
OS.flush();
642644
}
645+
646+
amd_comgr_status_t executeCommand(const Command &Job, raw_ostream &LogS,
647+
DiagnosticOptions &DiagOpts) {
648+
TextDiagnosticPrinter DiagClient(LogS, &DiagOpts);
649+
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs);
650+
DiagnosticsEngine Diags(DiagID, &DiagOpts, &DiagClient, false);
651+
652+
auto Arguments = Job.getArguments();
653+
SmallVector<const char *, 128> Argv;
654+
initializeCommandLineArgs(Argv);
655+
Argv.append(Arguments.begin(), Arguments.end());
656+
Argv.push_back(nullptr);
657+
658+
// By default clang driver will ask CC1 to leak memory.
659+
auto *IT = find(Argv, StringRef("-disable-free"));
660+
if (IT != Argv.end()) {
661+
Argv.erase(IT);
662+
}
663+
664+
clearLLVMOptions();
665+
666+
if (Argv[1] == StringRef("-cc1")) {
667+
if (env::shouldEmitVerboseLogs()) {
668+
logArgv(LogS, "clang", Argv);
669+
}
670+
671+
std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
672+
Clang->setVerboseOutputStream(LogS);
673+
if (!Argv.back()) {
674+
Argv.pop_back();
675+
}
676+
if (!CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv,
677+
Diags)) {
678+
return AMD_COMGR_STATUS_ERROR;
679+
}
680+
// Internally this call refers to the invocation created above, so at
681+
// this point the DiagnosticsEngine should accurately reflect all user
682+
// requested configuration from Argv.
683+
Clang->createDiagnostics(&DiagClient, /* ShouldOwnClient */ false);
684+
if (!Clang->hasDiagnostics()) {
685+
return AMD_COMGR_STATUS_ERROR;
686+
}
687+
if (!ExecuteCompilerInvocation(Clang.get())) {
688+
return AMD_COMGR_STATUS_ERROR;
689+
}
690+
} else if (Argv[1] == StringRef("-cc1as")) {
691+
if (env::shouldEmitVerboseLogs()) {
692+
logArgv(LogS, "clang", Argv);
693+
}
694+
Argv.erase(Argv.begin() + 1);
695+
if (!Argv.back()) {
696+
Argv.pop_back();
697+
}
698+
AssemblerInvocation Asm;
699+
if (!AssemblerInvocation::createFromArgs(Asm, Argv, Diags)) {
700+
return AMD_COMGR_STATUS_ERROR;
701+
}
702+
if (auto Status = parseLLVMOptions(Asm.LLVMArgs)) {
703+
return Status;
704+
}
705+
if (executeAssembler(Asm, Diags, LogS)) {
706+
return AMD_COMGR_STATUS_ERROR;
707+
}
708+
} else if (Job.getCreator().getName() == LinkerJobName) {
709+
if (env::shouldEmitVerboseLogs()) {
710+
logArgv(LogS, "lld", Argv);
711+
}
712+
if (auto Status = linkWithLLD(Arguments, LogS, LogS)) {
713+
return Status;
714+
}
715+
} else {
716+
return AMD_COMGR_STATUS_ERROR;
717+
}
718+
return AMD_COMGR_STATUS_SUCCESS;
719+
}
720+
643721
} // namespace
644722

645723
amd_comgr_status_t
@@ -686,76 +764,13 @@ AMDGPUCompiler::executeInProcessDriver(ArrayRef<const char *> Args) {
686764
}
687765

688766
std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
689-
if (!C) {
690-
return C->containsError() ? AMD_COMGR_STATUS_ERROR
691-
: AMD_COMGR_STATUS_SUCCESS;
767+
if (!C || C->containsError()) {
768+
return AMD_COMGR_STATUS_ERROR;
692769
}
693-
for (auto &Job : C->getJobs()) {
694-
auto Arguments = Job.getArguments();
695-
SmallVector<const char *, 128> Argv;
696-
initializeCommandLineArgs(Argv);
697-
Argv.append(Arguments.begin(), Arguments.end());
698-
Argv.push_back(nullptr);
699770

700-
// By default clang driver will ask CC1 to leak memory.
701-
auto *IT = find(Argv, StringRef("-disable-free"));
702-
if (IT != Argv.end()) {
703-
Argv.erase(IT);
704-
}
705-
706-
clearLLVMOptions();
707-
708-
if (Argv[1] == StringRef("-cc1")) {
709-
if (env::shouldEmitVerboseLogs()) {
710-
logArgv(LogS, "clang", Argv);
711-
}
712-
713-
std::unique_ptr<CompilerInstance> Clang(new CompilerInstance());
714-
Clang->setVerboseOutputStream(LogS);
715-
if (!Argv.back()) {
716-
Argv.pop_back();
717-
}
718-
if (!CompilerInvocation::CreateFromArgs(Clang->getInvocation(), Argv,
719-
Diags)) {
720-
return AMD_COMGR_STATUS_ERROR;
721-
}
722-
// Internally this call refers to the invocation created above, so at
723-
// this point the DiagnosticsEngine should accurately reflect all user
724-
// requested configuration from Argv.
725-
Clang->createDiagnostics(DiagClient, /* ShouldOwnClient */ false);
726-
if (!Clang->hasDiagnostics()) {
727-
return AMD_COMGR_STATUS_ERROR;
728-
}
729-
if (!ExecuteCompilerInvocation(Clang.get())) {
730-
return AMD_COMGR_STATUS_ERROR;
731-
}
732-
} else if (Argv[1] == StringRef("-cc1as")) {
733-
if (env::shouldEmitVerboseLogs()) {
734-
logArgv(LogS, "clang", Argv);
735-
}
736-
Argv.erase(Argv.begin() + 1);
737-
if (!Argv.back()) {
738-
Argv.pop_back();
739-
}
740-
AssemblerInvocation Asm;
741-
if (!AssemblerInvocation::createFromArgs(Asm, Argv, Diags)) {
742-
return AMD_COMGR_STATUS_ERROR;
743-
}
744-
if (auto Status = parseLLVMOptions(Asm.LLVMArgs)) {
745-
return Status;
746-
}
747-
if (executeAssembler(Asm, Diags, LogS)) {
748-
return AMD_COMGR_STATUS_ERROR;
749-
}
750-
} else if (Job.getCreator().getName() == LinkerJobName) {
751-
if (env::shouldEmitVerboseLogs()) {
752-
logArgv(LogS, "lld", Argv);
753-
}
754-
if (auto Status = linkWithLLD(Arguments, LogS, LogS)) {
755-
return Status;
756-
}
757-
} else {
758-
return AMD_COMGR_STATUS_ERROR;
771+
for (auto &Job : C->getJobs()) {
772+
if (auto Status = executeCommand(Job, LogS, *DiagOpts)) {
773+
return Status;
759774
}
760775
}
761776
return AMD_COMGR_STATUS_SUCCESS;
@@ -1353,7 +1368,7 @@ amd_comgr_status_t AMDGPUCompiler::linkBitcodeToBitcode() {
13531368
SMDiagnostic SMDiag;
13541369
LLVMContext Context;
13551370
Context.setDiagnosticHandler(
1356-
std::make_unique<AMDGPUCompilerDiagnosticHandler>(this), true);
1371+
std::make_unique<AMDGPUCompilerDiagnosticHandler>(this->LogS), true);
13571372

13581373
auto Composite = std::make_unique<llvm::Module>("llvm-link", Context);
13591374
Linker L(*Composite);
@@ -1867,7 +1882,7 @@ amd_comgr_status_t AMDGPUCompiler::translateSpirvToBitcode() {
18671882

18681883
LLVMContext Context;
18691884
Context.setDiagnosticHandler(
1870-
std::make_unique<AMDGPUCompilerDiagnosticHandler>(this), true);
1885+
std::make_unique<AMDGPUCompilerDiagnosticHandler>(this->LogS), true);
18711886

18721887
for (auto *Input : InSet->DataObjects) {
18731888

amd/comgr/src/comgr-compiler.h

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141

4242
#include "comgr.h"
4343
#include "clang/Driver/Driver.h"
44-
#include "clang/Frontend/TextDiagnosticPrinter.h"
45-
#include "llvm/IR/DiagnosticInfo.h"
46-
#include "llvm/IR/DiagnosticPrinter.h"
4744

4845
namespace COMGR {
4946

@@ -52,39 +49,6 @@ namespace COMGR {
5249
/// @warning No more than one public method should be called on a constructed
5350
/// object before it is destructed.
5451
class AMDGPUCompiler {
55-
struct AMDGPUCompilerDiagnosticHandler : public llvm::DiagnosticHandler {
56-
AMDGPUCompiler *Compiler = nullptr;
57-
58-
AMDGPUCompilerDiagnosticHandler(AMDGPUCompiler *Compiler)
59-
: Compiler(Compiler) {}
60-
61-
bool handleDiagnostics(const llvm::DiagnosticInfo &DI) override {
62-
assert(Compiler && "Compiler cannot be nullptr");
63-
unsigned Severity = DI.getSeverity();
64-
switch (Severity) {
65-
case llvm::DS_Error:
66-
Compiler->LogS << "ERROR: ";
67-
break;
68-
case llvm::DS_Warning:
69-
Compiler->LogS << "WARNING: ";
70-
break;
71-
case llvm::DS_Remark:
72-
Compiler->LogS << "REMARK: ";
73-
break;
74-
case llvm::DS_Note:
75-
Compiler->LogS << "NOTE: ";
76-
break;
77-
default:
78-
Compiler->LogS << "(Unknown DiagnosticInfo Severity): ";
79-
break;
80-
}
81-
llvm::DiagnosticPrinterRawOStream DP(Compiler->LogS);
82-
DI.print(DP);
83-
Compiler->LogS << "\n";
84-
return true;
85-
}
86-
};
87-
8852
DataAction *ActionInfo;
8953
DataSet *InSet;
9054
amd_comgr_data_set_t OutSetT;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*******************************************************************************
2+
*
3+
* University of Illinois/NCSA
4+
* Open Source License
5+
*
6+
* Copyright (c) 2003-2017 University of Illinois at Urbana-Champaign.
7+
* Modifications (c) 2018 Advanced Micro Devices, Inc.
8+
* All rights reserved.
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* with the Software without restriction, including without limitation the
13+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14+
* sell copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* * Redistributions of source code must retain the above copyright notice,
18+
* this list of conditions and the following disclaimers.
19+
*
20+
* * Redistributions in binary form must reproduce the above copyright
21+
* notice, this list of conditions and the following disclaimers in the
22+
* documentation and/or other materials provided with the distribution.
23+
*
24+
* * Neither the names of the LLVM Team, University of Illinois at
25+
* Urbana-Champaign, nor the names of its contributors may be used to
26+
* endorse or promote products derived from this Software without specific
27+
* prior written permission.
28+
*
29+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
35+
* THE SOFTWARE.
36+
*
37+
******************************************************************************/
38+
#include "comgr-diagnostic-handler.h"
39+
40+
#include "llvm/IR/DiagnosticPrinter.h"
41+
42+
namespace COMGR {
43+
using namespace llvm;
44+
bool AMDGPUCompilerDiagnosticHandler::handleDiagnostics(
45+
const DiagnosticInfo &DI) {
46+
unsigned Severity = DI.getSeverity();
47+
switch (Severity) {
48+
case DS_Error:
49+
LogS << "ERROR: ";
50+
break;
51+
case DS_Warning:
52+
LogS << "WARNING: ";
53+
break;
54+
case DS_Remark:
55+
LogS << "REMARK: ";
56+
break;
57+
case DS_Note:
58+
LogS << "NOTE: ";
59+
break;
60+
default:
61+
LogS << "(Unknown DiagnosticInfo Severity): ";
62+
break;
63+
}
64+
DiagnosticPrinterRawOStream DP(LogS);
65+
DI.print(DP);
66+
LogS << "\n";
67+
return true;
68+
}
69+
} // namespace COMGR
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*******************************************************************************
2+
*
3+
* University of Illinois/NCSA
4+
* Open Source License
5+
*
6+
* Copyright (c) 2003-2017 University of Illinois at Urbana-Champaign.
7+
* Modifications (c) 2018 Advanced Micro Devices, Inc.
8+
* All rights reserved.
9+
*
10+
* Permission is hereby granted, free of charge, to any person obtaining a copy
11+
* of this software and associated documentation files (the "Software"), to deal
12+
* with the Software without restriction, including without limitation the
13+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14+
* sell copies of the Software, and to permit persons to whom the Software is
15+
* furnished to do so, subject to the following conditions:
16+
*
17+
* * Redistributions of source code must retain the above copyright notice,
18+
* this list of conditions and the following disclaimers.
19+
*
20+
* * Redistributions in binary form must reproduce the above copyright
21+
* notice, this list of conditions and the following disclaimers in the
22+
* documentation and/or other materials provided with the distribution.
23+
*
24+
* * Neither the names of the LLVM Team, University of Illinois at
25+
* Urbana-Champaign, nor the names of its contributors may be used to
26+
* endorse or promote products derived from this Software without specific
27+
* prior written permission.
28+
*
29+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32+
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
35+
* THE SOFTWARE.
36+
*
37+
******************************************************************************/
38+
39+
#ifndef COMGR_DIAGNOSTIC_HANDLER_H
40+
#define COMGR_DIAGNOSTIC_HANDLER_H
41+
42+
#include <llvm/IR/DiagnosticInfo.h>
43+
44+
namespace COMGR {
45+
struct AMDGPUCompilerDiagnosticHandler : public llvm::DiagnosticHandler {
46+
llvm::raw_ostream &LogS;
47+
48+
AMDGPUCompilerDiagnosticHandler(llvm::raw_ostream &LogS) : LogS(LogS) {}
49+
50+
bool handleDiagnostics(const llvm::DiagnosticInfo &DI) override;
51+
};
52+
} // namespace COMGR
53+
54+
#endif

amd/comgr/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ add_comgr_test(get_data_isa_name_test c)
224224
add_comgr_test(include_subdirectory_test c)
225225
add_comgr_test(options_test c)
226226
add_comgr_test(demangle_test c)
227+
add_comgr_test(fail_to_build_driver c)
227228
add_comgr_test(file_map c)
228229
add_comgr_test(lookup_code_object_test c)
229230
add_comgr_test(symbolize_test c)

0 commit comments

Comments
 (0)