Skip to content

Commit 6f6e210

Browse files
committed
[COMGR][Cache] Move AMDGPUCompilerDiagnosticHandler into its own header
Additionally, break the dependency between AMDGPUCompilerDiagnosticHandler and AMDGPUCompiler. Only LogS is referenced. This is needed to properly cache the log stream for spirv support. The function that is cached must initialize the DiagnosticHandler with the raw_ostream passed by the cache. This raw_ostream may be LogS or a raw_string_ostream to capture the error stream. co-authored by anjenner and jmmartinez Change-Id: Id915184e01b9b1aaafdcb974154ff8cb2089cefb
1 parent 20af2d4 commit 6f6e210

File tree

5 files changed

+128
-38
lines changed

5 files changed

+128
-38
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: 4 additions & 2 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"
@@ -1353,7 +1355,7 @@ amd_comgr_status_t AMDGPUCompiler::linkBitcodeToBitcode() {
13531355
SMDiagnostic SMDiag;
13541356
LLVMContext Context;
13551357
Context.setDiagnosticHandler(
1356-
std::make_unique<AMDGPUCompilerDiagnosticHandler>(this), true);
1358+
std::make_unique<AMDGPUCompilerDiagnosticHandler>(this->LogS), true);
13571359

13581360
auto Composite = std::make_unique<llvm::Module>("llvm-link", Context);
13591361
Linker L(*Composite);
@@ -1867,7 +1869,7 @@ amd_comgr_status_t AMDGPUCompiler::translateSpirvToBitcode() {
18671869

18681870
LLVMContext Context;
18691871
Context.setDiagnosticHandler(
1870-
std::make_unique<AMDGPUCompilerDiagnosticHandler>(this), true);
1872+
std::make_unique<AMDGPUCompilerDiagnosticHandler>(this->LogS), true);
18711873

18721874
for (auto *Input : InSet->DataObjects) {
18731875

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

0 commit comments

Comments
 (0)