Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFC] Optimize OCLUtil::toString usage in checkError function #1229

Merged
merged 6 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions lib/SPIRV/OCLUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,6 @@ bool isSamplerTy(Type *Ty);
// Checks if the binary operator is an unfused fmul + fadd instruction.
bool isUnfusedMulAdd(BinaryOperator *B);

template <typename T> std::string toString(const T *Object) {
std::string S;
llvm::raw_string_ostream RSOS(S);
Object->print(RSOS);
RSOS.flush();
return S;
}

// Get data and vector size postfix for sugroup_block_{read|write} builtins
// as specified by cl_intel_subgroups* extensions.
// Scalar data assumed to be represented as vector of one element.
Expand Down
27 changes: 11 additions & 16 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,9 +1010,8 @@ SPIRV::SPIRVInstruction *LLVMToSPIRVBase::transUnaryInst(UnaryInstruction *U,
const auto DestAddrSpace = Cast->getDestTy()->getPointerAddressSpace();
if (DestAddrSpace == SPIRAS_Generic) {
getErrorLog().checkError(
SrcAddrSpace != SPIRAS_Constant, SPIRVEC_InvalidModule,
"Casts from constant address space to generic are illegal\n" +
toString(U));
SrcAddrSpace != SPIRAS_Constant, SPIRVEC_InvalidModule, U,
"Casts from constant address space to generic are illegal\n");
BOC = OpPtrCastToGeneric;
// In SPIR-V only casts to/from generic are allowed. But with
// SPV_INTEL_usm_storage_classes we can also have casts from global_device
Expand All @@ -1021,10 +1020,9 @@ SPIRV::SPIRVInstruction *LLVMToSPIRVBase::transUnaryInst(UnaryInstruction *U,
SrcAddrSpace == SPIRAS_GlobalHost) {
getErrorLog().checkError(DestAddrSpace == SPIRAS_Global ||
DestAddrSpace == SPIRAS_Generic,
SPIRVEC_InvalidModule,
SPIRVEC_InvalidModule, U,
"Casts from global_device/global_host only "
"allowed to global/generic\n" +
toString(U));
"allowed to global/generic\n");
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_usm_storage_classes)) {
if (DestAddrSpace == SPIRAS_Global)
Expand All @@ -1037,10 +1035,9 @@ SPIRV::SPIRVInstruction *LLVMToSPIRVBase::transUnaryInst(UnaryInstruction *U,
DestAddrSpace == SPIRAS_GlobalHost) {
getErrorLog().checkError(SrcAddrSpace == SPIRAS_Global ||
SrcAddrSpace == SPIRAS_Generic,
SPIRVEC_InvalidModule,
SPIRVEC_InvalidModule, U,
"Casts to global_device/global_host only "
"allowed from global/generic\n" +
toString(U));
"allowed from global/generic\n");
if (!BM->isAllowedToUseExtension(
ExtensionID::SPV_INTEL_usm_storage_classes)) {
if (SrcAddrSpace == SPIRAS_Global)
Expand All @@ -1051,14 +1048,12 @@ SPIRV::SPIRVInstruction *LLVMToSPIRVBase::transUnaryInst(UnaryInstruction *U,
}
} else {
getErrorLog().checkError(
SrcAddrSpace == SPIRAS_Generic, SPIRVEC_InvalidModule,
SrcAddrSpace == SPIRAS_Generic, SPIRVEC_InvalidModule, U,
"Casts from private/local/global address space are allowed only to "
"generic\n" +
toString(U));
"generic\n");
getErrorLog().checkError(
DestAddrSpace != SPIRAS_Constant, SPIRVEC_InvalidModule,
"Casts from generic address space to constant are illegal\n" +
toString(U));
DestAddrSpace != SPIRAS_Constant, SPIRVEC_InvalidModule, U,
"Casts from generic address space to constant are illegal\n");
BOC = OpGenericCastToPtr;
}
} else {
Expand Down Expand Up @@ -1877,7 +1872,7 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
if (!BM->getErrorLog().checkError(
!AtomicRMWInst::isFPOperation(Op) && Op != AtomicRMWInst::Nand,
SPIRVEC_InvalidInstruction,
OCLUtil::toString(V) + "\nAtomic " +
toString(V) + "\nAtomic " +
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
toString(V) + "\nAtomic " +
V + "\nAtomic " +

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean such change? It'll not work now as checkError accepts an Instruction, not Value.

Suggested change
toString(V) + "\nAtomic " +
V, "\nAtomic " +

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change checkError to accept Value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied in ff6cabe.

AtomicRMWInst::getOperationName(Op).str() +
" is not supported in SPIR-V!\n"))
return nullptr;
Expand Down
22 changes: 22 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVError.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include "SPIRVDebug.h"
#include "SPIRVUtil.h"
#include "llvm/IR/Instruction.h"
#include <iostream>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -109,12 +110,33 @@ class SPIRVErrorLog {
const std::string &DetailedMsg = "",
const char *CondString = nullptr,
const char *FileName = nullptr, unsigned LineNumber = 0);
// Check if Condition is satisfied and set ErrCode and DetailedMsg with Inst
// text representation if not. Returns true if no error.
bool checkError(bool Condition, SPIRVErrorCode ErrCode,
llvm::Instruction *Inst, const std::string &DetailedMsg = "",
const char *CondString = nullptr,
const char *FileName = nullptr, unsigned LineNumber = 0);

protected:
SPIRVErrorCode ErrorCode;
std::string ErrorMsg;
};

inline bool SPIRVErrorLog::checkError(bool Cond, SPIRVErrorCode ErrCode,
llvm::Instruction *Inst,
const std::string &Msg,
const char *CondString,
const char *FileName, unsigned LineNo) {
if (Cond)
return Cond;
// Do not overwrite previous failure.
if (ErrorCode != SPIRVEC_Success)
return Cond;
AlexeySotkin marked this conversation as resolved.
Show resolved Hide resolved
std::string InstName = toString(Inst);
return checkError(Cond, ErrCode, Msg + InstName, CondString, FileName,
LineNo);
}

inline bool SPIRVErrorLog::checkError(bool Cond, SPIRVErrorCode ErrCode,
const std::string &Msg,
const char *CondString,
Expand Down
10 changes: 10 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include <ostream>
#define spv_ostream std::ostream

#include "llvm/Support/raw_ostream.h"

#include <algorithm>
#include <cassert>
#include <cstdint>
Expand Down Expand Up @@ -422,6 +424,14 @@ getOrInsert(MapTy &Map, typename MapTy::key_type Key, FuncTy Func) {
return NF;
}

template <typename T> std::string toString(const T *Object) {
std::string S;
AlexeySotkin marked this conversation as resolved.
Show resolved Hide resolved
llvm::raw_string_ostream RSOS(S);
Object->print(RSOS);
RSOS.flush();
return S;
}

} // namespace SPIRV

#endif // SPIRV_LIBSPIRV_SPIRVUTIL_H